# Different optimal bandwidths of Newey West (1994) in R and STATA

R and STATA gave very different optimal bandwidths for the same data set. It will be greatly appreciated if someone can give me any hint why this happens. Here are two sample codes from R and STATA and their results:

rm(list=ls())
library('vars')
library('sandwich')
library('AER')
my.ts=as.matrix(my.ts)
dep = my.ts[,2]
reg = my.ts[,-c(1,2,31,32)]
exo = my.ts[,-c(1:4)]
iv = my.ts[,c(31,32)]
m = ivreg(dep~reg-1| exo + iv)
opt.bw = bwNeweyWest(m, prewhite=0)
cat("Optimal Bandwidth:", opt.bw, '\n')
print(coeftest(m, vcov=NeweyWest(m,prewhite=0)))


Optimal Bandwidth: 8.048093

t test of coefficients:

Estimate Std. Error t value  Pr(>|t|)
reg.sum.g.exp     1.106501   0.345920  3.1987  0.001473 **
reg.sum.g.rec    -1.923236   1.485894 -1.2943  0.196186
reg.const.exp     0.278318   0.095520  2.9137  0.003741 **
reg.L1.newsy.exp -0.043360   0.060837 -0.7127  0.476373
reg.L2.newsy.exp -0.160847   0.092446 -1.7399  0.082530 .
reg.L3.newsy.exp -0.037167   0.059712 -0.6224  0.533953
reg.L4.newsy.exp  0.101035   0.057360  1.7614  0.078818 .
reg.L1.y.exp      2.562082   0.327755  7.8171 3.565e-14 ***
reg.L2.y.exp     -0.759036   0.420711 -1.8042  0.071843 .
reg.L3.y.exp      0.237435   0.344001  0.6902  0.490398
reg.L4.y.exp     -0.350762   0.296555 -1.1828  0.237490
reg.L1.g.exp     -3.681902   1.197826 -3.0738  0.002236 **
reg.L2.g.exp      1.680821   0.799517  2.1023  0.036058 *
reg.L3.g.exp      0.248755   0.396698  0.6271  0.530921
reg.L4.g.exp     -0.271041   0.251835 -1.0763  0.282359
reg.const.rec     0.069961   0.051694  1.3534  0.176591
reg.L1.newsy.rec  0.249436   0.229317  1.0877  0.277270
reg.L2.newsy.rec  0.761978   0.455853  1.6715  0.095279 .
reg.L3.newsy.rec  0.602214   0.359619  1.6746  0.094678 .
reg.L4.newsy.rec  0.575172   0.338058  1.7014  0.089528 .
reg.L1.y.rec      2.824780   0.220032 12.8380 < 2.2e-16 ***
reg.L2.y.rec     -0.591850   0.470803 -1.2571  0.209338
reg.L3.y.rec     -0.683720   0.551778 -1.2391  0.215917
reg.L4.y.rec      0.394905   0.318481  1.2400  0.215607
reg.L1.g.rec      4.605353   2.582563  1.7832  0.075190 .
reg.L2.g.rec     -1.714590   1.133304 -1.5129  0.130973
reg.L3.g.rec      0.336162   0.957711  0.3510  0.725741
reg.L4.g.rec      0.528508   0.854584  0.6184  0.536585
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


## STATA

insheet using "https://www.dropbox.com/s/btg5i3a2xjyfs1d/sample.csv?dl=1", clear
tsset v1
ivreg2 sumy constexp l1newsyexp l2newsyexp l3newsyexp l4newsyexp l1yexp l2yexp l3yexp l4yexp l1gexp l2gexp l3gexp l4gexp constrec l1newsyrec l2newsyrec l3newsyrec l4newsyrec l1yrec l2yrec l3yrec l4yrec l1grec l2grec l3grec l4grec (sumgexp sumgrec = newsyexp newsyrec), nocons robust bw(auto)


IV (2SLS) estimation
--------------------

