Learn R Programming

marginaleffects (version 0.3.4)

marginaleffects: Marginal Effects

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). 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). Additional information can be found in the Details and Examples sections below, and in the vignette on the marginaleffects website.

Usage

marginaleffects(
  model,
  newdata = NULL,
  variables = NULL,
  vcov = TRUE,
  type = "response",
  ...
)

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
random.only lme4::predict.merMod

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/

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)))

# 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