Solved – Extracting regularization path from glmnet

elastic netglmnetr

Does glmnet provide any mechanisms to extract the regularization path from a final model? I'm using Elastic Nets (and L1) to build a binomial classifier and would like to be able to get the coefficients at each step along the path (until convergence).

The elasticnet R package has this in the beta variable of the model, but it looks like glmnet only provides the final coefficients for each model. Any suggestions for how to go about pulling this out?

Best Answer

glmnet provides a full coefficient path. Try the following example out for yourself:

# code partially scavenged from the glmnet docs because I'm lazy
library(glmnet)

set.seed(123)

#binomial
x=matrix(rnorm(100*20),100,20)
g2=sample(1:2,100,replace=TRUE)
fit2=glmnet(x,g2,family="binomial")

plot(fit2) # plot coefficient paths

summary(fit2) # NB: 63 lambda values tested

dim(fit2$beta) # 20 by 63, as we'd expect

The number of lambdas tested is contingent on the random seed and properties in the data. To illustrate this, change the above example such that you are starting with set.seed(111) and you'll end up with 55 lambdas tested instead of 63. It's possible that lasso is converging extremely quickly for you and glmnet isn't even bothering to give you more than one lambda. I doubt that's the case, but it's probably possible.

If you are specifying lambda in your call to glmnet, then you won't get a coefficient path since the path is only meaningful over varying lambdas. If this doesn't answer your question, try to provide a minimal working example of your code.