整洁的输出表和观星者 [英] Tidy output table and stargazer
问题描述
我正在使用我发现的这个奇妙的方法对子组(年份和组的所有组合)进行一系列回归此处.
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()
这给了我一个表格,其中包含年份和组的所有可能组合作为单独的模型估计.我的问题是我想使用 stargazer 将这些回归输出呈现为单独的模型.但是观星者不会将它们识别为回归输出.
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
===================================================
有没有一种巧妙的方法可以让 Stargazer 以一种它能够识别的方式提供所有回归输出?
Is there a nifty way to feed Stargazer all the regression output in a way that it would recognize it?
推荐答案
尝试使用 dplyr
中的 tidy_split
函数将基于组的数据帧拆分为列表数据帧.获得该列表后,您可以使用 lapply
将函数应用于列表中的每个项目.如下所示,首先在列表中的每个数据帧上放置一个 lm
,然后生成 stargazer
输出.
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")
这篇关于整洁的输出表和观星者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!