ModelSummary, add_rows and rbind

117 views Asked by At

I am trying to add rows immediately below the regression coefficients, using modelsummary output that uses rcollapse or rbind to stack regression models. I am able to make this work when I stack two or three panels, but the rows do not position correctly when I add a fourth. See below for a minimal reproducible example.

library(AER)
library(modelsummary)

# The rows position correctly with three stacks ####

stacks3 <- list(
  "Panel A: MPG" = list(
    "A" = lm(mpg ~ hp, data = mtcars),
    "B" = lm(mpg ~ hp, data = mtcars)),
  "Panel B: MPG" = list(
    "A" = lm(cyl ~ hp, data = mtcars),
    "B" = lm(cyl ~ hp, data = mtcars)),
  "Panel C: MPG" = list(
    "A" = lm(disp ~ hp, data = mtcars),
    "B" = lm(disp ~ hp, data = mtcars))
)

rows3 <- tibble::tribble(~term, ~term, ~term,
                         'rowA', '-', '-',
                         'rowB', '-', '-',
                         'rowC', '-', '-')
attr(rows3, 'position') <- c(3, 7, 11)


modelsummary(
  stacks3,
  coef_omit = -2,
  fmt = 3,
  add_rows = rows3,
  shape = "rbind",
  gof_map = c("nobs"))


# The rows do not position correctly with four stacks ####
# Note that the third row with Nobs shifts here
stacks4 <- list(
  "Panel A: MPG" = list(
    "A" = lm(mpg ~ hp, data = mtcars),
    "B" = lm(mpg ~ hp, data = mtcars)),
  "Panel B: MPG" = list(
    "A" = lm(cyl ~ hp, data = mtcars),
    "B" = lm(cyl ~ hp, data = mtcars)),
  "Panel C: MPG" = list(
    "A" = lm(disp ~ hp, data = mtcars),
    "B" = lm(disp ~ hp, data = mtcars)),
  "Panel D: MPG" = list(
    "A" = lm(disp ~ hp, data = mtcars),
    "B" = lm(disp ~ hp, data = mtcars))
)

rows4 <- tibble::tribble(~term, ~term, ~term,
                         'rowA', '-', '-',
                         'rowB', '-', '-',
                         'rowC', '-', '-',
                         'rowD', '-', '-')
attr(rows4, 'position') <- c(3,7,11,15)

modelsummary(
  stacks4,
  coef_omit = -2,
  fmt = 3,
  add_rows = rows4,
  shape = "rbind",
  gof_map = c("nobs"))
0

There are 0 answers