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

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


mod <- glm(am ~ hp * wt, data = mtcars, family = binomial)
mfx <- marginaleffects(mod)
summary(mfx)
#> Average marginal effects 
#>       type Term    Effect Std. Error z value   Pr(>|z|)     2.5 %    97.5 %
#> 1 response   hp  0.002654   0.001941   1.367    0.17158 -0.001151  0.006458
#> 2 response   wt -0.435727   0.102024  -4.271 1.9474e-05 -0.635690 -0.235764
#> 
#> Model type:  glm 
#> Prediction type:  response 
tidy(mfx)
#>       type term     estimate   std.error statistic      p.value    conf.low
#> 1 response   hp  0.002653558 0.001940937  1.367153 1.715775e-01 -0.00115061
#> 2 response   wt -0.435726799 0.102023796 -4.270835 1.947425e-05 -0.63568977
#>      conf.high
#> 1  0.006457725
#> 2 -0.235763833
head(mfx)
#>   rowid     type term         dydx   std.error am  hp    wt predicted
#> 1     1 response   hp  0.006989229 0.005882349  1 110 2.620 0.8983110
#> 2     1 response   wt -0.827161739 0.819043730  1 110 2.620 0.8983110
#> 3     2 response   hp  0.016403141 0.013387937  1 110 2.875 0.4676447
#> 4     2 response   wt -2.253398912 1.128058512  1 110 2.875 0.4676447
#> 5     3 response   wt -0.265544570 0.321881990  1  93 2.320 0.9671038
#> 6     3 response   hp  0.002831769 0.003755191  1  93 2.320 0.9671038
plot(mfx)


# typical marginal effects
marginaleffects(mod,
                newdata = typical(hp = c(100, 110)))
#>   rowid     type term         dydx   std.error      wt  hp  predicted
#> 1     1 response   hp  0.001165863 0.001753306 3.21725 100 0.02311544
#> 2     1 response   wt -0.194911082 0.307492808 3.21725 100 0.02311544
#> 3     2 response   hp  0.001894129 0.002415076 3.21725 110 0.03814134
#> 4     2 response   wt -0.331939457 0.436422720 3.21725 110 0.03814134

# counterfactual average marginal effects
marginaleffects(mod,
                newdata = counterfactual(hp = c(100, 110)))
