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?
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)
}
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. ie
fcast <- matrix(NA, nrow=h, ncol=ns)
for(i in 1:ns) {
fcast[,i] <- forecast.HoltWinters(zips[,i], h=h)`$mean`
}
Try this once!
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.