Skip to content

Instantly share code, notes, and snippets.

@benzipperer
Created November 16, 2023 21:48
Show Gist options
  • Save benzipperer/1d1887ff84283425a9a181b0bc6792d9 to your computer and use it in GitHub Desktop.
Save benzipperer/1d1887ff84283425a9a181b0bc6792d9 to your computer and use it in GitHub Desktop.
stacked regression model tables using modelsummary and kableextra
library(tidyverse)
library(modelsummary)
library(kableExtra)
model_am1 = mtcars %>%
filter(am == 1) %>%
lm(mpg ~ wt, data = .)
model_am0 = mtcars %>%
filter(am == 0) %>%
lm(mpg ~ wt, data = .)
model_am1_hp = mtcars %>%
filter(am == 1) %>%
lm(mpg ~ wt + hp, data = .)
model_am0_hp = mtcars %>%
filter(am == 0) %>%
lm(mpg ~ wt + hp, data = .)
models <- list(
list(model_am1, model_am0),
list(model_am1_hp, model_am0_hp)
)
modelsummary(
models,
shape = "rcollapse",
gof_map = "nobs",
coef_map = "wt",
output = "data.frame"
) %>%
rename(row_names = 1) %>%
mutate(row_names = if_else(row_number() == 1, "No controls", row_names)) %>%
mutate(row_names = if_else(row_number() == 3, "Additional controls", row_names)) %>%
kbl(booktabs = T, format = "latex", col.names = NULL) %>%
kable_classic(full_width = FALSE) %>%
row_spec(c(2, 4), extra_latex_after = "\\\\[-0.3cm]") %>%
add_header_above(c(" " = 1, "Automatic transmission" = 1, "Manual transmission" = 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment