R中的k次嵌套重复交叉验证

k-fold nested repeated cross validation in R

我需要进行四重嵌套重复交叉验证来训练模型。 我写了下面的代码,它有内部交叉验证,但现在我正在努力创建外部。

fitControl <- trainControl(## 10-fold CV
                           method = "repeatedcv",
                           number = 10,
                           ## repeated five times
                           repeats = 5,
                           savePredictions = TRUE,
                           classProbs = TRUE,
                           summaryFunction = twoClassSummary)

model_SVM_P <- train(Group ~ ., data = training_set, 
                 method = "svmPoly", 
                 trControl = fitControl,
                 verbose = FALSE,
                 tuneLength = 5)

我尝试解决问题:

ntrain=length(training_set)    
train.ext=createFolds(training_set,k=4,returnTrain=TRUE)
test.ext=lapply(train.ext,function(x) (1:ntrain)[-x])

for (i in 1:4){
    model_SVM_P <- train(Group ~ ., data = training_set[train.ext[[i]]], 
                 method = "svmRadial", 
                 trControl = fitControl,
                 verbose = FALSE,
                 tuneLength = 5) 

    }

但是没有用。 我该如何做这个外循环?

rsample包在nested_cv()函数中实现了外循环,见documentation

要评估由 nested_cv 训练的模型,请查看此 vignette,其中显示了“繁重工作”完成的位置:

# `object` is an `rsplit` object in `results$inner_resamples` 
summarize_tune_results <- function(object) {
  # Return row-bound tibble that has the 25 bootstrap results
  map_df(object$splits, tune_over_cost) %>%
    # For each value of the tuning parameter, compute the 
    # average RMSE which is the inner bootstrap estimate. 
    group_by(cost) %>%
    summarize(mean_RMSE = mean(RMSE, na.rm = TRUE),
              n = length(RMSE),
              .groups = "drop")
}

tuning_results <- map(results$inner_resamples, summarize_tune_results)

此代码将 tune_over_cost 函数应用于训练数据的每个超参数和拆分(或折叠),此处称为“评估数据”。

请查看小插图以获得更多有用的代码,包括并行化。