contrasts {base} | R Documentation |
Set and view the contrasts associated with a factor.
contrasts(x, contrasts = TRUE)
contrasts(x, how.many) <- ctr
x |
a factor. |
contrasts |
logical. See Details. |
how.many |
How many contrasts should be made. Defaults to one
less than the number of levels of |
ctr |
either a matrix whose columns give coefficients for
contrasts in the levels of |
If contrasts are not set for a factor the default functions from
options("contrasts")
are used.
The argument contrasts
is ignored if x
has a matrix
contrasts
attribute set. Otherwise if contrasts = TRUE
it is passed to a contrasts function such as
contr.treatment
and if contrasts = FALSE
an identity matrix is returned.
Prior to R version 1.2.0, contrasts(, FALSE)
called a
contrasts function with contrasts = FALSE
. This normally gave
the same result, but not for contr.poly
, the default for
ordered factors.
C
,
contr.helmert
,
contr.poly
,
contr.sum
,
contr.treatment
;
glm
,
aov
,
lm
.
example(factor)
(fff <- factor(ff))
contrasts(fff) # treatment contrasts by default
contrasts(C(fff, sum))
contrasts(fff, contrasts = FALSE) # the 5x5 identity matrix
contrasts(fff) <- contr.sum(5); contrasts(fff) # set sum contrasts
contrasts(fff, 2) <- contr.sum(5); contrasts(fff) # set 2 contrasts
# supply 2 contrasts, compute 2 more to make full set of 4.
contrasts(fff) <- contr.sum(5)[,1:2]; contrasts(fff)