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)
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. |
# 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)