contr.sum
Contrast or Dummy Variable Matrix

Description

Calculates a matrix giving contrasts for encoding factor (categorical) data in a design matrix, or the dummy variable matrix for a factor.

Usage

contr.sum(n, contrasts = TRUE, sparse = FALSE)
contr.poly(n, scores = 1:n, contrasts = TRUE, sparse = FALSE)
contr.helmert(n, contrasts = TRUE, sparse = FALSE)
contr.treatment(n, base = 1, contrasts = TRUE, sparse = FALSE)
contr.SAS(n, contrasts = TRUE, sparse = FALSE)

Arguments

n a vector of levels for a category or the number of levels. For contr.poly, this can be the numeric values over which the contrasts should be orthogonal.
contrasts a logical value that specifies if the contrasts should be computed. If FALSE, all these functions save contr.poly will return an n by n identity matrix containing the "dummy variable" encoding and contr.poly will include the constant column in the encoding matrix.
sparse a logical value that specifies a sparse matrix as the output. In this release, sparse matrices are not available so this setting does not effect the return value and an ordinary (dense) matrix is always returned. If TRUE, a warning is displayed.
base Only used by contr.treatment, an integer between 1 and n, where n is the number of levels of the category, that specifies the treatment to designate as the baseline (zero-level) treatment.

Note: We recommend that you use the factor function to reorder levels so that the baseline level is the first (or the last if you prefer to use contr.SAS).

scores Only used by contr.poly, a numeric vector of length n, where n is the number of levels in the factor. Each number in the vector corresponds to one level in the factor. The polynomials used for the contrasts are chosen so that the when they are evaluated at these points the columns of the resulting matrix are orthogonal. By default, the scores are 1 through the number of levels.

Details

These functions are not generally called directly: they are most often called from model.matrix, which is called from many modelling functions (e.g., lm). You may set the default contrast encoding for unordered and ordered factors by calling options(contrasts=c("<unordered contrast name>", "<ordered contrast name>")
You can use the contrasts argument to model.matrix or functions that call it (like lm) to set the contrasts for a particular model.
You can use the contrasts function to attach the contrast name as an attribute to a factor variable so that that contrast will be used on that factor.
Value
returns a matrix with n rows and n-1 columns, if contrasts = TRUE. If contrasts = FALSE an n by n matrix is returned.
The interpretation of the matrix depends on the function:
See Also
contrasts, C, options, model.matrix.
Examples
# change default contrasts for ordinary factors to "contr.sum"
options(contrasts = c(unordered = "contr.sum", ordered = "contr.poly"))
# set default contrasts back to standard ones
options(contrasts = c(unordered = "contr.treatment", 
    ordered = "contr.poly"))
# In the Puromycin data, the 'state' variable has levels "treated" and 
# "untreated", in that order.  The following example makes "untreated" 
# the baseline level when Puromycin is used as the data argument to lm 
# or other modeling functions.
Puromycin <- Sdatasets::Puromycin
contrasts(Puromycin$state) <- 
    contr.treatment(levels(Puromycin$state), 
    base = which(levels(Puromycin$state) == "untreated"))
contrasts(Puromycin)
lm(rate ~ conc * state, data = Puromycin)
Package stats version 6.0.0-69
Package Index