#>     rowid     type term          dydx    std.error    wt  hp    predicted
#> 1       1 response   hp  1.204245e-02 9.995994e-03 2.620 100 8.043137e-01
#> 2       1 response   hp  1.204245e-02 9.995994e-03 2.620 110 8.983110e-01
#> 3       1 response   wt -1.359471e+00 8.718971e-01 2.620 100 8.043137e-01
#> 4       1 response   wt -1.359471e+00 8.718971e-01 2.620 110 8.983110e-01
#> 5       2 response   hp  1.415558e-02 1.080248e-02 2.875 100 3.124936e-01
#> 6       2 response   hp  1.415558e-02 1.080248e-02 2.875 110 4.676447e-01
#> 7       2 response   wt -1.854887e+00 9.071250e-01 2.875 100 3.124936e-01
#> 8       2 response   wt -1.854887e+00 9.071250e-01 2.875 110 4.676447e-01
#> 9       3 response   wt -1.519931e-01 2.308674e-01 2.320 110 9.925652e-01
#> 10      3 response   wt -1.519931e-01 2.308674e-01 2.320 100 9.820847e-01
#> 11      3 response   hp  1.566074e-03 2.204571e-03 2.320 110 9.925652e-01
#> 12      3 response   hp  1.566074e-03 2.204571e-03 2.320 100 9.820847e-01
#> 13      4 response   wt -1.985550e-01 3.116129e-01 3.215 110 3.889558e-02
#> 14      4 response   wt -1.985550e-01 3.116129e-01 3.215 100 2.355826e-02
#> 15      4 response   hp  1.189815e-03 1.779620e-03 3.215 110 3.889558e-02
#> 16      4 response   hp  1.189815e-03 1.779620e-03 3.215 100 2.355826e-02
#> 17      5 response   hp  1.453976e-04 3.409128e-04 3.440 100 3.445112e-03
#> 18      5 response   hp  1.453976e-04 3.409128e-04 3.440 110 5.252163e-03
#> 19      5 response   wt -2.963387e-02 7.036292e-02 3.440 100 3.445112e-03
#> 20      5 response   wt -2.963387e-02 7.036292e-02 3.440 110 5.252163e-03
#> 21      6 response   wt -2.496082e-02 6.103757e-02 3.460 100 2.900259e-03
#> 22      6 response   wt -2.496082e-02 6.103757e-02 3.460 110 4.386251e-03
#> 23      6 response   hp  1.200600e-04 2.909609e-04 3.460 100 2.900259e-03
#> 24      6 response   hp  1.200600e-04 2.909609e-04 3.460 110 4.386251e-03
#> 25      7 response   wt -9.689079e-03 2.748035e-02 3.570 110 1.625069e-03
#> 26      7 response   wt -9.689079e-03 2.748035e-02 3.570 100 1.123797e-03
#> 27      7 response   hp  4.145963e-05 1.189714e-04 3.570 110 1.625069e-03
#> 28      7 response   hp  4.145963e-05 1.189714e-04 3.570 100 1.123797e-03
#> 29      8 response   wt -2.436247e-01 3.600688e-01 3.190 110 4.829587e-02
#> 30      8 response   wt -2.436247e-01 3.600688e-01 3.190 100 2.906966e-02
#> 31      8 response   hp  1.489280e-03 2.093504e-03 3.190 110 4.829587e-02
#> 32      8 response   hp  1.489280e-03 2.093504e-03 3.190 100 2.906966e-02
#> 33      9 response   hp  2.119011e-03 2.680346e-03 3.150 110 6.793586e-02
#> 34      9 response   hp  2.119011e-03 2.680346e-03 3.150 100 4.057610e-02
#> 35      9 response   wt -3.360304e-01 4.471099e-01 3.150 110 6.793586e-02
#> 36      9 response   wt -3.360304e-01 4.471099e-01 3.150 100 4.057610e-02
#> 37     10 response   wt -2.963387e-02 7.036292e-02 3.440 100 3.445112e-03
#> 38     10 response   wt -2.963387e-02 7.036292e-02 3.440 110 5.252163e-03
#> 39     10 response   hp  1.453976e-04 3.409128e-04 3.440 100 3.445112e-03
#> 40     10 response   hp  1.453976e-04 3.409128e-04 3.440 110 5.252163e-03
#> 41     11 response   hp  1.453976e-04 3.409128e-04 3.440 110 5.252163e-03
#> 42     11 response   hp  1.453976e-04 3.409128e-04 3.440 100 3.445112e-03
#> 43     11 response   wt -2.963387e-02 7.036292e-02 3.440 110 5.252163e-03
#> 44     11 response   wt -2.963387e-02 7.036292e-02 3.440 100 3.445112e-03
#> 45     12 response   hp  2.415339e-07 1.361964e-06 4.070 110 1.761996e-05
#> 46     12 response   hp  2.415339e-07 1.361964e-06 4.070 100 1.499928e-05
#> 47     12 response   wt -1.294632e-04 6.017685e-04 4.070 110 1.761996e-05
#> 48     12 response   wt -1.294632e-04 6.017685e-04 4.070 100 1.499928e-05
#> 49     13 response   hp  8.548397e-06 3.053517e-05 3.730 110 3.823235e-04
#> 50     13 response   hp  8.548397e-06 3.053517e-05 3.730 100 2.825022e-04
#> 51     13 response   wt -2.437708e-03 8.311388e-03 3.730 110 3.823235e-04
#> 52     13 response   wt -2.437708e-03 8.311388e-03 3.730 100 2.825022e-04
#> 53     14 response   hp  5.170044e-06 1.970122e-05 3.780 100 1.834657e-04
#> 54     14 response   hp  5.170044e-06 1.970122e-05 3.780 110 2.431840e-04
#> 55     14 response   wt -1.583280e-03 5.684000e-03 3.780 100 1.834657e-04
#> 56     14 response   wt -1.583280e-03 5.684000e-03 3.780 110 2.431840e-04
#> 57     15 response   hp -1.862539e-11 1.590175e-10 5.250 100 5.634331e-10
#> 58     15 response   hp -1.862539e-11 1.590175e-10 5.250 110 4.048346e-10
#> 59     15 response   wt -4.863231e-09 4.406971e-08 5.250 100 5.634331e-10
#> 60     15 response   wt -4.863231e-09 4.406971e-08 5.250 110 4.048346e-10
#> 61     16 response   hp -5.054491e-12 4.636245e-11 5.424 110 8.380323e-11
#> 62     16 response   hp -5.054491e-12 4.636245e-11 5.424 100 1.254030e-10
#> 63     16 response   wt -1.082406e-09 1.052207e-08 5.424 110 8.380323e-11
#> 64     16 response   wt -1.082406e-09 1.052207e-08 5.424 100 1.254030e-10
#> 65     17 response   wt -2.141194e-09 2.017329e-08 5.345 110 1.713248e-10
#> 66     17 response   wt -2.141194e-09 2.017329e-08 5.345 100 2.480696e-10
#> 67     17 response   hp -9.182237e-12 8.154875e-11 5.345 110 1.713248e-10
#> 68     17 response   hp -9.182237e-12 8.154875e-11 5.345 100 2.480696e-10
#> 69     18 response   hp  6.006465e-04 1.053596e-03 2.200 100 9.935694e-01
#> 70     18 response   hp  6.006465e-04 1.053596e-03 2.200 110 9.974784e-01
#> 71     18 response   wt -5.519541e-02 1.047910e-01 2.200 100 9.935694e-01
#> 72     18 response   wt -5.519541e-02 1.047910e-01 2.200 110 9.974784e-01
#> 73     19 response   hp  4.902651e-06 1.789468e-05 1.615 100 9.999586e-01
#> 74     19 response   hp  4.902651e-06 1.789468e-05 1.615 110 9.999873e-01
#> 75     19 response   wt -3.577723e-04 1.377302e-03 1.615 100 9.999586e-01
#> 76     19 response   wt -3.577723e-04 1.377302e-03 1.615 110 9.999873e-01
#> 77     20 response   wt -2.390264e-03 7.406607e-03 1.835 110 9.999071e-01
#> 78     20 response   wt -2.390264e-03 7.406607e-03 1.835 100 9.997232e-01
#> 79     20 response   hp  3.021851e-05 8.798615e-05 1.835 110 9.999071e-01
#> 80     20 response   hp  3.021851e-05 8.798615e-05 1.835 100 9.997232e-01
#> 81     21 response   wt -4.870405e-01 5.208582e-01 2.465 110 9.729230e-01
#> 82     21 response   wt -4.870405e-01 5.208582e-01 2.465 100 9.400220e-01
#> 83     21 response   hp  4.677862e-03 4.884891e-03 2.465 110 9.729230e-01
#> 84     21 response   hp  4.677862e-03 4.884891e-03 2.465 100 9.400220e-01
#> 85     22 response   hp  6.736539e-05 1.794532e-04 3.520 110 2.552846e-03
#> 86     22 response   hp  6.736539e-05 1.794532e-04 3.520 100 1.729555e-03
#> 87     22 response   wt -1.490273e-02 3.961604e-02 3.520 110 2.552846e-03
#> 88     22 response   wt -1.490273e-02 3.961604e-02 3.520 100 1.729555e-03
#> 89     23 response   hp  1.525131e-04 3.546083e-04 3.435 100 3.596568e-03
#> 90     23 response   hp  1.525131e-04 3.546083e-04 3.435 110 5.493995e-03
#> 91     23 response   wt -3.093196e-02 7.289668e-02 3.435 100 3.596568e-03
#> 92     23 response   wt -3.093196e-02 7.289668e-02 3.435 110 5.493995e-03
#> 93     24 response   hp  2.806815e-06 1.154864e-05 3.840 110 1.412900e-04
#> 94     24 response   hp  2.806815e-06 1.154864e-05 3.840 100 1.092887e-04
#> 95     24 response   wt -9.432139e-04 3.591236e-03 3.840 110 1.412900e-04
#> 96     24 response   wt -9.432139e-04 3.591236e-03 3.840 100 1.092887e-04
#> 97     25 response   hp  2.666431e-06 1.104133e-05 3.845 110 1.350387e-04
#> 98     25 response   hp  2.666431e-06 1.104133e-05 3.845 100 1.046710e-04
#> 99     25 response   wt -9.033649e-04 3.455916e-03 3.845 110 1.350387e-04
#> 100    25 response   wt -9.033649e-04 3.455916e-03 3.845 100 1.046710e-04
#> 101    26 response   wt -5.664166e-03 1.565144e-02 1.935 110 9.997704e-01
#> 102    26 response   wt -5.664166e-03 1.565144e-02 1.935 100 9.993439e-01
#> 103    26 response   hp  6.887674e-05 1.779961e-04 1.935 110 9.997704e-01
#> 104    26 response   hp  6.887674e-05 1.779961e-04 1.935 100 9.993439e-01
#> 105    27 response   wt -3.304863e-02 6.910542e-02 2.140 100 9.961597e-01
#> 106    27 response   wt -3.304863e-02 6.910542e-02 2.140 110 9.985336e-01
#> 107    27 response   hp  3.692031e-04 7.146808e-04 2.140 100 9.961597e-01
#> 108    27 response   hp  3.692031e-04 7.146808e-04 2.140 110 9.985336e-01
#> 109    28 response   hp  2.104980e-06 8.419538e-06 1.513 100 9.999828e-01
#> 110    28 response   hp  2.104980e-06 8.419538e-06 1.513 110 9.999950e-01
#> 111    28 response   wt -1.482885e-04 6.231705e-04 1.513 100 9.999828e-01
#> 112    28 response   wt -1.482885e-04 6.231705e-04 1.513 110 9.999950e-01
#> 113    29 response   hp  1.778434e-03 2.373731e-03 3.170 110 5.733104e-02
#> 114    29 response   hp  1.778434e-03 2.373731e-03 3.170 100 3.436137e-02
#> 115    29 response   wt -2.864049e-01 4.022327e-01 3.170 110 5.733104e-02
#> 116    29 response   wt -2.864049e-01 4.022327e-01 3.170 100 3.436137e-02
#> 117    30 response   wt -2.151319e+00 9.807785e-01 2.770 110 6.944131e-01
#> 118    30 response   wt -2.151319e+00 9.807785e-01 2.770 100 5.295166e-01
#> 119    30 response   hp  1.750450e-02 1.426497e-02 2.770 110 6.944131e-01
#> 120    30 response   hp  1.750450e-02 1.426497e-02 2.770 100 5.295166e-01
#> 121    31 response   wt -9.689079e-03 2.748035e-02 3.570 100 1.123797e-03
#> 122    31 response   wt -9.689079e-03 2.748035e-02 3.570 110 1.625069e-03
#> 123    31 response   hp  4.145963e-05 1.189714e-04 3.570 100 1.123797e-03
#> 124    31 response   hp  4.145963e-05 1.189714e-04 3.570 110 1.625069e-03
#> 125    32 response   hp  1.745714e-02 1.417906e-02 2.780 100 5.079624e-01
#> 126    32 response   hp  1.745714e-02 1.417906e-02 2.780 110 6.748744e-01
#> 127    32 response   wt -2.158255e+00 9.780962e-01 2.780 100 5.079624e-01
#> 128    32 response   wt -2.158255e+00 9.780962e-01 2.780 110 6.748744e-01

