R中xts的回归 [英] regressions with xts in R

查看:118
本文介绍了R中xts的回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在使用以下类型的xts对象运行回归的实用程序:

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

其中,my_xts是包含xyxts对象.问题的重点在于,是否有一种方法可以避免发生一堆滞后,并合并所有滞后的data.frame?我认为包dyn适用于zoo对象,因此我希望它与xts相同,但是可能会有更新.

where my_xts is an xts object that contains an x and a y. The point of the question is is there a way to avoid doing a bunch of lags and merges to have a data.frame with all the lags? I think that the package dyn works for zoo objects so i would expect it to work the same way with xts but though there might be something updated.

推荐答案

dyn和dynlm软件包可以对Zoo对象执行此操作.对于dyn,只需写入dyn$lm而不是lm,然后将其传递给Zoo对象而不是数据帧.

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.

请注意,xts中的滞后与通常的R约定相反,因此,如果x为xts类,则如果x为zoo或ts类,则lag(x,1)与lag(x,-1)相同.

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  

这篇关于R中xts的回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