marginaleffects: Marginal effects using numerical derivatives

Description Usage Arguments Value Examples

View source: R/marginaleffects.R

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

1
2
3
4
5
6
7
8
marginaleffects(
  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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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))

marginaleffects documentation built on Oct. 19, 2021, 1:09 a.m.