Learn R Programming

marginaleffects (version 0.1.0)

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

Description

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). The list of supported models and of models whose numerical results have been validated against external software (Stata or margins) is held in the supported_models dataset which accompanies this package.

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 of marginal effect estimates with one row per observation per marginal effect.

Examples

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

# typical marginal effects
marginaleffects(mod,
                newdata = typical(hp = c(100, 110)))

# counterfactual average marginal effects
marginaleffects(mod,
                newdata = counterfactual(hp = c(100, 110)))

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

# }

Run the code above in your browser using DataLab