Set and view the contrasts associated with a factor.
a factor or a logical variable.
logical. See ‘Details’.
logical indicating if the result should be sparse
How many contrasts should be made. Defaults to one
less than the number of levels of
either a numeric matrix (or a sparse or dense matrix of a
If contrasts are not set for a factor the default functions from
options("contrasts") are used.
A logical vector
x is converted into a two-level factor with
c(FALSE, TRUE) (regardless of which levels occur in the
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. Suitable functions have a first
argument which is the character vector of levels, a named argument
contrasts (always called with
contrasts = TRUE) and
optionally a logical argument
value supplies more than
how.many contrasts, the
how.many are used. If too few are supplied, a suitable
contrast matrix is created by extending
value after ensuring
its columns are contrasts (orthogonal to the constant term) and not
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 14 15 16
utils::example(factor) fff <- ff[, drop = TRUE] # reduce to 5 levels. 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) ## using sparse contrasts: % useful, once model.matrix() works with these : ffs <- fff contrasts(ffs) <- contr.sum(5, sparse = TRUE)[, 1:2]; contrasts(ffs) stopifnot(all.equal(ffs, fff)) contrasts(ffs) <- contr.sum(5, sparse = TRUE); contrasts(ffs)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.