drop_constants | R Documentation |
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.
drop_constants(dat,
formula,
sub = NULL)
dat |
A |
formula |
An object of class |
sub |
An optional vector specifying a subset of observations to be used in the fitting process. |
The updated formula with constant variables removed.
Formulas with and without intercepts are accommodated.
Keefe Murphy - <keefe.murphy@mu.ie>
drop_levels
, I
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.