Singe Exponential Smoothing Forecasting with a loop

Hello everyone,

I have a question regarding Rolling Forecasting. I am currently working with the following loop. This loop gives a multi-step forecast with a re-estimation for 1-steps ahead. For each new value a new Single Exponential Smoothing model is calculated, right? But I would like to have a model where at the beginning a SES model is calculated for an alpha over the training set and this alpha is applied as rolling forecast to the values of the training set. With the Arima model (also enclosed) I manage to use the same ARIMA model. To sum it up I am looking for a loop not using re-estimation for the SES Model but for the values. Please help me to solve the problem for SES and similarly for holt() and stlf().

Best regards
Luke

#Timeseries
ts <- ts(data03$demand, ###CHANGE DATA
start = c(2016,1),
frequency = 52)

train <- window(ts,
start = c(2016, 1),
end = c(2018, 52))

test <- window(ts,
start = c(2019, 1),
end = c(2019, 52))

SES with reestimation

h <- 1
n <- length(test) - h + 1
ses_mat <- matrix(0, nrow=n, ncol=h)
for(i in 1:n)
{
x <- window(ts, end=c(2018, 52) + (i-1)/52)
refit <- ses(x, fan=FALSE, initial = c("optimal"),
alpha = NULL, lambda = NULL, biasadj = FALSE)
ses_mat[i,] <- forecast(refit, h=h)$mean
}
ses_mat2 <- ts(ses_mat, start=c(2019,1), end=c(2019,52), frequency=52)
accuracy(ses_mat2, test)
ses_mat2 %>% autopilot()

#for comparison my ARIMA Model without reestimation
fit <- auto.arima(train)
order <- arimaorder(fit)
fcmat <- matrix(0, nrow=n, ncol=h)
for(i in 1:n)
{
x <- window(ts, end=c(2018, 52) + (i-1)/52)
refit <- Arima(x, order=order[1:3], seasonal=order[4:6])
fcmat[i,] <- forecast(refit, h=h)$mean
}

ses(x,h) is a wrapper to forecast(ets(x, model="ANN"), h). To apply the model without re-estimation, you need to use ets() directly. Similarly for holt() and hw().

library(forecast)

ts <- ts(rnorm(52 * 4),
  start = c(2016, 1),
  frequency = 52
)
train <- window(ts,
  start = c(2016, 1),
  end = c(2018, 52)
)
test <- window(ts,
  start = c(2019, 1),
  end = c(2019, 52)
)

# SES without reestimation
h <- 1
n <- length(test) - h + 1
ses_mat <- matrix(0, nrow = n, ncol = h)
fit <- ets(train, model = "ANN")
for (i in seq(n)) {
  x <- window(ts, end = c(2018, 52) + (i - 1) / 52)
  refit <- ets(x, model = fit, use.initial.values = FALSE)
  ses_mat[i, ] <- forecast(refit, h = h)$mean
}

This formulation will use the same alpha parameter obtained from the model to all the training data, but it will re-estimate the initial state for each iteration. If you want to use the same initial state as well, set use.initial.values = TRUE.

Your ARIMA loop did use re-estimation. All you were doing was choosing the same model structure as for the training data, but you were re-estimating the model at each iteration. To do it without re-estimation, you can do this:

# ARIMA without reestimation
fit <- auto.arima(train)
fcmat <- matrix(0, nrow = n, ncol = h)
for (i in seq(n)) {
  x <- window(ts, end = c(2018, 52) + (i - 1) / 52)
  refit <- Arima(x, model = fit)
  fcmat[i, ] <- forecast(refit, h = h)$mean
}

Created on 2020-07-23 by the reprex package (v0.3.0)

Thank you for your fast reply. It really helped me a lot.
The only problem I still have left is, that I have a seasonal frequency of 52, which does not allow me to use ets() or hw() and I therefore use stlf().
However that is my current model, but I keep receiving error messages (model fits to more given arguments and 'start' value didn't change):

stlf_order <- stlf(x, h, damped=NULL, alpha=NULL, beta=NULL,
gamma=NULL, phi=NULL, lambda=NULL, biasadj=FALSE,
s.window="periodic", robust=FALSE, method = "ets")
stlf_mat <- matrix(0, nrow=n, ncol=h)
for(i in 1:n)
{
x <- window(ts, h, end=c(2018, 52) + (i-1)/52)
refit <- stlf(x, model = stlf_order)
stlf_mat[i,] <- forecast(refit, h=h)$mean
}

stlf() produces forecasts. You need stlm() which creates the model.

library(forecast)

ts <- ts(rnorm(52 * 4),
         start = c(2016, 1),
         frequency = 52
)
train <- window(ts,
                start = c(2016, 1),
                end = c(2018, 52)
)
test <- window(ts,
               start = c(2019, 1),
               end = c(2019, 52)
)

# STLF without reestimation
h <- 1
n <- length(test) - h + 1
stlf_mat <- matrix(0, nrow = n, ncol = h)
fit <- stlm(train)
for (i in seq(n)) {
  x <- window(ts, end = c(2018, 52) + (i - 1) / 52)
  refit <- stlm(x, model = fit)
  stlf_mat[i, ] <- forecast(refit, h = h)$mean
}

Created on 2020-07-24 by the reprex package (v0.3.0)

1 Like

Just to make sure: But I can use stlf for: # Multi-step forecasts, re-estimation, 1-steps ahead?

stlf_mat <- matrix(0, nrow=n, ncol=h)
for(i in 1:n)
{  
  x <- window(ts, h, end=c(2018, 52) + (i-1)/52)
  refit <- stlf(x, h, damped=NULL, alpha=NULL, beta=NULL,
                gamma=NULL, phi=NULL, lambda=NULL, biasadj=FALSE,
                s.window="periodic", robust=FALSE, method = "ets")
  stlf_mat[i,] <- forecast(refit, h=h)$mean
} 

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.