Learn R Programming

marginaleffects (version 0.4.1)

comparisons: Experimental function to compute contrasts between adjusted predictions

Description

This function calculates contrasts (or comparisons) between adjusted predictions for each row of the dataset. The resulting object can processed by the tidy() or summary() functions, which compute Average Contrasts. The datagrid() function and the newdata argument can be used to calculate contrasts Contrasts at the Mean or Contrasts at User-Specified values (aka Contrasts at Representative values). Additional information can be found in the Details and Examples sections below, and in the vignette on the marginaleffects website.

Usage

comparisons(
  model,
  variables = NULL,
  newdata = NULL,
  type = "response",
  vcov = TRUE,
  contrast_factor = "reference",
  contrast_numeric = 1,
  ...
)

Arguments

model

Model object

variables

Variables to consider (character vector). NULL calculates marginal effects for all terms in the model object.

newdata

A dataset over which to compute marginal effects. NULL uses the original data used to fit the model.

type

Type(s) of prediction as string or character vector. This can differ based on the model type, but will typically be a string such as: "response", "link", "probs", or "zero".

vcov

Matrix or boolean

  • FALSE: does not compute unit-level standard errors. This can speed up computation considerably.

  • TRUE: computes unit-level standard errors using the default vcov(model) variance-covariance matrix.

  • Named square matrix: computes standard errors with a user-supplied variance-covariance matrix. This matrix must be square and have dimensions equal to the number of coefficients in get_coef(model).

contrast_factor

string

  • "reference": Each factor level is compared to the factor reference (base) level

  • "sequential": Each factor level is compared to the previous factor level

  • "pairwise": Each factor level is compared to all other levels

contrast_numeric

string or numeric

  • Numeric of length 1: Contrast between the observed value and the observed value plus contrast_numeric

  • Numeric vector of length 2: Contrast between the 2nd element and the 1st element of the contrast_numeric vector.

  • "iqr": Contrast across the interquartile range of the regressor.

  • "sd": Contrast across one standard deviation around the regressor mean.

  • "2sd": Contrast across two standard deviations around the regressor mean.

  • "minmax": Contrast between the maximum and the minimum values of the regressor.

...

Additional arguments are passed to the predict() method used to compute adjusted predictions. These arguments are particularly useful for mixed-effects or bayesian models (see the online vignettes on the marginaleffects website). Available arguments can vary from model to model, depending on the range of supported arguments by each modeling package. See the "Model-Specific Arguments" section of the ?marginaleffects document for a non-exhaustive list of available arguments.

Details

A "contrast" is the difference between two adjusted predictions, calculated for meaningfully different regressor values (e.g., College graduates vs. Others). Uncertainty estimates are computed using the delta method.

Detailed vignettes on contrasts, marginal effects, predictions, and marginal means, as well as a list of supported models can be found on the package website:

https://vincentarelbundock.github.io/marginaleffects/

Examples

Run this code
# NOT RUN {
library(marginaleffects)
library(magrittr)

# Linear model
tmp <- mtcars
tmp$am <- as.logical(tmp$am)
mod <- lm(mpg ~ am + factor(cyl), tmp)
comparisons(mod, contrast_factor = "reference") %>% tidy()
comparisons(mod, contrast_factor = "sequential") %>% tidy()
comparisons(mod, contrast_factor = "pairwise") %>% tidy()

# GLM with different scale types
mod <- glm(am ~ factor(gear), data = mtcars)
comparisons(mod) %>% tidy()
comparisons(mod, type = "link") %>% tidy()

# Numeric contrasts
mod <- lm(mpg ~ hp, data = mtcars)
comparisons(mod, contrast_numeric = 1) %>% tidy()
comparisons(mod, contrast_numeric = 5) %>% tidy()
comparisons(mod, contrast_numeric = c(90, 100)) %>% tidy()
comparisons(mod, contrast_numeric = "iqr") %>% tidy()
comparisons(mod, contrast_numeric = "sd") %>% tidy()
comparisons(mod, contrast_numeric = "minmax") %>% tidy()

# }

Run the code above in your browser using DataLab