11

Can you give me an example of the use of sandwich estimators in order to perform robust regression inference?

I can see the example in ?sandwich, but I don't quite understand how we can go from lm(a ~ b, data) (-coded) to an estimate and a p value resulting from a regression model using the variance-covariance matrix returned by the function sandwich.

StasK
  • 29,235
  • 2
  • 80
  • 165
Remi.b
  • 4,572
  • 12
  • 34
  • 64
  • have you been through the examples in the associated [vignette](http://www.jstatsoft.org/v16/i09/paper)? – user603 Nov 18 '13 at 18:08
  • (1) Link works for me. (2) Is this an r coding question? How to get the Huber-White SE after fitting a model. Or something else? – charles Nov 18 '13 at 23:09
  • @charles 1) Indeed it works! Don't know what happened before. Yes it is an R coding question. 2) I don't know what are the Huber-White Standard Error and don't know how to compute them right now. Thanks for your help! – Remi.b Nov 19 '13 at 07:56
  • You get p-values & standard errors in the same way as usual, substituting the sandwich estimate of the variance-covariance matrix for the least-squares one. – Scortchi - Reinstate Monica Nov 19 '13 at 11:20
  • @Scortchi Ok. And once I have these estimates (OLS or sandwich estimates) what can I do with them? – Remi.b Nov 19 '13 at 11:37
  • Perform tests or calculate confidence intervals on the coefficient estimates just as usual. – Scortchi - Reinstate Monica Nov 19 '13 at 11:51
  • @Scortchi I think it kind of makes sense to me! How do you perform that with R? Should I set somewhere that I want to use sandwich estimates rather than least squares estimates in my `aov(.)` function? And how robust will my regression be? Again thanks a lot for your help! – Remi.b Nov 19 '13 at 12:03
  • Try the `coeftest` & `waldtest` functions using `vcov=` from the `lmtest` library (I'm sure there are others as well). – Scortchi - Reinstate Monica Nov 19 '13 at 12:06
  • @Scortchi Ok. So I do `coeftest(myModel, vcov=sandwich(myModel))`. And how can I now know whether my regression still suffers of heteroscedasticity? – Remi.b Nov 19 '13 at 12:22
  • The point's rather to make inference robust to heteroskedasticity. Though if the standard errors &c. are much the same whichever variance-covariance matrix you use, you might well conclude that heteroskedasticity isn't a problem. – Scortchi - Reinstate Monica Nov 19 '13 at 14:55

2 Answers2

11

One can use an alternative summary function to perform a robust regression.

lm.object <- lm(a~b+c)
summary(lm.object, robust=TRUE)

To obtain robust standard errors you set the parameter ''robust'' in your summary function to TRUE.

The following blog entry provides the function and a detailed description of the function: https://economictheoryblog.com/2016/08/08/robust-standard-errors-in-r

Daniel Kah
  • 111
  • 1
  • 4
9

I think there are a few approaches. I haven't looked at them all and not sure which is the best:

  1. The sandwich package:

    library(sandwich)    
    coeftest(model, vcov=sandwich)
    

But this doesn't give me the same answers I get from Stata for some reason. I've never tried to work out why, I just don't use this package.

  1. The rms package: I find this a bit of a pain to work with but usually get good answers with some effort. And it is the most useful for me.

    model = ols(a~b, x=TRUE)    
    robcov(model)
    
  2. You can code it from scratch (see this blog post). It looks like the most painful option, but remarkably easy and this option often works the best.

A simple / quick explanation is that Huber-White or Robust SE are derived from the data rather than from the model, and thus are robust to many model assumptions. But as always, a quick Google search will lay this out in excruciating detail if you're interested.

gung - Reinstate Monica
  • 132,789
  • 81
  • 357
  • 650
charles
  • 2,436
  • 11
  • 13
  • You should really see this answer here: http://stats.stackexchange.com/a/117066/12053 – chandler Oct 01 '16 at 12:23
  • 2
    It like how this answers implictly assumes that there is something wrong with R, because you get different results than Stata. For people who know how the sandwich estimators works, the difference is obvious and easy to remedy. For people who dont know, just please read the vignette (guide) which ships with the package – Repmat May 18 '18 at 06:40