Skip to content

Commit 55e7ed9

Browse files
Include d and mu in ARFIMA model estimates
1 parent 8562276 commit 55e7ed9

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

R/arfima.R

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,21 @@ train_arfima <- function(
325325
y, nar = p, nma = q,
326326
drange = specials$pdq[[1L]]$d_range
327327
)
328-
d <- fit$d
328+
329+
fd_par <- data.frame(
330+
term = c("mu", "d"),
331+
estimate = c(y_mu, fit$d),
332+
std.error = c(NA_real_, fit$stderror[[1L]]),
333+
statistic = c(NA_real_, dz <- fit$d/fit$stderror[[1L]]),
334+
p.value = c(NA_real_, 2 * (1 - pnorm(abs(dz))))
335+
)
329336

330337
# Refine ARIMA coefficients with MLE
331338
yd <- fracdiff(y, fit$d)
332339
.data[[measured_vars(.data)]] <- yd
333340
fit <- train_arima(.data, specials, ...)
334-
fit$spec$d <- d
335-
fit$spec$mu <- y_mu
341+
342+
fit$par <- rbind(fd_par, fit$par)
336343

337344
class(fit) <- c("fbl_ARFIMA", class(fit))
338345
fit
@@ -343,22 +350,18 @@ model_sum.fbl_ARFIMA <- function(x) {
343350
sprintf(
344351
"ARFIMA(%i,%.2f,%i)%s",
345352
x$spec$p,
346-
x$spec$d,
353+
x$par$estimate[x$par$term == "d"],
347354
x$spec$q,
348355
if (isTRUE(x$spec$constant)) " w/ mean" else ""
349356
)
350357
}
351358

352359
#' @export
353360
fitted.fbl_ARFIMA <- function(object, ...) {
354-
355-
# @RH: The {forecast} package does strange things with the fitted residuals
356-
# (perhaps it doesn't fractionally integrate them?)
357361
fracdiffinv(
358362
NextMethod(),
359-
d = object$spec$d,
360-
# xi = ??? -- are there suitable non-zero initial values for this?
361-
) + object$spec$mu
363+
d = object$par$estimate[object$par$term == "d"],
364+
) + object$par$estimate[object$par$term == "mu"]
362365
}
363366

364367
#' @importFrom stats frequency
@@ -380,14 +383,16 @@ forecast.fbl_ARFIMA <- function(
380383
h <- length(fc)
381384

382385
# Extract ARMA coefficients
386+
mu <- object$par$estimate[object$par$term == "mu"]
383387
p <- object$spec$p
388+
d <- object$par$estimate[object$par$term == "d"]
384389
q <- object$spec$q
385390
phi <- theta <- numeric(h)
386391
phi[seq_len(p)] <- object$model$coef[grep("^ar", names(object$model$coef))]
387392
theta[seq_len(q)] <- object$model$coef[grep("^ma", names(object$model$coef))]
388393

389394
# Binomial coefficient for expansion of d
390-
bin.c <- (-1)^(0:(n + h)) * choose(object$spec$d, (0:(n + h)))
395+
bin.c <- (-1)^(0:(n + h)) * choose(d, (0:(n + h)))
391396

392397
# Calculate psi weights
393398
new.phi <- psi <- numeric(h)
@@ -404,7 +409,7 @@ forecast.fbl_ARFIMA <- function(
404409

405410
distributional::dist_normal(
406411
# Fractionally integrated mean
407-
mu = fracdiffinv(mean(fc), d = object$spec$d, xi = y_train) + object$spec$mu,
412+
mu = fracdiffinv(mean(fc), d = d, xi = y_train) + mu,
408413
# Approximate standard error via psi-weights
409414
sigma = sqrt(cumsum(psi^2) * object$fit$sigma2)
410415
)
@@ -434,9 +439,11 @@ generate.fbl_ARFIMA <- function(
434439
y_init <- y_init[seq_len((t_init - x$tsp$range[1])/t_chronon)]
435440

436441
# Fractionally integrate simulated data
442+
mu <- object$par$estimate[object$par$term == "mu"]
443+
d <- object$par$estimate[object$par$term == "d"]
437444
transmute(
438445
group_by_key(.sim),
439-
".sim" := fracdiffinv(x = .data$.sim, d = x$spec$d, xi = y_init) + x$spec$mu
446+
".sim" := fracdiffinv(x = .data$.sim, d = d, xi = y_init) + mu
440447
)
441448
}
442449

@@ -461,16 +468,20 @@ refit.fbl_ARFIMA <- function(
461468
}
462469

463470
# Fractionally difference the new data
471+
mu <- object$par$estimate[object$par$term == "mu"]
472+
d <- object$par$estimate[object$par$term == "d"]
464473
y <- unclass(new_data)[[measured_vars(new_data)]]
465-
yd <- fracdiff(y - object$spec$mu, object$spec$d)
474+
yd <- fracdiff(y - mu, d)
466475
new_data[[measured_vars(new_data)]] <- yd
467476

468477
# Refit ARMA model
469478
fit <- NextMethod()
470479

471480
# Class the results
472-
fit$spec$d <- object$spec$d
473-
fit$spec$mu <- object$spec$mu
481+
fit$par <- rbind(
482+
object$par[object$par$term %in% c("mu", "d"), , drop = FALSE],
483+
fit$par
484+
)
474485
class(fit) <- c("fbl_ARFIMA", class(fit))
475486
fit
476487
}

0 commit comments

Comments
 (0)