cv.glmnetused by most research papers and companies. When building a similar function, such as cv.glmnetfor glmnet.cr(a similar package that implements lasso ordinal regression to continue), I ran into this problem in cv.glmnet.
`cv.glmnet` first fits the model:
glmnet.object = glmnet(x, y, weights = weights, offset = offset,
lambda = lambda, ...)
After creating an object glmnetwith full data, the next step will look like this:
lambdafrom the entire installed model
lambda = glmnet.object$lambda
Now they make sure that the number of folds is more than 3
if (nfolds < 3)
stop("nfolds must be bigger than 3; nfolds=10 recommended")
A list has been created to store cross-confirmed results.
outlist = as.list(seq(nfolds))
A for loopis performed to match different pieces of data in the theory of cross-validation
for (i in seq(nfolds)) {
which = foldid == i
if (is.matrix(y))
y_sub = y[!which, ]
else y_sub = y[!which]
if (is.offset)
offset_sub = as.matrix(offset)[!which, ]
else offset_sub = NULL
outlist[[i]] = glmnet(x[!which, , drop = FALSE],
y_sub, lambda = lambda, offset = offset_sub,
weights = weights[!which], ...)
}
}
. , lambdas . - , ?. - , . cv.glmnet !