Suppose that I have the following "for" loop in R to generate rolling forecasts with model-refitting from a set of four ARMA models. I built the code based on a previous post on the topic (See this link: https://stats.stackexchange.com/questions/208985/selecting-arima-order-using-rolling-forecast)
h <- 1
train <- window(USDlogreturns, end=1162)
test <- window(USDlogreturns, start=1163)
n <- length(test) - h + 1
fit1 <- Arima(train, order=c(0,0,0), include.mean=TRUE, method="ML")
fit2 <- Arima(train, order=c(0,0,1), include.mean=TRUE, method="ML")
fit3 <- Arima(train, order=c(1,0,0), include.mean=TRUE, method="ML")
fit4 <- Arima(train, order=c(1,0,1), include.mean=TRUE, method="ML")
fc1 <- ts(numeric(n), start=1163+1, freq=1)
fc2 <- ts(numeric(n), start=1163+1, freq=1)
fc3 <- ts(numeric(n), start=1163+1, freq=1)
fc4 <- ts(numeric(n), start=1163+1, freq=1)
for(i in 1:n)
{
x <- window(USDlogreturns, end=1162 + i)
refit1 <- Arima(x, model=fit1, include.mean=TRUE, method="ML")
refit2 <- Arima(x, model=fit2, include.mean=TRUE, method="ML")
refit3 <- Arima(x, model=fit3, include.mean=TRUE, method="ML")
refit4 <- Arima(x, model=fit4, include.mean=TRUE, method="ML")
fc1[i] <- forecast(refit1, h=h)$mean[h]
fc2[i] <- forecast(refit2, h=h)$mean[h]
fc3[i] <- forecast(refit3, h=h)$mean[h]
fc4[i] <- forecast(refit4, h=h)$mean[h]
}
result.fc<-cbind(fc1, fc2, fc3, fc4)
The following code computes various forecasting accuracy measures (see this link for a description of these measures: http://127.0.0.1:15135/library/forecast/html/accuracy.html).
accuracy(fc1, test)[,1:5]
accuracy(fc2, test)[,1:5]
accuracy(fc3, test)[,1:5]
accuracy(fc4, test)[,1:5]
My question is:
How can I tell the loop to rank the four estimated models by the five forecasting accuracy measures as above in five distinct matrices?
Thank you for your help.