0

I'm trying to group coefficients together in a modelsummary output table and add row titles for these groups :

library(modelsummary)

ols1 <- lm(mpg ~ cyl + disp + hp,
  data = mtcars,
  na.action = na.omit
)

modelsummary(ols1,
  title = "Table 1",
  stars = TRUE
)

The modelsummary documentation (https://cran.r-project.org/web/packages/modelsummary/modelsummary.pdf) suggests this might be something to do with the shape and group_map arguments, but I can't really figure out how to use them.

Any guidance would be very helpful, thanks!

Phil
  • 7,287
  • 3
  • 36
  • 66

1 Answers1

1

When the documentation mentions “groups”, it refers to models like multinomial logits where each predictor has one coefficient per outcome level. In this example, the “group” column is called “response”:

library(nnet)
library(modelsummary)

mod <- multinom(cyl ~ mpg + hp, data = mtcars, trace = FALSE)

modelsummary(
    mod,
    output = "markdown",
    shape = response ~ model)
Model 1
(Intercept) 6 0.500
(41.760)
8 8.400
(0.502)
mpg 6 -83.069
(416.777)
8 -120.167
(508.775)
hp 6 16.230
(81.808)
8 20.307
(87.777)
Num.Obs. 32
R2 1.000
R2 Adj. 0.971
AIC 12.0
BIC 20.8
RMSE 0.00

What you probably mean is something different: Adding manual labels to sets of coefficients. This is easy to achieve because modelsummary() produces a kableExtra or a gt table which can be customized in infinite ways.

https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html

For example, you may want to look at the group_rows function from kableExtra:

library(kableExtra)
mod <- lm(mpg ~ cyl + disp + hp, data = mtcars)
modelsummary(mod) |>
    group_rows(index = c("Uninteresting" = 4,
                         "Interesting" = 4,
                         "Other" = 7))

enter image description here

Vincent
  • 15,809
  • 7
  • 37
  • 39
  • 1
    Thanks so much! This is exactly what I was looking for. In case anyone is interested, it seems you can do a similar thing with pack_rows [link](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html) – Reuben Long Jun 24 '22 at 09:53