Is there a utility to run regressions using xts objects of the following type:
lm(y ~ lab(x, 1) + lag(x, 2) + lag(x,3), data=as.data.frame(coredata(my_xts)))
The dyn and dynlm packages can do that with zoo objects. In the case of dyn just write dyn$lm
instead of lm
and pass it a zoo object instead of a data frame.
Note that lag in xts works the opposite of the usual R convention so if x is of xts class then lag(x, 1) is the same as lag(x, -1) if x were of zoo or ts class.
> library(xts)
> library(dyn)
> x <- xts(anscombe[c("y1", "x1")], as.Date(1:11)) # test data
> dyn$lm(y1 ~ lag(x1, -(1:3)), as.zoo(x))
Call:
lm(formula = dyn(y1 ~ lag(x1, -(1:3))), data = as.zoo(x))
Coefficients:
(Intercept) lag(x1, -(1:3))1 lag(x1, -(1:3))2 lag(x1, -(1:3))3
3.80530 0.04995 -0.12042 0.46631