Sweepy Dodo
Sweepy Dodo

Reputation: 1863

Looping over forecast functions in tsCV

I have a loop below looping over 4 time series forecast methods in tsCV (rolling forecast origin). The 3rd method in list, y, does not break the loop. However, the results table, z, whose purpose is to store MAE (mean absolute error) records NaN for this user defined function.

This forecast function is user defined as I need to specify damped=T

library(forecast)
x <- 8  # t +
y <- list(ses
          , holt
          , function(j, k){forecast(holt(j, h=k, damped=T))}, hw
          )
z <- list()

for (i in seq_along(y))
{
  a <- data.frame(tsCV(AirPassengers, y[[i]], h=x))
  a[1:12, ] <- NA  # 1st 12 months' forecast likely to be meaningless
  b <- colMeans(abs(a), na.rm=T)
  c <- data.frame(model = i
                  ,h = 1:x
                  ,mae = b
                  )
  z[[i]] <- c
}

z <- do.call(rbind, z)
z$model <- as.factor(z$model)

I got my idea for the user defined function here: http://pkg.robjhyndman.com/forecast/reference/tsCV.html#see-also

Thank you.

Upvotes: 2

Views: 287

Answers (1)

MrFlick
MrFlick

Reputation: 206167

As per the documentation for tsCV, the supplied function needs to have an argument actually named "h". So just change your function to

y <- list(ses, holt, function(j, h){forecast(holt(j, h=h, damped=T))}, hw)

When you do that, no NaN values are returned.

Upvotes: 4

Related Questions