makeFormulae {statisfactory} | R Documentation |
Make all possible formula
Description
This functions creates a list of formulae that contain all possible linear, quadratic, and two-way interaction terms from individual terms in an object of class formula
. The formulae respect marginality conditions (i.e., they will always include lower-order terms if higher-order terms are included in a formula). Note that if there are more than several terms (i.e., >=3) and interactions and/or quadratic terms are desired, then formula generation may take a long time.
Usage
makeFormulae(
formula,
intercept = TRUE,
interceptOnly = TRUE,
linearOnly = TRUE,
quad = TRUE,
ia = TRUE,
verboten = NULL,
verbotenCombos = NULL,
minTerms = NULL,
maxTerms = NULL,
returnFx = stats::as.formula,
verbose = FALSE
)
Arguments
formula |
A |
intercept |
Logical, if |
interceptOnly |
Logical, if |
linearOnly |
Logical, if |
quad |
Logical, if |
ia |
Logical, if |
verboten |
Character vector of terms that should not appear in the models. Ignored if |
verbotenCombos |
List of lists, used to specify specific combinations of terms that should not occur together. See section Details below. Ignored if |
minTerms |
Either a positive integer representing the minimum number of terms required to be in a model, or |
maxTerms |
Either a positive integer representing the maximum number of terms allowed to be in a model, or |
returnFx |
Function used to generate the class of the output objects. Sensible functions in include |
verbose |
Logical, if |
Details
The argument verbotenCombos
can be used to specify variables or terms that should not occur in the same formula. The argument verbotenCombos
is composed of a list of lists. Each sublist comprises names of two variables or terms stated as characters followed by two logical values (TRUE
/FALSE
). The second variable/term is removed from the model if the first is in the model. If the first logical value is TRUE
then the second variable/term is removed if the first variable appears alone in the formula (e.g., not in an interaction with another variable). If the first logical value is FALSE
then the second variable/term is removed if the first variable/term appears in any term (e.g., as an interaction with another term).
Examples:
-
verbotenCombos=list(list('x1', 'x2', TRUE, TRUE))
: Removesx2
ifx1
occurs in the model as a linear term. -
verbotenCombos=list(list('x1', 'x2', FALSE, TRUE))
: Removes the linear termx2
ifx1
occurrs in any term in the model. -
verbotenCombos=list(list('x1', 'x2', TRUE, FALSE))
: Removes any term withx2
if the linear termx1
occurrs in the model. -
verbotenCombos=list(list('x1', 'x2', FALSE, FALSE))
: Removes any term withx2
if any term hasx1
.
Quadratic terms and interaction terms can also be used, so:
-
verbotenCombos=list(list('x1', 'x1:x2', TRUE, TRUE))
: Removesx1:x2
ifx1
were in the model. -
verbotenCombos=list(list('x1', 'I(x2^2)', TRUE, TRUE))
: RemovesI(x2^2)
ifx1
occurs in the model.
Note that inexact matching can remove terms incorrectly if inexact matches exist between names of terms or variables. For example, if using an inexact match, then verbotenCombos(list('x1', 'x2', FALSE, FALSE))
will find any term that has an x1
(e.g., x11
) and if it exists, remove any term with an x2
(e.g., x25
). Note that reciprocally removing predictors makes little sense since, for example list(list('x1', 'x2', FALSE, FALSE), list('x2', 'x1', FALSE, FALSE))
removes all formulae with x2
if x1
appears then tries to find any models with x2
that have x1
(of which there will be none after the first set is removed).
Value
A vector of formulae.
Examples
makeFormulae(y ~ x1 + x2 + x3, maxTerms=3)
makeFormulae(y ~ x1 + x2 + x3, ia=FALSE, maxTerms=3)
verboten <- c('x1:x2', 'I(x1^2)')
makeFormulae(y ~ x1 + x2 + x3, verboten=verboten, maxTerms=3)
makeFormulae(y ~ x1 + x2 + x3, maxTerms=3)
verbotenCombos <- list(list('x1', 'x2', TRUE, TRUE))
makeFormulae(y ~ x1 + x2 + x3, verbotenCombos=verbotenCombos, maxTerms=3)