0

I JUST found out about this amazing R package, modelsummary.

It doesn't seem like it offers an ability to transpose regression outputs.

I know that you cannot do a tranposition within kable-extra, which is my go-to for ordinary table outputs in R. Since modelsummary relies on kable-extra for post-processing, I'm wondering if this is possible. Has anyone else figured it out?

Ideally I'd like to preserve the stars of my regression output. This is available in STATA (below):

Thanks in advance!

enter image description here

Daycent
  • 455
  • 4
  • 15

1 Answers1

1

You can flip the order of the terms in the group argument formula. See documentation here and also here for many examples.

library(modelsummary)

mod <- list(
  lm(mpg ~ hp, mtcars),
  lm(mpg ~ hp + drat, mtcars))

modelsummary(mod, group = model ~ term)
(Intercept) hp drat
Model 1 30.099 -0.068
(1.634) (0.010)
Model 2 10.790 -0.052 4.698
(5.078) (0.009) (1.192)

The main problem with this strategy is that there is not (yet) an automatic way to append goodness of fit statistics. So you would probably have to rig something up by creating a data.frame and feeding it to the add_columns argument. For example:

N <- sapply(mod, function(x) get_gof(x)$nobs)
N <- data.frame(N = c(N[1], "", N[2], ""))
modelsummary(mod,
             group = model ~ term,
             add_columns = N,
             align = "lcccc")
(Intercept) hp drat N
Model 1 30.099 -0.068 32
(1.634) (0.010)
Model 2 10.790 -0.052 4.698 32
(5.078) (0.009) (1.192)

If you have ideas about the best default behavior for goodness of fit statistics, please file a feature request on Github.

Vincent
  • 15,809
  • 7
  • 37
  • 39