REWRITING ORIGINAL QUESTION
I want a function that takes a vector and pre-defined exponential smoothing model (in this example, simple exponential smoothing with alpha = 0.5), and does one-step ahead forecasting on the input vector. The below code, though clunky, does what I want. Is there a better way to do this, or a built in function / package function that does this?
The second part to this question is: If I fit an ETS or ARIMA model with, say, the {fable}
package, is there a function that can take the fitted ETS/ARIMA model and do the same one-step-ahead forecasting on a vector, like in the first part?
# Ref: # https://stats.stackexchange.com/questions/44984/how-do-you-use-simple-exponential-smoothing-in-r
# NOT fully tested, just in this case. Gives one-step-ahead forecasts for an exponential smoothing model
ses <- function(x, alpha, beta = FALSE, gamma = FALSE) {
## Populate this vector with return values
result_vec <- numeric(length(x))
result_vec[1] <- NA
for (i in 2:length(x)) {
mod <- HoltWinters(x[seq(i)], alpha=alpha, beta=beta, gamma=gamma)
result_vec[i] <- predict(mod, n.ahead = 1)
}
result_vec
}
new_wt <- c(1, 0, 0, 0, 0, 1, 1, 1)
ses(new_wt, 0.5)
#> [1] NA 0.5000000 0.2500000 0.1250000 0.0625000 0.5312500 0.7656250
#> [8] 0.8828125
Created on 2020-10-22 by the reprex package (v0.3.0)
This is the question and post I was looking for that solved it for me:
https://stats.stackexchange.com/questions/265115/one-step-ahead-forecast https://robjhyndman.com/hyndsight/out-of-sample-one-step-forecasts/
I was then able to leverage the
forecast::ets()
model builder to build what I want for a filter, and then use the technique there of re-callingets()
with the new data I want to do one-step-ahead forecasts on (and the original model), and then callfitted()
on that new model to get the one-step-ahead forecasts. It matches up close enough with what I want a filter to do (with some off-by-one differences in the output that I can deal with easily enough).It mostly worked with the
{fable}
packageETS()
call. However, I don't know how to specify the starting values to make sure we match. But it is pretty close overall, and over time, should roughly match.Below is sample code with what I had and what the ultimate solution was in the end.
Created on 2020-10-22 by the reprex package (v0.3.0)