Reputation: 375
I have a list of 14 dependent variables for which I am running identical regression models (same model type, same independent variables). I'm using gather
to get all of the dependent variables as a single outcome
variable, running tbl_uvregression
or tbl_regression
on that variable, and then using tbl_stack
from the gtsummary
package to organize the output. I am trying to figure out how to name each table using the value of the outcome
variable for each model. I understand that I can pass a list of names to tbl_stack(group_header)
, but I have noticed that's error-prone because I have to be careful of how the values in the outcome
variable are arranged and then make sure I type them in the same order, and I've made enough mistakes that I'm worried about that approach. Is there a way to source the group_header
arguments directly from the values of the dependent variable? The outcome variables are named, but of course that isn't preserved when I gather them to run the models.
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]**)
In this example, I would like stacked tables where the header for each table is "Condition A", "Condition B", "Condition C", "Condition D" (the values of the gather
ed outcome variable). The two column headings ("Adults" and "Children" in the example screenshot below) will come from running the models separately for adults and children, stacking them as described above, and then using tbl_merge
.
Upvotes: 2
Views: 256
Reputation: 46898
I cannot run the code in the post, this table = map(model, ~ ..
part throws some weird output.
If you look at the tibble you have before the pull, using the code below:
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))
You see that there is a corresponding column outcome
by which your results are nested:
# 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>
We can just stack it like this:
tbl_stack(regression_tables$table,group_header=regression_tables$outcome)
Upvotes: 1