How to apply lasso logistic regression with caret and glmnet?

CodingButStillAlive picture CodingButStillAlive · May 8, 2017 · Viewed 10.3k times · Source

I am trying to repeat the following lines of code:

x.mat <- as.matrix(train.df[,predictors])
y.class <- train.df$Response

cv.lasso.fit <- cv.glmnet(x = x.mat, y = y.class, 
                          family = "binomial", alpha = 1, nfolds = 10)

... with the caret package, but it doesn't work:

trainControl <- trainControl(method = "cv",
                       number = 10,
                       # Compute Recall, Precision, F-Measure
                       summaryFunction = prSummary,
                       # prSummary needs calculated class probs
                       classProbs = T)

modelFit <- train(Response ~ . -Id, data = train.df, 
            method = "glmnet", 
            trControl = trainControl,
            metric = "F", # Optimize by F-measure
            alpha=1,
            family="binomial")

The parameter "alpha" is not recognized, and "the model fit fails in every fold".

What am I doing wrong? Help would be much appreciated. Thanks.

Answer

Patrick Balada picture Patrick Balada · May 9, 2017

Try to use tuneGrid. For example as follows:

tuneGrid=expand.grid(
              .alpha=1,
              .lambda=seq(0, 100, by = 0.1))