I have a dataset like so:
set.seed(242)
df<- data.frame(month=order(seq(1,20,1),decreasing=TRUE),
psit=sample(1:100,20,replace=TRUE), var=sample(1:10,20,
Try the dyn package which allows lm
to process zoo and other time series objects:
library(dyn)
z <- read.zoo(df)
models <- lapply(1:3, function(i) dyn$lm(psit ~ lag(var, -i), tail(z, 12+i)))
sapply(models, function(x) summary(x)$r.squared)
## [1] 0.31209189 0.04923393 0.09995727
Note that typically if one uses lag k then one also includes all smaller values of k as well. In that case:
models <- lapply(1:3, function(i) dyn$lm(psit ~ lag(var, -(1:i)), tail(z, 12+i)))
do.call("anova", models)
giving:
Model 1: psit ~ lag(var, -(1:i))
Model 2: psit ~ lag(var, -(1:i))
Model 3: psit ~ lag(var, -(1:i))
Res.Df RSS Df Sum of Sq F Pr(>F)
1 10 8688.5
2 9 8221.7 1 466.73 0.4545 0.5192
3 8 8215.5 1 6.24 0.0061 0.9398