Multiple minimal models in R forward stepwise regression

Solution 1:

To specify several minimal models in stepwise forward regression, create the smallest formulas with, for instance, lapply and then loop through them.
In the example below, built-in data set mtcars is used to fit several models having mpg as response, one per each of the 3 last variables in the data set.

data(mtcars)

biggest <- mpg ~ .

sml <- names(mtcars)[9:11]
small_list <- lapply(sml, function(x) {
  fmla <- paste("mpg", x, sep = "~")
  as.formula(fmla)
})
names(small_list) <- sml

fit <- lm(mpg ~ ., mtcars)
fit_list <- lapply(small_list, function(smallest){
  step(fit, scope = list(lower = smallest, upper = biggest))  
})

Now select with AIC as criterion

min_aic <- sapply(fit_list, AIC)
min_aic
#      am     gear     carb 
#154.1194 155.9852 154.5631

fit_list[[which.min(min_aic)]]