1

I like to show logistic regression with and without exponentiated coefficients side by side with the modelsummary package. The package produces great html output. It comes with an easy option to turn exponentiate = TRUE on/off. But the option applies to all models in a list.

log_model = glm(vs ~ am, family = binomial(link = 'logit'), data = mtcars)

library(modelsummary)

modelsummary(list("No Odds Ratio" = log_model), exponentiate = FALSE)
modelsummary(list("Odds Ratio" = log_model), exponentiate = TRUE)

enter image description here

Marco
  • 2,368
  • 6
  • 22
  • 48

2 Answers2

2

The exponentiate argument in modelsummary 1.3.0 accepts a vector of logicals to flip the switch on and off for different models. This is well-documented in ?modelsummary. For example:

library(modelsummary)
mod = glm(am ~ mpg, data = mtcars, family = binomial)
mod = list("OR" = mod, "Coef" = mod)
modelsummary(mod, exponentiate = c(TRUE, FALSE))
OR Coef
(Intercept) 0.001 -6.604
(0.003) (2.351)
mpg 1.359 0.307
(0.156) (0.115)
Num.Obs. 32 32
AIC 33.7 33.7
BIC 36.6 36.6
Log.Lik. -14.838 -14.838
F 7.148 7.148
RMSE 0.39 0.39
Vincent
  • 15,809
  • 7
  • 37
  • 39
  • 1
    Thanks, you added it; I assumed the op wants the same design as in the attached image. But if somebody accepts one column for parameters, then such a solution is recommended:) – polkas Apr 15 '23 at 18:56
  • 1
    This also solves the incorrect Table labels in `bookdown`. I missed the vectorization of `exponentiate`. Thanks Vincent – Marco Apr 16 '23 at 10:50
1

Each modelsummary function call returns a string with a "kableExtra" class representing HTML/CSS. We can add a proper div around to get what you want.

log_model <- glm(vs ~ am, family = binomial(link = "logit"), data = mtcars)

library(modelsummary)

structure(
  paste(
    "<div style='display:flex; flex-wrap: wrap;'>",
    "<div style='flex-basis: 50%;'>",
    modelsummary(list("No Odds Ratio" = log_model), exponentiate = FALSE),
    "</div>",
    "<div style='flex-basis: 50%;'>",
    modelsummary(list("Odds Ratio" = log_model), exponentiate = TRUE),
    "</div>",
    "</div>",
    collapse = "\n"
  ),
  class = "kableExtra"
)

enter image description here

polkas
  • 3,797
  • 1
  • 12
  • 25
  • Side by side tables looking great. I embed this in `bookdown`. Both tables have the same table number. Do you have any more advice on this? – Marco Apr 15 '23 at 10:07
  • Can you help me to reproduce that? What output and args for it are you using? possibly some code chunk specs are important too. – polkas Apr 15 '23 at 10:27
  • This is a powerful strategy. Nice! But see my answer for a built-in solution in `modelsummary` which requires no HTML. – Vincent Apr 15 '23 at 12:24