Getting different results for LASSO using cv.glmnet and caret package in R

by student_R123   Last Updated September 12, 2019 00:19 AM

I saw few posts here regarding my issue. I went through with those but still i couldnt figure out what i did wrong.

Any help will be highly appreciated.

I fitted a LASSO logistic regression model using glmnet package and caret package (which is a wrapper for glmnet package) and i am getting different results .

Here is my code :

Using glment package ,

require(ISLR)
require(glmnet)
y <- Smarket$Direction
x <- model.matrix(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Volume, Smarket)[, -1]

lasso.mod <- cv.glmnet(x, y, alpha=1,family="binomial",nfolds = 5, type.measure="class",
                       lambda = seq(0.001,0.1,by = 0.001))

> lasso.mod$lambda.min
[1] 0.1

using caret package ,

require(caret)
set.seed(123)
fitControl1 <- trainControl(method = "cv",number = 5,savePredictions = T,returnResamp="all")
modelFitlassocvintm1 <- train(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Volume, data=Smarket, 
                              method = "glmnet", 
                              trControl = fitControl1,

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

                              family="binomial")

modelFitlassocvintm1$bestTune

   alpha lambda
26     1  0.026

as you can see, based on 5-fold cross validation i am getting different values for the tuning parameter lambda. can any one help me to figure out what did i do wrong ?

Thank you



Related Questions





Cross validating lasso regression in R

Updated June 09, 2015 12:08 PM