0

I'm using Rob Hyndman's batch forecasting approach to forecast for multiple columns in a dataframe. My code is as follows:

require(forecast)

zips <- read.csv(file.choose(), header = T)
zips <- zips[,-c(1,2)]
ns <- ncol(zips)

zips <- ts(zips, frequency = 12, start = c(2005,1), end = c(2014,12))
zips <- HoltWinters(zips, seasonal = "mult")

h <- 24

fcast <- matrix(NA, nrow=h, ncol=ns)
for(i in 1:ns) {
    fcast[,i] <- forecast.HoltWinters(zips[,i], h=h)
}

write(t(fcast), file="fcast.csv", sep=",",ncol=ncol(fcast))

Although it works just fine when using the regular forecast function, I keep getting the error

[Error in zips[,i] : incorrect number of dimensions]

How do I get this HoltWinters forecast to run using this loop I have constructed here?

989
  • 12,579
  • 5
  • 31
  • 53

3 Answers3

0

Try storing your forecasts to list instead of a matrix. And also, forecast.HoltWinters function requires object of class HoltWinters which is produced by HoltWinters function that requires a vector as input. Apart from point forecasts, by default the forecast.HoltWinters function produces also the prediction interval bounds.

fcast <- list()

for(i in 1:ns) {

  zips_fit <- HoltWinters(zips[, i], seasonal = "mult")
  fcast[[i]] <- forecast.HoltWinters(zips_fit, h = h)
}
Miha Trošt
  • 2,002
  • 22
  • 25
0

Even I'm also using similar kind of batch processing for Holts Winter forecasting method, but I use the function hw from forecast package created by Professor Rob J Hyndman. Anyhow for your question I prefer to use $mean in forecasting. i.e

fcast <- matrix(NA, nrow=h, ncol=ns) 
for(i in 1:ns) {
    fcast[,i] <- forecast.HoltWinters(zips[,i], h=h)`$mean`
}

Try this once!

gkubed
  • 1,849
  • 3
  • 32
  • 45
MUNIKUMAR N M
  • 13
  • 1
  • 4
0

hw() function gives forecast values anyway. to get point forecast you may use $mean (as used in the following code). you could also use seasonal = "additive" or seasonal = "multiplicative" arguments.

Good luck

best

require(forecast)

    zips <- read.csv(file.choose(), header = T)

    zips <- ts(zips, frequency = 12, start = c(2005,1), end = c(2014,12))

    ns <- ncol(zips)
    h <- 24

    fcast <- matrix(NA, nrow=h, ncol=ns)
    for(i in 1:ns) {fcast[,i] <- hw(zips[,i],h=h)$mean

    }

    write(t(fcast), file="fcast.csv", sep=",",ncol=ncol(fcast))
Econ_matrix
  • 405
  • 3
  • 9