2
\$\begingroup\$

I have implemented a quite long program in R and I have noticed that one block, concearning a backward model selection, is too slow and I would like to receive some suggestions in order to speed up all the program. Here is the code:

library(MASS)
df.Cars <- na.omit(Cars93[-57, -c(1,2,4,6,27)])
rownames(df.Cars) <- 1:82
for(j in c(1, 5, 6, 12, 22)) { df.Cars[, j] <- as.numeric(df.Cars[, j]) - 1}
df.Cars$Cylinders <- as.numeric(as.character(df.Cars$Cylinders))
df.Cars <- data.frame(df.Cars, RPM2 = df.Cars$RPM^2)

pos <- sample(1:82, 82)
list_folds <- c(list(pos[1:9]), list(pos[10:18]), lapply(0:7, function(x) pos[19:26 + x*8]))
names(list_folds) <- paste0("fold_", 1:10)

best_models <- list()

for(j in 1:10) {     

best_models[[j]] <- as.list(rep(NA,22))

}
The slow part begins now:
for(j in 1:length(list_folds)) {

   mydf <- df.Cars[unlist(list_folds[-j]),]

   best_models[[j]][[22]] <- glm(Origin ~., "binomial", mydf)

   for(i in 21:1) {

      matr <- colnames(model.matrix(best_models[[j]][[i+1]]))[-1]

      mod0 <- lapply(matr, function(x) {

          formul <- eval(parse(text=paste(".~. -", x)))

          update(best_models[[j]][[i+1]], formul)

          })  

DEVmin <- which.min(sapply(mod0, deviance))

best_models[[j]][[i]] <- mod0[[DEVmin]]   

    }

}
\$\endgroup\$
  • \$\begingroup\$ Have you looked at step? e.g. M <- step(glm(Origin ~ ., "binomial", df.Cars), dir='backward'). Although you might need to drop some of those predictors to achieve convergence, e.g. M <- step(glm(Origin ~ ., "binomial", df.Cars[, -c(17, 19, 23)]), dir='backward'). You can look at the steps taken with M$anova. \$\endgroup\$ – jbaums Jun 25 '14 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.