Solved – Cross-validation and weights with glmnet

cross-validationglmnet

Are any weights supplied to glmnet::cv.glmnet taken into account for the calculation of the figure of merit (AUC for logistic regression in my case) on the "held-out" dataset, or only used directly in the fitting?

Best Answer

I was looking for the answer to the same question and I took a look into the code (v1.8-2) in http://cran.r-project.org/web/packages/glmnet/index.html .

The file "cv.elnet.R" (in your case cv.lognet.r) contains the relevant pieces of code.

cvraw=switch(type.measure,

"mse"=(y-predmat)^2,

"deviance"=(y-predmat)^2,

"mae"=abs(y-predmat)

)

cvm=apply(cvraw,2,weighted.mean,w=weights,na.rm=TRUE)

cvsd=sqrt(apply(scale(cvraw,cvm,FALSE)^2,2,weighted.mean,w=weights,na.rm=TRUE)/(N-1))

So the package does use the weights in the computation of the figures of merit even on the held-out data. (You will find similar lines in cv.lognet.r as well.)