Quantcast
Channel: Active questions tagged lm - Stack Overflow
Viewing all articles
Browse latest Browse all 105

Loop over multiple regressions to store clustered standard errors [closed]

$
0
0

I need a loop to generate the clustered std. errors for several lm-s. The models are named m1_1 …m1_7. To export the results with stargazer I need a vector with the clustered std. errors for every model.

This is my code:

## librarieslibrary(sandwich)library(stargazer)## a vector of my stored models (used lm)dvs <- c('dv1', 'dv2', 'dv3', 'dv4', 'dv5', 'dv6', 'dv7') # here a "'" was missing after dv1## regressions (reference: https://druedin.com/2015/11/28/same-explanatory-variables-multiple-dependent-variables-in-r/)for(i in 1:length(dvs)) {model <- paste("m1",i, sep="_") m <- lm(as.formula(paste(dvs[i],"~ factor(treatment) + factor(region)")), data=Data)assign(model,m)}m <- c('m1_1', 'm1_2', 'm1_3', 'm1_4', 'm1_5', 'm1_6', 'm1_7')## empty list for storing the cse per model (not sure if this is necessary)cse1_= list()# clustered std. errors # commented outfor (i in 1: length(m)) { cse1_[[i]] <- vcovHC(m1_i, type = "HC0", cluster = "cluster") cse1_[[i]] <- sqrt(diag(cse1_i)) }

The idea is then to export the models with these std. errors into one table.

stargazer(list('m1_1', 'm1_2', 'm1_3', 'm1_4', 'm1_5', 'm1_6', 'm1_7'), se = list(cse1_1, cse1_2, cse1_3, cse1_4, cse1_5, cse1_6, cse1_7), omit.stat = c("rsq", "f"), notes.append = FALSE)

I get an error message that m1 for the clustered std. errors is not recognized. Most likely it has to do with the way ‘i’ is defined. I am still confused when to have the [[..]] in a loop and when not. I tried the double square brackets with m1_i but it results in the same error as before.

Eventually, I want to be able to refer to the model by name when retrieving the clustered std. errors and then store them as a vector with a name that corresponds to the model name, so that I know to which model they belong to.

Any ideas how I can fix the loop or alternatively retrieve the clustered std. errors in a more efficient way?


Viewing all articles
Browse latest Browse all 105

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>