0

I like to print the output of modelsummary in a two-column format. When I do a regression interaction model with a factor variable with several levels, I get quite a few regression coefficients. The R console prints them side-by-side. My final output should be html for r markdown / bookdown.

interaction <- lm(mpg ~ factor(hp)*factor(am), data=mtcars)
interaction

enter image description here

When I do this in modelsummary

library(modelsummary)

modelsummary(title = 'Interaction Model.',
             list("MPG" = interaction), 
             gof_omit = 'R2|AIC|BIC|RMSE|Log.Lik.|F')

it's just too long.

enter image description here

Any alternative?

Marco
  • 2,368
  • 6
  • 22
  • 48

3 Answers3

1

You can use broom::tidy and dplyr::select to get a 44 row table with 3 columns:

library(broom)
library(dplyr)
interaction <- lm(mpg ~ factor(hp)*factor(am), data=mtcars)
tidy(interaction) %>% select(term, estimate, std.error)

knitr::kable can format the table if you need, and you can filter the rows, etc. For example:

library(dplyr)
library(broom)
library(knitr)
interaction <- lm(mpg ~ factor(hp)*factor(am), data=mtcars)
tidy(interaction) %>% 
  select(term, estimate, std.error) %>% 
  filter(! is.na(estimate)) %>% 
  kable(digits=3)

results in

term estimate std.error
(Intercept) 29.65 2.296
factor(hp)62 -5.25 2.717
factor(hp)65 3.50 2.054
factor(hp)66 -0.55 1.779
factor(hp)91 -4.40 2.054
factor(hp)93 -7.60 2.054
factor(hp)95 -6.85 2.717
factor(hp)97 -8.15 2.717
factor(hp)105 -11.55 2.717
factor(hp)109 -9.00 2.054
factor(hp)110 -8.25 2.717
factor(hp)113 0.00 2.054
factor(hp)123 -11.15 2.516
factor(hp)150 -14.30 2.516
factor(hp)175 -10.70 2.054
factor(hp)180 -13.35 2.445
factor(hp)205 -19.25 2.717
factor(hp)215 -19.25 2.717
factor(hp)230 -14.95 2.717
factor(hp)245 -15.85 2.516
factor(hp)264 -14.60 2.054
factor(hp)335 -15.40 2.054
factor(am)1 0.75 1.779
factor(hp)110:factor(am)1 -1.15 2.516
James_D
  • 201,275
  • 16
  • 291
  • 322
1

You can use the shape argument. See the ?modelsummary documentation and the many examples on the website.

library(modelsummary)
interaction <- lm(mpg ~ factor(hp)*factor(am), data=mtcars)
modelsummary(interaction, shape = term ~ statistic)
(1) / Est. (1) / S.E.
(Intercept) 29.650 2.296
factor(hp)62 -5.250 2.717
factor(hp)65 3.500 2.054
factor(hp)66 -0.550 1.779
factor(hp)91 -4.400 2.054
factor(hp)93 -7.600 2.054
factor(hp)95 -6.850 2.717
factor(hp)97 -8.150 2.717
factor(hp)105 -11.550 2.717
factor(hp)109 -9.000 2.054
factor(hp)110 -8.250 2.717
factor(hp)113 0.000 2.054
factor(hp)123 -11.150 2.516
factor(hp)150 -14.300 2.516
factor(hp)175 -10.700 2.054
factor(hp)180 -13.350 2.445
factor(hp)205 -19.250 2.717
factor(hp)215 -19.250 2.717
factor(hp)230 -14.950 2.717
factor(hp)245 -15.850 2.516
factor(hp)264 -14.600 2.054
factor(hp)335 -15.400 2.054
factor(am)1 0.750 1.779
factor(hp)110 × factor(am)1 -1.150 2.516
:—————————- ———–: ———–:
Num.Obs. 32
R2 0.985
R2 Adj. 0.942
AIC 120.3
BIC 157.0
Log.Lik. -35.168
RMSE 0.73
Vincent
  • 15,809
  • 7
  • 37
  • 39
  • But there is no real multi-column output right? Even after outsourcing every second row (the SE), the list of coef is still too long. My format would allow for 2 to 3 columns (with diagnostics in the last column). – Marco May 04 '23 at 15:33
  • Yes, that's correct. – Vincent May 04 '23 at 16:14
0

building on James_D's answer, here's a way to reshape the long table into a wide columns-of-columns format (using an eclectic mix of base R and tidyverse pieces):

library(dplyr)
library(tidyr)
library(broom)
library(knitr)

parts = 3 ## multi-column parts to display alongside

tidy(interaction) |>
  select(term, coefficient = estimate) |>
  mutate(part = rep(1:parts, each = ceiling(n()/parts), length.out = n()),
         row_index = rep(1:ceiling(n()/parts), length.out = n())
         ) |>
  split(~ part) |>
  Reduce(f = \(x, y) x |> select(-part) |> left_join(y, by = 'row_index')) |>
  select(-c(row_index, part)) |>
  kable()

|term.x        | coefficient.x|term.y                   | coefficient.y|term                      | coefficient|
|:-------------|-------------:|:------------------------|-------------:|:-------------------------|-----------:|
|(Intercept)   |         29.65|factor(hp)180            |        -13.35|factor(hp)105:factor(am)1 |          NA|
|factor(hp)62  |         -5.25|factor(hp)205            |        -19.25|factor(hp)109:factor(am)1 |          NA|
|factor(hp)65  |          3.50|factor(hp)215            |        -19.25|factor(hp)110:factor(am)1 |       -1.15|
|factor(hp)66  |         -0.55|factor(hp)230            |        -14.95|factor(hp)113:factor(am)1 |          NA|
|factor(hp)91  |         -4.40|factor(hp)245            |        -15.85|factor(hp)123:factor(am)1 |          NA|
## [...]

You can polish up above output with {htmlTables} as described in this SO answer.

I_O
  • 4,983
  • 2
  • 2
  • 15