## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6.5,
  fig.height = 4
)

## ----simulate-----------------------------------------------------------------
library(PPMiss)

# First 6 MA coefficients of an ARFIMA(1, 0.3, 1) process
arfima.coefs(ar = -0.2, ma = 0.4, d = 0.3, trunc = 5)

# Simulate an ARFIMA(0, 0.25, 0) series of length n = 400
set.seed(123)
trunc <- 1000
n <- 400
cks <- arfima.coefs(d = 0.25, trunc = trunc)
eps <- rnorm(trunc + n)
x <- sapply(seq_len(n), function(t) sum(cks * rev(eps[t:(t + trunc)])))

# Create a version with 30% missing observations
xt <- x
xt[sample(seq_len(n), size = round(0.30 * n))] <- NA

## ----plot-series--------------------------------------------------------------
op <- par(mfrow = c(2, 1), mar = c(3, 4, 2, 1))
plot.ts(x, main = "Complete ARFIMA(0, 0.25, 0) series", ylab = expression(X[t]))
plot.ts(xt, main = "Same series with 30% missing values", ylab = expression(X[t]))
par(op)

## ----fit-copulas--------------------------------------------------------------
fit_all <- function(series, s = 1, m = 15, empirical = TRUE) {
  c(
    GAUSS = d.fit(
      series, copula = gauss, dCdtheta = dCtheta_gauss,
      s = s, m = m, empirical = empirical
    ),
    FRANK = d.fit(
      series, copula = frank, dCdtheta = dCtheta_frank,
      theta.lower = -100, theta.upper = 100,
      s = s, m = m, empirical = empirical
    ),
    AMH = d.fit(
      series, copula = amh, dCdtheta = dCtheta_amh,
      s = s, m = m, empirical = empirical
    ),
    FGM = d.fit(
      series, copula = fgm, dCdtheta = dCtheta_fgm,
      s = s, m = m, empirical = empirical
    )
  )
}

res_complete <- fit_all(x)
res_missing  <- fit_all(xt)

knitr::kable(
  rbind(Complete = res_complete, `Missing (30%)` = res_missing),
  digits = 3,
  caption = "Estimated long-memory parameter (true d = 0.25)."
)

## ----empirical-vs-gaussian----------------------------------------------------
d_emp <- d.fit(xt, s = 1, m = 15, empirical = TRUE)
d_gau <- d.fit(xt, s = 1, m = 15, empirical = FALSE)
c(Empirical = d_emp, Gaussian = d_gau)

## ----kdens-k1-----------------------------------------------------------------
x_obs <- xt[!is.na(xt)]
dfun <- kdens(x_obs)

hist(
  x_obs, probability = TRUE, breaks = 20, col = "grey90", border = "white",
  main = "Kernel density spline estimate (kdens)", xlab = "x"
)
curve(dfun, add = TRUE, col = "steelblue", lwd = 2)

c(
  K1_empirical = k1fun(dCtheta_gauss, fun = dfun, data = x_obs, empirical = TRUE),
  K1_gaussian  = k1fun(
    dCtheta_gauss, fun = NULL, data = NULL,
    empirical = FALSE, mean = mean(x_obs), sd = sd(x_obs)
  )
)

## ----arbitrary-lags-----------------------------------------------------------
my_lags <- c(1, 2, 4, 8, 12, 16)

# Estimate copula parameters theta_h alone
th <- theta.fit(xt, lags = my_lags)
th

## ----plot-thetas--------------------------------------------------------------
plot(
  my_lags, th, type = "b", pch = 19, col = "steelblue",
  xlab = "Lag (h)", ylab = expression(hat(theta)[h]),
  main = "Estimated Gaussian copula parameter across arbitrary lags"
)
abline(h = 0, lty = 2, col = "grey50")

## ----reuse-thetas-------------------------------------------------------------
c(
  r1 = d.fit(xt, thetas = th, r = 1),
  r2 = d.fit(xt, thetas = th, r = 2)
)

## ----full-output--------------------------------------------------------------
fit_full <- d.fit(xt, lags = my_lags, full.output = TRUE)
str(fit_full)

## ----na-action----------------------------------------------------------------
# Keep only even time indices: odd lags have no complete pairs
x_even <- rep(NA_real_, n)
x_even[seq(2, n, by = 2)] <- x[seq(2, n, by = 2)]

th_omit <- suppressWarnings(
  theta.fit(x_even, lags = 1:6, na.action = "omit")
)
th_approx <- suppressWarnings(
  theta.fit(x_even, lags = 1:6, na.action = "approx")
)

rbind(omit = th_omit, approx = th_approx)

c(
  d_omit   = suppressWarnings(d.fit(x_even, lags = 1:6, na.action = "omit")),
  d_approx = suppressWarnings(d.fit(x_even, lags = 1:6, na.action = "approx"))
)

## ----custom-copula------------------------------------------------------------
plackett <- copula::plackettCopula()
dCtheta_plackett <- function(u, v) u * v * (1 - u) * (1 - v)

d.fit(
  xt = xt,
  copula = plackett,
  dCdtheta = dCtheta_plackett,
  theta.lower = 0.01,
  theta.upper = 50,
  theta.start = 1.5,
  a = 1,
  lags = my_lags
)

