I currently have code similar to the below.
set.seed(1)
library(glmnet)
matrix_example <- matrix(runif(100000), ncol = 50)
grid <- 10 ^ seq(20, -2, length = 500)
x <- matrix_example[, -1]
y <- matrix_example[, 1] %>%
scale(center = TRUE, scale = FALSE) %>%
as.matrix()
fit_cv_glmnet <- cv.glmnet(x, y, lambda = grid, alpha = 1)
fit_glmnet <- glmnet(x, y, lambda = grid, alpha = 1)
When I run cv.glmnet
, however, I get the following warning messages
Warning messages:
1: In regularize.values(x, y, ties, missing(ties), na.rm = na.rm) :
collapsing to unique 'x' values
2: In regularize.values(x, y, ties, missing(ties), na.rm = na.rm) :
collapsing to unique 'x' values
3: In regularize.values(x, y, ties, missing(ties), na.rm = na.rm) :
collapsing to unique 'x' values
4: In regularize.values(x, y, ties, missing(ties), na.rm = na.rm) :
collapsing to unique 'x' values
5: In regularize.values(x, y, ties, missing(ties), na.rm = na.rm) :
collapsing to unique 'x' values
6: In regularize.values(x, y, ties, missing(ties), na.rm = na.rm) :
collapsing to unique 'x' values
7: In regularize.values(x, y, ties, missing(ties), na.rm = na.rm) :
collapsing to unique 'x' values
8: In regularize.values(x, y, ties, missing(ties), na.rm = na.rm) :
collapsing to unique 'x' values
9: In regularize.values(x, y, ties, missing(ties), na.rm = na.rm) :
collapsing to unique 'x' values
10: In regularize.values(x, y, ties, missing(ties), na.rm = na.rm) :
collapsing to unique 'x' values
When I run glmnet
, I do not get this message at all. Is there a reason why I am getting the warning with cv.glmnet
? Does it have to do with the way that the function calculates the cross-validation error? As I said, the glmnet
function works fine and I am able to identify a lambda whose model corresponds to the minimum BIC.
Thanks.
The issue seems to be the user-supplied lambda sequence tripping up the algorithm. This sequence is producing the same results/error for each lambda (as seen in the cvm
vector in the cv.glmnet
object).
If you need a user-supplied sequence, I'd suggest guiding the algorithm to create it using the nlambda
and lambda.min.ratio
arguments in your call to the function. Something like the below, changing the parameters to suit your purposes.
cv.glmnet(x, y, alpha = 1, nlambda = 500, lambda.min.ratio = 10^(-2))
For what it's worth, the warning message itself is related to an internally called stats::regularize.values
function, itself called from an approx
function. It seems to be a symptom of the issue with the lambda sequence, not a problem in and of itself.