rcoefficientsrobust

How to extract robust standard errors in r?


I calculated robust standard errors after running a regression with lm() function.

# robust standard errors
cov2I         <- vcovHC(ols2I, type = "HC1")
robust_se2I    <- sqrt(diag(cov2I))
print(robust_se2I)

I would like to extract the second value out of the resulting matrix and save it under a new variable. I've tried the following code but it didn't work.

stderrorols2I <- (summary(robust_se2I))[2]

Thanks for your help!


Solution

  • This is how you could add RSE manually to your summary output. Alternativly, you could have a look at coeftest()

    library(sandwich)  
    mod1 <- lm(mpg ~ cyl + disp, data = mtcars) 
    
    # robust standard errors
    cov2I         <- vcovHC(mod1, type = "HC1")
    robust_se2I    <- sqrt(diag(cov2I)) 
    
    mod1 %>% 
      broom::tidy() %>% 
      mutate(rse = robust_se2I)