2
votes

I am running several regressions with lm and lapply so that I get a list of models. From there I want to create a stargazer table. The problem I am having is with stargazer. I can create a table without issue, but I can't figure out how to include more than one dependent variable names. Here is some sample code:

library(stargazer)
library(magrittr)
x1 <- rnorm(1000,0,1)
x2 <- rnorm(1000,0,1)
x3 <- rnorm(1000,0,1)
x4 <- rnorm(1000,0,1)
x5 <- rnorm(1000,0,1)
x6 <- rnorm(1000,0,1)

data <- cbind(x1, x2, x3, x4, x5, x6) %>%
  as.data.frame()

mod_list_test <- lapply(data[, 1:4], function(x) lm(x ~ data$x5 + data$x6))
dep_vars_test <- c("A", "B", "C", "D")
stargazer(mod_list_test, header = F,
          dep.var.labels = dep_vars_test,
          type = "text")

The issue I believe is that when the formula is called with lapply, it reads:

Call: lm(formula = x ~ data$x5 + data$x6))

so stargazer seems to think the dep var is "x" in all models. Specifying the dependent variable names as I've done in my code above then only uses the first name in the dep_vars vector.

This is kind of a follow-up to this post here, but in that thread it seems that OP is satisfied with excluding the dependent variable labels entirely. I would like to have them if possible. I tried with their solution of pasting in the formula text but that gave me the same result.

Any help would be much appreciated.

1

1 Answers

0
votes

I suggest to use lapply as follows:

mod_list_test <- lapply(data[, 1:4], function(x) {
     df <- data.frame(y = x, x5=data$x5, x6=data$x6)
     lm(y ~ x5 + x6, data=df)
 })

or:

mod_list_test <- lapply(1:4, function(k) {
     frm <- as.formula(paste(names(data)[k],"~ x5+x6"))
     lm(frm, data=data)
 })

stargazer(mod_list_test, header = F, type = "text")

The output is:

=================================================================
                                      Dependent variable:        
                               ----------------------------------
                                 x1      x2       x3       x4    
                                 (1)     (2)     (3)       (4)   
-----------------------------------------------------------------
x5                             -0.041   0.011  -0.077**  -0.002  
                               (0.033) (0.033) (0.033)   (0.033) 

x6                             -0.021   0.027   0.027   -0.095***
                               (0.033) (0.034) (0.033)   (0.033) 

Constant                       -0.013  -0.016   0.014     0.016  
                               (0.033) (0.033) (0.033)   (0.033) 

-----------------------------------------------------------------
Observations                    1,000   1,000   1,000     1,000  
R2                              0.002   0.001   0.006     0.008  
Adjusted R2                    -0.0001 -0.001   0.004     0.006  
Residual Std. Error (df = 997)  1.035   1.041   1.029     1.036  
F Statistic (df = 2; 997)       0.939   0.377  3.145**   4.016** 
=================================================================
Note:                                 *p<0.1; **p<0.05; ***p<0.01