Learn R Programming

marginaleffects (version 0.5.0)

meffects: meffects() is a shortcut to marginaleffects()

Description

This function calculates marginal effects (slopes) for each row of the dataset. The resulting object can processed by the tidy() or summary() functions, which compute Average Marginal Effects (AME) or Group-Average Marginal Effects (G-AME). The datagrid() function and the newdata argument can be used to calculate Marginal Effects at the Mean (MEM) or Marginal Effects at User-Specified values (aka Marginal Effects at Representative values, MER). For more information, see the Details and Examples sections below, and in the vignettes on the marginaleffects website: https://vincentarelbundock.github.io/marginaleffects/

Usage

meffects(
  model,
  newdata = NULL,
  variables = NULL,
  vcov = TRUE,
  conf_level = 0.95,
  type = "response",
  eps = 1e-04,
  ...
)

Value

A data.frame with one row per observation (per term/group) and several columns:

  • rowid: row number of the newdata data frame

  • type: prediction type, as defined by the type argument

  • group: (optional) value of the grouped outcome (e.g., categorical outcome models)

  • term: the variable whose marginal effect is computed

  • dydx: marginal effect of the term on the outcome for a given combination of regressor values

  • std.error: standard errors computed by via the delta method.

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

A "marginal effect" is the partial derivative of the regression equation with respect to a variable in the model. This function uses automatic differentiation to compute marginal effects for a vast array of models, including non-linear models with transformations (e.g., polynomials). Uncertainty estimates are computed using the delta method.

A detailed vignette on marginal effects and a list of supported models can be found on the package website:

https://vincentarelbundock.github.io/marginaleffects/

Numerical derivatives for the marginaleffects function are calculated using a simple epsilon difference approach: \(\partial Y / \partial X = (f(X + \varepsilon) - f(X)) / \varepsilon\), where f is the predict() method associated with the model class, and \(\varepsilon\) is determined by the eps argument.

Warning: Some models are particularly sensitive to eps, so it is good practice to try different values of this argument.

Standard errors for the marginal effects are obtained using the Delta method. See the "Technical Notes" vignette on the package website for details.

Examples

Run this code
# NOT RUN {
mod <- glm(am ~ hp * wt, data = mtcars, family = binomial)
mfx <- marginaleffects(mod)
head(mfx)

# Average Marginal Effect (AME)
summary(mfx)
tidy(mfx)
plot(mfx)


# Marginal Effect at the Mean (MEM)
marginaleffects(mod, newdata = datagrid())

# Marginal Effect at User-Specified Values
# Variables not explicitly included in `datagrid()` are held at their means
marginaleffects(mod,
                newdata = datagrid(hp = c(100, 110)))

# Group-Average Marginal Effects (G-AME)
# Calculate marginal effects for each observation, and then take the average
# marginal effect within each subset of observations with different observed
# values for the `cyl` variable:
mod2 <- lm(mpg ~ hp * cyl, data = mtcars)
mfx2 <- marginaleffects(mod2, variables = "hp")
summary(mfx2, by = "cyl")

# Marginal Effects at User-Specified Values (counterfactual)
# Variables not explicitly included in `datagrid()` are held at their
# original values, and the whole dataset is duplicated once for each
# combination of the values in `datagrid()`
mfx <- marginaleffects(mod,
                       newdata = datagrid(hp = c(100, 110),
                                          grid_type = "counterfactual"))
head(mfx)

# Heteroskedasticity robust standard errors
marginaleffects(mod, vcov = sandwich::vcovHC(mod))

# }

Run the code above in your browser using DataLab