model.frame.Formula {Formula} | R Documentation |
Computation of model frames, model matrices, and model responses for
extended formulas of class Formula
.
## S3 method for class 'Formula': model.frame(formula, data = NULL, ..., lhs = NULL, rhs = NULL) ## S3 method for class 'Formula': model.matrix(object, data = environment(object), ..., lhs = NULL, rhs = 1) ## S3 method for class 'Formula': terms(x, ..., lhs = NULL, rhs = NULL) model.part(object, ...) ## S3 method for class 'Formula': model.part(object, data, lhs = 0, rhs = 0, drop = FALSE, ...)
formula, object, x |
an object of class Formula . |
data |
a data.frame, list or environment containing the variables in
formula . For model.part it needs to be the model.frame . |
lhs, rhs |
indexes specifying which elements of the left- and
right-hand side, respectively, should be employed. NULL
corresponds to all parts, 0 to none. At least one lhs or
one rhs has to be specified. |
drop |
logical. Should the data.frame be dropped for single
column data frames? |
... |
further arguments passed to the respective
formula methods. |
All three model computations leverage the corresponding standard methods. Additionally, they allow specification of the part(s) of the left- and right-hand side (LHS and RHS) that should be included in the computation.
The idea underlying all three model computations is to extract a suitable
formula
from the more general Formula
and then calling
the standard model.frame
, model.matrix
,
and terms
methods.
More specifically, if the Formula
has multiple parts on the RHS,
they are collapsed, essentially replacing |
by +
. If there
is only a single response on the LHS, then it is kept on the LHS.
Otherwise all parts of the formula are collapsed on the RHS (because formula
objects can not have multiple responses). Hence, for multi-response Formula
objects, the (non-generic) model.response
does
not give the correct results. To avoid confusion a new generic model.part
with suitable formula
method is provided which can always
be used instead of model.response
. Note, however, that it has a different
syntax: It requires the Formula
object in addition to the readily
processed model.frame
supplied in data
(and optionally the lhs
). Also, it returns either a data.frame
with
multiple columns or a single column (dropping the data.frame
property)
depending on whether multiple responses are employed or not.
Formula
, model.frame
,
model.matrix
, terms
,
model.response
## artificial example data set.seed(1090) dat <- as.data.frame(matrix(round(runif(21), digits = 2), ncol = 7)) colnames(dat) <- c("y1", "y2", "y3", "x1", "x2", "x3", "x4") for(i in c(2, 6:7)) dat[[i]] <- factor(dat[[i]] > 0.5, labels = c("a", "b")) dat$y2[1] <- NA dat ###################################### ## single response and two-part RHS ## ###################################### ## single response with two-part RHS F1 <- Formula(log(y1) ~ x1 + x2 | I(x1^2)) length(F1) ## set up model frame mf1 <- model.frame(F1, data = dat) mf1 ## extract single response model.part(F1, data = mf1, lhs = 1, drop = TRUE) model.response(mf1) ## model.response() works as usual ## extract model matrices model.matrix(F1, data = mf1, rhs = 1) model.matrix(F1, data = mf1, rhs = 2) ######################################### ## multiple responses and multiple RHS ## ######################################### ## set up Formula F2 <- Formula(y1 + y2 | log(y3) ~ x1 + I(x2^2) | 0 + log(x1) | x3 / x4) length(F2) ## set up full model frame mf2 <- model.frame(F2, data = dat) mf2 ## extract responses model.part(F2, data = mf2, lhs = 1) model.part(F2, data = mf2, lhs = 2) ## model.response(mf2) does not give correct results! ## extract model matrices model.matrix(F2, data = mf2, rhs = 1) model.matrix(F2, data = mf2, rhs = 2) model.matrix(F2, data = mf2, rhs = 3) ########## ## misc ## ########## ## Formulas with '.' F3 <- Formula(y1 | y2 ~ .) mf3 <- model.frame(F3, data = dat) ## without y1 or y2 model.matrix(F3, data = mf3) ## without y1 but with y2 model.matrix(F3, data = mf3, lhs = 1) ## without y2 but with y1 model.matrix(F3, data = mf3, lhs = 2)