Skip to contents

Partial derivative (slope) of the regression equation with respect to a regressor of interest. The tidy() and summary() functions can be used to aggregate and summarize the output of marginaleffects(). To learn more, read the marginal effects vignette, visit the package website, or scroll down this page for a full list of vignettes:

Usage

marginaleffects(
  model,
  newdata = NULL,
  variables = NULL,
  vcov = TRUE,
  conf_level = 0.95,
  type = NULL,
  slope = "dydx",
  by = NULL,
  wts = NULL,
  hypothesis = NULL,
  eps = NULL,
  ...
)

Arguments

model

Model object

newdata

NULL, data frame, string, or datagrid() call. Determines the predictor values for which to compute marginal effects.

  • NULL (default): Unit-level marginal effects for each observed value in the original dataset.

  • data frame: Unit-level marginal effects for each row of the newdata data frame.

  • string:

    • "mean": Marginal Effects at the Mean. Marginal effects when each predictor is held at its mean or mode.

    • "median": Marginal Effects at the Median. Marginal effects when each predictor is held at its median or mode.

    • "marginalmeans": Marginal Effects at Marginal Means. See Details section below.

    • "tukey": Marginal Effects at Tukey's 5 numbers.

    • "grid": Marginal Effects on a grid of representative numbers (Tukey's 5 numbers and unique values of categorical predictors).

  • datagrid() call to specify a custom grid of regressors. For example:

    • newdata = datagrid(cyl = c(4, 6)): cyl variable equal to 4 and 6 and other regressors fixed at their means or modes.

    • See the Examples section and the datagrid() documentation.

variables

NULL or character vector. The subset of variables for which to compute marginal effects.

  • NULL: compute contrasts for all the variables in the model object (can be slow).

  • Character vector: subset of variables (usually faster).

vcov

Type of uncertainty estimates to report (e.g., for robust standard errors). Acceptable values:

  • FALSE: Do not compute standard errors. This can speed up computation considerably.

  • TRUE: Unit-level standard errors using the default vcov(model) variance-covariance matrix.

  • String which indicates the kind of uncertainty estimates to return.

    • Heteroskedasticity-consistent: "HC", "HC0", "HC1", "HC2", "HC3", "HC4", "HC4m", "HC5". See ?sandwich::vcovHC

    • Heteroskedasticity and autocorrelation consistent: "HAC"

    • Mixed-Models degrees of freedom: "satterthwaite", "kenward-roger"

    • Other: "NeweyWest", "KernHAC", "OPG". See the sandwich package documentation.

  • One-sided formula which indicates the name of cluster variables (e.g., ~unit_id). This formula is passed to the cluster argument of the sandwich::vcovCL function.

  • Square covariance matrix

  • Function which returns a covariance matrix (e.g., stats::vcov(model))

conf_level

numeric value between 0 and 1. Confidence level to use to build a confidence interval.

type

string indicates the type (scale) of the predictions used to compute marginal effects or contrasts. This can differ based on the model type, but will typically be a string such as: "response", "link", "probs", or "zero". When an unsupported string is entered, the model-specific list of acceptable values is returned in an error message. When type is NULL, the default value is used. This default is the first model-related row in the marginaleffects:::type_dictionary dataframe.

slope

string indicates the type of slope or (semi-)elasticity to compute:

  • "dydx": dY/dX

  • "eyex": dY/dX * Y / X

  • "eydx": dY/dX * Y

  • "dyex": dY/dX / X

by

Character vector of variable names over which to compute group-wise estimates.

wts

string or numeric: weights to use when computing average contrasts or marginaleffects. These weights only affect the averaging in tidy() or summary(), and not the unit-level estimates themselves.

  • string: column name of the weights variable in newdata. When supplying a column name to wts, it is recommended to supply the original data (including the weights variable) explicitly to newdata.

  • numeric: vector of length equal to the number of rows in the original data or in newdata (if supplied).

hypothesis

specify a hypothesis test or custom contrast using a vector, matrix, string, or string formula.

  • String:

    • "pairwise": pairwise differences between estimates in each row.

    • "reference": differences between the estimates in each row and the estimate in the first row.

    • "sequential": difference between an estimate and the estimate in the next row.

    • "revpairwise", "revreference", "revsequential": inverse of the corresponding hypotheses, as described above.

  • String formula to specify linear or non-linear hypothesis tests. If the term column uniquely identifies rows, terms can be used in the formula. Otherwise, use b1, b2, etc. to identify the position of each parameter. Examples:

    • hp = drat

    • hp + drat = 12

    • b1 + b2 + b3 = 0

  • Numeric vector: Weights to compute a linear combination of (custom contrast between) estimates. Length equal to the number of rows generated by the same function call, but without the hypothesis argument.

  • Numeric matrix: Each column is a vector of weights, as describe above, used to compute a distinct linear combination of (contrast between) estimates. The column names of the matrix are used as labels in the output.

  • See the Examples section below and the vignette: https://vincentarelbundock.github.io/marginaleffects/articles/hypothesis.html

eps

NULL or numeric value which determines the step size to use when calculating numerical derivatives: (f(x+eps)-f(x))/eps. When eps is NULL, the step size is 0.0001 multiplied by the difference between the maximum and minimum values of the variable with respect to which we are taking the derivative. Changing eps may be necessary to avoid numerical problems in certain models.

...

Additional arguments are passed to the predict() method supplied by the modeling package.These arguments are particularly useful for mixed-effects or bayesian models (see the online vignettes on the marginaleffects website). Available arguments can vary from model to model, depending on the range of supported arguments by each modeling package. See the "Model-Specific Arguments" section of the ?marginaleffects documentation for a non-exhaustive list of available arguments.

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). Uncertainty estimates are computed using the delta method.

