Extract marginal effects from a model object, conditional on data, using dydx.

marginal_effects(model, data, variables = NULL, ...)

# S3 method for margins
marginal_effects(model, data, variables = NULL, ...)

# S3 method for clm
marginal_effects(model, data = find_data(model, parent.frame()),
  variables = NULL, type = NULL, eps = 1e-07, varslist = NULL,
  as.data.frame = TRUE, ...)

# S3 method for default
marginal_effects(model, data = find_data(model,
  parent.frame()), variables = NULL, type = c("response", "link"),
  eps = 1e-07, as.data.frame = TRUE, varslist = NULL, ...)

# S3 method for glm
marginal_effects(model, data = find_data(model, parent.frame()),
  variables = NULL, type = c("response", "link"), eps = 1e-07,
  varslist = NULL, as.data.frame = TRUE, ...)

# S3 method for lm
marginal_effects(model, data = find_data(model, parent.frame()),
  variables = NULL, type = c("response", "link"), eps = 1e-07,
  varslist = NULL, as.data.frame = TRUE, ...)

# S3 method for loess
marginal_effects(model, data = find_data(model,
  parent.frame()), variables = NULL, type = c("response", "link"),
  eps = 1e-07, as.data.frame = TRUE, varslist = NULL, ...)

# S3 method for merMod
marginal_effects(model, data = find_data(model),
  variables = NULL, type = c("response", "link"), eps = 1e-07,
  as.data.frame = TRUE, varslist = NULL, ...)

# S3 method for lmerMod
marginal_effects(model, data = find_data(model),
  variables = NULL, type = c("response", "link"), eps = 1e-07,
  as.data.frame = TRUE, varslist = NULL, ...)

# S3 method for multinom
marginal_effects(model, data = find_data(model,
  parent.frame()), variables = NULL, type = NULL, eps = 1e-07,
  varslist = NULL, as.data.frame = TRUE, ...)

# S3 method for nnet
marginal_effects(model, data = find_data(model,
  parent.frame()), variables = NULL, type = NULL, eps = 1e-07,
  varslist = NULL, as.data.frame = TRUE, ...)

# S3 method for polr
marginal_effects(model, data = find_data(model,
  parent.frame()), variables = NULL, type = NULL, eps = 1e-07,
  varslist = NULL, as.data.frame = TRUE, ...)

Arguments

model

A model object, perhaps returned by lm or glm

data

A data.frame over which to calculate marginal effects. This is optional, but may be required when the underlying modelling function sets model = FALSE.

variables

A character vector with the names of variables for which to compute the marginal effects. The default (NULL) returns marginal effects for all variables.

Arguments passed to methods, and onward to dydx methods and possibly further to prediction methods. This can be useful, for example, for setting type (predicted value type), eps (precision), or category (category for multi-category outcome models), etc.

type

A character string indicating the type of marginal effects to estimate. Mostly relevant for non-linear models, where the reasonable options are “response” (the default) or “link” (i.e., on the scale of the linear predictor in a GLM).

eps

A numeric value specifying the “step” to use when calculating numerical derivatives. By default this is the smallest floating point value that can be represented on the present architecture.

varslist

A list structure used internally by margins. Users should not set this.

as.data.frame

A logical indicating whether to return a data frame (the default) or a matrix.

Value

An data frame with number of rows equal to nrow(data), where each row is an observation and each column is the marginal effect of a variable used in the model formula.

Details

Users likely want to use the fully featured margins function rather than marginal_effects, which merely performs estimation of the marginal effects but simply returns a data frame. margins, by contrast, does some convenient packaging around these results and supports additional functionality, like variance estimation and counterfactual estimation procedures. The methods for this function provide lower-level functionality that extracts unit-specific marginal effects from an estimated model with respect to all variables specified in data (or the subset specified in variables) and returns a data frame. See dydx for computational details. Note that for factor and logical class variables, discrete changes in the outcome are reported rather than instantaneous marginal effects.

Methods are currently implemented for the following object classes:

