Stargazer cluster standard errors

Does anyone know how to get stargazer

to display clustered SEs for models lm

? (And the corresponding F-test?) If possible, I would like to follow an approach like calculating heterosection-resistant SE s sandwich

and inserting them in stargazer

, as in http://jakeruss.com/cheatsheets/stargazer.html#robust-standard-errors -replicating-statas-robust-option .

I am using lm

regression models to get, and I am grouping by firm (a factor variable that I do not include in regression models). I also have a bunch of NA values, which makes me think what multiwayvcov

would be the best package (see landroni's answer below here - Double Cluster Standard Errors for Panel Data - and also https://sites.google.com/site/npgraham1/research / code )? Please note, I don't want to use plm

.

Edit: I think I found a solution using the package multiwayvcov

...

library(lmtest) # load packages
library(multiwayvcov)

data(petersen) # load data
petersen$z <- petersen$y + 0.35  # create new variable

ols1 <- lm(y ~ x, data = petersen) # create models
ols2 <- lm(y ~ x + z, data = petersen)

cl.cov1 <- cluster.vcov(ols1, data$firmid) # cluster-robust SEs for ols1
cl.robust.se.1 <- sqrt(diag(cl.cov1))
cl.wald1 <- waldtest(ols1, vcov = cl.cov1)

cl.cov2 <- cluster.vcov(ols2, data$ticker) # cluster-robust SEs for ols2
cl.robust.se.2 <- sqrt(diag(cl.cov2))
cl.wald2 <- waldtest(ols2, vcov = cl.cov2)

stargazer(ols1, ols2, se=list(cl.robust.se.1, cl.robust.se.2), type = "text") # create table in stargazer

      

The only drawback to this approach is that you have to manually re-enter the F-statistic from the output waldtest()

for each model.

+3


source to share


2 answers


Using the lmtest and multiwayvcov packages introduces a lot of unnecessary overhead. The easiest way to compute clustered standard errors in R is with a modified function summary()

. This function allows you to add an additional parameter called a cluster to a regular function summary()

. The following article describes how to use this function to compute clustered standard errors in R:

https://economictheoryblog.com/2016/12/13/clustered-standard-errors-in-r/



You can easily summarize the function to get the clustered standard errors and add them to the output of stargazer. Based on your example, you can simply use the following code:

# estimate models
ols1 <- lm(y ~ x) 

# summary with cluster-robust SEs
summary(ols1, cluster="cluster_id") 

# create table in stargazer
stargazer(ols1, se=list(coef(summary(ols1,cluster = c("cluster_id")))[, 2]), type = "text") 

      

+7


source


I would recommend a package lfe

, which is a much more powerful package than a package lm

. You can easily specify a cluster in a regression model:

ols1 <- felm(y ~ x + z|0|0|firmid, data = petersen)
summary(ols1)

stargazer(OLS1, type="html")

      

Grouped standard errors will be automatically generated. And stargazer

will report an error with the clustered standard.



By the way (let me do more marketing), for microeconomic analysis is recommended felm

. You can easily define fixed effects and IVs using felm

. The grammar looks like this:

ols1 <- felm(y ~ x + z|FixedEffect1 + FixedEffect2 | IV | Cluster, data = Data)

      

0


source







All Articles