Learn R Programming

marginaleffects (version 0.5.0)

comparisons: 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 (see ?summary.marginaleffects). The newdata argument can be used to calculate a variety of contrasts, including "Contrasts at the Mean," "Contrasts at User-Specified values" (aka Contrasts at Representative values), "Contrasts in Marginal Means", "Adjusted Risk Ratios", and much more. For more information, see the Details and Examples sections below, and in the vignettes on the marginaleffects website: https://vincentarelbundock.github.io/marginaleffects/

Usage

comparisons(
  model,
  newdata = NULL,
  variables = NULL,
  type = "response",
  vcov = TRUE,
  conf_level = 0.95,
  contrast_factor = "reference",
  contrast_numeric = 1,
  transform_pre = "difference",
  transform_post = NULL,
  interaction = NULL,
  eps = 1e-04,
  ...
)

Model-Specific Arguments

Some model types allow model-specific arguments to modify the nature of marginal effects, predictions, marginal means, and contrasts.

Package Class Argument Documentation
brms brmsfit ndraws brms::posterior_predict
re_formula
lme4 merMod include_random insight::get_predicted
re.form lme4::predict.merMod
allow.new.levels lme4::predict.merMod
glmmTMB glmmTMB re.form glmmTMB::predict.glmmTMB
allow.new.levels glmmTMB::predict.glmmTMB
zitype glmmTMB::predict.glmmTMB
mgcv bam exclude mgcv::predict.bam
robustlmm rlmerMod re.form robustlmm::predict.rlmerMod
allow.new.levels robustlmm::predict.rlmerMod

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, type = "response") %>% tidy()
comparisons(mod, type = "link") %>% tidy()

# Contrasts at the mean
comparisons(mod, newdata = "mean")

# Contrasts between marginal means
comparisons(mod, newdata = "marginalmeans")

# Contrasts at user-specified values
comparisons(mod, newdata = datagrid(am = 0, cyl = tmp$cyl))

# 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()

# Adjusted Risk Ratio (see Case Study vignette on the website)
mod <- glm(vs ~ mpg, data = mtcars, family = binomial)
cmp <- comparisons(mod, transform_pre = "lnratioavg")
summary(cmp, transform_post = exp)

# Adjusted Risk Ratio: Manual specification of the `transform_pre`
cmp <- comparisons(mod, transform_pre = function(hi, lo) log(mean(hi) / mean(lo)))
summary(cmp, transform_post = exp)
# Interactions between contrasts
mod <- lm(mpg ~ factor(cyl) * factor(gear) + hp, data = mtcars)
cmp <- comparisons(mod, variables = c("cyl", "gear"))
summary(cmp)

# variable-specific contrasts
cmp <- comparisons(mod, variables = list(gear = "sequential", hp = 10))
summary(cmp)

# }

Run the code above in your browser using DataLab