Here is an example:
library(glmnet)
n <- 500L
x1 <- rnorm(n, 2.0, 0.5)
x2 <- rnorm(n, -1.0, 2)
y <- factor(rbinom(n, 1L, plogis(-0.6 + 1.0 * x1 - 0.8 * x2)))
X <- matrix(c(x1, x2), ncol = 2)
mod <- glmnet(X, y, "binomial")
Now you can see the degrees of freedom and corresponding lambda by simply:
> print(mod)
Call: glmnet(x = X, y = y, family = "binomial")
Df %Dev Lambda
[1,] 0 -1.026e-14 0.149300
[2,] 1 3.314e-02 0.136000
[3,] 1 6.073e-02 0.123900
...
So in this case the coefficients are all 0 when lambda is 0.149300 (or higher). To confirm:
> coef(mod, s = 0.149300)
3 x 1 sparse Matrix of class "dgCMatrix"
1
(Intercept) 1.688296
V1 .
V2 .
> coef(mod, s = 0.136000)
3 x 1 sparse Matrix of class "dgCMatrix"
1
(Intercept) 1.64233062
V1 .
V2 -0.04946726
Note that the vectors for the degrees of freedom and lambda can also be accessed via mod$df
and mod$lambda
, and you can also change the values of lambda
which glmnet
tries (if say you wanted to home in on "lambda_max") using for example:
mod <- glmnet(X, y, "binomial", lambda = seq(0.149, 0.151, by = 0.0001))