rglmnet

cv.glmnet: printed lambda.min and lambda.1se differ from their stored values


set.seed(0)
a <- runif(100)
b <- runif(100)
c <- runif(100)
d <- b + runif(100)/10
e <- a + runif(100)/10

library(glmnet)
test <- cv.glmnet(cbind(a,b,c), cbind(d,e), family = "mgaussian",
                  relax = TRUE, gamma = 0.5)

In this example, printing test directly gives something like:

test
#Measure: Mean-Squared Error 
#
#    Gamma Index   Lambda Index  Measure        SE Nonzero
#min   0.5     1 0.001617    56 0.001872 0.0001121       4
#1se   0.5     1 0.015082    32 0.001967 0.0001393       3

It appears that lambda.min should be 0.001617. However, I get a different number when I extract this value:

test$lambda.min
#[1] 0.0007682971

Anyone know why there is a difference? I'm using glmnet 4.1-4. My R version is:

> R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          4                           
minor          1.2                         
year           2021                        
month          11                          
day            01                          
svn rev        81115                       
language       R                           
version.string R version 4.1.2 (2021-11-01)
nickname       Bird Hippie       

Solution

  • With relax = TRUE in cv.glmnet, two sets of cross-validation are performed:

    1. the usual one for the relax = FALSE case;
    2. the special one for the relax = TRUE case.

    Their results are stored in different places, and there are two methods for print.

    Result for relax = FALSE:

    print.cv.glmnet(test)
    
    test$lambda.min
    

    Result for relax = TRUE (stored in test$relaxed):

    test
    #print(test)
    #glmnet:::print.cv.relaxed(test)
    
    test$relaxed$lambda.min
    

    Thank you. This is the correct answer. Could you clarify why the relax = FALSE and relax = TRUE models are different, given that I only supplied one value for gamma? Are the default gamma values c(0, 0.25, 0.5, 0.75, 1) always used when relax = FALSE?

    If relax = FALSE, whatever you supply for gamma will be ignored and there is no test$relaxed.

    When relaxed = TRUE, any gamma value in [0, 1) causes "relaxation", whereas gamma = 1 reverts to the relax = FALSE case. If you provide more than one gamma values, the 2nd set of cross-validation will also select gamma (see gamma.min and gamma.1se in test$relaxed).