marginal_effects: Differentiate a Model Object with Respect to All (or...

View source: R/marginal_effects.R

marginal_effectsR 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

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.

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

  • “glm”, see glm, glm.nb

  • “ivreg”, see ivreg

  • “lm”, see lm

  • “loess”, see loess

  • “merMod”, see lmer, glmer

  • “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

dydx, margins

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
}


margins documentation built on Sept. 11, 2024, 5:21 p.m.