Search code examples
rdata-sciencecross-validationlasso-regression

How to get the coefficents of a Cross Validated Lasso for a specific lambda (Not the “1se” or “min” lambda)


I run a CV Lasso with the cv.gamlr function in R. I can get the coefficients for the lambdas that correspond to the “1se” or “min” criterion.

set.seed(123)
lasso<-cv.gamlr(x = X, y = Y, family ='binomial')
coef(lasso,select = "1se")
coef(lasso,select = "min")

But what if I want to obtain the coefficients for a specific lambda, stored in the lasso$gamlr$lambda vector? Is it possible to obtain them? For example, to get the coefficients for the first lambda in the model... Something like this:

lambda_100<-  lasso$gamlr$lambda[100]  
coef(lasso,select = lambda_100)

Of course, this sends the following error:

Error in match.arg(select) : 'arg' must be NULL or a character vector

Thanks :)


Solution

  • The coefficients are stored under lasso$gamlr$beta, in your example, you can access them like this:

    library(gamlr)
    x = matrix(runif(500),ncol=5)
    y = rnorm(100)
    cvfit <- cv.gamlr(x, y, gamma=1)
    
    dim(cvfit$gamlr$beta)
    [1]   5 100
    
    length(cvfit$gamlr$lambda)
    [1] 100
    
    cvfit$gamlr$lambda[100]
        seg100 
    0.00125315 
        
    cvfit$gamlr$beta[,drop=FALSE,100]
    5 x 1 sparse Matrix of class "dgCMatrix"
           seg100
    1  0.12960060
    2 -0.16406246
    3 -0.46566731
    4  0.08197053
    5 -0.54170494
    

    Or if you prefer it in a vector:

    cvfit$gamlr$beta[,100]
             1           2           3           4           5 
     0.12960060 -0.16406246 -0.46566731  0.08197053 -0.54170494