Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-step fitted ARIMA faster solution #949

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions R/arima.R
Original file line number Diff line number Diff line change
Expand Up @@ -964,3 +964,30 @@ is.Arima <- function(x) {
fitted.ar <- function(object, ...) {
getResponse(object) - residuals(object)
}

hfitted.Arima <- function(object, h, ...) {
# As implemented in Fable
if(h == 1){
return(object$fitted)
}
y <- object$fitted+residuals(object, "innovation")
yx <- residuals(object, "regression")
# Get fitted model
mod <- object$model
# Reset model to initial state
mod <- stats::makeARIMA(mod$phi, mod$theta, mod$Delta)
# Calculate regression component
xm <- y - yx
fits <- rep_len(NA_real_, length(y))

start <- length(mod$Delta) + 1
end <- length(yx) - h
idx <- if(start > end) integer(0L) else start:end
for(i in idx) {
fc_mod <- attr(stats::KalmanRun(yx[seq_len(i)], mod, update = TRUE), "mod")
fits[i + h] <- stats::KalmanForecast(h, fc_mod)$pred[h] + xm[i+h]
}
fits <- ts(fits)
tsp(fits) <- tsp(object$x)
fits
}
4 changes: 4 additions & 0 deletions R/forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ predict.default <- function(object, ...) {
}

hfitted <- function(object, h=1, FUN=NULL, ...) {
UseMethod("hfitted")
}

hfitted.default <- function(object, h=1, FUN=NULL, ...) {
if (h == 1) {
return(fitted(object))
}
Expand Down