R: HAC by NeweyWest using dynlm

我与影子孤独终老i 提交于 2019-12-11 11:47:06

问题


This is what I would like to do:

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)

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!

Sample Data:

    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

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!


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/30003047/r-hac-by-neweywest-using-dynlm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!