I have run a series of logistic regression models using nest
, map
, and tidy
to get the exponentiated coefficients and 99% CIs. I would like to export the model output to formatted regression tables in RMarkdown. The best option for this seems to be manually extracting the ORs and CIs from the tidied model output and passing them to a package like stargazer
, but I am wondering if I am missing a native tidyverse approach to doing this. I essentially need this vignette to go one step farther and tell me how to output the tidied model results as a formatted regression table from my multiple models.
Sample data with code for my models:
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)
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))
Exporting model results manually, using stargazer
(thanks, @StupidWolf) - this works, but I would like to know if there is a tidyverse way of doing this:
CI = lapply(result$tidy, function(i) as.matrix(i[ ,c("conf.low","conf.high")]))
Coef = lapply(result$tidy, "[[", "estimate")
stargazer(result$model, type = "text",
coef = Coef,
ci.custom = CI)