specify_contrast: Contrasts Written with Level Names

View source: R/specify_contrast.R

specify_contrastR Documentation

Contrasts Written with Level Names

Description

Build a contrast matrix for contrastmeans by writing each contrast as an expression in the level names of a model term, so there is no need to count columns, even when some treatment combinations are missing. Each level name (in backticks when it is not a syntactic name, e.g. `Wet:Bio`) stands for that mean.

Usage

specify_contrast(.x, ..., .zero_sum = TRUE)

Arguments

.x

A result of Kmatrix or predictmeans for the term, or a character vector of its level names (as in the row names of Kmatrix(model, modelterm)$K).

...

Named expressions, one per contrast, e.g. Bio_vs_None = (`Wet:Bio` + `Dry:Bio`)/2 - (`Wet:None` + `Dry:None`)/2.

.zero_sum

If TRUE (the default), every contrast's weights must sum to zero. Use FALSE for other linear combinations of the means, such as the average of two means.

Details

The arguments start with a dot so that contrasts can be given any name, including x.

Value

A matrix with one row per contrast (named as given) and one column per level. Columns of levels that are not mentioned are zero. Means that cannot be estimated (e.g. missing cells) cannot be used.

Author(s)

Dongwen Luo, Siva Ganesh and John Koolaard

See Also

contrastmeans, contrast_family, pm_joint

Examples

  library(predictmeans)
  Oats <- as.data.frame(nlme::Oats)
  Oats$nitro <- factor(Oats$nitro)
  fm <- lmer(yield ~ nitro*Variety + (1|Block/Variety), data = Oats)
  cm <- specify_contrast(Kmatrix(fm, "nitro:Variety"),
    N_effect_Victory = `0.6:Victory` - `0:Victory`,
    N_effect_vs_others = (`0.6:Victory` - `0:Victory`) -
                         ((`0.6:Golden Rain` - `0:Golden Rain`) +
                          (`0.6:Marvellous` - `0:Marvellous`))/2)
  contrastmeans(fm, "nitro:Variety", ctrmatrix = cm)

predictmeans documentation built on Nov. 24, 2026, 5:10 p.m.