简体   繁体   English

R中的时间序列回归效率:如何更好地做到这一点?

[英]Efficiency in time-series regression in R: How can I do this better?

I am working on time series, and want to check all the lagged differences for significance(and essentially doing a dickey-fuller test by hand) but that's not important. 我正在研究时间序列,并且想检查所有滞后差异的显着性(并且本质上是手工进行dickey-fuller测试),但这并不重要。 I can do it, but it's really mechanical, and there must be a way to do this more elegantly. 我可以做到,但是它确实是机械的,必须有一种更优雅的方法。 Or at least more efficiently. 或至少更有效。 Any ideas? 有任何想法吗?

y <- log.real.gdp.ts

delta.y.t <- diff(y,differences=1)
lag.y <- lag(y, -1)
L1Dy <- lag(delta.y.t, k=-1)
L2Dy <- lag(delta.y.t, k=-2)    
L3Dy <- lag(delta.y.t, k=-3)    
L4Dy <- lag(delta.y.t, k=-4)    
L5Dy <- lag(delta.y.t, k=-5)    
L6Dy <- lag(delta.y.t, k=-6)    
L7Dy <- lag(delta.y.t, k=-7)    
L8Dy <- lag(delta.y.t, k=-8)    
L9Dy <- lag(delta.y.t, k=-9)    
L10Dy <- lag(delta.y.t, k=-10)  
L11Dy <- lag(delta.y.t, k=-11)  
L12Dy <- lag(delta.y.t, k=-12)  

d = ts.union(delta.y.t, lag.y, L1Dy, L2Dy, L3Dy, L4Dy, L5Dy, L6Dy, L7Dy, L8Dy, L9Dy, L10Dy, L11Dy, L12Dy)               ## takes care of NA's

lm.model.III <- lm(delta.y.t~ lag.y + time(lag.y) + L1Dy + L2Dy + L3Dy + L4Dy + L5Dy + L6Dy + L7Dy + L8Dy + L9Dy + L10Dy + L11Dy + L12Dy, data=d)

I'd really like some kind of loop where I can generate 1:n lagged differences, and then some way to insert all n into my linear model, like 我真的很喜欢某种可以生成1:n滞后差的循环,然后通过某种方式将所有n插入线性模型,例如

 lm.model.III <- lm(delta.y.t ~ lag.y + time(lag.y) + lagged.diffs.mts)

how about 怎么样

require(zoo)

delta.y.t <- diff(y,differences=1)
lag.y <- lag(y, -1)
L1Dy <- lag(delta.y.t, -(0:12), na.pad=T)

#for any regression you can then access the number of lags you want:
# 0 lag and na.pad=T are crucial

lm(lag.y ~ L1Dy[,1:5])

Hope this helps 希望这可以帮助

-Chris -克里斯

The package dynlm adds handling of time series operators to R formulas: dynlm软件包将时间序列运算符的处理添加到R公式中:

The interface and internals of dynlm are very similar to lm , but currently dynlm offers three advantages over the direct use of lm : 1. extended formula processing, 2. preservation of time series attributes, 3. instrumental variables regression (via two-stage least squares). dynlm的界面和内部与lm非常相似,但是与直接使用lm相比, dynlm三个优点:1.扩展公式处理,2.保存时间序列属性,3.工具变量回归(至少通过两阶段)方块)。 For specifying the formula of the model to be fitted, there are additional functions available which allow for convenient specification of dynamics (via d() and L() ) or linear/cyclical patterns (via trend() , season() , and harmon() ). 为了指定要拟合的模型的公式,还有其他可用的函数,这些函数允许方便地指定动力学(通过d()L() )或线性/循环模式(通过trend()season()harmon() )。 All new formula functions require that their arguments are time series objects (ie, " ts " or " zoo "). 所有新的公式函数均要求其参数为时间序列对象(即“ ts ”或“ zoo ”)。

Here is an example: 这是一个例子:

library(foreign)
library(zoo)
library(dynlm)

dfKlein = read.dta('http://www.stata-press.com/data/r12/klein.dta')
summary(dfKlein)

zooKlein = as.zoo(dfKlein, order.by = dfKlein$year)
lmKlein = dynlm(consump ~ L(profits, 1) + profits + wagetot, 
                data = zooKlein)
summary(lmKlein)

Note, in particular, that it allows you to specify a vector of lags in the formula object, such as y ~ L(y, 1:4) . 请特别注意,它允许您在公式对象中指定滞后向量,例如y ~ L(y, 1:4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM