我有一个问题,涉及到
caret
库中的rfe
函数。在caret
主页的链接中,他们给出了以下RFE算法:
算法
对于这个例子,我使用了带有3折交叉验证的rfe
函数和带有线性SVM和5折交叉验证的train函数。请注意保留HTML标记。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")
- 根据上述算法,我假设算法将使用2个嵌套的交叉验证:
rfe
将数据(150个样本)分为3个折叠- 使用5倍交叉验证在训练集(100个样本)上运行
train
函数来调整模型参数,并进行后续的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
从这个结果来看,使用5倍交叉验证的训练集大小为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