(Possibly Sparse) Contrast Matrices
Return a matrix of contrasts.
1 2 3 4 5
a vector of levels for a factor, or the number of levels.
a logical indicating whether contrasts should be computed.
logical indicating if the result should be sparse
the set of values over which orthogonal polynomials are to be computed.
an integer specifying which group is considered the
baseline group. Ignored if
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
a square indicator matrix (the dummy coding) is returned except
contr.poly (which includes the 0-degree, i.e.\sspaceconstant,
contrasts = FALSE).
contr.helmert returns Helmert contrasts, which contrast the
second level with the first, the third with the average of the first
two, and so on.
contr.poly returns contrasts based on
contr.sum uses ‘sum to zero
contr.treatment contrasts each level with the baseline level
base): the baseline level is omitted. Note that
this does not produce ‘contrasts’ as defined in the standard
theory for linear models as they are not orthogonal to the intercept.
contr.SAS is a wrapper for
contr.treatment that sets
the base level to be the last level of the factor. The coefficients
produced when using these contrasts should be equivalent to those
produced by many (but not all) SAS procedures.
sparse is an argument to all these contrast
sparse = TRUE for
is typically pointless and is rarely useful for
A matrix with
n rows and
k columns, with
Chambers, J. M. and Hastie, T. J. (1992) Statistical models. Chapter 2 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
1 2 3 4 5 6 7 8 9 10 11 12 13
(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 (cT. <- contr.SAS(5)) all(crossprod(cT.) == diag(4)) # TRUE zapsmall(cP <- contr.poly(3)) # Linear and Quadratic zapsmall(crossprod(cP), digits = 15) # orthonormal up to fuzz