Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
robjhyndman committed Feb 28, 2024
2 parents 118e017 + 7d5a856 commit cc481a8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ S3method(getResponse,lm)
S3method(getResponse,mforecast)
S3method(getResponse,tbats)
S3method(head,ts)
S3method(hfitted,default)
S3method(logLik,ets)
S3method(nobs,ets)
S3method(plot,Arima)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# forecast (development version)
* hfitted now much faster for ARIMA models (danigiro, #949)


# forecast 8.21.1
* nnetar now allows p or P to be 0
Expand Down
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
}
5 changes: 5 additions & 0 deletions R/forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,11 @@ predict.default <- function(object, ...) {
}

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

#' @export
hfitted.default <- function(object, h=1, FUN=NULL, ...) {
if (h == 1) {
return(fitted(object))
}
Expand Down
2 changes: 1 addition & 1 deletion inst/CITATION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bibentry(bibtype = "Article",
title = "Automatic time series forecasting: the forecast package for {R}",
author = c(as.person("Rob J Hyndman"),as.person("Yeasmin Khandakar")),
journal = "Journal of Statistical Software",
volume = 26,
volume = 27,
number = 3,
pages = "1--22",
year = 2008,
Expand Down

0 comments on commit cc481a8

Please sign in to comment.