Estimates efficient for homoskedasticity only
Statistics robust to heteroskedasticity and autocorrelation
kernel=Bartlett; bandwidth=19
Automatic bw selection according to Newey-West (1994)
time variable (t):  v1

Number of obs =      499
F( 28,   471) = 62459.36
Prob > F      =   0.0000
Total (centered) SS     =  15.91494644                Centered R2   =   0.9165
Total (uncentered) SS   =  1955.368811                Uncentered R2 =   0.9993
Residual SS             =  1.329025976                Root MSE      =   .05161

------------------------------------------------------------------------------
|               Robust
sumy |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
sumgexp |   1.106502   .3154082     3.51   0.000     .4883132    1.724691
sumgrec |  -1.923237   1.536398    -1.25   0.211    -4.934522    1.088048
constexp |    .278318   .0887598     3.14   0.002      .104352    .4522839
l1newsyexp |  -.0433599   .0520314    -0.83   0.405    -.1453396    .0586197
l2newsyexp |  -.1608472   .0901899    -1.78   0.075    -.3376161    .0159217
l3newsyexp |  -.0371676   .0538555    -0.69   0.490    -.1427223    .0683872
l4newsyexp |   .1010346   .0560071     1.80   0.071    -.0087373    .2108064
l1yexp |   2.562082   .3485714     7.35   0.000     1.878895    3.245269
l2yexp |   -.759036   .4096429    -1.85   0.064    -1.561921    .0438494
l3yexp |   .2374351   .3076886     0.77   0.440    -.3656234    .8404937
l4yexp |  -.3507623   .2786922    -1.26   0.208    -.8969889    .1954642
l1gexp |  -3.681904   1.031009    -3.57   0.000    -5.702645   -1.661163
l2gexp |   1.680822   .6789219     2.48   0.013     .3501592    3.011484
l3gexp |   .2487545   .3122592     0.80   0.426    -.3632623    .8607714
l4gexp |  -.2710408   .1723631    -1.57   0.116    -.6088664    .0667847
constrec |    .069961   .0456136     1.53   0.125    -.0194401     .159362
l1newsyrec |   .2494359   .2377063     1.05   0.294      -.21646    .7153317
l2newsyrec |   .7619778   .4796875     1.59   0.112    -.1781925    1.702148
l3newsyrec |   .6022149   .3502127     1.72   0.086    -.0841893    1.288619
l4newsyrec |   .5751727   .3356722     1.71   0.087    -.0827327    1.233078
l1yrec |    2.82478   .1907978    14.81   0.000     2.450823    3.198736
l2yrec |  -.5918488   .4048256    -1.46   0.144    -1.385292    .2015948
l3yrec |  -.6837207   .5120002    -1.34   0.182    -1.687223    .3197812
l4yrec |   .3949049   .3520525     1.12   0.262    -.2951053    1.084915
l1grec |   4.605354   2.735779     1.68   0.092    -.7566743    9.967383
l2grec |  -1.714588   .9030786    -1.90   0.058    -3.484589    .0554138
l3grec |   .3361612    .792788     0.42   0.672    -1.217675    1.889997
l4grec |   .5285083    .816543     0.65   0.517    -1.071886    2.128903
------------------------------------------------------------------------------
Underidentification test (Kleibergen-Paap rk LM statistic):              2.566
Chi-sq(1) P-val =    0.1092
------------------------------------------------------------------------------
Weak identification test (Cragg-Donald Wald F statistic):                1.530
(Kleibergen-Paap rk Wald F statistic):          1.147
Stock-Yogo weak ID test critical values: 10% maximal IV size              7.03
15% maximal IV size              4.58
20% maximal IV size              3.95
25% maximal IV size              3.63
Source: Stock-Yogo (2005).  Reproduced by permission.
NB: Critical values are for Cragg-Donald F statistic and i.i.d. errors.
------------------------------------------------------------------------------
Hansen J statistic (overidentification test of all instruments):         0.000
(equation exactly identified)
------------------------------------------------------------------------------
Instrumented:         sumgexp sumgrec
Included instruments: constexp l1newsyexp l2newsyexp l3newsyexp l4newsyexp
l1yexp l2yexp l3yexp l4yexp l1gexp l2gexp l3gexp l4gexp
constrec l1newsyrec l2newsyrec l3newsyrec l4newsyrec
l1yrec l2yrec l3yrec l4yrec l1grec l2grec l3grec l4grec
Excluded instruments: newsyexp newsyrec
------------------------------------------------------------------------------


