I have a model that looks like
lm(y ~ lag(x, -1) + lag(z, -1))
So basically, this is a time series regression with exogenous variables, and I want to carry out a rolling analysis of sample forecasts, meaning that:
I first used a subsample (e.g., 1990-1995) for estimation, then I performed a one step ahead forecast, then I added one observation and made another one step ahead forecast, and so on.
I have tried to work with rollapply
, defining the model as arima(0,0,0)
with xreg=lags
of the other variables, but that doesn't work.
Your help would be much appreciated!
Best Answer
Here's a brute-force method, which in general I prefer if a) I can't find an appropriate R function in about 3 minutes, and b) I can see that the brute force function's going to be easy to write.
First, I would start by realigning the variables in a data frame so you don't need to use the lag function:
Then, define a prediction function:
Of course, you could make it more terse, but I'm trying to make it (a little) clearer than it could be. Using this function looks like:
It should be pretty obvious how to change the function to accept a parameter for the forecast horizon. If you want your one-step-ahead forecasts to always use the same number of data points in the history (instead of growing the number of data points in the history by one each step) that's a pretty simple change too. I pass the formula for the regression function in "f" so that, if I'm comparing different models, I don't have to change the interior of predict.1 each time.