 # Robust Standard Errors

Hi! I want to control for heteroscedasticity with robust standard errors.
I have read a lot about the pain of replicate the easy `robust` option from STATA to R to use robust standard errors. I replicated following approaches: StackExchange and Economic Theory Blog. They work but the problem I face is, if I want to print my results using the `stargazer` function (this prints the `.tex` code for Latex files).

Here is the illustration to my problem:

``````reg1 <- lm(rev~id + source + listed + country , data=data2_rev)
stargazer(reg1)
``````

This prints the R output as `.tex` code (non-robust SE) If i want to use robust SE, i can do it with the `sandwich` package as follow:

``````vcov <- vcovHC(reg1, "HC1")
``````

if I now use `stargazer(vcov)` only the output of the vcovHC function is printed and not the regression output itself.

How can I use robust standard errors in the lm function? Did anybody face the same problem?

Hi @LAH_17,

You may be interested in the `lmtest` package which provides some nice functions for generating robust standard errors and returning results in the same format as `lm()`.

``````library(lmtest)
library(sandwich)

lmtest::coeftest(reg1, vcov. = sandwich::vcovHC(reg1, type = 'HC1'))
``````
2 Likes

Thank you @mattwarkentin , that worked!
Do you now by chance how i can add, that the observations, R2, adj. R2, Residual, Residual St.Error and the F-Statistics will also be printed?
At the moment it just the coefficients are printed:

``````t test of coefficients:

Estimate Std. Error t value Pr(>|t|)
(Intercept) -2.54923    6.85521 -0.3719 0.710611
id           0.39634    0.12376  3.2026 0.001722 **
source       1.48164    4.20183  0.3526 0.724960
country     -4.00398    4.00256 -1.0004 0.319041
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
``````

While I'd like to have the following as well (example is from actual `lm` function):

``````Residual standard error: 17.43 on 127 degrees of freedom
Multiple R-squared:  0.09676,	Adjusted R-squared:  0.07543
F-statistic: 4.535 on 3 and 127 DF,  p-value: 0.00469
``````