regressions with xts in R

前端 未结 2 788
野性不改
野性不改 2021-02-06 18:32

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)))
         


        
2条回答
  •  心在旅途
    2021-02-06 18:57

    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  
    

提交回复
热议问题