marginal_effects {margins} | R Documentation |
Differentiate a Model Object with Respect to All (or Specified) Variables
Description
Extract marginal effects from a model object, conditional on data, using dydx
.
Usage
marginal_effects(model, data, variables = NULL, ...)
## S3 method for class 'margins'
marginal_effects(model, data, variables = NULL, ...)
## S3 method for class 'clm'
marginal_effects(
model,
data = find_data(model, parent.frame()),
variables = NULL,
type = NULL,
eps = 1e-07,
varslist = NULL,
as.data.frame = TRUE,
...
)
## Default S3 method:
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 class '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 class '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 class '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 class '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 class '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 class '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 class '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 class '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 |
|
data |
A data.frame over which to calculate marginal effects. This is optional, but may be required when the underlying modelling function sets |
variables |
A character vector with the names of variables for which to compute the marginal effects. The default ( |
... |
Arguments passed to methods, and onward to |
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 |
as.data.frame |
A logical indicating whether to return a data frame (the default) or a matrix. |
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:
-
“betareg”, see
betareg
-
“ivreg”, see
ivreg
-
“lm”, see
lm
-
“loess”, see
loess
-
“multinom”, see
multinom
-
“nnet”, see
nnet
-
“polr”, see
polr
-
“svyglm”, see
svyglm
A method is also provided for the object classes “margins” to return a simplified data frame from complete “margins” objects.
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.
See Also
Examples
require("datasets")
x <- lm(mpg ~ cyl * hp + wt, data = mtcars)
marginal_effects(x)
# factor variables report discrete differences
x <- lm(mpg ~ factor(cyl) * factor(am), data = mtcars)
marginal_effects(x)
# get just marginal effects from "margins" object
require('datasets')
m <- margins(lm(mpg ~ hp, data = mtcars[1:10,]))
marginal_effects(m)
marginal_effects(m)
# 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
}