I'm trying to produce a table of multiple models with standardized coefficients color-coded and sized based on the coefficient size. I'll be doing this with dozens of models and it seems using color and size would be a way to show patterns across predictors and models. Something like this from {corrplot}
is what I'm interested in, but I'd only need one color:
I'm also interested in having cells for p-values > .05 to be blank or made very faint or something.
Here is an example I'm working with.
library(dplyr)
library(modelsummary)
dat <- mtcars %>% mutate(
cyl = as.factor(cyl),
gear = as.factor(gear)
)
## List of models
models = list(
"MPG" = lm(mpg ~ cyl + hp + wt + qsec + vs + am + gear + carb, data = dat),
"Disp" = lm(disp ~ cyl + hp + wt + qsec + vs + am + gear + carb, data = dat),
"Drat" = lm(drat ~ cyl + hp + wt + qsec + vs + am + gear + carb, data = dat)
)
## I feed the list of models to modelsummary() and ask for only coefficients and p.value as side by side columns
models.ep <- modelsummary(models,
standardize = "basic",
shape = term ~ model + statistic,
estimate = "{estimate}",
statistic = "p.value" ,
gof_map = NA,
output = "data.frame")
## I started trying to use {formattable} but the color bars aren't what I want (I'm interested in the image shown above with the size and darkness/lightness of the circles representing effect magnitude.
library(formattable)
formattable(models.ep, list(
"MPG / Est." = color_bar("#e9c46a"),
"Disp / Est." = color_bar("#80ed99"),
"Drat / Est." = color_bar("#f28482")
))
## I also looked around in the {flextable} but did not see a way to do what I need.
You may be able to achieve something similar to this with the get_estimates()
function from modelsummary
and the ggplot2
package. This is not exactly the image you gave, but it may help you get started:
library(ggplot2)
library(modelsummary)
dat <- mtcars |> transform(
cyl = as.factor(cyl),
gear = as.factor(gear)
)
models = list(
"MPG" = lm(mpg ~ cyl + hp + wt + qsec + vs + am + gear + carb, data = dat),
"Disp" = lm(disp ~ cyl + hp + wt + qsec + vs + am + gear + carb, data = dat),
"Drat" = lm(drat ~ cyl + hp + wt + qsec + vs + am + gear + carb, data = dat)
)
results <- lapply(models, get_estimates)
results <- lapply(names(results), \(n) transform(results[[n]], model = n))
results <- do.call("rbind", results)
ggplot(results, aes(x = model, y = term, size = estimate, color = p.value)) +
geom_point() +
theme_minimal() +
theme(panel.grid = element_blank()) +
labs(x = "", y = "")