| 1 |
\name{contr} |
| 2 |
\alias{contr.treatment} |
| 3 |
\alias{contr.sum} |
| 4 |
\alias{contr} |
| 5 |
\alias{contrasts} |
| 6 |
\alias{contrasts,item-method} |
| 7 |
\alias{contrasts,ANY-method} |
| 8 |
\alias{contrasts<-} |
| 9 |
\alias{contrasts<-,item-method} |
| 10 |
\alias{contrasts<-,ANY-method} |
| 11 |
\title{Convenience Methods for Setting Contrasts} |
| 12 |
\description{ |
| 13 |
This package provides modified versions of |
| 14 |
\code{\link[stats:contrast]{contr.treatment}} and |
| 15 |
\code{\link[stats:contrast]{contr.sum}}. \code{contr.sum} |
| 16 |
gains an optional \code{base} argument, analog to the |
| 17 |
one of \code{contr.treatment}, furthermore, |
| 18 |
the \code{base} argument may be the name of a |
| 19 |
factor level. |
| 20 |
|
| 21 |
\code{contr} returns a function that calls either |
| 22 |
\code{contr.treatment}, \code{contr.sum}, etc., |
| 23 |
according to the value given to its first argument. |
| 24 |
|
| 25 |
The \code{contrasts} method for \code{"item"} objects |
| 26 |
returns a contrast matrix or a function to produce |
| 27 |
a contrast matrix for the factor into which |
| 28 |
the item would be coerced via \code{as.factor} or \code{as.ordered}. |
| 29 |
This matrix or function can be specified by |
| 30 |
using \code{contrasts(x)<-value} |
| 31 |
} |
| 32 |
\usage{ |
| 33 |
contr(type,\dots) |
| 34 |
contr.treatment(n, base=1,contrasts=TRUE) |
| 35 |
contr.sum(n,base=NULL,contrasts=TRUE) |
| 36 |
\S4method{contrasts}{item}(x,contrasts=TRUE,\dots) |
| 37 |
\S4method{contrasts}{item}(x,how.many) <- value |
| 38 |
# These methods are defined implicitely by making 'contrasts' generic. |
| 39 |
\S4method{contrasts}{ANY}(x,contrasts=TRUE,\dots) |
| 40 |
\S4method{contrasts}{ANY}(x,how.many) <- value |
| 41 |
} |
| 42 |
\arguments{ |
| 43 |
\item{type}{a character vector, specifying the type of the contrasts. |
| 44 |
This argument should have a value such that, if e.g. \code{type="something"}, |
| 45 |
then there is a function \code{contr.something} that produces |
| 46 |
a contrast matrix. |
| 47 |
} |
| 48 |
\item{\dots}{further arguments, passed to \code{contr.treatment}, etc. } |
| 49 |
\item{n}{a number of factor levels or a vector of factor levels names, see e.g. \code{\link[stats:contrast]{contr.treatment}}.} |
| 50 |
\item{base}{a number of a factor level or the names of a factor level, |
| 51 |
which specifies the baseline category, |
| 52 |
see e.g. \code{\link[stats:contrast]{contr.treatment}} or NULL. |
| 53 |
} |
| 54 |
\item{contrasts}{a logical value, see \code{\link[stats]{contrasts}}} |
| 55 |
\item{how.many}{the number of contrasts to generate, see \code{\link[stats]{contrasts}}} |
| 56 |
\item{x}{a factor or an object of class "item"} |
| 57 |
\item{value}{a matrix, a function or the name of a function} |
| 58 |
} |
| 59 |
\value{ |
| 60 |
\code{contr} returns a funtion that calls one of \code{contr.treatment}, |
| 61 |
\code{contr.sum,\dots}. |
| 62 |
\code{contr.treatment} and \code{contr.sum} return contrast matrices. |
| 63 |
\code{contrasts(x)} returns the "contrasts" attribute of an |
| 64 |
object, which may be a function name, a function, a contrast matrix or NULL. |
| 65 |
} |
| 66 |
\examples{ |
| 67 |
ctr.t <- contr("treatment",base="c") |
| 68 |
ctr.t |
| 69 |
ctr.s <- contr("sum",base="c") |
| 70 |
ctr.h <- contr("helmert") |
| 71 |
ctr.t(letters[1:7]) |
| 72 |
ctr.s(letters[1:7]) |
| 73 |
ctr.h(letters[1:7]) |
| 74 |
|
| 75 |
x <- factor(rep(letters[1:5],3)) |
| 76 |
contrasts(x) |
| 77 |
x <- as.item(x) |
| 78 |
contrasts(x) |
| 79 |
contrasts(x) <- contr.sum(letters[1:5],base="c") |
| 80 |
contrasts(x) |
| 81 |
missing.values(x) <- 5 |
| 82 |
contrasts(x) |
| 83 |
contrasts(as.factor(x)) |
| 84 |
|
| 85 |
# Obviously setting missing values after specifying |
| 86 |
# contrast matrix breaks the contrasts. |
| 87 |
# Using the 'contr' function, however, prevents this: |
| 88 |
|
| 89 |
missing.values(x) <- NULL |
| 90 |
contrasts(x) <- contr("sum",base="c") |
| 91 |
contrasts(x) |
| 92 |
missing.values(x) <- 5 |
| 93 |
contrasts(x) |
| 94 |
contrasts(as.factor(x)) |
| 95 |
} |