R插入符号/rfe内培训的交叉验证如何工作
我对R插入符号/rfe内培训的交叉验证如何工作,r,cross-validation,r-caret,rfe,R,Cross Validation,R Caret,Rfe,我对caret库中的rfe函数有一个问题。在插入符号主页上,他们给出了以下RFE算法: 对于这个例子,我使用带有3倍交叉验证的rfe函数和带有线性SVM和5倍交叉验证的训练函数 library(kernlab) library(caret) data(iris) # parameters for the tune function, used for fitting the svm trControl <- trainControl(method = "cv", number = 5)
caret
库中的rfe
函数有一个问题。在插入符号主页上,他们给出了以下RFE算法:
对于这个例子,我使用带有3倍交叉验证的rfe
函数和带有线性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")
从这一点来看,5倍cv的训练集的大小似乎是120个样本,而我预期的大小是80
所以,如果有人能澄清rfe和train是如何协同工作的,那就太好了
干杯
> 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
这里的问题是
lappy(rf1$fit$control$index,length)
没有存储我们认为它能存储的内容
让我明白有必要研究代码。发生的情况如下:
调用rfe
时,整个数据将传递到nominalRfeWorkflow
在nominalRfeWorkflow
中,根据rfeControl
分割的列车和测试数据(在我们的示例中,根据3倍CV规则分割3次)被传递给rfeIter
。
我们可以在rf1$control$index
下的结果中找到这些拆分
在rfeIter
中,使用~100个训练样本(我们的示例)查找最终变量(即该函数的输出)。
据我所知,50个测试样本(我们的示例)用于计算不同变量集的性能,但它们仅存储为外部性能,而不用于选择最终变量。
为了选择这些,使用了5倍交叉验证的性能估计。
但是我们无法在rfe
返回的最终结果中找到这些索引。
如果我们真的需要它们,我们需要从rfeIter
中的fitObject$control$index
中获取它们,将它们返回到nominalRfeWorkflow
,然后返回到rfe
,并从那里得到rfe
返回的rfe
类对象
那么lappy(rf1$fit$control$index,length)
中存储了什么呢当rfe
找到最佳变量时,将使用最佳变量和完整参考数据(150)创建最终模型拟合rf1$fit
在rfe
中创建,如下所示:
fit 5-fold CV为每个CV臂保留五分之一的数据集。因此,每次训练120次,测试集是剩余的30个样本。30个样本*5=150个样本。是的,但根据算法描述,5倍CV应应用于由3倍CV产生的训练数据。因此,第一套训练集=150/3*2,第二套100/5*4=80。@Fabian_G你有没有想过?我遇到了同样的问题,正在考虑联系topepo或提交错误报告。
> 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