Learn R Programming

marginaleffects (version 0.1.0)

marginalmeans: Marginal Means

Description

Compute estimated marginal means for specified factors.

Usage

marginalmeans(model, variables = NULL, vcov = insight::get_varcov(model))

Arguments

model

Model object

variables

predictors over which to compute marginal means (character vector). NULL calculates marginal means for all logical, character, or factor variables in the dataset used to fit model.

vcov

Matrix or boolean

  • FALSE: does not compute unit-level standard errors.

  • TRUE: computes unit-level standard errors using the default vcov(model) variance-covariance matrix.

  • Named square matrix: computes standard errors with a user-supplied variance-covariance matrix. This matrix must be square and have dimensions equal to the number of coefficients in get_coef(model).

Value

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

Details

This function begins by calling the predictions function to obtain a grid of predictors, including cells for all combinations of all categorical variables used to fit model, with numeric variables held at their means. Then, it computes marginal means for the variables listed in the variables argument.

The marginaleffects website compares the output of this function to the popular emmeans package, which provides similar functionality and more advanced options: 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