terms.object
. The function is
primarily used as an internal call in other models functions.
model.matrix(object, ...) model.matrix.default(terms.object, data, contrasts, xlevels=NULL)
lm
or class
bdLm
.
lm
or class
bdLm
.
model.frame
.
In general, can be any data frame or source for the data, or can be
missing.
If it is not the model frame, it will be turned into one by a call
to
model.frame
.
It then supplies the data from which columns of the matrix are computed.
In standard use of
model.matrix
,
the variables will be numeric vectors, factors or ordered factors,
or numeric matrices.
The computations will also handle character vectors, logical vectors,
bdCharacter
vectors, or
bdLogical
vectors (which are
turned into factors) or subsidiary data frames or
bdFrames
(provided their columns are numeric).
"contrasts"
attribute of the model matrix, and hence as the
"contrasts"
component of fitted models returned by
lm()
and
its descendants.
"data"
and values
corresponding to levels to retain in the resulting model frame. Note that levels may not
dropped if they occur in
"data"
. However, if some levels are not
contained in
"data"
but it is desired to retain those levels in the resulting
model frame,
"xlevels"
allows you to do so.
"model.matrix"
which inherits from
"matrix"
.
This is a matrix of predictor variables, including contrasts for all factors
and ordered factors in the terms object.
If the model includes an intercept, the first column will be the
vector of
1
s.
The matrix has several special attributes:
assign
attribute operates differently from the S-PLUS
assign
attribute.
model.extract
.
0
and
"(Intercept)"
if there was an intercept).
Note that the names attribute of the
"assign"
attribute is also equal
to the term labels.
These two attributes basically exist to save functions the trouble of
inferring the corresponding information from a terms object.
They are somewhat vestigial---they probably won't go away,
but the use of the term labels, in particular, is less common than
to rely on the names of the
"assign"
attribute.
bdCharacter
s.
Any contrast matrices used will be returned in an element of the list
with the same name as the corresponding variable.
See
lm.object
for further details.
Factors, including ordered factors, are turned into columns of numeric variables
using contrasts or dummy variables according to the instructions coded
in the terms objects
"factors"
attribute.
Particular contrasts are chosen using the
contrasts
argument
as supplied (typically as passed down from
lm()
, and so on),
from the
"contrasts"
attribute of
the factor, if any,
or from the default choice of contrast functions.
In the absence of this attribute, the two character strings in
options("contrasts")
define the
choice of contrast function for factors and ordered factors.
Note that the same variable may be used both with contrasts and without.
Interaction terms are formed by computing the various main effects
and then taking all products of the corresponding columns
(in principle---in practice the computations look back at previously
computed terms an an attempt to avoid re-computation).
See
contrasts
and
C
for details of specifying contrast functions
as arguments.
The column labels are constructed by the following definition.
Numeric variables inherit the corresponding term label.
Numeric matrices produce column labels that concatenate the term
label with the column labels of the matrix, if any, or with
"1"
,
"2"
, etc.
Main effects for factors or ordered factors use the column label
concatenated with the column labels of the contrast matrix,
again using
"1"
,
"2"
, etc. as default.
For both cases, the term label is used alone if there is only one
column or one contrast.
This is primarily a support routine, called by
lm
and by other
model-fitting functions that call or derive from
lm
, such as
aov
,
glm
, and
gam
.
Note that the model-fitting functions
loess
and
tree
do not
use
model.matrix
, chiefly because they do not use contrasts to
handle factors.