0
$\begingroup$

I am using cross validation in glmnet package and have some difficulties in understanding foldid option. Why do I still get random results if I use foldid?

load("QuickStartExample.RData")
foldid=sample(1:10,size=length(y),replace=TRUE)
for(i in 1:10){
   print(cv.glmnet(x,y,foldid)$lambda.min)
}

# The outputs

[1] 0.1035043
[1] 0.09430923
[1] 0.09430923
[1] 0.09430923
[1] 0.02336112
[1] 0.1035043
[1] 0.08593106
[1] 0.03389298
[1] 0.01767183
[1] 0.09430923
$\endgroup$
1
$\begingroup$

You did not pass cv.glmnet a foldid argument.

Let's look at the signature of cv.glmnet, taken from the documentation

cv.glmnet(x, y, weights, offset, lambda, type.measure, nfolds, foldid, grouped, keep,
      parallel, ...)

The third positional argument to cv.glmnet is weights, this is where you placed your foldid vector. So cv.glmnet is interpreting the vector you named foldid as the weights argument, and using it as sample weights.

The correct call to do what you are after is

cv.glmnet(x, y, foldid = foldid)

You need to pass in your foldid as a named argument.

$\endgroup$
1
  • $\begingroup$ You are right. I forgot to use the named argument. Thanks for this :) $\endgroup$
    – soeci92
    Jun 25 '17 at 2:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.