model.matrixcreates a design (or model) matrix, e.g., by expanding factors to a set of dummary variables (depending on the contrasts) and expanding interactions similarly.
# S3 method for default model.matrix(object, data = environment(object), contrasts.arg = NULL, xlev = NULL, …)
model.frame. If another sort of object,
model.frameis called first.
datais such that
"assign", an integer vector with an entry for each column in the matrix giving the term in the formula which gave rise to the column. Value
0corresponds to the intercept (if any), and positive values to terms in the order given by the
term.labelsattribute of the
termsstructure corresponding to
object. If there are any factors in terms in the model, there is an attribute
"contrasts", a named list with an entry for each factor. This specifies the contrasts that would be used in terms in which the factor 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.
model.matrixcreates a design matrix from the description given in
terms(object), using the data in
datawhich must supply variables with the same names as would be created by a call to
model.frame(object)or, more precisely, by evaluating
attr(terms(object), "variables"). If
datais a data frame, there may be other columns and the order of columns is not important. Any character variables are coerced to factors. After coercion, all the variables used on the right-hand side of the formula must be logical, integer, numeric or factor. If
contrasts.argis specified for a factor it overrides the default factor coding for that variable and any
"contrasts"attribute set by
contrasts. In an interaction term, the variable whose levels vary fastest is the first one to appear in the formula (and not in the term), so in
~ a + b + b:athe interaction will have
avarying 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.
sparse.model.matrixfrom package Matrix">https://CRAN.R-project.org/package=Matrix for creating sparse model matrices, which may be more efficient in large dimensions.
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-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