Is there a tidyverse approach to outputting formatted regression tables?

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)
1 Like

If you want to lean more into tidyverse then replace lapply with purrr::map (or its cousins)

Ok, but is there a more streamlined approach to outputting formatted regression tables from tidy data without having to manually extract coefficients and CIs?

1 Like

If I were in your position I would turn your last code chunk into a function and reuse it in future work.

Someone has done something similar in this Stack Overflow post, I guess I'm just surprised that there is no established tidyverse/broom way of doing this, as it seems like a pretty common need.

I think this is what you are looking for:

When I try to use gtsummary to export tables, I get the error, "PhantomJS not found." I am on a controlled computer that cannot download packages/software from anywhere except CRAN, so I cannot get gtsummary to work for exporting tables and opening/knitting them in RMarkdown. The one discussion I've seen on SO about this essentially seems to say, "give up on gtsummary." Do you have any idea how I can get around this? I don't want to have all my code in an RMD document - I just want to save specific tables and graphs from my R script, open them in RMD, and knit them into an html document.

If that is all your looking to do, you need to save the data associated with this tables and graph to disk. Then you can load() the data set into the Rmd file at the top, rebuild you graphs and tables, then knit.

I would go through the trouble of saving each graph and take individually, only the data you need to build them.

You could also be interested in the huxtable package to complement tidyied model result with broom

I tried to explore huxtable, but apparently it has been removed from CRAN for some reason having to do with the upkeep of the package? Which seems strange because I read it was just updated in fall 2020. But regardless, I couldn't access it. I have been looking more into gtsummary, which I had given up on until the package author chimed in on my SO post about the phantomjs error, which I'm linking here in case others are having this problem.

huxtable is definitely on cran though.
5.2.0 as of Feb 2021
CRAN - Package huxtable (r-project.org)

1 Like

Try formattable package when you already have the table of data you need. It has a lot of options and can be outputed to html easily.

I guess you can try out the gtsummary package. It works fine.

Thank you for pointing me in the direction of gtsummary! I am using it in conjunction with nest as described here, and it's working very well. I appreciate your help!

Sure! I'm glad you got working.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.