rglmnet

glmnet lasso produces different results for the same lambda


I found R function glmnet::glmnet() produces different results for the same value in a different lambda sequence:

  # Generate a small dataset at random.
  set.seed(123)
  X = matrix(runif(12), ncol = 3)
  y = runif(nrow(X))
  
  
  lda = sort(c((runif(1) + 1) * 0.001, 0.001, 0.001 * (1 - runif(1))), decreasing = T)
  theModel = glmnet::glmnet(X, y, lambda = lda, intercept = T, standardize = T)
  coef(theModel)[, 2] # Show the coefficients corresponding to lambda = 0.001
  # (Intercept)          V1          V2          V3 
  # 0.8794007     1.6709897  -0.9141978  -1.5334346 
  
  
  lda = sort(c((runif(1) + 1) * 0.001, 0.001, 0.001 * (1 - runif(1))), decreasing = T)
  theModel = glmnet::glmnet(X, y, lambda = lda, intercept = T, standardize = T)
  coef(theModel)[, 2] # Show the coefficients corresponding to lambda = 0.001
  # (Intercept)          V1          V2          V3 
  # 0.8793756     1.6708981  -0.9141037  -1.5333721 

Why is this happening and how to avoid it?

Thank you!


Solution

  • lambda sequence will be normalized internally.