R:NeweyWest 使用 dynlm 的 HAC [英] R: HAC by NeweyWest using dynlm
问题描述
这就是我想做的:
library("lmtest")
library("dynlm")
test$Date = as.Date(test$Date, format = "%d.%m.%Y")
zooX = zoo(test[, -1], order.by = test$Date)
f <- d(Euribor3) ~ d(Ois3) + d(CDS) + d(Vstoxx) + d(log(omo)) + d(L(Euribor3))
m1 <- dynlm(f, data = zooX, start = as.Date("2005-01-05"),end = as.Date("2005-01-24"))
m2 <- dynlm(f, data = zooX, start = as.Date("2005-01-25"), end=as.Date("2005-02-14"))
summary(m1)
summary(m2)
coeftest(m1, vcov=NeweyWest)
coeftest(m2, vcov=NeweyWest)
函数summary(m1)
没有问题但是,如果我想通过 NeweyWest 使用 HAC,即coeftest(m1, vcov=NeweyWest)
我收到以下错误消息,但我不知道为什么:Error in na.fail.default(as.ts(x)) : missing values in object
为了使用 coeftest()
获得结果,我必须对代码进行哪些更改?注意:从样本数据中可以看出,没有缺失值.非常感谢!
There is no problem with the function summary(m1)
However if I want to use HAC by NeweyWest i.e.coeftest(m1, vcov=NeweyWest)
I get the following error message and I do not know why: Error in na.fail.default(as.ts(x)) : missing values in object
What do I have to change on my code in order to get the result using coeftest()
? Note: there are no missing values as you can see from the sample data. Many thanks!
样本数据:
Date Euribor3 Ois3 Vstoxx CDS omo
1 03.01.2005 2.154 2.089 14.47 17.938 344999
2 04.01.2005 2.151 2.084 14.51 17.886 344999
3 05.01.2005 2.151 2.087 14.42 17.950 333998
4 06.01.2005 2.150 2.085 13.80 17.950 333998
5 07.01.2005 2.146 2.086 13.57 17.913 333998
6 10.01.2005 2.146 2.087 12.92 17.958 333998
7 11.01.2005 2.146 2.089 13.68 17.962 333998
8 12.01.2005 2.145 2.085 14.05 17.886 339999
9 13.01.2005 2.144 2.084 13.64 17.568 339999
10 14.01.2005 2.144 2.085 13.57 17.471 339999
11 17.01.2005 2.143 2.085 13.20 17.365 339999
12 18.01.2005 2.144 2.085 13.17 17.214 347999
13 19.01.2005 2.143 2.086 13.63 17.143 354499
14 20.01.2005 2.144 2.087 14.17 17.125 354499
15 21.01.2005 2.143 2.087 13.96 17.193 354499
16 24.01.2005 2.143 2.086 14.11 17.283 354499
17 25.01.2005 2.144 2.086 13.63 17.083 354499
18 26.01.2005 2.143 2.086 13.32 17.348 347999
19 27.01.2005 2.144 2.085 12.46 17.295 352998
20 28.01.2005 2.144 2.084 12.81 17.219 352998
21 31.01.2005 2.142 2.084 12.72 17.143 352998
22 01.02.2005 2.142 2.083 12.36 17.125 352998
23 02.02.2005 2.141 2.083 12.25 17.000 357499
24 03.02.2005 2.144 2.088 12.38 16.808 357499
25 04.02.2005 2.142 2.084 11.60 16.817 357499
26 07.02.2005 2.142 2.084 11.99 16.798 359999
27 08.02.2005 2.141 2.083 11.92 16.804 355500
28 09.02.2005 2.142 2.080 12.19 16.589 355500
29 10.02.2005 2.140 2.080 12.04 16.500 355500
30 11.02.2005 2.140 2.078 11.99 16.429 355500
31 14.02.2005 2.139 2.078 12.52 16.042 355500
编辑我认为问题出在这个命令上:zooX = zoo(test[, -1], order.by = test$Date)
,即函数order.by()
.如果你删除这部分,其他的都可以通过NeweyWest计算HAC.(当然你也需要把start = as.Date("2005-01-25")
改成索引号,例如start=50
.)但是删除它会丢失回归输出中的开始和结束日期,这非常有用.所以如果有人知道解决方法,请告诉我!
EditI think the problem is in this command: zooX = zoo(test[, -1], order.by = test$Date)
, namely the function order.by()
. If you delete this part, all else equal you can compute HAC by NeweyWest.( Of course you also need to change start = as.Date("2005-01-25")
to the index number, e.gstart=50
.) But by deleting it you lose the start and end Date in the regression output, which was very useful. So if anyone knows a way around it, please let me know!
推荐答案
NeweyWest
使用以下代码计算滞后":
NeweyWest
calculates the 'lag' with this code:
lag <- floor(bwNeweyWest(x, order.by = order.by, prewhite = prewhite,
ar.method = ar.method, data = data))
... 当使用默认参数调用时,它会复制您(以及我对它的复制)错误:
... and when called with the default arguments it replicates your (and my replication of it) error:
>bwNeweyWest(m2,lag = NULL, order.by = NULL, prewhite = TRUE, adjust = FALSE,
+ diagnostics = FALSE, sandwich = TRUE, ar.method = "ols",
+ data = list(), verbose = FALSE)
Error in na.fail.default(as.ts(x)) : missing values in object
?NeweyWest
页面上的示例表明预先指定延迟是最初的策略.我已经用 2,3 和 4 的滞后运行了您的 coeftest
调用,并没有看到对滞后的选择有太大的敏感性:
The example on the ?NeweyWest
page suggests that pre-specifying a lag was the original strategy. I've run your coeftest
calls with lags of 2,3, and 4 and do not see much sensitivity to the choice of lags:
coeftest(m1, vcov=NeweyWest(m1, lag = 2, prewhite = FALSE) )
#-------------
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.00088591 0.00024838 -3.5667 0.007329 **
d(Ois3) -0.01256761 0.20243315 -0.0621 0.952020
d(CDS) 0.00010732 0.00097169 0.1104 0.914774
d(Vstoxx) 0.00121163 0.00051398 2.3573 0.046150 *
d(log(omo)) 0.01245017 0.01916762 0.6495 0.534190
d(L(Euribor3)) -0.42173541 0.11765274 -3.5846 0.007141 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#----------
coeftest(m2, vcov=NeweyWest(m2 , lag = 2, prewhite = FALSE ) )
#------------
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.00055562 0.00029246 -1.8998 0.08992 .
d(Ois3) 0.25659641 0.17004507 1.5090 0.16558
d(CDS) -0.00276703 0.00197776 -1.3991 0.19530
d(Vstoxx) -0.00091397 0.00063662 -1.4357 0.18493
d(log(omo)) -0.01524269 0.02810579 -0.5423 0.60076
d(L(Euribor3)) -0.51430803 0.17335182 -2.9668 0.01578 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
我不确定这是否是 dynlm
作者或 sandwich
作者的疏忽.您可以向他们发送电子邮件,以获得有关统计有效性问题的更权威的评论.
I'm not sure whether this is an oversight on the part of the dynlm
authors or the sandwich
authors. You might send them an email to get more authoritative comment on statistical validity matters.
这篇关于R:NeweyWest 使用 dynlm 的 HAC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!