Learn R Programming

marginaleffects (version 0.5.0)

marginalmeans: Marginal Means

Description

Compute estimated marginal means for specified factors. Marginal means are adjusted predictions, averaged across a grid of categorical predictors, holding other numeric predictors at their means. For more information, see the Details and Examples sections below, and in the vignettes on the marginaleffects website: https://vincentarelbundock.github.io/marginaleffects/

Usage

marginalmeans(
  model,
  variables = NULL,
  variables_grid = NULL,
  vcov = TRUE,
  conf_level = 0.95,
  type = "response",
  transform_post = NULL,
  interaction = NULL,
  ...
)

Value

Data frame of marginal means with one row per variable-value combination.

Model-Specific Arguments

Some model types allow model-specific arguments to modify the nature of marginal effects, predictions, marginal means, and contrasts.

Package Class Argument Documentation
brms brmsfit ndraws brms::posterior_predict
re_formula
lme4 merMod include_random insight::get_predicted
re.form lme4::predict.merMod
allow.new.levels lme4::predict.merMod
glmmTMB glmmTMB re.form glmmTMB::predict.glmmTMB
allow.new.levels glmmTMB::predict.glmmTMB
zitype glmmTMB::predict.glmmTMB
mgcv bam exclude mgcv::predict.bam
robustlmm rlmerMod re.form robustlmm::predict.rlmerMod
allow.new.levels robustlmm::predict.rlmerMod

Details

This function begins by calling the predictions function to obtain a grid of predictors, and adjusted predictions for each cell. The grid includes all combinations of the categorical variables listed in the variables and variables_grid arguments, or all combinations of the categorical variables used to fit the model if variables_grid is NULL. In the prediction grid, numeric variables are held at their means.

After constructing the grid and filling the grid with adjusted predictions, marginalmeans computes marginal means for the variables listed in the variables argument, by average across all categories in the grid.

marginalmeans can only compute standard errors for linear models, or for predictions on the link scale, that is, with the type argument set to "link".

The marginaleffects website compares the output of this function to the popular emmeans package, which provides similar but more advanced functionality: https://vincentarelbundock.github.io/marginaleffects/

Examples

Run this code
# NOT RUN {
library(marginaleffects)

# Convert numeric variables to categorical before fitting the model
dat <- mtcars
dat$cyl <- as.factor(dat$cyl)
dat$am <- as.logical(dat$am)
mod <- lm(mpg ~ hp + cyl + am, data = dat)

# Compute and summarize marginal means
mm <- marginalmeans(mod)
summary(mm)
# }

Run the code above in your browser using DataLab