Rev 51393 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/stats/man/model.matrix.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2007 R Core Development Team% Distributed under GPL 2 or later\name{model.matrix}\alias{model.matrix}\alias{model.matrix.default}\alias{model.matrix.lm}\title{Construct Design Matrices}\usage{model.matrix(object, \dots)\method{model.matrix}{default}(object, data = environment(object),contrasts.arg = NULL, xlev = NULL, \dots)}\arguments{\item{object}{an object of an appropriate class. For the defaultmethod, a model \link{formula} or a \code{\link{terms}} object.}\item{data}{a data frame created with \code{\link{model.frame}}. Ifanother sort of object, \code{model.frame} is called first.}\item{contrasts.arg}{A list, whose entries are values (numericmatrices or character strings naming functions) to be usedas replacement values for the \code{\link{contrasts}}replacement function and whose names are the names ofcolumns of \code{data} containing \code{\link{factor}}s.}\item{xlev}{to be used as argument of \code{\link{model.frame}} if\code{data} has no \code{"terms"} attribute.}\item{\dots}{further arguments passed to or from other methods.}}\description{\code{model.matrix} creates a design matrix.}\details{\code{model.matrix} creates a design matrix from the description givenin \code{terms(object)}, using the data in \code{data} which mustsupply variables with the same names as would be created by a call to\code{model.frame(object)} or, more precisely, by evaluating\code{attr(terms(object), "variables")}. If \code{data} is a dataframe, there may be other columns and the order of columns is notimportant. Any character variables are coerced to factors, with awarning. After coercion, all the variables used on the right-handside of the formula must be logical, integer, numeric or factor.If \code{contrasts.arg} is specified for a factor it overrides thedefault factor coding for that variable and any \code{"contrasts"}attribute set by \code{\link{C}} or \code{\link{contrasts}}.In an interaction term, the variable whose levels vary fastest is thefirst one to appear in the formula (and not in the term), so in\code{~ a + b + b:a} the interaction will have \code{a} varyingfastest.By convention, if the response variable also appears on theright-hand side of the formula it is dropped (with a warning),although interactions involving the term are retained.}\value{The design matrix for a regression model with the specified formulaand data.There is an attribute \code{"assign"}, an integer vector with an entryfor each column in the matrix giving the term in the formula whichgave rise to the column. Value \code{0} corresponds to the intercept(if any), and positive values to terms in the order given by the\code{term.labels} attribute of the \code{terms} structurecorresponding to \code{object}.If there are any factors in terms in the model, there is an attribute\code{"contrasts"}, a named list with an entry for each factor. Thisspecifies the contrasts that would be used in terms in which thefactor is coded by contrasts (in some terms dummy coding may be used),either as a character vector naming a function or as a numeric matrix.}\references{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.}\seealso{\code{\link{model.frame}}, \code{\link{model.extract}},\code{\link{terms}}}\examples{ff <- log(Volume) ~ log(Height) + log(Girth)utils::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-wayoptions("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}\keyword{models}