Search code examples
rglmnetlasso-regression

R: how to add L1 norm line to plot from glmnet


I'm doing lasso regression, and I want to choose some beta coefficients that best explain my model by using Leave one out cross validation. Here is my code:

library(glmnet)
set.seed(19875)
n=100
p=500
real_p=15
x=matrix(rnorm(n*p), nrow=n, ncol=p)
y=as.matrix(apply(x[, 1:real_p], 1, sum) + rnorm(n))
lasso=glmnet(x,y,alpha = 1)
plot(lasso)
#computing loocv 
cvlassofit<-cv.glmnet(x,y, nfolds =n, grouped = FALSE )
plot(cvlassofit)

The first plot generates the beta coefficient paths: enter image description here

Then I want to add a vertical line that chooses the best coefficients that have small mean square error. The plot should then look like this: enter image description here

In the code part where I do CV I get the best lambda that has the smallest mse(mean square error). Here is the plot:

enter image description here

Now, can I somehow based on the lambda get a value for the L1 norm, so that I could add a vertical line to the first plot? Or instead of log(lambda) in the last plot, could I do a L1 norm?


Solution

  • Now, can I somehow based on the lambda get a value for the L1 norm, so that I could add a vertical line to the first plot? Or instead of log(lambda) in the last plot, could I do a L1 norm?

    You can do it as follows:

    lambda_min <- cvlassofit$lambda.min
    estimates <- as.vector(coef(lasso, s = lambda_min, exact = TRUE))
    norm. <- sum(abs(estimates))
    plot(lasso, xlim = range(0, norm., as.vector(lasso$beta)))
    abline(v = norm., col = "red")
    

    Here is the result:

    plot with line of CV L1 norm