A method is also provided for the object classes “margins” to return a simplified data frame from complete “margins” objects.

See also

Examples

require("datasets") x <- lm(mpg ~ cyl * hp + wt, data = mtcars) marginal_effects(x)
#> dydx_cyl dydx_hp dydx_wt #> 1 -0.6572244 -0.04987248 -3.119815 #> 2 -0.6572244 -0.04987248 -3.119815 #> 3 -0.9794364 -0.08777977 -3.119815 #> 4 -0.6572244 -0.04987248 -3.119815 #> 5 0.5747624 -0.01196519 -3.119815 #> 6 -0.7519926 -0.04987248 -3.119815 #> 7 1.9015174 -0.01196519 -3.119815 #> 8 -1.5669993 -0.08777977 -3.119815 #> 9 -0.9415291 -0.08777977 -3.119815 #> 10 -0.4108271 -0.04987248 -3.119815 #> 11 -0.4108271 -0.04987248 -3.119815 #> 12 0.6695306 -0.01196519 -3.119815 #> 13 0.6695306 -0.01196519 -3.119815 #> 14 0.6695306 -0.01196519 -3.119815 #> 15 1.1433716 -0.01196519 -3.119815 #> 16 1.3329081 -0.01196519 -3.119815 #> 17 1.6172127 -0.01196519 -3.119815 #> 18 -1.4911847 -0.08777977 -3.119815 #> 19 -1.7565357 -0.08777977 -3.119815 #> 20 -1.5101384 -0.08777977 -3.119815 #> 21 -0.9036218 -0.08777977 -3.119815 #> 22 0.1009213 -0.01196519 -3.119815 #> 23 0.1009213 -0.01196519 -3.119815 #> 24 1.9015174 -0.01196519 -3.119815 #> 25 0.5747624 -0.01196519 -3.119815 #> 26 -1.4911847 -0.08777977 -3.119815 #> 27 -1.0173436 -0.08777977 -3.119815 #> 28 -0.6003635 -0.08777977 -3.119815 #> 29 2.2616366 -0.01196519 -3.119815 #> 30 0.5747624 -0.04987248 -3.119815 #> 31 3.6073452 -0.01196519 -3.119815 #> 32 -0.6761781 -0.08777977 -3.119815
# factor variables report discrete differences x <- lm(mpg ~ factor(cyl) * factor(am), data = mtcars) marginal_effects(x)
#> dydx_cyl6 dydx_cyl8 dydx_am1 #> 1 -7.508333 -12.675 1.441667 #> 2 -7.508333 -12.675 1.441667 #> 3 -7.508333 -12.675 5.175000 #> 4 -3.775000 -7.850 1.441667 #> 5 -3.775000 -7.850 0.350000 #> 6 -3.775000 -7.850 1.441667 #> 7 -3.775000 -7.850 0.350000 #> 8 -3.775000 -7.850 5.175000 #> 9 -3.775000 -7.850 5.175000 #> 10 -3.775000 -7.850 1.441667 #> 11 -3.775000 -7.850 1.441667 #> 12 -3.775000 -7.850 0.350000 #> 13 -3.775000 -7.850 0.350000 #> 14 -3.775000 -7.850 0.350000 #> 15 -3.775000 -7.850 0.350000 #> 16 -3.775000 -7.850 0.350000 #> 17 -3.775000 -7.850 0.350000 #> 18 -7.508333 -12.675 5.175000 #> 19 -7.508333 -12.675 5.175000 #> 20 -7.508333 -12.675 5.175000 #> 21 -3.775000 -7.850 5.175000 #> 22 -3.775000 -7.850 0.350000 #> 23 -3.775000 -7.850 0.350000 #> 24 -3.775000 -7.850 0.350000 #> 25 -3.775000 -7.850 0.350000 #> 26 -7.508333 -12.675 5.175000 #> 27 -7.508333 -12.675 5.175000 #> 28 -7.508333 -12.675 5.175000 #> 29 -7.508333 -12.675 0.350000 #> 30 -7.508333 -12.675 1.441667 #> 31 -7.508333 -12.675 0.350000 #> 32 -7.508333 -12.675 5.175000
# get just marginal effects from "margins" object require('datasets') m <- margins(lm(mpg ~ hp, data = mtcars[1:10,])) marginal_effects(m)
#> Average marginal effects
#> lm(formula = mpg ~ hp, data = mtcars[1:10, ])
#> hp #> -0.0505
marginal_effects(m)
#> Average marginal effects
#> lm(formula = mpg ~ hp, data = mtcars[1:10, ])
#> hp #> -0.0505
# multi-category outcome if (requireNamespace("nnet")) { data("iris3", package = "datasets") ird <- data.frame(rbind(iris3[,,1], iris3[,,2], iris3[,,3]), species = factor(c(rep("s",50), rep("c", 50), rep("v", 50)))) m <- nnet::nnet(species ~ ., data = ird, size = 2, rang = 0.1, decay = 5e-4, maxit = 200, trace = FALSE) marginal_effects(m) # default marginal_effects(m, category = "v") # explicit category }
#> Loading required namespace: nnet
#> dydx_Sepal.L. dydx_Sepal.W. dydx_Petal.L. dydx_Petal.W. #> 1 -7.051271e-07 -2.052312e-06 3.514936e-06 1.675324e-06 #> 2 -2.151130e-06 -6.261015e-06 1.072339e-05 5.109200e-06 #> 3 -1.177273e-06 -3.426522e-06 5.868559e-06 2.796840e-06 #> 4 -3.090140e-06 -8.994083e-06 1.540458e-05 7.338492e-06 #> 5 -6.224677e-07 -1.811726e-06 3.102878e-06 1.479004e-06 #> 6 -9.843186e-07 -2.864892e-06 4.906381e-06 2.339968e-06 #> 7 -1.397017e-06 -4.066084e-06 6.963751e-06 3.319857e-06 #> 8 -1.271772e-06 -3.701575e-06 6.339730e-06 3.020847e-06 #> 9 -3.808188e-06 -1.108402e-05 1.898409e-05 9.043857e-06 #> 10 -2.106436e-06 -6.130955e-06 1.050094e-05 5.001401e-06 #> 11 -5.492563e-07 -1.598641e-06 2.737944e-06 1.305000e-06 #> 12 -2.052301e-06 -5.973374e-06 1.023083e-05 4.873978e-06 #> 13 -1.954305e-06 -5.688160e-06 9.742478e-06 4.640471e-06 #> 14 -9.948020e-07 -2.895437e-06 4.959052e-06 2.362935e-06 #> 15 -9.403195e-08 -2.736807e-07 4.686752e-07 2.236826e-07 #> 16 -1.668465e-07 -4.856028e-07 8.315335e-07 3.971989e-07 #> 17 -2.702323e-07 -7.865056e-07 1.346799e-06 6.432828e-07 #> 18 -8.240693e-07 -2.398486e-06 4.107675e-06 1.958711e-06 #> 19 -8.362528e-07 -2.433956e-06 4.168526e-06 1.987103e-06 #> 20 -6.447290e-07 -1.876509e-06 3.213725e-06 1.532467e-06 #> 21 -1.912312e-06 -5.565928e-06 9.533023e-06 4.541303e-06 #> 22 -9.122039e-07 -2.654990e-06 4.546800e-06 2.169108e-06 #> 23 -2.235473e-07 -6.506385e-07 1.114237e-06 5.316339e-07 #> 24 -4.944118e-06 -1.439005e-05 2.464453e-05 1.175222e-05 #> 25 -6.227421e-06 -1.812545e-05 3.104518e-05 1.478457e-05 #> 26 -4.123226e-06 -1.200098e-05 2.055484e-05 9.790796e-06 #> 27 -2.491063e-06 -7.250352e-06 1.241719e-05 5.920211e-06 #> 28 -9.164195e-07 -2.667296e-06 4.568269e-06 2.176984e-06 #> 29 -7.992454e-07 -2.326250e-06 3.984119e-06 1.898853e-06 #> 30 -3.342657e-06 -9.729062e-06 1.666351e-05 7.937629e-06 #> 31 -3.850823e-06 -1.120812e-05 1.919684e-05 9.144097e-06 #> 32 -1.337868e-06 -3.893906e-06 6.668626e-06 3.180636e-06 #> 33 -2.538395e-07 -7.388152e-07 1.265359e-06 6.030371e-07 #> 34 -1.477495e-07 -4.300291e-07 7.364530e-07 3.512868e-07 #> 35 -2.490459e-06 -7.248663e-06 1.241507e-05 5.914606e-06 #> 36 -6.942313e-07 -2.020594e-06 3.460564e-06 1.649712e-06 #> 37 -3.939564e-07 -1.146627e-06 1.963742e-06 9.362959e-07 #> 38 -5.692382e-07 -1.656806e-06 2.837639e-06 1.352058e-06 #> 39 -2.153387e-06 -6.267576e-06 1.073456e-05 5.114918e-06 #> 40 -1.189086e-06 -3.460914e-06 5.927537e-06 2.824494e-06 #> 41 -6.348122e-07 -1.847640e-06 3.164226e-06 1.509214e-06 #> 42 -1.170198e-05 -3.405942e-05 5.833446e-05 2.779557e-05 #> 43 -1.441398e-06 -4.195279e-06 7.185237e-06 3.424127e-06 #> 44 -2.842473e-06 -8.273009e-06 1.416714e-05 6.763525e-06 #> 45 -2.940437e-06 -8.558310e-06 1.465764e-05 6.986092e-06 #> 46 -2.732932e-06 -7.954356e-06 1.362327e-05 6.493020e-06 #> 47 -7.647180e-07 -2.225759e-06 3.812052e-06 1.816598e-06 #> 48 -1.768016e-06 -5.145929e-06 8.813511e-06 4.199491e-06 #> 49 -5.860945e-07 -1.705861e-06 2.921582e-06 1.392495e-06 #> 50 -1.106556e-06 -3.220700e-06 5.516077e-06 2.628692e-06 #> 51 -3.308270e-04 -9.328372e-04 1.268526e-03 2.578749e-03 #> 52 -6.772191e-04 -1.909681e-03 2.598219e-03 5.271903e-03 #> 53 -4.837141e-03 -1.364451e-02 1.861278e-02 3.739975e-02 #> 54 -6.509447e-04 -1.835797e-03 2.500036e-03 5.055018e-03 #> 55 -5.670180e-03 -1.599418e-02 2.181636e-02 4.384958e-02 #> 56 -9.822168e-04 -2.770426e-03 3.777054e-03 7.605369e-03 #> 57 -5.170755e-03 -1.458489e-02 1.988804e-02 4.001904e-02 #> 58 -3.220893e-06 -7.969617e-06 -1.736563e-06 9.145689e-05 #> 59 -3.655220e-04 -1.030815e-03 1.403428e-03 2.840400e-03 #> 60 -3.834587e-04 -1.080993e-03 1.467167e-03 3.003948e-03 #> 61 -4.036631e-05 -1.134874e-04 1.505509e-04 3.345659e-04 #> 62 -6.045658e-04 -1.704574e-03 2.316521e-03 4.720239e-03 #> 63 -5.451886e-05 -1.535600e-04 2.069272e-04 4.349442e-04 #> 64 -3.581290e-03 -1.010205e-02 1.378073e-02 2.768809e-02 #> 65 -2.159420e-05 -5.959600e-05 6.642147e-05 2.454700e-04 #> 66 -1.815702e-04 -5.116778e-04 6.924382e-04 1.433088e-03 #> 67 -5.205039e-03 -1.468195e-02 2.002441e-02 4.026319e-02 #> 68 -2.883766e-05 -8.103599e-05 1.070559e-04 2.413568e-04 #> 69 -1.368537e-01 -3.860565e-01 5.268793e-01 1.056814e+00 #> 70 -5.121478e-05 -1.441034e-04 1.924830e-04 4.175505e-04 #> 71 -5.785503e-01 -1.632036e+00 2.227167e+00 4.467869e+00 #> 72 -8.154203e-05 -2.294451e-04 3.065870e-04 6.642272e-04 #> 73 -4.004084e-01 -1.129536e+00 1.541658e+00 3.091197e+00 #> 74 -5.829030e-04 -1.644143e-03 2.241718e-03 4.512465e-03 #> 75 -1.372603e-04 -3.867941e-04 5.232665e-04 1.084256e-03 #> 76 -2.688252e-04 -7.578365e-04 1.028589e-03 2.105792e-03 #> 77 -3.391752e-03 -9.567441e-03 1.305180e-02 2.622076e-02 #> 78 -4.677926e-01 -1.319612e+00 1.800950e+00 3.612012e+00 #> 79 -4.252792e-03 -1.199579e-02 1.635925e-02 3.290525e-02 #> 80 3.816546e-06 1.214760e-05 -3.218662e-05 5.292630e-05 #> 81 -5.093067e-05 -1.432755e-04 1.910547e-04 4.169323e-04 #> 82 -2.005929e-05 -5.608837e-05 7.092490e-05 1.845717e-04 #> 83 -5.054036e-05 -1.420422e-04 1.878771e-04 4.218079e-04 #> 84 -2.902382e-01 -8.187566e-01 1.117553e+00 2.240436e+00 #> 85 -7.417869e-03 -2.092409e-02 2.854210e-02 5.735885e-02 #> 86 -1.556468e-03 -4.389470e-03 5.976695e-03 1.209248e-02 #> 87 -1.925287e-03 -5.430371e-03 7.402756e-03 1.491167e-02 #> 88 -1.803992e-03 -5.088553e-03 6.940172e-03 1.395433e-02 #> 89 -1.086502e-04 -3.059936e-04 4.119407e-04 8.688880e-04 #> 90 -3.336326e-04 -9.406950e-04 1.278610e-03 2.603794e-03 #> 91 -5.896383e-04 -1.663092e-03 2.267004e-03 4.567511e-03 #> 92 -1.284914e-03 -3.624165e-03 4.940486e-03 9.951931e-03 #> 93 -8.921303e-05 -2.512698e-04 3.384666e-04 7.124064e-04 #> 94 -6.027464e-06 -1.600382e-05 1.055078e-05 1.061463e-04 #> 95 -3.757469e-04 -1.059556e-03 1.441494e-03 2.925476e-03 #> 96 -7.176199e-05 -2.020605e-04 2.715224e-04 5.765191e-04 #> 97 -1.857794e-04 -5.236525e-04 7.099202e-04 1.459576e-03 #> 98 -1.660438e-04 -4.680036e-04 6.342455e-04 1.305740e-03 #> 99 2.920859e-05 8.613326e-05 -1.597838e-04 -2.591674e-06 #> 100 -1.678648e-04 -4.730807e-04 6.404995e-04 1.323365e-03 #> 101 -2.654112e-06 -7.487187e-06 1.021943e-05 2.048771e-05 #> 102 -1.310598e-03 -3.697186e-03 5.046558e-03 1.011684e-02 #> 103 -3.838546e-05 -1.082849e-04 1.478051e-04 2.962896e-04 #> 104 -1.111922e-03 -3.136731e-03 4.281638e-03 8.582727e-03 #> 105 -1.503573e-05 -4.241555e-05 5.789518e-05 1.160592e-04 #> 106 -5.907586e-06 -1.666524e-05 2.274763e-05 4.559757e-05 #> 107 -2.283101e-01 -6.440560e-01 8.790641e-01 1.762618e+00 #> 108 -8.471160e-05 -2.389710e-04 3.261938e-04 6.538452e-04 #> 109 -1.086850e-04 -3.065999e-04 4.185060e-04 8.388925e-04 #> 110 -6.478197e-06 -1.827482e-05 2.494354e-05 5.000787e-05 #> 111 -2.164049e-02 -6.104735e-02 8.332414e-02 1.670762e-01 #> 112 -9.602287e-04 -2.708799e-03 3.697457e-03 7.412093e-03 #> 113 -1.743856e-04 -4.919392e-04 6.714732e-04 1.346120e-03 #> 114 -2.281324e-04 -6.435581e-04 8.784256e-04 1.761014e-03 #> 115 -1.366213e-05 -3.854036e-05 5.260243e-05 1.054739e-04 #> 116 -7.842880e-05 -2.212452e-04 3.019751e-04 6.054726e-04 #> 117 -6.237476e-03 -1.759592e-02 2.401846e-02 4.814750e-02 #> 118 -1.492471e-05 -4.210243e-05 5.746856e-05 1.151982e-04 #> 119 -6.607927e-07 -1.864090e-06 2.544454e-06 5.100176e-06 #> 120 -1.758884e-01 -4.961804e-01 6.772784e-01 1.357688e+00 #> 121 -2.540746e-05 -7.167388e-05 9.782958e-05 1.961284e-04 #> 122 -1.523471e-03 -4.297688e-03 5.866069e-03 1.176092e-02 #> 123 -5.597061e-06 -1.578926e-05 2.155201e-05 4.320040e-05 #> 124 -1.088174e-01 -3.069719e-01 4.189939e-01 8.400831e-01 #> 125 -1.686582e-04 -4.757827e-04 6.494236e-04 1.301891e-03 #> 126 -1.633639e-03 -4.608498e-03 6.290638e-03 1.260966e-02 #> 127 -3.414447e-01 -9.631992e-01 1.314599e+00 2.636225e+00 #> 128 -3.874637e-01 -1.093016e+00 1.491769e+00 2.991506e+00 #> 129 -3.426615e-05 -9.666432e-05 1.319427e-04 2.644967e-04 #> 130 -7.313048e-02 -2.063014e-01 2.816048e-01 5.644852e-01 #> 131 -6.781546e-05 -1.913070e-04 2.611313e-04 5.234379e-04 #> 132 -4.790724e-04 -1.351461e-03 1.844734e-03 3.697861e-03 #> 133 -1.648056e-05 -4.649136e-05 6.345819e-05 1.272133e-04 #> 134 -3.571999e-01 -1.007648e+00 1.375315e+00 2.757588e+00 #> 135 -8.888042e-02 -2.507323e-01 3.422558e-01 6.860416e-01 #> 136 -8.383570e-06 -2.364991e-05 3.228094e-05 6.471176e-05 #> 137 -1.720157e-05 -4.852511e-05 6.623179e-05 1.327913e-04 #> 138 -9.259390e-03 -2.612075e-02 3.565482e-02 7.147442e-02 #> 139 -5.446042e-01 -1.536289e+00 2.096638e+00 4.205074e+00 #> 140 -5.693511e-04 -1.606130e-03 2.192276e-03 4.395136e-03 #> 141 -1.178656e-05 -3.324955e-05 4.538246e-05 9.098726e-05 #> 142 -3.043450e-04 -8.585449e-04 1.171781e-03 2.349814e-03 #> 143 -1.310598e-03 -3.697186e-03 5.046558e-03 1.011684e-02 #> 144 -1.177644e-05 -3.322111e-05 4.534485e-05 9.090278e-05 #> 145 -7.455583e-06 -2.103195e-05 2.870623e-05 5.755569e-05 #> 146 -9.105744e-05 -2.568700e-04 3.505966e-04 7.029808e-04 #> 147 -2.428161e-03 -6.849821e-03 9.349784e-03 1.874402e-02 #> 148 -2.783418e-03 -7.851988e-03 1.071757e-02 2.148720e-02 #> 149 -7.646882e-05 -2.157163e-04 2.944303e-04 5.903333e-04 #> 150 -5.193763e-02 -1.465156e-01 1.999884e-01 4.009439e-01