mrb
mrb

Reputation: 193

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]

Upvotes: 1

Views: 905

Answers (2)

Jan Felix
Jan Felix

Reputation: 127

Just to build up on mrb (without being able to comment on it):

xtab <- stargazer(fit)
xtab2 <- stargazer(fit, se=robust.se)
n <- length(coef(fit))
sq <- seq(16, 16 + (n-1)*3, by=3) #16 is the first line in which stargazer (usually) puts the first SE. From there every third line is a SE...

xtab2[sq2]<-gsub("\\(","\\[",xtab2[sq2]) #For replacing brackets
xtab2[sq2]<-gsub("\\)","\\]",xtab2[sq2]) 

xtab[sq] <- paste(xtab2[sq], " & & \\\\\n", sep="\n")

sink("Table.tex", append=FALSE, split=FALSE) #for getting your LaTex ready table
cat(xtab, sep="\n")
sink()

From here on its just copy and paste. I hope this makes implementation easier for other R beginners.

Upvotes: 1

mrb
mrb

Reputation: 193

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)

Upvotes: 2

Related Questions