使用 gtsummary 中因变量的值命名模型

Naming models using the value of the dependent variable in gtsummary

我有一个包含 14 个因变量的列表,我运行正在使用相同的回归模型(相同的模型类型,相同的自变量)。我正在使用 gather 将所有因变量作为单个 outcome 变量,运行ning tbl_uvregressiontbl_regression 该变量,然后使用tbl_stack 来自 gtsummary 包来组织输出。我试图弄清楚如何使用每个模型的 outcome 变量的值来命名每个 table。我知道我可以将名称列表传递给 tbl_stack(group_header),但我注意到那是 error-prone,因为我必须注意 outcome 变量中的值是如何排列的,然后使确保我以相同的顺序输入它们,并且我犯了很多错误以至于我担心这种方法。有没有办法直接从因变量的值中获取 group_header 参数?结果变量已命名,但当我将它们收集到 运行 模型时,当然不会保留这些变量。

library(tidyverse)
library(magrittr)
library(gtsummary)
library(broom)

id <- 1:2000
gender <- sample(0:1, 2000, replace = T)
age <- sample(17:64, 2000, replace = T)
race <- sample(0:1, 2000, replace = T)
health_score <- sample(0:25, 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, health_score, cond_a, cond_b, cond_c, cond_d)

regression_tables <- df %>% select(-id) %>% 
  gather(c(cond_a, cond_b, cond_c, cond_d), key = "outcome", value = "case") %>% 
  group_by(outcome) %>% nest() %>% 
  mutate(model = map(data, ~glm(case ~ gender + age + race + health_score, family = "binomial", data = .)), 
table = map(model, tbl_regression, exponentiate = T, conf.level = 0.99)) %>% 
pull(table) %>% tbl_stack(**[model names to become table headers]**)

在这个例子中,我想要堆叠 tables,其中每个 table 的 header 是“条件 A”、“条件 B”、“条件 C”、“条件 D”(gathered 结果变量的值)。两个列标题(下面示例屏幕截图中的“成人”和“Children”)将来自 运行 分别为成人和 children 模型,如上所述堆叠它们,并且然后使用 tbl_merge

我不能 运行 post 中的代码,这个 table = map(model, ~ .. 部分会抛出一些奇怪的输出。

如果您在拉取之前查看小标题,请使用以下代码:

regression_tables <- df %>% select(-id) %>% 
gather(c(cond_a, cond_b, cond_c, cond_d), key = "outcome", value = "case") %>% 
group_by(outcome) %>% nest() %>% 
mutate(model = map(data, ~glm(case ~ gender + age + race + health_score, family = "binomial", data = .)), 
table = map(model,tbl_regression, exponentiate = T, conf.level = 0.99))

您看到有一个对应的列 outcome 嵌套了您的结果:

# A tibble: 4 x 4
# Groups:   outcome [4]
  outcome data                 model  table     
  <chr>   <list>               <list> <list>    
1 cond_a  <tibble [2,000 × 5]> <glm>  <tbl_rgrs>
2 cond_b  <tibble [2,000 × 5]> <glm>  <tbl_rgrs>
3 cond_c  <tibble [2,000 × 5]> <glm>  <tbl_rgrs>
4 cond_d  <tibble [2,000 × 5]> <glm>  <tbl_rgrs>

我们可以这样堆叠:

tbl_stack(regression_tables$table,group_header=regression_tables$outcome)