R插入符号/如何在rfe工作中对列车进行交叉验证

Fab*_*n_G 9 r cross-validation rfe r-caret

rfecaret库中的功能有疑问.在插入符号主页链接上,他们提供以下RFE算法: 算法

对于这个例子,我使用rfe具有3倍交叉验证的功能和具有线性SVM和5倍交叉验证的列车功能.

library(kernlab)
library(caret)
data(iris)

# parameters for the tune function, used for fitting the svm
trControl <- trainControl(method = "cv", number = 5)

# parameters for the RFE function
rfeControl <- rfeControl(functions = caretFuncs, method = "cv",
                     number= 4, verbose = FALSE )

rf1 <- rfe(as.matrix(iris[,1:4]), as.factor(iris[,5]) ,sizes = c( 2,3) ,  
           rfeControl = rfeControl, trControl = trControl, method = "svmLinear")
Run Code Online (Sandbox Code Playgroud)
  • 从上面的算法我假设该算法可以使用2个嵌套的交叉验证:
    1. rfe 将数据(150个样本)分成3倍
    2. train功能将在训练集(100个样本)上运行,具有5倍交叉验证以调整模型参数 - 随后的RFE.

令我困惑的是,当我看一下rfe函数的结果时:

> lapply(rf1$control$index, length)
$Fold1
[1] 100
$Fold2
[1] 101
$Fold3
[1] 99

> lapply(rf1$fit$control$index, length)
$Fold1
[1] 120
$Fold2
[1] 120
$Fold3
[1] 120
$Fold4
[1] 120
$Fold5
[1] 120
Run Code Online (Sandbox Code Playgroud)

从那看起来,5倍cv的训练集的大小是120个样本,当我期望大小为80.

如果有人能够澄清rfe火车如何一起工作,那将会很棒.

干杯

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: i386-apple-darwin9.8.0/i386 (32-bit)

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] pROC_1.5.4      e1071_1.6-1     class_7.3-5     caret_5.15-048 
 [5] foreach_1.4.0   cluster_1.14.3  plyr_1.7.1      reshape2_1.2.1 
 [9] lattice_0.20-10 kernlab_0.9-15 

loaded via a namespace (and not attached):
 [1] codetools_0.2-8 compiler_2.15.1 grid_2.15.1     iterators_1.0.6
 [5] stringr_0.6.1   tools_2.15.1   
Run Code Online (Sandbox Code Playgroud)

小智 1

这里的问题是它lapply(rf1$fit$control$index, length)不存储我们认为它所做的事情。

为了让我明白有必要研究代码。发生的情况如下:

当您调用时,rfe整个数据都会传递到nominalRfeWorkflow.

在 中nominalRfeWorkflow,根据rfeControl(在我们的示例中根据 3 折 CV 规则分割 3 次)分割的训练和测试数据被传递到rfeIter。我们可以在下面的结果中找到这些分裂rf1$control$index

rfeIter大约 100 个训练样本(我们的示例)中,用于查找最终变量(即该函数的输出)。据我了解,大约 50 个测试样本(我们的示例)用于计算不同变量集的性能,但它们仅存储为外部性能,而不用于选择最终变量。为了选择这些,使用 5 倍交叉验证的性能估计。但我们在返回的最终结果中找不到这些索引rfe。如果我们确实需要它们,我们需要从fitObject$control$indexin获取它们rfeIter,将它们返回到nominalRfeWorkflow,然后rfe在返回的结果rfe-Class 对象中往返rfe

那么 中存储了什么lapply(rf1$fit$control$index, length)?-rfe找到最佳变量后,将使用最佳变量和完整参考数据 (150) 创建最终模型拟合。rf1$fit创建rfe如下:

fit <- rfeControl$functions$fit(x[, bestVar, drop = FALSE], y, first = FALSE, last = TRUE, ...)

该函数再次运行该train函数,并使用完整参考数据、最终特征集(trControl通过省略号 ( ...) 给出)进行最终交叉验证。由于我们trControl应该执行 5 倍 CV,因此lapply(rf1$fit$control$index, length)返回 120 是正确的,因为我们必须计算 150/5*4=120。