Search code examples
rlogistic-regression

R stats::step function with forward direction param is not optimizing the LR model(AIC)


I've used AIC and step function for the variable selection before, but for some reason not able to get it to work.

library(ISLR)
d = data("Caravan")
train_data = Caravan[-c(1:500,]

m0 <- glm(Purchase ~ 1, data = train_data, family = "binomial")
stats::step(m0, direction = "forward", trace = 1 )

PN - I tried the stepAIC function and tried passing the scope as scope = Purchase ~., but not those change solve the issue.

The output of the step function is a model that is the same as the base model(m0).


Solution

  • This is how I solved the issue. As Onyambu mentioned in his reply, in AIC, the dot doesn't work the way it does in lm. Instead of concatenating the 84 predictors manually, I used the paste function with collapse="+".

    glmnet( formula(paste0("Y~", paste(names(Caravan)[1:85], collapse="+"))), 
                           ....)