\Header{model.matrix}{Construct Design Matrices}
\methalias{model.matrix.default}{model.matrix}
\methalias{model.matrix.lm}{model.matrix}
\keyword{models}{model.matrix}
\begin{Description}\relax
\code{model.matrix} creates a design matrix.
\end{Description}
\begin{Usage}
\begin{verbatim}
model.matrix(object, ...)

## Default S3 method:
model.matrix(object, data = environment(object),
             contrasts.arg = NULL, xlev = NULL, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{object}] an object of an appropriate class. For the default
method, a model formula or terms object.
\item[\code{data}] a data frame created with \code{\Link{model.frame}}.
\item[\code{contrasts.arg}] A list, whose entries are contrasts suitable for
input to the \code{\Link{contrasts}} replacement function and
whose names are the names of columns of \code{data} containing
\code{\Link{factor}}s.
\item[\code{xlev}] to be used as argument of \code{\Link{model.frame}} if
\code{data} has no \code{"terms"} attribute.
\item[\code{...}] further arguments passed to or from other methods.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
\code{model.matrix} creates a design matrix from the description given
in \code{terms(formula)}, using the data in \code{data} which must
contain columns with the same names as would be created by a call to
\code{model.frame(formula)} or, more precisely, by evaluating
\code{attr(terms(formula), "variables")}.  There may be other columns
and the order is not important.  If \code{contrasts} is specified it
overrides the default factor coding for that variable.

In interactions, the variable whose levels vary fastest is the first
one to appear in the formula (and not in the term), so in \code{\textasciitilde{} a +
    b + b:a} the interaction will have \code{a} varying fastest.

By convention, if the response variable also appears on the
right-hand side of the formula it is dropped (with a warning),
although interactions involving the term are retained.
\end{Details}
\begin{Value}
The design matrix for a regression model with the specified formula
and data.
\end{Value}
\begin{References}\relax
Chambers, J. M. (1992)
\emph{Data for models.}
Chapter 3 of \emph{Statistical Models in S}
eds J. M. Chambers and T. J. Hastie, Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\Link{model.frame}}, \code{\Link{model.extract}},
\code{\Link{terms}}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
data(trees)
ff <- log(Volume) ~ log(Height) + log(Girth)
str(m <- model.frame(ff, trees))
mat <- model.matrix(ff, m)

dd <- data.frame(a = gl(3,4), b = gl(4,1,12))# balanced 2-way
options("contrasts")
model.matrix(~ a + b, dd)
model.matrix(~ a + b, dd, contrasts = list(a="contr.sum"))
model.matrix(~ a + b, dd, contrasts = list(a="contr.sum", b="contr.poly"))
m.orth <- model.matrix(~a+b, dd, contrasts = list(a="contr.helmert"))
crossprod(m.orth)# m.orth is  ALMOST  orthogonal
\end{ExampleCode}
\end{Examples}

