这些参数背后的实际代码可以在包的“caret/R/”文件夹中的selectByFilter.R
和createDataPartition.R
(以前的)源文件中找到。createFolds.R
请参阅这些文件,例如此处和此处(请注意,这些永久链接最终可能指向旧版本的代码)。为方便起见,相关片段(截至 2017 年 11 月版本 6.0-78)如下所示
在 selectByFilter.R c。第 157 行
sbf <- function (x, ...) UseMethod("sbf")
...
"sbf.default" <-
function(x, y,
sbfControl = sbfControl(), ...)
{
...
if(is.null(sbfControl$index)) sbfControl$index <- switch(
tolower(sbfControl$method),
cv = createFolds(y, sbfControl$number, returnTrain = TRUE),
repeatedcv = createMultiFolds(y, sbfControl$number, sbfControl$repeats),
loocv = createFolds(y, length(y), returnTrain = TRUE),
boot =, boot632 = createResample(y, sbfControl$number),
test = createDataPartition(y, 1, sbfControl$p),
lgocv = createDataPartition(y, sbfControl$number, sbfControl$p))
...
在 createDataPartition.R c。第 227 行
createMultiFolds <- function(y, k = 10, times = 5) {
if(class(y)[1] == "Surv") y <- y[,"time"]
prettyNums <- paste("Rep", gsub(" ", "0", format(1:times)), sep = "")
for(i in 1:times) {
tmp <- createFolds(y, k = k, list = TRUE, returnTrain = TRUE)
names(tmp) <- paste("Fold",
gsub(" ", "0", format(seq(along = tmp))),
".",
prettyNums[i],
sep = "")
out <- if(i == 1) tmp else c(out, tmp)
}
out
}