contrast                package:base                R Documentation

Contrast Matrices

Description:

     Return a matrix of contrasts.

Usage:

     contr.helmert(n, contrasts = TRUE)
     contr.poly(n, contrasts = TRUE)
     contr.sum(n, contrasts = TRUE)
     contr.treatment(n, base = 1, contrasts = TRUE)

Arguments:

       n: a vector of levels for a factor, or the number of levels.

contrasts: a logical indicating whether contrasts should be computed.

    base: an integer specifying which group is considered the baseline
          group. Ignored if `contrasts' is `FALSE'.

Details:

     These functions are used for creating contrast matrices for use in
     fitting analysis of variance and regression models.  The columns
     of the resulting matrices contain contrasts which can be used for
     coding a factor with `n' levels.  The returned value contains the
     computed contrasts.  If the argument `contrasts' is `FALSE' then a
     square indicator matrix is returned.

     Note that as from R version 0.62.2, `contr.poly' returns contrasts
     based on orthogonal (rather than raw) polynomials.

Value:

     A matrix with `n' rows and `k' columns, with `k=n-1' if
     `contrasts' is `TRUE' and `k=n' if `contrasts' is `FALSE'.

See Also:

     `contrasts', `C', and `aov', `glm', `lm'.

Examples:

     (cH <- contr.helmert(4))
     apply(cH, 2,sum) # column sums are 0!
     crossprod(cH) # diagonal -- columns are orthogonal
     contr.helmert(4, contrasts = FALSE) # just the 4 x 4 identity matrix

     (cT <- contr.treatment(5))
     all(crossprod(cT) == diag(4)) # TRUE: even orthonormal

     (cP <- contr.poly(3)) # Linear and Quadratic
     zapsmall(crossprod(cP), dig=15) # orthonormal up to fuzz

