Congratulations, you have found a bug. Prediction for dynlm
with new data is broken if lagged variables are used. To see why look at the output of
predict(model)
predict(model,newdata=data)
The results should be the same, but they are not. Without newdata
argument, the predict
function basically grabs model
element from the dynlm
output. With newdata
argument predict
tries to form new model matrix from newdata
. Since this involves parsing formula supplied to dynlm
and the formula has function L
, which is defined only internaly in function dynlm
, the incorrect model matrix is formed. If you try to debug, you will see, that the lagged dependent variable is not being lagged in the case of newdata
argument is supplied.
What you can do is to lag the dependent variable and include it in the newdata
. Here is the code illustrating this approach. I use set.seed
so it would be easily reproducible.
library(dynlm)
set.seed(1)
y<-arima.sim(model=list(ar=c(.9)),n=10) #Create AR(1) dependant variable
A<-rnorm(10) #Create independant variables
B<-rnorm(10)
C<-rnorm(10)
y<-y+.5*A+.2*B-.3*C #Add relationship to independant variables
data=cbind(y,A,B,C)
#Fit linear model
model<-dynlm(y~A+B+C+L(y,1),data=data)
Here is the buggy behaviour:
> predict(model)
2 3 4 5 6 7 8 9 10
3.500667 2.411196 2.627915 2.813815 2.468595 1.733852 2.114553 1.423225 1.470738
> predict(model,newdata=data)
1 2 3 4 5 6 7 8 9 10
2.1628335 3.7063579 2.9781417 2.1374301 3.2582376 1.9534558 1.3670995 2.4547626 0.8448223 1.8762437
Form the newdata
#Forecast fix.
A<-c(A,rnorm(1)) #Assume we already have 1-step forecasts for A,B,C
B<-c(B,rnorm(1))
C<-c(C,rnorm(1))
newdata<-ts(cbind(A,B,C),start=start(y),freq=frequency(y))
newdata<-cbind(lag(y,-1),newdata)
colnames(newdata) <- c("y","A","B","C")
Compare forecast with model fit:
> predict(model)
2 3 4 5 6 7 8 9 10
3.500667 2.411196 2.627915 2.813815 2.468595 1.733852 2.114553 1.423225 1.470738
> predict(model,newdata=newdata)
1 2 3 4 5 6 7 8 9 10 11
NA 3.500667 2.411196 2.627915 2.813815 2.468595 1.733852 2.114553 1.423225 1.470738 1.102367
As you can see for historical data the forecast coincides and the last element contains the 1-step ahead forecast.
dynlm
package will not provide forecasts for your dependent variables. Providing forecasts for your dependent variables will require a model to explain them and probably additional data. I suggest you to read something about multivariate regression such as "Applied Multivariate Statistical Analysis" by Johnson and Wichern. or a course on forecasting: duke.edu/~rnau/411home.htm $\endgroup$