Fairy
Fairy

Reputation: 389

Returning predicted values of a forecasting model as a matrix

I have a data set and I am using auto.arima in forecast package to do the forecasting. I want to save the forecast results into a matrix for further analysis. I make an empty data frame before doing the analysis using the following line of code:

    predictions <- data.frame(point = numeric(), Lo80= numeric(), High80= numeric(), Lo95= numeric(), High95= numeric())

And then I want to add forecasts for (ie. 7 days ) to this data frame by using the following lines of code:

    model <- auto.arima(x)
    pred <- forecast(model,h=7,l=c(50,80))
    for (kk in 1:7){
    predictions <- rbind(predictions, data.frame(point = pred[kk,1], Lo50= pred[kk,2], High50= pred[kk,3], Lo80= pred[kk,4], High80= pred[kk,5])}

However I am getting an error and the reason is that the pred values are not in a matrix format and there is no dimension defined for them. Is there anyway that I can store the results as I desire. A sample of "pred" is as follows:

             Point Forecast     Lo 80    Hi 80     Lo 95    Hi 95
                53.14286       19.84681  6.384444 33.30918 -0.742103      
                53.42857       24.46304 10.016633 38.90945  2.369167 

Upvotes: 0

Views: 2301

Answers (2)

Rob Hyndman
Rob Hyndman

Reputation: 31820

library(forecast)
model <- auto.arima(x)
pred <- as.data.frame(forecast(model, h=7, level=c(50,80)))

Upvotes: 2

Rorschach
Rorschach

Reputation: 32436

summary(pred) will give you a data.frame like you want, and you can convert it to a matrix using as.matrix.

library(forecast)
model <- auto.arima(WWWusage)
pred <- forecast(model, h=7, level=c(50,80))

predDF <- summary(pred)
predMatrix <- as.matrix(predDF)

Upvotes: 0

Related Questions