[英]Tidy output table and stargazer
我正在使用我在此處找到的這種奇妙方法對子組(年份和組的所有組合)進行一系列回歸。
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()
這給了我一個表格,列出了年份和組的所有可能組合作為單獨的 model 估計值。 我的問題是我想使用 stargazer 將這些回歸 output 作為單獨的模型呈現。 但是 stargazer 並沒有將它們識別為回歸 output。
stargazer(table, type = "text")
而不是通常的回歸 output 我得到這個:
==================================================
Statistic N Mean St. Dev. Min Pctl(25) Pctl(75) Max
===================================================
有沒有一種絕妙的方法可以讓 Stargazer 以一種可以識別的方式將所有回歸 output 提供給它?
嘗試使用tidy_split
中的dplyr
function 根據組將 dataframe 拆分為數據幀列表。 獲得該列表后,您可以使用lapply
將函數應用於列表中的每個項目。 如下圖,先在列表中的每個dataframe上裝一個lm
,然后生成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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.