I am fitting an Elastic Net model to a very wide matrix. I like the pre-processing functions in caret but I have found that it takes about 5 times longer to train that if I just use glmnet. Why?
# Sample data.
set.seed(123)
trainX <- replicate(1000, rnorm(30))
colnames(trainX) <- paste0("var", 1:1000)
trainOutcome <- gl(2, 15)
# Train model using glmnet.
alpha_to_test <- seq(0, 1, 0.1)
system.time({
sapply(alpha_to_test, function(a) {
fit <- glmnet::cv.glmnet(
x = trainX, y = as.numeric(trainOutcome),
alpha = a,
nfolds = nrow(trainX) # LOOCV
)
})
}) # 7.272s
# Train model using caret using same search space.
fit <- glmnet::cv.glmnet(trainX, y = as.numeric(trainOutcome))
lambda_to_test <- fit$lambda
grid <- expand.grid(alpha = alpha_to_test, lambda = lambda_to_test)
system.time({
fit <- train(
x = trainX, y = trainOutcome,
method = "glmnet",
trControl = trainControl(method = "loocv", selectionFunction = "oneSE"),
tuneGrid = grid
)
}) # 45.316s