1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
\name{sparse.model.matrix}
\Rdversion{1.1}
\alias{sparse.model.matrix}
\alias{model.Matrix}% for now
\title{Construct Sparse Design / Model Matrices}
\description{
Construct a Model or \dQuote{Design} Matrix
}
\usage{
sparse.model.matrix(object, data = environment(object),
contrasts.arg = NULL, xlev = NULL, transpose = FALSE, ...)
}
\arguments{
\item{object}{an object of an appropriate class. For the default
method, a model formula or terms object.}
\item{data}{a data frame created with \code{\link{model.frame}}. If
another sort of object, \code{model.frame} is called first.}
\item{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{xlev}{to be used as argument of \code{\link{model.frame}} if
\code{data} has no \code{"terms"} attribute.}
\item{transpose}{logical indicating if the \emph{transpose} should be
returned; if the transposed is used anyway, setting \code{transpose = TRUE}
is more efficient.}
\item{\dots}{further arguments passed to or from other methods.}
}
\value{
a sparse matrix extending \code{\linkS4class{CsparseMatrix}}.
Note that \code{\link[MatrixModels]{model.Matrix}(*, sparse=TRUE)}
from package \pkg{MatrixModels} may be often be preferable to
\code{sparse.model.matrix()} nowadays, as \code{model.Matrix()}
returns \code{\link[MatrixModels:modelMatrix-class]{modelMatrix}}
objects with additional slots \code{assign} and \code{contrasts} which
relate back to the variables used.
%% FIXME: Drop this eventually (say in early Fall, 2011):
As \code{model.Matrix()} used to be part of the \pkg{Matrix} package
for a few months in summer 2010, it is currently provided as a stub
which loads the \pkg{MatrixModels} package and uses its \code{model.Matrix}.
}
\author{Doug Bates and Martin Maechler, with initial suggestions from Tim
Hesterberg.
}
\seealso{
\code{\link{model.matrix}} in standard \R's package \pkg{stats}.\cr
\code{\link[MatrixModels]{model.Matrix}} which calls
\code{sparse.model.matrix} or \code{model.matrix} depending on its
\code{sparse} argument may be preferred to \code{sparse.model.matrix}.
\code{as(f, "sparseMatrix")} (see \code{coerce(from = "factor", ..)}
in the class doc \linkS4class{sparseMatrix}) produces the
\emph{transposed} sparse model matrix for a single factor \code{f}
(and \emph{no} contrasts).
}
\examples{
dd <- data.frame(a = gl(3,4), b = gl(4,1,12))# balanced 2-way
options("contrasts") # the default: "contr.treatment"
sparse.model.matrix(~ a + b, dd)
sparse.model.matrix(~ -1+ a + b, dd)# no intercept --> even sparser
sparse.model.matrix(~ a + b, dd, contrasts = list(a="contr.sum"))
sparse.model.matrix(~ a + b, dd, contrasts = list(b="contr.SAS"))
## Sparse method is equivalent to the traditional one :
stopifnot(all(sparse.model.matrix(~ a + b, dd) ==
Matrix(model.matrix(~ a + b, dd), sparse=TRUE)),
all(sparse.model.matrix(~ 0+ a + b, dd) ==
Matrix(model.matrix(~ 0+ a + b, dd), sparse=TRUE)))
%% many more and tougher examples ---> ../tests/spModel.matrix.R
}
\keyword{models}
|