I would like to compare my elastic net model to a unregularized model. For the sake of fairness and simplicity, I would like to train both models using the glmnet
package. However, I recently discovered that glmnet
overrides lambda=0
on some datasets.
How can I force glmnet
to behave like glm
?
x <- structure(c(0.028, 0.023, 0.0077, 0.14, 0.027, 0.084, 0.018,
0.055, 0.0089, 0.016, 0.037, 0.043, 0.046, 0.031, 0.034, 0.056,
0.016, 0.048, 0.013, 0.02, 0.067, 0.046, 0.058, 0.054, 0.036,
0.043, 0.009, 0.12, 0.024, 0.018, 0.066, 0.046, 0.057, 0.054,
0.036, 0.043, 0.009, 0.12, 0.024, 0.018, 0.051, 0.043, 0.047,
0.045, 0.034, 0.04, 0.009, 0.085, 0.022, 0.016, 0.028, 0.023,
0.0089, 0.14, 0.028, 0.084, 0.02, 0.055, 0.0089, 0.016, 0.067,
0.049, 0.058, 0.055, 0.038, 0.043, 0.009, 0.12, 0.024, 0.018,
0.067, 0.046, 0.058, 0.054, 0.036, 0.043, 0.009, 0.12, 0.024,
0.018), .Dim = c(10L, 8L), .Dimnames = list(NULL, NULL))
y <- gl(2, 5)
fit <- glmnet::glmnet(x, y, family = "binomial", lambda = 0)
fit$lambda # should be 0 but actually infinity
Warning messages:
1: In lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, :
one multinomial or binomial class has fewer than 8 observations; dangerous ground
2: from glmnet Fortran code (error code -1); Convergence for 1th lambda value not reached after maxit=100000 iterations; solutions for larger lambdas returned
3: In getcoef(fit, nvars, nx, vnames) :
an empty model has been returned; probably a convergence issue