# heteroskedasticity robust standard errors
marginaleffects(mod, vcov = sandwich::vcovHC(mod))
#>    rowid     type term          dydx    std.error am  hp    wt    predicted
#> 1      1 response   hp  6.989229e-03 9.188426e-03  1 110 2.620 8.983110e-01
#> 2      1 response   wt -8.271617e-01 1.279010e+00  1 110 2.620 8.983110e-01
#> 3      2 response   hp  1.640314e-02 1.340865e-02  1 110 2.875 4.676447e-01
#> 4      2 response   wt -2.253399e+00 1.360905e+00  1 110 2.875 4.676447e-01
#> 5      3 response   wt -2.655446e-01 5.253557e-01  1  93 2.320 9.671038e-01
#> 6      3 response   hp  2.831769e-03 4.916395e-03  1  93 2.320 9.671038e-01
#> 7      4 response   wt -3.382383e-01 3.505055e-01  0 110 3.215 3.889558e-02
#> 8      4 response   hp  1.933575e-03 1.842493e-03  0 110 3.215 3.889558e-02
#> 9      5 response   hp  2.991347e-03 2.781261e-03  0 175 3.440 7.648382e-02
#> 10     5 response   wt -8.302249e-01 6.823524e-01  0 175 3.440 7.648382e-02
#> 11     6 response   hp  1.475603e-04 2.544284e-04  0 105 3.460 3.566962e-03
#> 12     6 response   wt -3.141797e-02 5.517898e-02  0 105 3.460 3.566962e-03
#> 13     7 response   wt -2.279347e+00 2.474881e+00  0 245 3.570 1.923979e-01
#> 14     7 response   hp  5.738829e-03 8.191035e-03  0 245 3.570 1.923979e-01
#> 15     8 response   wt -2.819198e-02 5.701635e-02  0  62 3.190 4.015235e-03
#> 16     8 response   hp  2.110144e-04 3.884630e-04  0  62 3.190 4.015235e-03
#> 17     9 response   wt -2.546923e-01 2.860002e-01  0  95 3.150 3.120992e-02
#> 18     9 response   hp  1.645791e-03 1.726980e-03  0  95 3.150 3.120992e-02
#> 19    10 response   hp  3.807685e-04 5.148223e-04  0 123 3.440 9.073330e-03
#> 20    10 response   wt -8.621334e-02 1.158666e-01  0 123 3.440 9.073330e-03
#> 21    11 response   wt -8.621334e-02 1.158666e-01  0 123 3.440 9.073330e-03
#> 22    11 response   hp  3.807685e-04 5.148223e-04  0 123 3.440 9.073330e-03
#> 23    12 response   wt -6.505458e-04 3.315794e-03  0 180 4.070 5.439236e-05
#> 24    12 response   hp  8.758474e-07 6.572651e-06  0 180 4.070 5.439236e-05
#> 25    13 response   wt -3.782430e-02 9.184598e-02  0 180 3.730 3.172383e-03
#> 26    13 response   hp  9.571749e-05 3.218446e-04  0 180 3.730 3.172383e-03
#> 27    14 response   hp  4.913415e-05 1.903042e-04  0 180 3.780 1.746319e-03
#> 28    14 response   wt -2.085110e-02 5.870368e-02  0 180 3.780 1.746319e-03
#> 29    15 response   hp -5.789952e-13 8.597919e-12  0 205 5.250 1.751508e-11
#> 30    15 response   wt -2.277157e-10 3.866959e-09  0 205 5.250 1.751508e-11
#> 31    16 response   wt -1.632782e-11 3.223634e-10  0 215 5.424 1.216933e-12
#> 32    16 response   hp -4.904969e-14 8.740137e-13  0 215 5.424 1.216933e-12
#> 33    17 response   wt -2.832600e-11 5.807088e-10  0 230 5.345 2.017342e-12
#> 34    17 response   hp -7.467144e-14 1.373822e-12  0 230 5.345 2.017342e-12
#> 35    18 response   hp  1.108683e-02 1.422135e-02  1  66 2.200 8.634101e-01
#> 36    18 response   wt -8.515432e-01 8.227415e-01  1  66 2.200 8.634101e-01
#> 37    19 response   hp  1.405293e-03 4.020638e-03  1  52 1.615 9.879847e-01
#> 38    19 response   wt -7.879409e-02 1.840977e-01  1  52 1.615 9.879847e-01
#> 39    20 response   wt -8.861724e-02 1.767656e-01  1  65 1.835 9.875007e-01
#> 40    20 response   hp  1.348054e-03 2.935489e-03  1  65 1.835 9.875007e-01
#> 41    21 response   wt -5.952843e-01 9.374444e-01  0  97 2.465 9.243530e-01
#> 42    21 response   hp  5.801587e-03 7.810194e-03  0  97 2.465 9.243530e-01
#> 43    22 response   wt -1.274388e-01 1.579215e-01  0 150 3.520 1.204113e-02
#> 44    22 response   hp  4.641522e-04 6.989948e-04  0 150 3.520 1.204113e-02
#> 45    23 response   hp  1.215176e-03 1.226520e-03  0 150 3.435 2.941871e-02
#> 46    23 response   wt -3.058856e-01 2.816151e-01  0 150 3.435 2.941871e-02
#> 47    24 response   wt -6.583631e-02 2.836171e-01  0 245 3.840 4.509574e-03
#> 48    24 response   hp  1.153078e-04 6.933458e-04  0 245 3.840 4.509574e-03
#> 49    25 response   wt -8.303109e-03 2.718630e-02  0 175 3.845 7.069764e-04
#> 50    25 response   hp  1.799896e-05 8.051093e-05  0 175 3.845 7.069764e-04
#> 51    26 response   wt -1.610539e-01 2.839053e-01  1  66 1.935 9.771760e-01
#> 52    26 response   hp  2.342925e-03 4.557330e-03  1  66 1.935 9.771760e-01
#> 53    27 response   hp  8.707316e-04 1.977080e-03  1  91 2.140 9.908948e-01
#> 54    27 response   wt -7.455609e-02 1.882778e-01  1  91 2.140 9.908948e-01
#> 55    28 response   hp  4.274703e-07 2.710374e-06  1 113 1.513 9.999965e-01
#> 56    28 response   wt -3.200338e-05 2.157386e-04  1 113 1.513 9.999965e-01
#> 57    29 response   wt -6.565758e-02 3.314830e-01  1 264 3.170 9.957403e-01
#> 58    29 response   hp  2.273419e-04 1.131595e-03  1 264 3.170 9.957403e-01
#> 59    30 response   wt -5.330220e-02 2.398867e-01  1 175 2.770 9.954493e-01
#> 60    30 response   hp  3.182868e-04 1.378430e-03  1 175 2.770 9.954493e-01
#> 61    31 response   hp  4.212583e-03 7.657185e-03  1 335 3.570 8.687038e-01
#> 62    31 response   wt -2.103001e+00 4.578801e+00  1 335 3.570 8.687038e-01
#> 63    32 response   hp  1.568764e-02 7.585714e-03  1 109 2.780 6.593657e-01
#> 64    32 response   wt -2.023983e+00 7.493504e-01  1 109 2.780 6.593657e-01