As you can see from the results, point estimates are roughly the same but their robust HAC standard errors are quite different because of different bandwidth choice (8 vs. 19). Both use the Bartlette kernel function. Any advice will be greatly appreciated.

Updated: September 12, 2017

Here are results when the same bandwidth was used in R. The standard errors are very close to each other.

print(coeftest(m, vcov=NeweyWest(m,prewhite=0, lag=19)))

t test of coefficients:

Estimate Std. Error t value  Pr(>|t|)
reg.sum.g.exp     1.106501   0.316271  3.4986 0.0005121 ***
reg.sum.g.rec    -1.923236   1.548016 -1.2424 0.2147118
reg.const.exp     0.278318   0.088797  3.1343 0.0018301 **
reg.L1.newsy.exp -0.043360   0.051786 -0.8373 0.4028560
reg.L2.newsy.exp -0.160847   0.090223 -1.7828 0.0752685 .
reg.L3.newsy.exp -0.037167   0.053407 -0.6959 0.4868195
reg.L4.newsy.exp  0.101035   0.055891  1.8077 0.0712892 .
reg.L1.y.exp      2.562082   0.351150  7.2963 1.265e-12 ***
reg.L2.y.exp     -0.759036   0.410291 -1.8500 0.0649403 .
reg.L3.y.exp      0.237435   0.303414  0.7825 0.4342882
reg.L4.y.exp     -0.350762   0.275317 -1.2740 0.2032818
reg.L1.g.exp     -3.681902   1.038019 -3.5470 0.0004286 ***
reg.L2.g.exp      1.680821   0.690575  2.4339 0.0153061 *
reg.L3.g.exp      0.248755   0.315558  0.7883 0.4309165
reg.L4.g.exp     -0.271041   0.166783 -1.6251 0.1048072
reg.const.rec     0.069961   0.044698  1.5652 0.1182075
reg.L1.newsy.rec  0.249436   0.238441  1.0461 0.2960468
reg.L2.newsy.rec  0.761978   0.482872  1.5780 0.1152342
reg.L3.newsy.rec  0.602214   0.353165  1.7052 0.0888183 .
reg.L4.newsy.rec  0.575172   0.337283  1.7053 0.0887958 .
reg.L1.y.rec      2.824780   0.189118 14.9366 < 2.2e-16 ***
reg.L2.y.rec     -0.591850   0.399977 -1.4797 0.1396191
reg.L3.y.rec     -0.683720   0.511684 -1.3362 0.1821243
reg.L4.y.rec      0.394905   0.355053  1.1122 0.2666013
reg.L1.g.rec      4.605353   2.761195  1.6679 0.0960034 .
reg.L2.g.rec     -1.714590   0.900123 -1.9048 0.0574097 .
reg.L3.g.rec      0.336162   0.775042  0.4337 0.6646802
reg.L4.g.rec      0.528508   0.814679  0.6487 0.5168276
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

• Stata is also using a small sample adjustment to the variance of N/(N-K), which R typically does not use. That is probably not enough to explain the difference. If you run the code with the same bandwidth and do this correction, how close are the standard errors then? Sep 12 '17 at 18:57
• @DimitriyV.Masterov Thanks for your comment. Yes, the standard errors are quite close to each other when they use the same bandwith. Sep 12 '17 at 20:14
• Hmm, hard to say what exactly is going on within Stata. My first guess was that just the scores from the second stage instead of the combined first+second stage are used in Stata. However, this would increase the bandwidth only from about 8 to about 9. Note sure what is responsible for the rest of the difference. Sep 17 '17 at 22:59