\Header{contrast}{Contrast Matrices}
\alias{contr.helmert}{contrast}
\alias{contr.poly}{contrast}
\alias{contr.sum}{contrast}
\alias{contr.treatment}{contrast}
\keyword{design}{contrast}
\keyword{regression}{contrast}
\keyword{array}{contrast}
\begin{Description}\relax
Return a matrix of contrasts.
\end{Description}
\begin{Usage}
\begin{verbatim}
contr.helmert(n, contrasts = TRUE)
contr.poly(n, scores = 1:n, contrasts = TRUE)
contr.sum(n, contrasts = TRUE)
contr.treatment(n, base = 1, contrasts = TRUE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{n}] a vector of levels for a factor, or the number of levels.
\item[\code{contrasts}] a logical indicating whether contrasts should be
computed.
\item[\code{scores}] the set of values over which orthogonal polynomials are
to be computed.
\item[\code{base}] an integer specifying which group is considered the
baseline group. Ignored if \code{contrasts} is \code{FALSE}.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
These functions are used for creating contrast matrices for use in
fitting analysis of variance and regression models.  The columns of
the resulting matrices contain contrasts which can be used for coding
a factor with \code{n} levels.  The returned value contains the
computed contrasts.  If the argument \code{contrasts} is \code{FALSE}
then a square indicator matrix is returned.

\code{cont.helmert} returns Helmert contrasts, which contrast the
second level with the first, the third with the average of the first
two, and so on.  \code{contr.poly} returns contrasts based on
orthogonal polynomials. \code{contr.sum} uses \dQuote{sum to zero
contrasts}.

\code{contr.treatment} contrasts each level with the baseline level
(specified by \code{base}): the baseline level is omitted.  Note that
this does not produce \dQuote{contrasts} as defined in the standard
theory for linear models as they are not orthogonal to the constant.
\end{Details}
\begin{Value}
A matrix with \code{n} rows and \code{k} columns, with \code{k=n-1} if
\code{contrasts} is \code{TRUE} and \code{k=n} if \code{contrasts} is
\code{FALSE}.
\end{Value}
\begin{References}\relax
Chambers, J. M. and Hastie, T. J. (1992)
\emph{Statistical models.}
Chapter 2 of \emph{Statistical Models in S}
eds J. M. Chambers and T. J. Hastie, Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\Link{contrasts}},
\code{\Link{C}},
and
\code{\Link{aov}},
\code{\Link{glm}},
\code{\Link{lm}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
(cH <- contr.helmert(4))
apply(cH, 2,sum) # column sums are 0!
crossprod(cH) # diagonal -- columns are orthogonal
contr.helmert(4, contrasts = FALSE) # just the 4 x 4 identity matrix

(cT <- contr.treatment(5))
all(crossprod(cT) == diag(4)) # TRUE: even orthonormal

(cP <- contr.poly(3)) # Linear and Quadratic
zapsmall(crossprod(cP), dig=15) # orthonormal up to fuzz
\end{ExampleCode}
\end{Examples}

