# sparse.model.matrix

##### Construct Sparse Design / Model Matrices

Construct a Model or

- Keywords
- models

##### Usage

```
sparse.model.matrix(object, data = environment(object),
contrasts.arg = NULL, xlev = NULL, transpose = FALSE,
drop.unused.levels = FALSE, row.names=TRUE, ...)
```

##### Arguments

- object
- an object of an appropriate class. For the default method, a model formula or terms object.
- data
- a data frame created with
`model.frame`

. If another sort of object,`model.frame`

is called first. - contrasts.arg
- A list, whose entries are contrasts suitable for
input to the
`contrasts`

replacement function and whose names are the names of columns of`data`

containing - xlev
- to be used as argument of
`model.frame`

if`data`

has no`"terms"`

attribute. - transpose
- logical indicating if the
*transpose*should be returned; if the transposed is used anyway, setting`transpose = TRUE`

is more efficient. - drop.unused.levels
- should factors have unused levels dropped?
(This used to be true,
*implicitly*in versions ofMatrix up to July 2010; the default has been changed for compatibility with R's standard (dense) - row.names
- logical indicating if row names should be used.
- ...
- further arguments passed to or from other methods.

##### Value

- a sparse matrix extending

.CsparseMatrix Note that

`model.Matrix(*, sparse=TRUE)`

from packageMatrixModels may be often be preferable to`sparse.model.matrix()`

nowadays, as`model.Matrix()`

returns`modelMatrix`

objects with additional slots`assign`

and`contrasts`

which relate back to the variables used. As`model.Matrix()`

used to be part of theMatrix package for a few months in summer 2010, it is currently provided as a stub which loads theMatrixModels package and uses its`model.Matrix`

.

##### See Also

`model.matrix`

in standard R's package `model.Matrix`

which calls
`sparse.model.matrix`

or `model.matrix`

depending on its
`sparse`

argument may be preferred to `sparse.model.matrix`

.

`as(f, "sparseMatrix")`

(see `coerce(from = "factor", ..)`

in the class doc *transposed* sparse model matrix for a single factor `f`

(and *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)))
```

*Documentation reproduced from package Matrix, version 1.0-4, License: GPL (>= 2)*