The newdata argument can be used to control the kind of marginal effects to report:

  • Average Marginal Effects (AME)

  • Group-Average Marginal Effects (G-AME)

  • Marginal Effects at the Mean (MEM) or

  • Marginal Effects at User-Specified values (aka Marginal Effects at Representative values, MER).

See the marginaleffects vignette for worked-out examples of each kind of marginal effect.

Numerical derivatives for the marginaleffects function are calculated using a simple epsilon difference approach: \(\partial Y / \partial X = (f(X + \varepsilon) - f(X)) / \varepsilon\), where f is the predict() method associated with the model class, and \(\varepsilon\) is determined by the eps argument.

Warning: Some models are particularly sensitive to eps, so it is good practice to try different values of this argument.

Standard errors for the marginal effects are obtained using the Delta method. See the "Standard Errors" vignette on the package website for details (link above).

Model-Specific Arguments

Some model types allow model-specific arguments to modify the nature of marginal effects, predictions, marginal means, and contrasts.

PackageClassArgumentDocumentation
brmsbrmsfitndrawsbrms::posterior_predict
re_formula
lme4merModinclude_randominsight::get_predicted
re.formlme4::predict.merMod
allow.new.levelslme4::predict.merMod
glmmTMBglmmTMBre.formglmmTMB::predict.glmmTMB
allow.new.levelsglmmTMB::predict.glmmTMB
zitypeglmmTMB::predict.glmmTMB
mgcvbamexcludemgcv::predict.bam
robustlmmrlmerModre.formrobustlmm::predict.rlmerMod
allow.new.levelsrobustlmm::predict.rlmerMod

Examples


mod <- glm(am ~ hp * wt, data = mtcars, family = binomial)
mfx <- marginaleffects(mod)
head(mfx)
#>   rowid     type term         dydx    std.error statistic   p.value
#> 1     1 response   hp 0.0069832251 0.0058793768 1.1877492 0.2349322
#> 2     2 response   hp 0.0164041227 0.0133880594 1.2252801 0.2204697
#> 3     3 response   hp 0.0028284517 0.0037506567 0.7541217 0.4507761
#> 4     4 response   hp 0.0019348755 0.0024508961 0.7894564 0.4298453
#> 5     5 response   hp 0.0029928602 0.0033775105 0.8861142 0.3755560
#> 6     6 response   hp 0.0001476461 0.0003452784 0.4276146 0.6689317
#>        conf.low    conf.high   predicted predicted_hi predicted_lo am  hp    wt
#> 1 -0.0045401417 0.0185065919 0.898311019   0.89850864  0.898311019  1 110 2.620
#> 2 -0.0098359916 0.0426442370 0.467644655   0.46810889  0.467644655  1 110 2.875
#> 3 -0.0045227004 0.0101796038 0.967103810   0.96718386  0.967103810  1  93 2.320
#> 4 -0.0028687925 0.0067385435 0.038895584   0.03895034  0.038895584  0 110 3.215
#> 5 -0.0036269388 0.0096126591 0.076483825   0.07656852  0.076483825  0 175 3.440
#> 6 -0.0005290872 0.0008243794 0.003566962   0.00357114  0.003566962  0 105 3.460
#>      eps
#> 1 0.0283
#> 2 0.0283
#> 3 0.0283
#> 4 0.0283
#> 5 0.0283
#> 6 0.0283

