Learn R Programming

marginaleffects (version 0.3.2)

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() , which compute and print Average Marginal Effects (AME). The datagrid() function and the newdata argument can be used to calculate Marginal Effects at the Mean. See below for details and examples.

Usage

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

Arguments

model

Model object

newdata

A dataset over which to compute marginal effects. NULL uses the original data used to fit the model.

variables

Variables to consider (character vector). NULL calculates marginal effects for all terms in the model object.

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

type

Type(s) of prediction as string or vector This can differ based on the model type, but will typically be a string such as: "response", "link", "probs", or "zero".

...

Additional arguments are pushed forward to predict().

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.

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

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 (Counterfactual)
marginaleffects(mod, newdata = datagrid(hp = c(100, 110)))

# Marginal Effects at User-Specified Values (Counterfactual)
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