Using the train() function from caret in R, I'm trying to run a stepwise ANCOVA, but each level of my 9-level factor is being treated as an independent variable, so the best model might have only a subset of factor levels. Instead, I'd like the levels to be treated as a group: all in or all out.

Does train or another function in the caret package have the flexibility to achieve this?

Here's a reproducible example with the iris dataset. Here I have to set nmax = 5, because there are 3 continuous variables, plus 3 - 1 = 2 additional parameters to estimate beyond the intercept. Really though, I'd like to say nmax = 3, where the categorial variable with three levels is treated as one variable for the purposes of stepwise feature selection.

# Set seed for reproducibility

# Set up repeated k-fold cross-validation
train.control <- trainControl(method = "cv", number = 10)

# Train the model
step.model <- train(Petal.Length ~., data = iris, 
                    method = "leapSeq", 
                    tuneGrid = data.frame(nvmax = 1:5),
                    trControl = train.control)

  • $\begingroup$ you can't. caret converts to a model.matrix (i.e 0 and 1s for each level). most like you have to write your own... or I don't know if there are packages implementing that $\endgroup$
    – StupidWolf
    Commented May 15, 2020 at 21:50
  • $\begingroup$ you might have to write your own CV around cran.r-project.org/web/packages/leaps/leaps.pdf $\endgroup$
    – StupidWolf
    Commented May 15, 2020 at 21:50


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.