# Average Marginal Effect (AME)
summary(mfx)
#>   Term    Effect Std. Error z value   Pr(>|z|)     2.5 %    97.5 %
#> 1   hp  0.002653   0.001939   1.368    0.17121 -0.001147  0.006452
#> 2   wt -0.435783   0.102063  -4.270 1.9568e-05 -0.635822 -0.235744
#> 
#> Model type:  glm 
#> Prediction type:  response 
tidy(mfx)
#>       type term     estimate   std.error statistic      p.value     conf.low
#> 1 response   hp  0.002652526 0.001938531  1.368318 1.712127e-01 -0.001146925
#> 2 response   wt -0.435783199 0.102062710 -4.269759 1.956841e-05 -0.635822435
#>      conf.high
#> 1  0.006451977
#> 2 -0.235743963
plot(mfx)



# Marginal Effect at the Mean (MEM)
marginaleffects(mod, newdata = datagrid())
#>   rowid     type term         dydx   std.error statistic   p.value     conf.low
#> 1     1 response   hp  0.008526944 0.007849817  1.086260 0.2773639 -0.006858415
#> 2     1 response   wt -1.744526869 1.586306474 -1.099741 0.2714448 -4.853630428
#>   conf.high predicted predicted_hi predicted_lo       hp      wt am       eps
#> 1 0.0239123  0.208598    0.2088393     0.208598 146.6875 3.21725  1 0.0283000
#> 2 1.3645767  0.208598    0.2079157     0.208598 146.6875 3.21725  1 0.0003911

# Marginal Effect at User-Specified Values
# Variables not explicitly included in `datagrid()` are held at their means
marginaleffects(mod,
                newdata = datagrid(hp = c(100, 110)))
#>   rowid     type term         dydx   std.error  statistic   p.value
#> 1     1 response   hp  0.001166673 0.001754130  0.6651008 0.5059860
#> 2     2 response   hp  0.001895403 0.002416124  0.7844807 0.4327581
#> 3     1 response   wt -0.194677637 0.307227707 -0.6336591 0.5263033
#> 4     2 response   wt -0.331535828 0.436074375 -0.7602736 0.4470911
#>       conf.low   conf.high  predicted predicted_hi predicted_lo      wt  hp am
#> 1 -0.002271359 0.004604705 0.02311544   0.02314846   0.02311544 3.21725 100  1
#> 2 -0.002840114 0.006630920 0.03814134   0.03819498   0.03814134 3.21725 110  1
#> 3 -0.796832879 0.407477604 0.02311544   0.02303930   0.02311544 3.21725 100  1
#> 4 -1.186225897 0.523154240 0.03814134   0.03801168   0.03814134 3.21725 110  1
#>         eps
#> 1 0.0283000
#> 2 0.0283000
#> 3 0.0003911
#> 4 0.0003911

# Group-Average Marginal Effects (G-AME)
# Calculate marginal effects for each observation, and then take the average
# marginal effect within each subset of observations with different observed
# values for the `cyl` variable:
mod2 <- lm(mpg ~ hp * cyl, data = mtcars)
mfx2 <- marginaleffects(mod2, variables = "hp", by = "cyl")
summary(mfx2)
#>   Term    Contrast cyl   Effect Std. Error z value  Pr(>|z|)    2.5 %   97.5 %
#> 1   hp mean(dY/dX)   6 -0.05226    0.02041 -2.5608 0.0104442 -0.09225 -0.01226
#> 2   hp mean(dY/dX)   4 -0.09173    0.03533 -2.5964 0.0094216 -0.16098 -0.02248
#> 3   hp mean(dY/dX)   8 -0.01278    0.01434 -0.8912 0.3727993 -0.04089  0.01533
#> 
#> Model type:  lm 
#> Prediction type:  response 

