2

I am running a series of regressions on subgroups (all combinations of year and group) using this fantastic method I found here.

year <- rep(2014:2015, length.out = 10000)
group <- sample(c(0,1,2,3,4,5,6), replace=TRUE, size=10000)
value <- sample(10000, replace = T)
female <- sample(c(0,1), replace=TRUE, size=10000)
smoker <- sample(c(0,1), replace=TRUE, size=10000)

dta <- data.frame(year = year, group = group, value = value, female=female, smoker = smoker)

library(dplyr)
library(broom)
library(stargazer)

table <- dta %>%
  group_by(year, group) %>%
  do(tidy(lm(smoker ~ female, data = .))) %>%
  ungroup()

This gives me a table of all possible combinations of year and group as separate model estimates. My problem is that I would like to present these regression output as separate models using stargazer. But stargazer doesn't recognize them as regression output.

stargazer(table, type = "text")

Instead of the usual regression output I get this:

==================================================
Statistic N Mean St. Dev. Min Pctl(25) Pctl(75) Max
===================================================

Is there a nifty way to feed Stargazer all the regression output in a way that it would recognize it?

Stata_user
  • 562
  • 3
  • 14

1 Answers1

0

Try using the tidy_split function in dplyr to split your dataframe based on on the groups into a list of dataframes. Once you have that list, you can use lapply to apply functions to each item on the list. Shown below, first fit an lm on each dataframe in the list, then produce the stargazer output.

# create list of dfs
table_list <- dta %>%
    group_by(year, group) %>%
    group_split()

# apply the model to each df and produce stargazer result
model_list <- lapply(table_list, function(x) lm(smoker ~ female, data = x))
stargaze_list <- lapply(model_list, stargazer, type = "text")
Colin H
  • 600
  • 4
  • 9
  • Interestingly the above code gives me 12 separate stargazer outputs. I tried this and it works ```stargazer(model_list, type="text")```. – Stata_user Feb 16 '21 at 16:40
  • Would you mind looking at this one as well? https://stackoverflow.com/questions/66228661/feeding-probitmfx-output-in-a-list-to-stargazer Thank you. – Stata_user Feb 16 '21 at 16:50