0

我曾经tidyr::nest运行过一系列具有不同因变量的逻辑回归模型。我想将结果作为 RMarkdown 中的单个 html 表输出,每个模型作为一列,行作为具有 99% CI 的指数系数。我无法弄清楚 、 和一个表格渲染包之间的工作流程nesttidy比如stargazer让它工作。如果我unnesttidy输出并将其传递给stargazer,或者如果我只是尝试将nested 输出(下面称为“模型”的嵌套数据框中的变量)stargazer直接传递给,我不会得到任何输出。tidy由于指数系数和 99% CI,我更愿意使用输出。我基本上需要这个小插曲更进一步,解释如何使用输出nesttidy创建格式化回归表。我也看过这个 SO post,但我很难相信没有更简单的方法可以做到这一点,而我只是想念。

示例数据,以及我运行模型的一般方法:

id <- 1:2000
gender <- sample(0:1, 2000, replace = T)
age <- sample(17:64, 2000, replace = T)
race <- sample(0:1, 2000, replace = T)
cond_a <- sample(0:1, 2000, replace = T)
cond_b <- sample(0:1, 2000, replace = T)
cond_c <- sample(0:1, 2000, replace = T)
cond_d <- sample(0:1, 2000, replace = T)

df <- data.frame(id, gender, age, race, cond_a, cond_b, cond_c, cond_d)

df %>% gather(c(cond_a, cond_b, cond_c, cond_d), key = "condition", value = "case") %>% 
  group_by(condition) %>% nest() %>% 
  mutate(model = map(data, ~glm(case ~ gender + age + race, family = "binomial", data = .)),
         tidy = map(model, tidy, exponentiate = T, conf.int = T, conf.level = 0.99))
4

1 回答 1

1

希望我得到它是正确的,基本上你将 lm 对象和置信区间分别传递给stargazer,使用来自链接问题的答案

您可以阅读stargazer 帮助页面,了解如何输入自定义 ci 所需的示例:

ci.custom:两列数字矩阵的列表,将替换每个模型的默认置信区间。第一列和第二列分别代表下限和上限。由元素名称匹配。

所以在你的情况下,这需要更多的工作,我们首先存储结果。

result = df %>% gather(c(cond_a, cond_b, cond_c, cond_d), key = "condition", value = "case") %>% 
  group_by(condition) %>% nest() %>% 
  mutate(model = map(data, ~glm(case ~ gender + age + race, family = "binomial", data = .)),
         tidy = map(model, tidy, exponentiate = T, conf.int = T, conf.level = 0.99))

tidy_model = result$tidy
fit = result$model

然后拉出CI和系数:

CI = lapply(tidy_model,function(i)as.matrix(i[,c("conf.low","conf.high")]))
Coef = lapply(tidy_model,"[[","estimate")

然后申请stargazer

stargazer(fit, type = "text", 
  coef = Coef,
  ci.custom = CI)


=============================================================================
                                      Dependent variable:                    
                  -----------------------------------------------------------
                                             case                            
                       (1)            (2)            (3)            (4)      
-----------------------------------------------------------------------------
gender               0.996***       1.182***       1.196***       0.921***   
                  (0.790, 1.254) (0.938, 1.489) (0.950, 1.508) (0.731, 1.161)
                                                                             
age                  1.004***       1.001***       0.999***       1.005***   
                  (0.995, 1.012) (0.993, 1.009) (0.990, 1.007) (0.996, 1.013)
                                                                             
race                 0.911***       0.895***       0.944***       1.213***   
                  (0.724, 1.148) (0.711, 1.128) (0.749, 1.189) (0.963, 1.529)
                                                                             
Constant             0.919***       0.997***       0.959***       0.761***   
                  (0.623, 1.356) (0.676, 1.472) (0.649, 1.415) (0.515, 1.123)
                                                                             
-----------------------------------------------------------------------------
Observations          2,000          2,000          2,000          2,000     
Log Likelihood      -1,385.107     -1,382.664     -1,383.411     -1,382.272  
Akaike Inf. Crit.   2,778.215      2,773.329      2,774.821      2,772.544   
=============================================================================
Note:                                             *p<0.1; **p<0.05; ***p<0.01
于 2021-02-13T10:01:57.597 回答