# Marginal Effects at User-Specified Values (counterfactual)
# Variables not explicitly included in `datagrid()` are held at their
# original values, and the whole dataset is duplicated once for each
# combination of the values in `datagrid()`
mfx <- marginaleffects(mod,
                       newdata = datagrid(hp = c(100, 110),
                                          grid_type = "counterfactual"))
head(mfx)
#>   rowid     type term         dydx    std.error statistic   p.value
#> 1     1 response   hp 0.0120345428 0.0099871557 1.2050020 0.2282025
#> 2     2 response   hp 0.0141605125 0.0108083854 1.3101413 0.1901480
#> 3     3 response   hp 0.0015641805 0.0022024745 0.7101923 0.4775849
#> 4     4 response   hp 0.0011906427 0.0017804554 0.6687293 0.5036682
#> 5     5 response   hp 0.0001454839 0.0003410785 0.4265408 0.6697138
#> 6     6 response   hp 0.0001201299 0.0002911014 0.4126737 0.6798457
#>        conf.low    conf.high rowidcf   predicted predicted_hi predicted_lo am
#> 1 -0.0075399227 0.0316090083       1 0.804313722  0.804654300  0.804313722  1
#> 2 -0.0070235336 0.0353445586       2 0.312493620  0.312894363  0.312493620  1
#> 3 -0.0027525902 0.0058809511       3 0.982084695  0.982128961  0.982084695  1
#> 4 -0.0022989857 0.0046802710       4 0.023558258  0.023591954  0.023558258  0
#> 5 -0.0005230176 0.0008139854       5 0.003445112  0.003449229  0.003445112  0
#> 6 -0.0004504183 0.0006906781       6 0.002900259  0.002903658  0.002900259  0
#>      wt  hp    eps
#> 1 2.620 100 0.0283
#> 2 2.875 100 0.0283
#> 3 2.320 100 0.0283
#> 4 3.215 100 0.0283
#> 5 3.440 100 0.0283
#> 6 3.460 100 0.0283

