Is there any way to create a stargazer::stargazer()
style (or something close to it) coef table using a broom::tidy()
object? I have tried gt()
but it doesn't seem tailored for publication-ready LaTeX/Rmd tables.
Asked
Active
Viewed 311 times
0
-
1Where is the MCVE? – IRTFM Aug 22 '21 at 04:17
-
Perhaps closer: http://www.danieldsjoberg.com/gtsummary/ – Jon Spring Aug 22 '21 at 05:00
1 Answers
1
The modelsummary
package is compatible with broom
. It produces highly-customizable stargazer
-style regression tables (and more!), which can be saved to many formats such as HTML, LaTeX, or Word. (Disclaimer: I am the maintainer.)
You can summarize models side-by-side as in stargazer
by storing them in a list. Under the hood, modelsummary
will use broom
to extract coefficients and such:
library(modelsummary)
mod <- list(
lm(mpg ~ hp, data = mtcars),
lm(mpg ~ hp + drat, data = mtcars))
modelsummary(mod)
If you want to work from raw broom::tidy
object, you can also do it by creating a named list of class modelsummary_list
. This second option allows you to use the default broom
output, or to modify the broom
output manually, or to create your own.
Example:
mod <- list(
tidy = broom::tidy(mod),
glance = broom::glance(mod))
class(mod) <- c("modelsummary_list", class(mod))
modelsummary(mod)

Vincent
- 15,809
- 7
- 37
- 39
-
Thanks Vincent, I'm trying out your package now. One question: is it possible to efficiently summarize nested models in modelsummary? For example code see @EmilHvitfeldt's answer to my question here: https://stackoverflow.com/questions/68777221/augmenting-coxph-output-in-a-tidymodels-workflow I know I could achieve a similar result by filtering based on the nested variable and estimating models separately inside list(), but suspect there is a cleaner way to estimate and present the models. – Rob Lytle Aug 22 '21 at 16:58
-
Do you mean something like this? `mtcars %>% nest_by(cyl) %>% mutate(models = list(lm(mpg~hp, data = data))) %>% pull(models) %>% modelsummary` – Vincent Aug 22 '21 at 17:07
-
Yes! This is actually a more efficient strategy for estimating the models than what I was doing before, and outputting into a nice table by default is really nice for quickly interpreting results. Much appreciation, I am going to be evangelizing this package to all the R users in my department. One final question: is it possible to automatically name the models based on the values of the variable in `nest_by()`? – Rob Lytle Aug 22 '21 at 17:25
-
Cool. If you pass a *named* list to `modelsummary`, the names will be used as column headers. The `pull` function accepts a `name` argument that assigns names to the elements of the column you are pulling. So in my example you could just swap the `pull` call for: `pull(models, name = cyl)` – Vincent Aug 22 '21 at 17:59