#============================================ # cross validation by hand #============================================ # get a vector of fold id used in cv.glmnet # to replicate the same result. # Therefore, this is subject to the change foldid – cvfit$foldid # from glmnet #foldid – user.foldid # user-defined # candidate lambda range fit – glmnet(x, y, family = “binomial”) v.lambda – fit$lambda nla – length(v.lambda) m.mce – matrix(0,nrow = nfolds, ncol=nla) m.tot – matrix(0,nrow = nfolds, ncol=nla) m.mcr – matrix(0,nrow = nfolds, ncol=nla) #——————————- # iteration over all folds #——————————- for (i in 1:nfolds) { # training fold : tr # validation fold : va ifd – which(foldid==i) # i-th fold tr.x – x[–ifd,]; tr.y – y[–ifd] va.x – x[ifd,]; va.y – y[ifd] # estimation using training fold fit – glmnet(tr.x, tr.y, family = “binomial”, lambda = v.lambda) # prediction on validation fold prd – predict(fit, newx = va.x, type = “class”) # misclassification error for each lambda for(c in 1:nla) { # confusion matrix cfm – confusionMatrix( as.factor(prd[,c]), as.factor(va.y)) # misclassification count m.mce[i,c] – cfm$table[1,2]+cfm$table[2,1] # total count m.tot[i,c] – sum(cfm$table) # misclassification rate m.mcr[i,c] – m.mce[i,c]/m.tot[i,c] } } # average misclassification error rate (mcr) v.mcr – colMeans(m.mcr) … Read more