# Heteroskedasticity robust standard errors
marginaleffects(mod, vcov = sandwich::vcovHC(mod))
#>    rowid     type term          dydx    std.error   statistic     p.value
#> 1      1 response   hp  6.983225e-03 9.187144e-03  0.76010842 0.447189783
#> 2      2 response   hp  1.640412e-02 1.340247e-02  1.22396222 0.220966481
#> 3      3 response   hp  2.828452e-03 4.912949e-03  0.57571361 0.564808772
#> 4      4 response   hp  1.934876e-03 1.843501e-03  1.04956553 0.293917911
#> 5      5 response   hp  2.992860e-03 2.782662e-03  1.07553836 0.282133766
#> 6      6 response   hp  1.476461e-04 2.545412e-04  0.58004785 0.561882353
#> 7      7 response   hp  5.740668e-03 8.192346e-03  0.70073547 0.483468119
#> 8      8 response   hp  2.111702e-04 3.886726e-04  0.54331124 0.586915530
#> 9      9 response   hp  1.646976e-03 1.727929e-03  0.95314999 0.340514082
#> 10    10 response   hp  3.809918e-04 5.150443e-04  0.73972622 0.459466136
#> 11    11 response   hp  3.809918e-04 5.150443e-04  0.73972622 0.459466136
#> 12    12 response   hp  8.760463e-07 6.574577e-06  0.13324755 0.893997607
#> 13    13 response   hp  9.575809e-05 3.219972e-04  0.29738799 0.766170309
#> 14    14 response   hp  4.915362e-05 1.903900e-04  0.25817324 0.796273208
#> 15    15 response   hp -5.787254e-13 8.594637e-12 -0.06733565 0.946314499
#> 16    16 response   hp -4.902183e-14 8.735888e-13 -0.05611545 0.955249833
#> 17    17 response   hp -7.463248e-14 1.373213e-12 -0.05434881 0.956657263
#> 18    18 response   hp  1.107615e-02 1.420807e-02  0.77956725 0.435645637
#> 19    19 response   hp  1.403006e-03 4.013438e-03  0.34957719 0.726656031
#> 20    20 response   hp  1.346032e-03 2.931206e-03  0.45920764 0.646085064
#> 21    21 response   hp  5.795830e-03 7.807259e-03  0.74236419 0.457866710
#> 22    22 response   hp  4.644015e-04 6.992967e-04  0.66409789 0.506627662
#> 23    23 response   hp  1.215863e-03 1.227051e-03  0.99088198 0.321743214
#> 24    24 response   hp  1.153492e-04 6.936387e-04  0.16629576 0.867924202
#> 25    25 response   hp  1.800542e-05 8.054465e-05  0.22354577 0.823110765
#> 26    26 response   hp  2.339616e-03 4.551005e-03  0.51408786 0.607190567
#> 27    27 response   hp  8.695693e-04 1.975180e-03  0.44024813 0.659757401
#> 28    28 response   hp  4.267318e-07 2.730750e-06  0.15626907 0.875820938
#> 29    29 response   hp  2.271717e-04 1.130909e-03  0.20087535 0.840796046
#> 30    30 response   hp  3.179746e-04 1.377287e-03  0.23087027 0.817415585
#> 31    31 response   hp  4.210965e-03 7.657335e-03  0.54992568 0.582370347
#> 32    32 response   hp  1.568271e-02 7.580280e-03  2.06888256 0.038557109
#> 33     1 response   wt -8.280303e-01 1.279527e+00 -0.64713787 0.517542720
#> 34     2 response   wt -2.253205e+00 1.362419e+00 -1.65382633 0.098162804
#> 35     3 response   wt -2.658461e-01 5.257923e-01 -0.50561042 0.613130163
#> 36     4 response   wt -3.378277e-01 3.501323e-01 -0.96485732 0.334616280
#> 37     5 response   wt -8.290222e-01 6.811943e-01 -1.21701272 0.223599375
#> 38     6 response   wt -3.137785e-02 5.512417e-02 -0.56922132 0.569205956
#> 39     7 response   wt -2.276351e+00 2.471021e+00 -0.92121870 0.356936257
#> 40     8 response   wt -2.816330e-02 5.696766e-02 -0.49437345 0.621042459
#> 41     9 response   wt -2.543997e-01 2.857380e-01 -0.89032510 0.373291345
#> 42    10 response   wt -8.609526e-02 1.157515e-01 -0.74379421 0.457000986
#> 43    11 response   wt -8.609526e-02 1.157515e-01 -0.74379421 0.457000986
#> 44    12 response   wt -6.494143e-04 3.310778e-03 -0.19615159 0.844491515
#> 45    13 response   wt -3.775892e-02 9.172958e-02 -0.41163299 0.680608450
#> 46    14 response   wt -2.081495e-02 5.862543e-02 -0.35504993 0.722552188
#> 47    15 response   wt -2.272851e-10 3.859969e-09 -0.05888264 0.953045588
#> 48    16 response   wt -1.629596e-11 3.217589e-10 -0.05064649 0.959607218
#> 49    17 response   wt -2.826816e-11 5.795693e-10 -0.04877442 0.961099064
#> 50    18 response   wt -8.521936e-01 8.229549e-01 -1.03552892 0.300421966
#> 51    19 response   wt -7.886841e-02 1.842446e-01 -0.42806372 0.668604733
#> 52    20 response   wt -8.870757e-02 1.769120e-01 -0.50142206 0.616074120
#> 53    21 response   wt -5.959105e-01 9.379795e-01 -0.63531295 0.525224328
#> 54    22 response   wt -1.272450e-01 1.577696e-01 -0.80652389 0.419940837
#> 55    23 response   wt -3.054369e-01 2.813059e-01 -1.08578197 0.277575463
#> 56    24 response   wt -6.569718e-02 2.831304e-01 -0.23203857 0.816508055
#> 57    25 response   wt -8.288936e-03 2.714899e-02 -0.30531290 0.760127851
#> 58    26 response   wt -1.612155e-01 2.841263e-01 -0.56740790 0.570437083
#> 59    27 response   wt -7.464417e-02 1.884619e-01 -0.39607029 0.692053177
#> 60    28 response   wt -3.204617e-05 2.159959e-04 -0.14836470 0.882054955
#> 61    29 response   wt -6.580439e-02 3.321209e-01 -0.19813386 0.842940336
#> 62    30 response   wt -5.339272e-02 2.402385e-01 -0.22224887 0.824120154
#> 63    31 response   wt -2.107162e+00 4.582922e+00 -0.45978567 0.645670073
#> 64    32 response   wt -2.024827e+00 7.486883e-01 -2.70449963 0.006840735
#>         conf.low     conf.high    predicted predicted_hi predicted_lo am  hp
#> 1  -1.102325e-02  2.498970e-02 8.983110e-01 8.985086e-01 8.983110e-01  1 110
#> 2  -9.864245e-03  4.267249e-02 4.676447e-01 4.681089e-01 4.676447e-01  1 110
#> 3  -6.800752e-03  1.245766e-02 9.671038e-01 9.671839e-01 9.671038e-01  1  93
#> 4  -1.678321e-03  5.548072e-03 3.889558e-02 3.895034e-02 3.889558e-02  0 110
#> 5  -2.461058e-03  8.446778e-03 7.648382e-02 7.656852e-02 7.648382e-02  0 175
#> 6  -3.512456e-04  6.465378e-04 3.566962e-03 3.571140e-03 3.566962e-03  0 105
#> 7  -1.031604e-02  2.179737e-02 1.923979e-01 1.925604e-01 1.923979e-01  0 245
#> 8  -5.506142e-04  9.729546e-04 4.015235e-03 4.021211e-03 4.015235e-03  0  62
#> 9  -1.739703e-03  5.033655e-03 3.120992e-02 3.125653e-02 3.120992e-02  0  95
#> 10 -6.284766e-04  1.390460e-03 9.073330e-03 9.084112e-03 9.073330e-03  0 123
#> 11 -6.284766e-04  1.390460e-03 9.073330e-03 9.084112e-03 9.073330e-03  0 123
#> 12 -1.200989e-05  1.376198e-05 5.439236e-05 5.441715e-05 5.439236e-05  0 180
#> 13 -5.353448e-04  7.268609e-04 3.172383e-03 3.175093e-03 3.172383e-03  0 180
#> 14 -3.240040e-04  4.223112e-04 1.746319e-03 1.747710e-03 1.746319e-03  0 180
#> 15 -1.742390e-11  1.626645e-11 1.751508e-11 1.749870e-11 1.751508e-11  0 205
#> 16 -1.761224e-12  1.663181e-12 1.216933e-12 1.215546e-12 1.216933e-12  0 215
#> 17 -2.766080e-12  2.616815e-12 2.017342e-12 2.015230e-12 2.017342e-12  0 230
#> 18 -1.677116e-02  3.892346e-02 8.634101e-01 8.637236e-01 8.634101e-01  1  66
#> 19 -6.463187e-03  9.269200e-03 9.879847e-01 9.880244e-01 9.879847e-01  1  52
#> 20 -4.399027e-03  7.091091e-03 9.875007e-01 9.875388e-01 9.875007e-01  1  65
#> 21 -9.506117e-03  2.109778e-02 9.243530e-01 9.245170e-01 9.243530e-01  0  97
#> 22 -9.061949e-04  1.834998e-03 1.204113e-02 1.205428e-02 1.204113e-02  0 150
#> 23 -1.189113e-03  3.620838e-03 2.941871e-02 2.945312e-02 2.941871e-02  0 150
#> 24 -1.244158e-03  1.474856e-03 4.509574e-03 4.512838e-03 4.509574e-03  0 245
#> 25 -1.398592e-04  1.758700e-04 7.069764e-04 7.074860e-04 7.069764e-04  0 175
#> 26 -6.580190e-03  1.125942e-02 9.771760e-01 9.772422e-01 9.771760e-01  1  66
#> 27 -3.001712e-03  4.740851e-03 9.908948e-01 9.909194e-01 9.908948e-01  1  91
#> 28 -4.925440e-06  5.778904e-06 9.999965e-01 9.999965e-01 9.999965e-01  1 113
#> 29 -1.989368e-03  2.443712e-03 9.957403e-01 9.957467e-01 9.957403e-01  1 264
#> 30 -2.381458e-03  3.017407e-03 9.954493e-01 9.954583e-01 9.954493e-01  1 175
#> 31 -1.079714e-02  1.921907e-02 8.687038e-01 8.688229e-01 8.687038e-01  1 335
#> 32  8.256332e-04  3.053978e-02 6.593657e-01 6.598095e-01 6.593657e-01  1 109
#> 33 -3.335857e+00  1.679796e+00 8.983110e-01 8.979872e-01 8.983110e-01  1 110
#> 34 -4.923497e+00  4.170878e-01 4.676447e-01 4.667634e-01 4.676447e-01  1 110
#> 35 -1.296380e+00  7.646879e-01 9.671038e-01 9.669998e-01 9.671038e-01  1  93
#> 36 -1.024074e+00  3.484190e-01 3.889558e-02 3.876346e-02 3.889558e-02  0 110
#> 37 -2.164139e+00  5.060942e-01 7.648382e-02 7.615959e-02 7.648382e-02  0 175
#> 38 -1.394192e-01  7.666354e-02 3.566962e-03 3.554690e-03 3.566962e-03  0 105
#> 39 -7.119463e+00  2.566761e+00 1.923979e-01 1.915076e-01 1.923979e-01  0 245
#> 40 -1.398179e-01  8.349126e-02 4.015235e-03 4.004220e-03 4.015235e-03  0  62
#> 41 -8.144358e-01  3.056364e-01 3.120992e-02 3.111043e-02 3.120992e-02  0  95
#> 42 -3.129639e-01  1.407734e-01 9.073330e-03 9.039658e-03 9.073330e-03  0 123
#> 43 -3.129639e-01  1.407734e-01 9.073330e-03 9.039658e-03 9.073330e-03  0 123
#> 44 -7.138419e-03  5.839590e-03 5.439236e-05 5.413837e-05 5.439236e-05  0 180
#> 45 -2.175456e-01  1.420277e-01 3.172383e-03 3.157615e-03 3.172383e-03  0 180
#> 46 -1.357187e-01  9.408877e-02 1.746319e-03 1.738178e-03 1.746319e-03  0 180
#> 47 -7.792685e-09  7.338115e-09 1.751508e-11 1.742619e-11 1.751508e-11  0 205
#> 48 -6.469318e-10  6.143399e-10 1.216933e-12 1.210560e-12 1.216933e-12  0 215
#> 49 -1.164203e-09  1.107667e-09 2.017342e-12 2.006286e-12 2.017342e-12  0 230
#> 50 -2.465156e+00  7.607684e-01 8.634101e-01 8.630768e-01 8.634101e-01  1  66
#> 51 -4.399811e-01  2.822443e-01 9.879847e-01 9.879538e-01 9.879847e-01  1  52
#> 52 -4.354487e-01  2.580335e-01 9.875007e-01 9.874660e-01 9.875007e-01  1  65
#> 53 -2.434317e+00  1.242495e+00 9.243530e-01 9.241200e-01 9.243530e-01  0  97
#> 54 -4.364678e-01  1.819778e-01 1.204113e-02 1.199137e-02 1.204113e-02  0 150
#> 55 -8.567864e-01  2.459126e-01 2.941871e-02 2.929926e-02 2.941871e-02  0 150
#> 56 -6.206226e-01  4.892282e-01 4.509574e-03 4.483880e-03 4.509574e-03  0 245
#> 57 -6.149997e-02  4.492210e-02 7.069764e-04 7.037346e-04 7.069764e-04  0 175
#> 58 -7.180927e-01  3.956618e-01 9.771760e-01 9.771130e-01 9.771760e-01  1  66
#> 59 -4.440228e-01  2.947344e-01 9.908948e-01 9.908656e-01 9.908948e-01  1  91
#> 60 -4.553905e-04  3.912981e-04 9.999965e-01 9.999965e-01 9.999965e-01  1 113
#> 61 -7.167493e-01  5.851405e-01 9.957403e-01 9.957145e-01 9.957403e-01  1 264
#> 62 -5.242514e-01  4.174660e-01 9.954493e-01 9.954285e-01 9.954493e-01  1 175
#> 63 -1.108952e+01  6.875201e+00 8.687038e-01 8.678796e-01 8.687038e-01  1 335
#> 64 -3.492230e+00 -5.574252e-01 6.593657e-01 6.585738e-01 6.593657e-01  1 109
#>       wt       eps
#> 1  2.620 0.0283000
#> 2  2.875 0.0283000
#> 3  2.320 0.0283000
#> 4  3.215 0.0283000
#> 5  3.440 0.0283000
#> 6  3.460 0.0283000
#> 7  3.570 0.0283000
#> 8  3.190 0.0283000
#> 9  3.150 0.0283000
#> 10 3.440 0.0283000
#> 11 3.440 0.0283000
#> 12 4.070 0.0283000
#> 13 3.730 0.0283000
#> 14 3.780 0.0283000
#> 15 5.250 0.0283000
#> 16 5.424 0.0283000
#> 17 5.345 0.0283000
#> 18 2.200 0.0283000
#> 19 1.615 0.0283000
#> 20 1.835 0.0283000
#> 21 2.465 0.0283000
#> 22 3.520 0.0283000
#> 23 3.435 0.0283000
#> 24 3.840 0.0283000
#> 25 3.845 0.0283000
#> 26 1.935 0.0283000
#> 27 2.140 0.0283000
#> 28 1.513 0.0283000
#> 29 3.170 0.0283000
#> 30 2.770 0.0283000
#> 31 3.570 0.0283000
#> 32 2.780 0.0283000
#> 33 2.620 0.0003911
#> 34 2.875 0.0003911
#> 35 2.320 0.0003911
#> 36 3.215 0.0003911
#> 37 3.440 0.0003911
#> 38 3.460 0.0003911
#> 39 3.570 0.0003911
#> 40 3.190 0.0003911
#> 41 3.150 0.0003911
#> 42 3.440 0.0003911
#> 43 3.440 0.0003911
#> 44 4.070 0.0003911
#> 45 3.730 0.0003911
#> 46 3.780 0.0003911
#> 47 5.250 0.0003911
#> 48 5.424 0.0003911
#> 49 5.345 0.0003911
#> 50 2.200 0.0003911
#> 51 1.615 0.0003911
#> 52 1.835 0.0003911
#> 53 2.465 0.0003911
#> 54 3.520 0.0003911
#> 55 3.435 0.0003911
#> 56 3.840 0.0003911
#> 57 3.845 0.0003911
#> 58 1.935 0.0003911
#> 59 2.140 0.0003911
#> 60 1.513 0.0003911
#> 61 3.170 0.0003911
#> 62 2.770 0.0003911
#> 63 3.570 0.0003911
#> 64 2.780 0.0003911

