我很奇怪,为什么从“Fold1的预言实际上是我的预定义的褶皱从第二倍的预测。 附上我的意思的例子。
# load the library
library(caret)
# load the cars dataset
data(cars)
# define folds
cv_folds <- createFolds(cars$Price, k = 5, list = TRUE, returnTrain = TRUE)
# define training control
train_control <- trainControl(method="cv", index = cv_folds, savePredictions = 'final')
# fix the parameters of the algorithm
# train the model
model <- caret::train(Price~., data=cars, trControl=train_control, method="gbm", verbose = F)
model$pred$rowIndex[model$pred$Resample == 'Fold1'] %in% cv_folds[[2]]
的重新取样数据'Fold1'
是其不是在记录cv_folds[[1]]
这些记录包含在cv_folds
2-5。 当你运行的是5倍交叉验证这是正确的。 重新取样折1对培训褶皱2-5的模型进行测试。 重新取样倍2抵抗训练测试的褶皱1,3-5,依此类推。
总结:在这些预测Fold1
从培训cv_folds 2-5的模型试验预测。
编辑:根据评论
所有需要的信息是在模型$预计值表。 我加了一点澄清代码:
model$pred %>%
select(rowIndex, pred, Resample) %>%
rename(predection = pred, holdout = Resample) %>%
mutate(trained_on = case_when(holdout == "Fold1" ~ "Folds 2, 3, 4, 5",
holdout == "Fold2" ~ "Folds 1, 3, 4, 5",
holdout == "Fold3" ~ "Folds 1, 2, 4, 5",
holdout == "Fold4" ~ "Folds 1, 2, 3, 5",
holdout == "Fold5" ~ "Folds 1, 2, 3, 4"))
rowIndex predection holdout trained_on
1 610 13922.60 Fold2 Folds 1, 3, 4, 5
2 623 38418.83 Fold2 Folds 1, 3, 4, 5
3 604 12383.55 Fold2 Folds 1, 3, 4, 5
4 607 15040.07 Fold2 Folds 1, 3, 4, 5
5 95 33549.40 Fold2 Folds 1, 3, 4, 5
6 624 40357.35 Fold2 Folds 1, 3, 4, 5
Basicly您需要进一步与预测堆叠什么是pred
和rowIndex
从模型$ PRED表列。
该rowIndex位置是指从原始数据行。 所以rowIndex位置610是指记录在汽车数据集610。 您可以比较,在OBS的数据,这是从汽车数据集的价格列的值。