0
votes

I have found my best lambda(for lasso) through 10 fold cross validation on my training data set and validated with testing dataset. Now I would like to use my best lambda to fit a model on the whole dataset(using both training and test). How do I specify the chosen lambda to fit my Final model. Can I use the below code?

Final_model<-glmnet(x,y,family = "binomial",alpha = 1,lambda=lambda.min)

Please help, Thanks in advance.

1

1 Answers

0
votes

Yes you can. Here an example of this code working with a lambda.min set to 1:

library(glmnet)
x=matrix(rnorm(100*20),100,20)
y=rep(0:1,50)
lambda.min=1
Final_model<-glmnet(x,y, family="binomial",alpha = 1,lambda=lambda.min)
Final_model

Call:  glmnet(x = x, y = y, family = "binomial", alpha = 1, lambda = lambda.min) 

     Df       %Dev Lambda
[1,]  0 -1.121e-15      1

Update

If you have warning messages during the exacution, this could be related to the use of a single lambda value, this is deprecated in the documentation ?glmnet:

lambda: A user supplied lambda sequence. Typical usage is to have the program compute its own lambda sequence based on nlambda and lambda.min.ratio. Supplying a value of lambda overrides this. WARNING: use with care. Avoid supplying a single value for lambda (for predictions after CV use predict() instead). Supply instead a decreasing sequence of lambda values. glmnet relies on its warms starts for speed, and its often faster to fit a whole path than compute a single fit.

Related questions here and here