contrasts
Contrasts Attribute

Description

Sets or gets the contrast matrix for a factor.

Usage

 contrasts(x, contrasts = TRUE, sparse = FALSE)
 contrasts(x, how.many) <- value

Arguments

x a factor or ordered factor.
contrasts a logical value that specifies if the contrasts should be returned. If TRUE (default), a nlevels(x) by nlevels(x) - 1 matrix, with row and column names levels(x), is returned. If FALSE, a nlevels(x) by nlevels(x) identity matrix, with row and column names levels(x), is returned.
sparse a logical value that indicates if the output is a sparse matrix. Not used in this release.
how.many an integer that specifies how many contrasts to return. This value can be greater than, less than, or the same as the number of columns in value. The default is to return one fewer contrast than the number of levels in x.
value a matrix, a function that specifies the contrasts matrix to use, or a character string that names a contrasts function. You can specify the following:
  • a matrix that contains the same number of rows as there are levels in x (the number of columns should be x - 1). This output is common for the standard contrast functions, such as contr.sum(levels(x)).

  • a contrasts function, such as contr.poly or function(n, ...)contr.treatment(n, base=2, ...).

  • a character string that names a contrasts function, such as "contr.treatment", in which case the matrix will be generated by calling value(levels(x)).

Details

The contrasts() function returns the contrasts matrix of x, which is computed using the contrasts attribute assigned by contrasts(x) <- contr if it exists. Otherwise, the contrasts matrix is computed based on getOption("contrasts"): ordinary factors will use the first entry and ordered factors the second.
The model.matrix function has an optional contrasts argument that may be used to override contrasts previously attached to the data; it does this by calling contrasts <-.
There are three functions that specify contrasts for factor variables C, contrasts <-, and options.
Value
returns the contrasts matrix for levels of the factor x, if contrasts is TRUE (the default). Otherwise it returns the nlevels(x) by nlevels(x) identity matrix.
The contrasts assignment function attaches the contrasts attribute to x. This attribute contains either the name of contrasts function to use or the contrasts matrix itself, depending on the format of the value argument.
See Also
C, contr.treatment, contr.sum, contr.poly, contr.SAS, contr.helmert, C, options, model.matrix, dummy.coef
Examples
height <- c(72, 60, 73, 58, 71, 30)
type <- factor(c("I", "II", "I", "II", "I", "III"),
               levels=c("I","II","III"))
contrasts(type) # contrasts made using getOption("contrasts")[1]
lm(height ~ type)
# Now use SAS-style treatment contrasts for type
contrasts(type) <- "contr.SAS"
lm(height ~ type) # coefficients change but fitted values do not
model.matrix(~type)
# Now use sum contrasts for type
model.matrix(~type, contrasts=list(type="contr.sum"))
Package stats version 6.1.1-7
Package Index