How can we specify a custom lambda sequence for glmnet
I am new to package glmnet
in R
and wanted to specify a function lambda
based on a guess in the published function research document glmnet.cv
. The documentation states that we can provide a decreasing sequence lambdas
as a parameter. However, there are no examples in the documentation on how to do this.
It would be very grateful if someone can suggest how to do this. Am I passing a vector of 100 odd values (the default for nlambda
) the function? What are the limits for the minimum and maximum value of this vector, if any? Also, their things should be considered in relation nvars
, nobs
etc., by specifying a vector?
Thanks in advance.
source to share
It's pretty easy, although not well explained in the original documentation;)
In the next case, I used the cox family, but you can change it depending on your needs.
my_cvglmnet_fit <- cv.glmnet(x=regression_data, y=glmnet_response, family="cox", maxit = 100000)
Then you can plot the object generated by cv.glmnet and in the graph you can easily see where the lambda is minimal. one of these dashed vertical lines is the minimum lambda and the other is 1se.
plot(my_cvglmnet_fit)
the following lines will help you see non-zero coefficients and their corresponding values:
coef(my_cvglmnet_fit, s = "lambda.min")[which(coef(my_cvglmnet_fit, s = "lambda.min") != 0)] # the non zero coefficients
colnames(regression_data)[which(coef(my_cvglmnet_fit, s = "lambda.min") != 0)] # The features that are selected
here are some links that might help:
http://web.stanford.edu/~hastie/glmnet/glmnet_alpha.html
http://blog.revolutionanalytics.com/2013/05/hastie-glmnet.html
source to share