Skip to contents

Difference, ratio, or function of adjusted predictions, calculated for meaningfully different predictor values. The tidy() and summary() functions can be used to aggregate and summarize the output of comparisons(). To learn more, read the contrasts vignette, visit the package website, or scroll down this page for a full list of vignettes:

Usage

comparisons(
  model,
  newdata = NULL,
  variables = NULL,
  type = NULL,
  vcov = TRUE,
  conf_level = 0.95,
  transform_pre = "difference",
  transform_post = NULL,
  interaction = NULL,
  by = NULL,
  wts = NULL,
  hypothesis = NULL,
  eps = NULL,
  ...
)

Arguments

model

Model object

newdata

NULL, data frame, string, or datagrid() call. Determines the predictor values for which to compute contrasts.

  • NULL (default): Unit-level contrasts for each observed value in the original dataset.

  • data frame: Unit-level contrasts for each row of the newdata data frame.

  • string:

    • "mean": Contrasts at the Mean. Contrasts when each predictor is held at its mean or mode.

    • "median": Contrasts at the Median. Contrasts when each predictor is held at its median or mode.

    • "marginalmeans": Contrasts at Marginal Means.

    • "tukey": Contrasts at Tukey's 5 numbers.

    • "grid": Contrasts on a grid of representative numbers (Tukey's 5 numbers and unique values of categorical predictors).

  • datagrid() call to specify a custom grid of regressors. For example:

    • newdata = datagrid(cyl = c(4, 6)): cyl variable equal to 4 and 6 and other regressors fixed at their means or modes.

    • newdata = datagrid(mpg = fivenum): mpg variable held at Tukey's five numbers (using the fivenum function), and other regressors fixed at their means or modes.

    • See the Examples section and the datagrid documentation.

variables

NULL, character vector, or named list. The subset of variables for which to compute contrasts.

  • NULL: compute contrasts for all the variables in the model object (can be slow).

  • Character vector: subset of variables (usually faster).

  • Named list: names identify the subset of variables of interest, and values define the type of contrast to compute. Acceptable values depend on the variable type:

    • Factor or character variables:

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

      • "all": All combinations of observed levels

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

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

    • Logical variables:

      • NULL: contrast between TRUE and FALSE

    • Numeric variables:

      • Numeric of length 1: Contrast for a gap of x, computed at the observed value plus and minus x / 2. For example, estimating a +1 contrast compares adjusted predictions when the regressor is equal to its observed value minus 0.5 and its observed value plus 0.5.

      • Numeric vector of length 2: Contrast between the 2nd element and the 1st element of the x 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.

    • Examples:

      • variables = list(gear = "pairwise", hp = 10)

      • variables = list(gear = "sequential", hp = c(100, 120))

      • See the Examples section below for more.

type

string indicates the type (scale) of the predictions used to compute marginal effects or contrasts. This can differ based on the model type, but will typically be a string such as: "response", "link", "probs", or "zero". When an unsupported string is entered, the model-specific list of acceptable values is returned in an error message. When type is NULL, the default value is used. This default is the first model-related row in the marginaleffects:::type_dictionary dataframe.

vcov

Type of uncertainty estimates to report (e.g., for robust standard errors). Acceptable values:

  • FALSE: Do not compute standard errors. This can speed up computation considerably.

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

  • String which indicates the kind of uncertainty estimates to return.

    • Heteroskedasticity-consistent: "HC", "HC0", "HC1", "HC2", "HC3", "HC4", "HC4m", "HC5". See ?sandwich::vcovHC

    • Heteroskedasticity and autocorrelation consistent: "HAC"

    • Mixed-Models degrees of freedom: "satterthwaite", "kenward-roger"

    • Other: "NeweyWest", "KernHAC", "OPG". See the sandwich package documentation.

  • One-sided formula which indicates the name of cluster variables (e.g., ~unit_id). This formula is passed to the cluster argument of the sandwich::vcovCL function.

  • Square covariance matrix

  • Function which returns a covariance matrix (e.g., stats::vcov(model))

conf_level

numeric value between 0 and 1. Confidence level to use to build a confidence interval.

transform_pre

string or function. How should pairs of adjusted predictions be contrasted?

  • string: shortcuts to common contrast functions.

    • Supported shortcuts strings: difference, differenceavg, differenceavgwts, dydx, eyex, eydx, dyex, dydxavg, eyexavg, eydxavg, dyexavg, dydxavgwts, eyexavgwts, eydxavgwts, dyexavgwts, ratio, ratioavg, ratioavg, lnratio, lnratioavg, lnratioavg, lnor, lnoravg, lnoravgwts, expdydx, expdydxavg, expdydxavgwts

    • See the Transformations section below for definitions of each transformation.

  • function: accept two equal-length numeric vectors of adjusted predictions (hi and lo) and returns a vector of contrasts of the same length, or a unique numeric value.

    • See the Transformations section below for examples of valid functions.

transform_post

(experimental) A function applied to unit-level estimates and confidence intervals just before the function returns results.

interaction

TRUE, FALSE, or NULL

  • FALSE: Contrasts represent the change in adjusted predictions when one predictor changes and all other variables are held constant.

  • TRUE: Contrasts represent the changes in adjusted predictions when the predictors specified in the variables argument are manipulated simultaneously.

  • NULL (default): Behaves like TRUE when the variables argument is specified and the model formula includes interactions. Behaves like FALSE otherwise.

by

Compute group-wise average estimates. Valid inputs:

  • Character vector of column names in newdata or in the data frame produced by calling the function without the by argument.

  • Data frame with a by column of group labels, and merging columns shared by newdata or the data frame produced by calling the same function without the by argument.

  • See examples below.

wts

string or numeric: weights to use when computing average contrasts or marginaleffects. These weights only affect the averaging in tidy() or summary(), and not the unit-level estimates themselves.

  • string: column name of the weights variable in newdata. When supplying a column name to wts, it is recommended to supply the original data (including the weights variable) explicitly to newdata.

  • numeric: vector of length equal to the number of rows in the original data or in newdata (if supplied).

hypothesis

specify a hypothesis test or custom contrast using a vector, matrix, string, or string formula.

  • String:

    • "pairwise": pairwise differences between estimates in each row.

    • "reference": differences between the estimates in each row and the estimate in the first row.

    • "sequential": difference between an estimate and the estimate in the next row.

    • "revpairwise", "revreference", "revsequential": inverse of the corresponding hypotheses, as described above.

  • String formula to specify linear or non-linear hypothesis tests. If the term column uniquely identifies rows, terms can be used in the formula. Otherwise, use b1, b2, etc. to identify the position of each parameter. Examples:

    • hp = drat

    • hp + drat = 12

    • b1 + b2 + b3 = 0

  • Numeric vector: Weights to compute a linear combination of (custom contrast between) estimates. Length equal to the number of rows generated by the same function call, but without the hypothesis argument.

  • Numeric matrix: Each column is a vector of weights, as describe above, used to compute a distinct linear combination of (contrast between) estimates. The column names of the matrix are used as labels in the output.

  • See the Examples section below and the vignette: https://vincentarelbundock.github.io/marginaleffects/articles/hypothesis.html

eps

NULL or numeric value which determines the step size to use when calculating numerical derivatives: (f(x+eps)-f(x))/eps. When eps is NULL, the step size is 0.0001 multiplied by the difference between the maximum and minimum values of the variable with respect to which we are taking the derivative. Changing eps may be necessary to avoid numerical problems in certain models.

...

Additional arguments are passed to the predict() method supplied by the modeling package.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 documentation for a non-exhaustive list of available arguments.

Details

A "contrast" is a difference, ratio of function of adjusted predictions, calculated for meaningfully different predictor values (e.g., College graduates vs. Others). Uncertainty estimates are computed using the delta method.

The newdata argument can be used to control the kind of contrasts to report:

  • Average Contrasts

  • Adjusted Risk Ratios

  • Adjusted Risk Differences

  • Group-Average Contrasts

  • Contrasts at the Mean

  • Contrasts at User-Specified values (aka Contrasts at Representative values, MER).

  • Custom contrasts using arbitrary functions

Model-Specific Arguments

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

PackageClassArgumentDocumentation
brmsbrmsfitndrawsbrms::posterior_predict
re_formula
lme4merModinclude_randominsight::get_predicted
re.formlme4::predict.merMod
allow.new.levelslme4::predict.merMod
glmmTMBglmmTMBre.formglmmTMB::predict.glmmTMB
allow.new.levelsglmmTMB::predict.glmmTMB
zitypeglmmTMB::predict.glmmTMB
mgcvbamexcludemgcv::predict.bam
robustlmmrlmerModre.formrobustlmm::predict.rlmerMod
allow.new.levelsrobustlmm::predict.rlmerMod

Transformations

The following transformations can be applied by supplying one of the shortcut strings to the transform_pre argument. hi is a vector of adjusted predictions for the "high" side of the contrast. lo is a vector of adjusted predictions for the "low" side of the contrast. y is a vector of adjusted predictions for the original data. x is the predictor in the original data. eps is the step size to use to compute derivatives and elasticities.

ShortcutFunction
difference\(hi, lo) hi - lo
differenceavg\(hi, lo) mean(hi) - mean(lo)
differenceavgwts\(hi, lo, w) wmean(hi, w) - wmean(lo, w)
dydx\(hi, lo, eps) (hi - lo)/eps
eyex\(hi, lo, eps, y, x) (hi - lo)/eps * (x/y)
eydx\(hi, lo, eps, y, x) ((hi - lo)/eps)/y
dyex\(hi, lo, eps, x) ((hi - lo)/eps) * x
dydxavg\(hi, lo, eps) mean((hi - lo)/eps)
eyexavg\(hi, lo, eps, y, x) mean((hi - lo)/eps * (x/y))
eydxavg\(hi, lo, eps, y, x) mean(((hi - lo)/eps)/y)
dyexavg\(hi, lo, eps, x) mean(((hi - lo)/eps) * x)
dydxavgwts\(hi, lo, eps, w) wmean((hi - lo)/eps, w)
eyexavgwts\(hi, lo, eps, y, x, w) wmean((hi - lo)/eps * (x/y), w)
eydxavgwts\(hi, lo, eps, y, x, w) wmean(((hi - lo)/eps)/y, w)
dyexavgwts\(hi, lo, eps, x, w) wmean(((hi - lo)/eps) * x, w)
ratio\(hi, lo) hi/lo
ratioavg\(hi, lo) mean(hi)/mean(lo)
ratioavg\(hi, lo) wmean(hi)/wmean(lo)
lnratio\(hi, lo) log(hi/lo)
lnratioavg\(hi, lo) log(mean(hi)/mean(lo))
lnratioavg\(hi, lo) log(wmean(hi)/wmean(lo))
lnor\(hi, lo) log((hi/(1 - hi))/(lo/(1 - lo)))
lnoravg\(hi, lo) log((mean(hi)/(1 - mean(hi)))/(mean(lo)/(1 - mean(lo))))
lnoravgwts\(hi, lo, w) log((wmean(hi, w)/(1 - wmean(hi, w)))/(wmean(lo, w)/(1 - wmean(lo, w))))
expdydx\(hi, lo, eps) ((exp(hi) - exp(lo))/exp(eps))/eps
expdydxavg\(hi, lo, eps) mean(((exp(hi) - exp(lo))/exp(eps))/eps)
expdydxavgwts\(hi, lo, eps, w) wmean(((exp(hi) - exp(lo))/exp(eps))/eps, w)

Examples


library(marginaleffects)
library(magrittr)

# Linear model
tmp <- mtcars
tmp$am <- as.logical(tmp$am)
mod <- lm(mpg ~ am + factor(cyl), tmp)
comparisons(mod, variables = list(cyl = "reference")) %>% tidy()
#>       type term contrast   estimate std.error statistic      p.value   conf.low
#> 1 response  cyl    6 - 4  -6.156118  1.535723 -4.008612 6.107658e-05  -9.166079
#> 2 response  cyl    8 - 4 -10.067560  1.452082 -6.933187 4.114626e-12 -12.913589
#>   conf.high
#> 1 -3.146156
#> 2 -7.221530
comparisons(mod, variables = list(cyl = "sequential")) %>% tidy()
#>       type term contrast  estimate std.error statistic      p.value  conf.low
#> 1 response  cyl    6 - 4 -6.156118  1.535723 -4.008612 6.107658e-05 -9.166079
#> 2 response  cyl    8 - 6 -3.911442  1.470254 -2.660385 7.805144e-03 -6.793087
#>   conf.high
#> 1 -3.146156
#> 2 -1.029797
comparisons(mod, variables = list(cyl = "pairwise")) %>% tidy()
#>       type term contrast   estimate std.error statistic      p.value   conf.low
#> 1 response  cyl    6 - 4  -6.156118  1.535723 -4.008612 6.107658e-05  -9.166079
#> 2 response  cyl    8 - 4 -10.067560  1.452082 -6.933187 4.114626e-12 -12.913589
#> 3 response  cyl    8 - 6  -3.911442  1.470254 -2.660385 7.805144e-03  -6.793087
#>   conf.high
#> 1 -3.146156
#> 2 -7.221530
#> 3 -1.029797

# GLM with different scale types
mod <- glm(am ~ factor(gear), data = mtcars)
comparisons(mod, type = "response") %>% tidy()
#>       type term contrast  estimate std.error statistic      p.value  conf.low
#> 1 response gear    4 - 3 0.6666667 0.1174440  5.676462 1.375090e-08 0.4364806
#> 2 response gear    5 - 3 1.0000000 0.1565921  6.386020 1.702589e-10 0.6930852
#>   conf.high
#> 1 0.8968528
#> 2 1.3069148
comparisons(mod, type = "link") %>% tidy()
#>   type term contrast  estimate std.error statistic      p.value  conf.low
#> 1 link gear    4 - 3 0.6666667 0.1174440  5.676462 1.375090e-08 0.4364806
#> 2 link gear    5 - 3 1.0000000 0.1565921  6.386020 1.702589e-10 0.6930852
#>   conf.high
#> 1 0.8968528
#> 2 1.3069148

# Contrasts at the mean
comparisons(mod, newdata = "mean")
#>   rowid     type term contrast comparison std.error statistic      p.value
#> 1     1 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08
#> 2     1 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10
#>    conf.low conf.high     predicted predicted_hi  predicted_lo gear am
#> 1 0.4364806 0.8968528 -2.355139e-16    0.6666667 -2.355139e-16    3  1
#> 2 0.6930852 1.3069148 -2.355139e-16    1.0000000 -2.355139e-16    3  1

# Contrasts between marginal means
comparisons(mod, newdata = "marginalmeans")
#>       type term contrast comparison std.error statistic      p.value  conf.low
#> 1 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08 0.4364806
#> 2 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10 0.6930852
#>   conf.high predicted predicted_hi  predicted_lo
#> 1 0.8968528 0.5555556    0.6666667 -2.355139e-16
#> 2 1.3069148 0.5555556    1.0000000 -2.355139e-16

# Contrasts at user-specified values
comparisons(mod, newdata = datagrid(am = 0, gear = tmp$gear))
#>   rowid     type term contrast comparison std.error statistic      p.value
#> 1     1 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08
#> 2     2 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08
#> 3     3 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08
#> 4     1 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10
#> 5     2 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10
#> 6     3 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10
#>    conf.low conf.high     predicted predicted_hi  predicted_lo am gear
#> 1 0.4364806 0.8968528  6.666667e-01    0.6666667 -2.355139e-16  0    4
#> 2 0.4364806 0.8968528 -2.355139e-16    0.6666667 -2.355139e-16  0    3
#> 3 0.4364806 0.8968528  1.000000e+00    0.6666667 -2.355139e-16  0    5
#> 4 0.6930852 1.3069148  6.666667e-01    1.0000000 -2.355139e-16  0    4
#> 5 0.6930852 1.3069148 -2.355139e-16    1.0000000 -2.355139e-16  0    3
#> 6 0.6930852 1.3069148  1.000000e+00    1.0000000 -2.355139e-16  0    5
comparisons(mod, newdata = datagrid(am = unique, gear = max))
#>   rowid     type term contrast comparison std.error statistic      p.value
#> 1     1 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08
#> 2     2 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08
#> 3     1 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10
#> 4     2 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10
#>    conf.low conf.high predicted predicted_hi  predicted_lo am gear
#> 1 0.4364806 0.8968528         1    0.6666667 -2.355139e-16  1    5
#> 2 0.4364806 0.8968528         1    0.6666667 -2.355139e-16  0    5
#> 3 0.6930852 1.3069148         1    1.0000000 -2.355139e-16  1    5
#> 4 0.6930852 1.3069148         1    1.0000000 -2.355139e-16  0    5

# Numeric contrasts
mod <- lm(mpg ~ hp, data = mtcars)
comparisons(mod, variables = list(hp = 1)) %>% tidy()
#>       type term contrast    estimate std.error statistic      p.value
#> 1 response   hp       +1 -0.06822828 0.0101193 -6.742389 1.558037e-11
#>      conf.low   conf.high
#> 1 -0.08806175 -0.04839481
comparisons(mod, variables = list(hp = 5)) %>% tidy()
#>       type term contrast   estimate  std.error statistic      p.value
#> 1 response   hp       +5 -0.3411414 0.05059652 -6.742389 1.558038e-11
#>     conf.low conf.high
#> 1 -0.4403087 -0.241974
comparisons(mod, variables = list(hp = c(90, 100))) %>% tidy()
#>       type term contrast   estimate std.error statistic      p.value   conf.low
#> 1 response   hp 100 - 90 -0.6822828  0.101193 -6.742389 1.558038e-11 -0.8806175
#>    conf.high
#> 1 -0.4839481
comparisons(mod, variables = list(hp = "iqr")) %>% tidy()
#>       type term contrast  estimate std.error statistic      p.value  conf.low
#> 1 response   hp  Q3 - Q1 -5.697061 0.8449619 -6.742389 1.558038e-11 -7.353156
#>   conf.high
#> 1 -4.040966
comparisons(mod, variables = list(hp = "sd")) %>% tidy()
#>       type term                contrast  estimate std.error statistic
#> 1 response   hp (x + sd/2) - (x - sd/2) -4.677926 0.6938085 -6.742389
#>        p.value  conf.low conf.high
#> 1 1.558038e-11 -6.037766 -3.318087
comparisons(mod, variables = list(hp = "minmax")) %>% tidy()
#>       type term  contrast estimate std.error statistic      p.value  conf.low
#> 1 response   hp Max - Min -19.3086  2.863763 -6.742389 1.558038e-11 -24.92147
#>   conf.high
#> 1 -13.69573

# Adjusted Risk Ratio: see the contrasts vignette
mod <- glm(vs ~ mpg, data = mtcars, family = binomial)
cmp <- comparisons(mod, transform_pre = "lnratioavg")
summary(cmp, transform_avg = exp)
#>   Term Contrast Effect   Pr(>|z|) 2.5 % 97.5 %
#> 1  mpg mean(+1)  1.135 2.3808e-10 1.091   1.18
#> 
#> Model type:  glm 
#> Prediction type:  response 
#> Average-transformation:   

# 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_avg = exp)
#>   Term Contrast Effect   Pr(>|z|) 2.5 % 97.5 %
#> 1  mpg       +1  1.135 2.3808e-10 1.091   1.18
#> 
#> Model type:  glm 
#> Prediction type:  response 
#> Pre-transformation:  function(hi, lo) log(mean(hi)/mean(lo)) 
#> Average-transformation:   
# Interactions between contrasts
mod <- lm(mpg ~ factor(cyl) * factor(gear) + hp, data = mtcars)
cmp <- comparisons(mod, variables = c("cyl", "gear"))
#> Warning: Model matrix is rank deficient. Some variance-covariance parameters are
#>   missing.
summary(cmp)
#>     cyl  gear  Effect Std. Error z value Pr(>|z|)   2.5 % 97.5 %
#> 1 4 - 4 4 - 3  4.2194      3.238  1.3033 0.192489 -2.1262 10.565
#> 2 4 - 4 5 - 3  6.9870      3.700  1.8883 0.058987 -0.2652 14.239
#> 3 6 - 4 3 - 3 -1.1472      3.706 -0.3096 0.756898 -8.4109  6.116
#> 4 6 - 4 4 - 3 -0.6306      3.405 -0.1852 0.853068 -7.3035  6.042
#> 5 6 - 4 5 - 3  2.6778      4.617  0.5800 0.561933 -6.3715 11.727
#> 6 8 - 4 3 - 3 -0.8719      3.829 -0.2277 0.819850 -8.3757  6.632
#> 7 8 - 4 4 - 3  3.3475      6.427  0.5208 0.602495 -9.2501 15.945
#> 8 8 - 4 5 - 3  5.5250      5.868  0.9416 0.346419 -5.9760 17.026
#> 
#> Model type:  lm 
#> Prediction type:  response 

# variable-specific contrasts
cmp <- comparisons(mod, variables = list(gear = "sequential", hp = 10))
#> Warning: Model matrix is rank deficient. Some variance-covariance parameters are
#>   missing.
summary(cmp)
#>    gear  hp Effect Std. Error z value Pr(>|z|)  2.5 % 97.5 %
#> 1 4 - 3 +10  2.835      2.625  1.0801  0.28010 -2.310  7.980
#> 2 5 - 4 +10  2.054      2.611  0.7866  0.43151 -3.063  7.171
#> 
#> Model type:  lm 
#> Prediction type:  response 

# hypothesis test: is the `hp` marginal effect at the mean equal to the `drat` marginal effect
mod <- lm(mpg ~ wt + drat, data = mtcars)

comparisons(
    mod,
    newdata = "mean",
    hypothesis = "wt = drat")
#>       type    term comparison std.error statistic      p.value conf.low
#> 1 response wt=drat  -6.225381  1.051769 -5.918963 3.239776e-09 -8.28681
#>   conf.high
#> 1 -4.163952

# same hypothesis test using row indices
comparisons(
    mod,
    newdata = "mean",
    hypothesis = "b1 - b2 = 0")
#>       type    term comparison std.error statistic      p.value conf.low
#> 1 response b1-b2=0  -6.225381  1.051769 -5.918963 3.239776e-09 -8.28681
#>   conf.high
#> 1 -4.163952

# same hypothesis test using numeric vector of weights
comparisons(
    mod,
    newdata = "mean",
    hypothesis = c(1, -1))
#>       type   term comparison std.error statistic      p.value conf.low
#> 1 response custom  -6.225381  1.051769 -5.918963 3.239776e-09 -8.28681
#>   conf.high
#> 1 -4.163952

# two custom contrasts using a matrix of weights
lc <- matrix(c(
    1, -1,
    2, 3),
    ncol = 2)
comparisons(
    mod,
    newdata = "mean",
    hypothesis = lc)
#>       type   term comparison std.error  statistic      p.value  conf.low
#> 1 response custom  -6.225381  1.051769 -5.9189631 3.239776e-09  -8.28681
#> 2 response custom  -5.238308  5.623757 -0.9314607 3.516153e-01 -16.26067
#>   conf.high
#> 1 -4.163952
#> 2  5.784052


# `by` argument
mod <- lm(mpg ~ hp * am * vs, data = mtcars)
cmp <- comparisons(mod, variables = "hp", by = c("vs", "am"))
summary(cmp)
#>   Term Contrast vs am   Effect Std. Error z value  Pr(>|z|)    2.5 %    97.5 %
#> 1   hp mean(+1)  0  1 -0.03685    0.01240  -2.971 0.0029676 -0.06116 -0.012540
#> 2   hp mean(+1)  1  1 -0.11115    0.04634  -2.399 0.0164473 -0.20197 -0.020336
#> 3   hp mean(+1)  1  0 -0.09941    0.05345  -1.860 0.0628876 -0.20417  0.005344
#> 4   hp mean(+1)  0  0 -0.04215    0.02477  -1.702 0.0887904 -0.09070  0.006394
#> 
#> Model type:  lm 
#> Prediction type:  response 

library(nnet)
mod <- multinom(factor(gear) ~ mpg + am * vs, data = mtcars, trace = FALSE)
by <- data.frame(
    group = c("3", "4", "5"),
    by = c("3,4", "3,4", "5"))
comparisons(mod, type = "probs", by = by)
#>    type term    comparison   std.error   statistic     p.value    conf.low
#> 1 probs  mpg  0.0004634916 0.005796055  0.07996674 0.936263711 -0.01089657
#> 2 probs   am -0.2227925482 0.079557885 -2.80038299 0.005104201 -0.37872314
#> 3 probs   vs  0.1021020743 0.073235805  1.39415514 0.163270760 -0.04143747
#> 4 probs  mpg -0.0009269832 0.011592109 -0.07996674 0.936263711 -0.02364710
#> 5 probs   am  0.4455850963 0.159115770  2.80038299 0.005104201  0.13372392
#> 6 probs   vs -0.2042041486 0.146471610 -1.39415514 0.163270760 -0.49128323
#>     conf.high  by
#> 1  0.01182355 3,4
#> 2 -0.06686196 3,4
#> 3  0.24564161 3,4
#> 4  0.02179313   5
#> 5  0.75744628   5
#> 6  0.08287493   5