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.