Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply lasso logistic regression with caret and glmnet?

Tags:

r

r-caret

glmnet

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.

like image 876
CodingButStillAlive Avatar asked Mar 09 '23 03:03

CodingButStillAlive


1 Answers

Try to use tuneGrid. For example as follows:

tuneGrid=expand.grid(
              .alpha=1,
              .lambda=seq(0, 100, by = 0.1))
like image 175
Patrick Balada Avatar answered Mar 15 '23 08:03

Patrick Balada