Learn R Programming

margins (version 0.3.0)

marginal_effects: Differentiate a Model Object with Respect to All Variables

Description

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

Usage

marginal_effects(model, data, ...)

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

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

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

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

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

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.

Arguments passed to methods, and onward to dydx methods.

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.

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

This function extracts unit-specific marginal effects from an estimated model with respect to all variables specified in data and returns a data.frame. (Note that this is not each coefficient.) See dydx for computational details, or to extract the marginal effect for only one variable. 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 methods is also provided for the object classes “margins” to return a simplified data frame from complete “margins” objects.

See Also

dydx, margins

Examples

Run this code
# NOT RUN {
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)

# }

Run the code above in your browser using DataLab