Newey-West standard error


Hello the lm_robust team!

I was surprised not to find in lm_robust thenewey-west correction for autocorrelation in the error term (see here for some hints

This is a very common correction that is used in many papers (not only in the time-series field). Do you plan to add it at some point?



lm_robust is suppossed to be compatible with things that use lm - in the case of the sandwich package, providing an estfun method should suffice. Generally aliasing an lm method does the trick (eg for stargazer, texreg, etc)

However, there looks like there’s a bug somewhere:

> m <- lm_robust(extra~group, sleep)
> estfun.lm_robust <- sandwich:::estfun.lm
> coeftest(m, vcov=NeweyWest(m, verbose=TRUE))
Error in eval(predvars, data, env) : object 'extra' not found
> traceback()
12: eval(predvars, data, env) 
11: eval(predvars, data, env)
10: model.frame.default(object, data, xlev = xlev)
9: model.frame(object, data, xlev = xlev)
8: model.matrix.default(x)
7: model.matrix(x)
6: estfun.lm_robust(x)
5: estfun(x)
4: bwNeweyWest(x, =, prewhite = prewhite, ar.method = ar.method, 
       data = data)
3: NeweyWest(m, verbose = TRUE)
2: coeftest.default(m, vcov = NeweyWest(m, verbose = TRUE))
1: coeftest(m, vcov = NeweyWest(m, verbose = TRUE))

Going down the rabbit hole, you can also shim model.matrix, model.frame, and write a residuals method -

residuals.lm_robust <- function(x) sleep$extra - fitted(x)

After all of that, we can finally get numbers out:

> coeftest(m, vcov=NeweyWest(m, verbose=TRUE))

Lag truncation parameter chosen: 2 

t test of coefficients:

             Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.7500000  2.4822724 0.30214  0.76601
group2      1.5800000  4.1168097 0.38379  0.70563

@luke.sonnet - Not sure if this correction belongs in the Rcpp code or if it’s better to hook up to sandwich by providing an estfun. Half the benefit of lm_robust is that it’s much faster than sandwich, no?


@nfultz thanks! but i am not quite sure to understand what you are saying. this feature is broken in the current release of lm_robust then?


I don’t think anyone really ever tried to make estimatr work with sandwich before, because many of the types of SEs you would use sandwich to calculate are already built in to lm_robust.


@nfultz the sandwich is not yummy without a slice of newey-west bro!!! :smiley:


more seriously you cant seriously expect to replicate all the standard error adjustments out there. you would reinvent the wheel and incur the risk of creating bugs (no matter how smart you are - can happen to everyone) so my take would be to make lm_robust as open as possible to external packages. what do you think


@nfultz? are you there?


I think Luke is adding the helper functions for sandwich, although I think the doing the same for broom may be higher priority.


amazing, thanks!!!