Reputation: 23
I have made 1000 observations for xt = γ1xt−1 + γ2xt−2 + εt [AR(2)]. What I would like to do is to use the first 900 observations to estimate the model, and use the remaining 100 observations to predict one-step ahead. This is what I have done so far:
data2=arima.sim(n=1000, list(ar=c(0.5, -0.7))) #1000 observations simulated, (AR (2))
arima(data2, order = c(2,0,0), method= "ML") #estimated parameters of the model with ML
fit2<-arima(data2[1:900], c(2,0,0), method="ML") #first 900 observations used to estimate the model
predict(fit2, 100)
But the problem with my code right now is that the n.ahead=100 but I would like to use n.ahead=1 and make 100 predictions in total. I think I need to use for loops for this, but since I am a very new user of Rstudio I haven't been able to figure out how to use for loops to make predictions. Can anyone help me with this?
Upvotes: 2
Views: 1678
Reputation: 31820
If I've understood you correctly, you want one-step predictions on the test set. This should do what you want without loops:
library(forecast)
data2 <- arima.sim(n=1000, list(ar=c(0.5, -0.7)))
fit2 <- Arima(data2[1:900], c(2,0,0), method="ML")
fit2a <- Arima(data2[901:1000], model=fit2)
fc <- fitted(fit2a)
The Arima
command allows a model to be applied to a new data set without the parameters being re-estimated. Then fitted
gives one-step in-sample forecasts.
If you want multi-step forecasts on the test data, you will need to use a loop. Here is an example for two-step ahead forecasts:
fcloop <- numeric(100)
h <- 2
for(i in 1:100)
{
fit2a <- Arima(data2[1:(899+i)], model=fit2)
fcloop[i] <- forecast(fit2a, h=h)$mean[h]
}
If you set h <- 1
above you will get almost the same results as using fitted
in the previous block of code. The first two values will be different because the approach using fitted
does not take account of the data at the end of the training set, while the approach using the loop uses the end of the training set when making the forecasts.
Upvotes: 2