
Last chance! 50% off unlimited learning
Sale ends in
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.
marginaleffects(
model,
newdata = NULL,
variables = NULL,
vcov = TRUE,
type = "response",
...
)
Model object
A dataset over which to compute marginal effects. NULL
uses
the original data used to fit the model.
Variables to consider (character vector). NULL
calculates marginal effects for all terms in the model object.
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(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()
.
A data.frame of marginal effect estimates with one row per observation per marginal effect.
# 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