请问glmnet是否会产生交叉验证预测,即基于模型构建中被留出的折叠来进行预测(通常所说的交叉验证),而不是基于通过交叉验证确定的最佳lambda的同一模型的交叉验证预测?
请问glmnet是否会产生交叉验证预测,即基于模型构建中被留出的折叠来进行预测(通常所说的交叉验证),而不是基于通过交叉验证确定的最佳lambda的同一模型的交叉验证预测?
predict.cv.glmnet
只是将所有数据的'glmnet'拟合传递给predict.glmnet
,正如您所猜测的那样。
然而,参数keep返回基于留出数据集的训练数据(拟合值)的预测结果。每个记录分配的折叠被记录为元素foldid。
> library(glmnet)
> # keep prevalidated array
> cvf1 <- cv.glmnet(x = as.matrix(mtcars[, c("disp", "hp", "mpg")]),
+ y = mtcars$am, family = "binomial", keep = TRUE)
> dim(mtcars)
# [1] 32 11
> length(cvf1$lambda)
# [1] 84
> # leave-n out fitted predictions
> # 84 columns, 2 columns padded with NAs
> dim(cvf1$fit.preval)
# [1] 32 86
> # performance of cross-validated model predictions
> round(mtcars$am - cvf1$fit.preval[, cvf1$lambda == cvf1$lambda.min])
# [1] 1 1 0 0 0 0 0 0 -1 0 0 0 0 0 0
# [16] 0 0 0 0 0 -1 0 0 0 0 0 0 0 1 0
# [31] 0 0
> cvf1$foldid
# [1] 1 6 6 1 1 8 9 6 2 5 9 4 4 2 2
# [16] 10 5 2 3 4 10 3 1 3 10 9 7 8 7 8
# [31] 7 5