微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 nest 和 tidy 在 RMarkdown 中创建格式化的 html 回归输出

如何解决使用 nest 和 tidy 在 RMarkdown 中创建格式化的 html 回归输出

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

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

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

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

df %>% gather(c(cond_a,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))

解决方法

希望我理解正确,本质上是您使用 this answer from your linked question 将 lm 对象和置信区间分别传递给 stargazer

您可以阅读 stargazer help page 以了解如何输入例如自定义 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     
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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。