drop_constants {MoEClust} | R Documentation |
Drop constant variables from a formula
Description
Drops constant variables from the RHS of a formula taking the data set (dat
), the formula (formula
), and an optional subset vector (sub
) as arguments.
Usage
drop_constants(dat,
formula,
sub = NULL)
Arguments
dat |
A |
formula |
An object of class |
sub |
An optional vector specifying a subset of observations to be used in the fitting process. |
Value
The updated formula with constant variables removed.
Note
Formulas with and without intercepts are accommodated.
Author(s)
Keefe Murphy - <keefe.murphy@mu.ie>
See Also
Examples
data(ais)
hema <- as.matrix(ais[,3:7])
sex <- ais$sex
BMI <- ais$BMI
# Set up a no-intercept regression formula with constant column 'sex'
form1 <- as.formula(hema ~ sex + BMI + I(BMI^2) - 1)
sub <- ais$sex == "male"
# Try fitting a linear model
mod1 <- try(lm(form1, data=ais, subset=sub), silent=TRUE)
inherits(mod1, "try-error") # TRUE
# Remove redundant variables from formula & try again
form2 <- drop_constants(ais, form1, sub)
mod2 <- try(lm(form2, data=ais, subset=sub), silent=TRUE)
inherits(mod2, "try-error") # FALSE