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

查看:20
本文介绍了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 是一个 xts 对象,其中包含一个 x 和一个 y.问题的关键是有没有办法避免做一堆滞后和合并以使 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 中的 lag 与通常的 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天全站免登陆