Skip to content

Commit

Permalink
Merge pull request #950 from mitchelloharawild/hfitted-ets
Browse files Browse the repository at this point in the history
Added faster and better hfitted method for ETS model
  • Loading branch information
robjhyndman committed Mar 1, 2024
2 parents cc481a8 + 7f2e837 commit 208768a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ S3method(getResponse,mforecast)
S3method(getResponse,tbats)
S3method(head,ts)
S3method(hfitted,default)
S3method(hfitted,ets)
S3method(logLik,ets)
S3method(nobs,ets)
S3method(plot,Arima)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# forecast (development version)
* hfitted now much faster for ARIMA models (danigiro, #949)

* hfitted now much faster for ETS models, and produces fitted values from
initial states (#950)

# forecast 8.21.1
* nnetar now allows p or P to be 0
Expand Down
20 changes: 20 additions & 0 deletions R/ets.R
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,26 @@ fitted.ets <- function(object, h=1, ...) {
}
}

#' @export
hfitted.ets <- function(object, h=1, ...) {
n <- length(object$x)
out <- rep(NA_real_, n)
for(i in seq_len(n-h+1)) {
out[i+h-1] <- .C(
"etsforecast",
as.double(object$states[i, ]),
as.integer(object$m),
as.integer(switch(object$components[2], "N" = 0, "A" = 1, "M" = 2)),
as.integer(switch(object$components[3], "N" = 0, "A" = 1, "M" = 2)),
as.double(ifelse(object$components[4] == "FALSE", 1, object$par["phi"])),
as.integer(h),
as.double(numeric(h)),
PACKAGE = "forecast"
)[[7]][h]
}
out
}

#' @export
logLik.ets <- function(object, ...) {
structure(object$loglik, df = length(object$par) + 1, class = "logLik")
Expand Down

0 comments on commit 208768a

Please sign in to comment.