首页 > 解决方案 > 将列表中的 probitmfx 输出提供给 Stargazer

问题描述

与此处类似,我正在使用 tidyr 对子组(年份和组的所有组合)进行一系列回归。

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)

# 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) probitmfx(smoker ~ female, data = x))
stargazer(model_list, type = "text")

我收到一条错误消息

% Error: Unrecognized object type.

有人知道我该如何解决这个问题吗?

标签: rstargazermodelsummary

解决方案


正如 Colin 在评论中指出的那样,这种类型的模型似乎不受stargazer. 但是,它由modelsummary提供开箱即用的支持(免责声明:我是作者)。

您可以完全省略该output参数以获得漂亮的 HTML 表格,或更改它以将表格保存为 LaTeX、Word 或许多其他格式。

# Code from the original question
library(mfx)
library(dplyr)

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)
table_list <- dta %>%
    group_by(year, group) %>%
    group_split()

model_list <- lapply(table_list, function(x) probitmfx(smoker ~ female, data = x))

# New code
library(modelsummary)
modelsummary(model_list, output = "markdown")
模型 1 模型 2 模型 3 模型 4 型号 5 型号 6 型号 7 型号 8 型号 9 型号 10 型号 11 型号 12 型号 13 型号 14
女性 0.022 -0.026 -0.033 0.013 -0.030 -0.001 -0.003 -0.073 0.006 -0.041 0.075 -0.006 -0.009 0.023
(0.039) (0.038) (0.036) (0.037) (0.037) (0.038) (0.037) (0.038) (0.036) (0.038) (0.037) (0.038) (0.037) (0.038)
数量。 670 688 761 727 740 675 739 688 751 703 733 710 737 678
AIC 932.4 957.3 1058.1 1009.6 1029.2 939.7 1027.6 953.8 1044.9 977.4 1016.0 988.2 1025.6 943.5
BIC 941.4 966.4 1067.4 1018.8 1038.4 948.7 1036.8 962.9 1054.1 986.5 1025.2 997.4 1034.8 952.5
日志。 -464.206 -476.648 -527.074 -502.806 -512.588 -467.837 -511.809 -474.924 -520.425 -486.682 -506.004 -492.123 -510.810 -469.740

推荐阅读