contrast.lm {contrast}R Documentation

General Contrasts of Regression Coefficients


This function computes one or more contrasts of the estimated regression coefficients in a fit from one of the functions in Design, along with standard errors, confidence limits, t or Z statistics, P-values.


## S3 method for class 'lm'
contrast(fit, ...)

## S3 method for class 'gls'
contrast(fit, ...)

## S3 method for class 'lme'
contrast(fit, ...)

## S3 method for class 'geese'
contrast(fit, ...)

  cnames = NULL,
  type = c("individual", "average"),
  weights = "equal", = 0.95,
  fcType = "simple",
  fcFunc = I,
  covType = NULL,
  env = parent.frame(2)



A fit of class lm, glm, etc.


For contrast(), these pass arguments to contrast_calc(). For contrast_calc(), they are not used.

a, b

Lists containing conditions for all predictors in the model that will be contrasted to form the hypothesis H0: a = b. The gendata function will generate the necessary combinations and default values for unspecified predictors. See examples below.


A vector of character strings naming the contrasts when type = "individual". Usually cnames is not necessary as the function tries to name the contrasts by examining which predictors are varying consistently in the two lists. cnames will be needed when you contrast "non-comparable" settings, e.g., you compare list(treat = "drug", age = c(20,30)) with ⁠list(treat = "placebo", age = c(40,50)⁠.


A character string. Set type="average" to average the individual contrasts (e.g., to obtain a "Type II" or "Type III" contrast).


A numeric vector, used when type = "average", to obtain weighted contrasts.

The confidence level for confidence intervals for the contrasts.


A character string: "simple", "log" or "signed".


A function to transform the numerator and denominator of fold changes.


A string matching the method for estimating the covariance matrix. The default value produces the typical estimate. See sandwich::vcovHC() for options.


An environment in which evaluate fit.


These functions mirror rms::contrast.rms() but have fewer options.

There are some between-package inconsistencies regarding degrees of freedom in some models. See the package vignette for more details.

Fold changes are calculated for each hypothesis. When fcType ="simple", the ratio of the a group predictions over the b group predictions are used. When fcType = "signed", the ratio is used if it is greater than 1; otherwise the negative inverse (e.g., -1/ratio) is returned.


a list of class contrast.Design containing the elements Contrast, SE, Z, var, df.residual Lower, Upper, Pvalue, X, cnames, and foldChange, which denote the contrast estimates, standard errors, Z or t-statistics, variance matrix, residual degrees of freedom (this is NULL if the model was not ols), lower and upper confidence limits, 2-sided P-value, design matrix, and contrast names (or NULL).

See Also

rms::contrast.rms(), sandwich::vcovHC()


Orthodont2 <- Orthodont
Orthodont2$newAge <- Orthodont$age - 11
fm1Orth.lme2 <- lme(distance ~ Sex * newAge,
    data = Orthodont2,
    random = ~ newAge | Subject

    a = list(Sex = levels(Orthodont2$Sex), newAge = 8 - 11),
    b = list(Sex = levels(Orthodont2$Sex), newAge = 10 - 11)

# ---------------------------------------------------------------------------

anova_model <- lm(expression ~ diet * group, data = two_factor_crossed)

theme_set(theme_bw() + theme(legend.position = "top"))
ggplot(two_factor_crossed) +
    aes(x = diet, y = expression, col = group, shape = group) +
    geom_point() +
    geom_smooth(aes(group = group), method = lm, se = FALSE)

int_model <- lm(expression ~ diet * group, data = two_factor_crossed)
main_effects <- lm(expression ~ diet + group, data = two_factor_crossed)

# Interaction effect is probably real:
anova(main_effects, int_model)

# Test treatment in low fat diet:
veh_group <- list(diet = "low fat", group = "vehicle")
trt_group <- list(diet = "low fat", group = "treatment")
contrast(int_model, veh_group, trt_group)

# ---------------------------------------------------------------------------

car_mod <- lm(mpg ~ am + wt, data = mtcars)
print(summary(car_mod), digits = 5)

mean_wt <- mean(mtcars$wt)

manual_trans <- list(am = 0, wt = mean_wt)
auto_trans <- list(am = 1, wt = mean_wt)
print(contrast(car_mod, manual_trans, auto_trans), digits = 5)

[Package contrast version 0.24.2 Index]