2

Lasso uses cross-validation to determine both the number of included predictors and the degree of shrinkage to avoid over-fitting. I have used the glmnet package to do this.

    fit=glmnet(x,y)
    cvob1= cv.glmnet(x,y) 
    lambda1=cvob1$lambda.min  
    cvob2=cv.glmnet(x,y)
    lambda2=cvob2$lambda.min

There is a high possibility that lambda1 and lambda2 are different. I need to get a proper lambda.min for the next step to obtain fitted values for the sparse solution to the model:

coef(fit,s=cvob1$lambda.min)

How can I decide which lambda to use? Should I repeat the cv.glmnet() for many times? How many time should I go for?

ay__ya
  • 23
  • 1
  • 8
  • You have to run cv.glmnet many times, follow the below answer: https://stats.stackexchange.com/questions/97777/variablity-in-cv-glmnet-results/173895#173895 – forever Mar 15 '18 at 07:20

1 Answers1

0

CV is a random process, which is why you are getting different lambda.mins. But they should be close to eachother. If you want a singular, reproducible lambda, try setting a seed:

set.seed(1234)

Tim Atreides
  • 708
  • 3
  • 6