Search code examples
rstargazer

Print regression tables with multiple standard errors


I would like to print two types of standard errors for each coefficient (e.g., robust and not) in the same regression table. For example:

# Simulate some data    
dat <- data.frame(y=runif(100), matrix(rnorm(200), 100, 2))
fit <- lm(y ~ X1 + X2, data=dat)

# Compute robust SE
library(sandwich)
cov1        <- vcovHC(fit, type="HC")
robust.se  <- sqrt(diag(cov1))

# print regression table in latex
library(stargazer)
stargazer(fit)

How to add the robust SE as an additional row below each coefficient in the square brackets?

Something like:

    Model 1   

X1 0.012
   (0.14) 
   [0.21]

X2 0.72
   (0.64) 
   [0.88]

Solution

  • Here is a quick and dirty solution:

    xtab <- stargazer(fit)
    xtab2 <- stargazer(fit, se=robust.se)
    n <- length(coef(fit))
    sq <- seq(16, 16 + (n-1)*3, by=3)
    xtab[sq] <- paste(xtab2[sq], " & & \\\\\n", sep="\n")
    
    cat(xtab, sep="\n")
    

    This procedure basically replaces the empty rows printed by stargazer with the robust standard errors (and another empty row). One may further decide to pre-process xtab2 by replacing round with square brackets (e.g., using gsub)