# hypothesis test: is the `hp` marginal effect at the mean equal to the `drat` marginal effect
mod <- lm(mpg ~ wt + drat, data = mtcars)

marginaleffects(
    mod,
    newdata = "mean",
    hypothesis = "wt = drat")
#>       type    term      dydx std.error statistic      p.value conf.low
#> 1 response wt=drat -6.225381  1.051769 -5.918963 3.239775e-09 -8.28681
#>   conf.high
#> 1 -4.163952

# same hypothesis test using row indices
marginaleffects(
    mod,
    newdata = "mean",
    hypothesis = "b1 - b2 = 0")
#>       type    term      dydx std.error statistic      p.value conf.low
#> 1 response b1-b2=0 -6.225381  1.051769 -5.918963 3.239775e-09 -8.28681
#>   conf.high
#> 1 -4.163952

# same hypothesis test using numeric vector of weights
marginaleffects(
    mod,
    newdata = "mean",
    hypothesis = c(1, -1))
#>       type   term      dydx std.error statistic      p.value conf.low conf.high
#> 1 response custom -6.225381  1.051769 -5.918963 3.239775e-09 -8.28681 -4.163952

# two custom contrasts using a matrix of weights
lc <- matrix(c(
    1, -1,
    2, 3),
    ncol = 2)
colnames(lc) <- c("Contrast A", "Contrast B")
marginaleffects(
    mod,
    newdata = "mean",
    hypothesis = lc)
#>       type       term      dydx std.error  statistic      p.value  conf.low
#> 1 response Contrast A -6.225381  1.051769 -5.9189632 3.239775e-09  -8.28681
#> 2 response Contrast B -5.238308  5.623757 -0.9314607 3.516153e-01 -16.26067
#>   conf.high
#> 1 -4.163952
#> 2  5.784052