I am doing multivariable regression on a list of outcome variables with a consistent set of independent variables. For univariable regression, I have followed this example to use tl_uvregression
from gtsummary
on a nested data frame, but I am trying to generalize this to multivariable regression using tbl_regression
on a nested data frame, and when I try to unnest
the tables, I get the error that "the input must be a list of vectors." Below is what I have tried - I assume there's some small but critical step that I'm missing, but I can't figure out what it is. My desired output is a table of multivariable regression output, with each model as a column and all the covariates as rows (similar to performing tbl_merge
on a list of each of these multivariable models run separately in tbl_regression
).
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 = "condition", value = "case") %>%
group_by(condition) %>% 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)) %>%
select(table) %>% unnest(table)