Search code examples
rr-caretglmnet

Make cv.glmnet select something between lambda.min and lambda.1se


I'm training an Elastic Net model and am finding that lambda.1se is much higher than lambda.min, often the maximum lambda tested with zero features selected. I'm guessing that this is because my standard deviations are really large.

Is there a way to make cv.glmnet select a value somewhere between lambda.1se and lambda.min?

x = matrix(rnorm(100 * 20), 100, 20) # not the actual data
y = gl(2, 50)

fit <- cv.glmnet(
  x = x, y = y,
  family = "binomial",
  nfolds = nrow(x), grouped = F,
  standardize = T,
  alpha = 0.2
)

Solution

  • Thanks @Nutle, I was able to use your advice to implement a selection function similar to the tolerance function in caret. It selects the largest value of lambda within a percent difference of the minimum error.

    get_lambda <- function(fit, tol = 1.05) {
      error <- fit$cvm[fit$lambda == fit$lambda.min]
      tolerance <- error * tol
      max(fit$lambda[fit$cvm <= tolerance])
    }