Learn R Programming

margins (version 0.2.0)

margins: Marginal Effects Estimation

Description

margins is an S3 generic function for building a “margins” object from a model object. Methods are currently implemented for “lm” (and, implicitly, “glm”) class objects.

Usage

margins(model, ...)

# S3 method for lm margins(model, data, at = NULL, atmeans = FALSE, ...)

Arguments

model

A model object of class “lm”.

data

A data.frame containing the data at which to evaluate the marginal effects, as in predict.

at

A list of one or more named vectors, specifically values at which to calculate the marginal effects. See build_datalist for details on use.

atmeans

A logical indicating whether to calculate marginal effects at the means (i.e., partial effects at the average of all covariates), as opposed to the default average marginal effects (i.e., average partial effects), which is the default.

Arguments passed to methods, and in turn to build_margins.

Value

An object of class “marginslist”, composed of one or more objects of class “margins”.

Details

Methods for this generic return a “marginslist” list of one or more “margins” objects. A “margins” object is a data.frame consisting of the original data, predicted values and standard errors thereof, estimated marginal effects from the model model, with attributes describing various features of the marginal effects estimates.

See marginal_effects for details on estimation of marginal effects.

The atmeans option controls what types of effects are returned. Estimates are either “average marginal effects” (when atmeans = FALSE, the default) or “marginal effects at means” (when atmeans = TRUE). In the former case, the marginal effects are estimated for each observation in the dataset and returned in full. In the latter, column means are taken for data and estimation is performed only these “averaged” cases. The former is generally preferred because the latter may estimate marginal effects for cases that are unintuitive or not covered by the observed data (e.g., the effect when a binary variable in data is averaged to 0.6 rather than at 0 and 1, respectively).

The margins method for objects of class “lm” or “glm” simply constructs a list of data.frames (using build_datalist) and calls build_margins on each. You can call build_margins directly, but it requires the explicit specification of a dataset over which to estimate the quantities of interest.

Alternatively, you can use marginal_effects to retrieve a data.frame of marginal effects without constructing a “margins” object. That can be efficient for plotting, etc., given the time-consuming nature of variance estimation. Use extract_marginal_effects to retrieve a data.frame of marginal effects from a “margins” object.

References

Greene, W.H. 2012. Econometric Analysis, 7th Ed. Boston: Pearson.

Stata manual: margins. Retrieved 2014-12-15 from http://www.stata.com/manuals13/rmargins.pdf.

See Also

marginal_effects, build_margins

Examples

Run this code
# NOT RUN {
# linear model
require("datasets")
x <- lm(mpg ~ cyl * hp + wt, data = head(mtcars))
margins(x)

# generalized linear model
x <- glm(am ~ hp, data = head(mtcars), family = binomial)
margins(x, type = "response")
margins(x, type = "link")

# }

Run the code above in your browser using DataLab