Reputation: 3
I have searched through the forum and has not been able to find any specific information that has been able to solve the below.
The main question is how to export (1) the model information (chosen model and parameters) from the forecast function, and (2) the accuracy information (MAPE, MSE etc.) from the generated forecast?
I am able to find the information just by calling the object forecast(retail)$model but not able to actually output the information column by column in the file. This has presumably something to do with the predefined matrix.
The only post I have been able to find is the folloing, but not been able to apply it onto my example. Export accuracy of multiple timeseries forecasts in r into csv-document
I am working of the example from the post: https://robjhyndman.com/hyndsight/batch-forecasting/
The following code loops through in a for loop columns by columns and generates a statistical point forecast (mean object) based on the forecast() function and outputs to a column by column csv file.
library(forecast)
retail <- read.csv("https://robjhyndman.com/data/ausretail.csv",header=FALSE)
retail <- ts(retail[,-1],f=12,s=1982+3/12)
ns <- ncol(retail)
h <- 24
fcast <- matrix(NA,nrow=h,ncol=ns)
for(i in 1:ns)
fcast[,i] <- forecast(retail[,i],h=h)$mean
write(t(fcast),file="retailfcasts.csv",sep=",",ncol=ncol(fcast))
Hope someone is able to help out with refactoring the above code.
Highly appreciate all the help / guidance you can provide.
Upvotes: 0
Views: 1298
Reputation: 621
You can use the following code to successfully export a csv
file.
After you have created your matrix, do this:
fcast <- as.data.frame(fcast)
write.table(fcast, file="retailfcasts.csv", quote=F, sep=";", dec=",", na="", row.names=T, col.names=T)
Is the solution you were looking for?
Upvotes: 0