我想使用插入符号模型中的非折叠预测来训练包含一些原始预测变量的第二阶段模型。我可以按如下方式收集超出预期的预测:
#Load Data
set.seed(1)
library(caret)
library(mlbench)
data(BostonHousing)
#Build Model (see ?train)
rpartFit <- train(medv ~ . + rm:lstat, data = BostonHousing, method="rpart",
trControl=trainControl(method='cv', number=folds,
savePredictions=TRUE))
#Collect out-of-fold predictions
out_of_fold <- rpartFit$pred
bestCP <- rpartFit$bestTune[,'.cp']
out_of_fold <- out_of_fold[out_of_fold$.cp==bestCP,]
这很好,但它们的顺序错误:
> all.equal(out_of_fold$obs, BostonHousing$medv)
[1] "Mean relative difference: 0.4521906"
我知道该train
对象返回一个用于训练每个折叠的索引的列表:
> str(rpartFit$control$index)
List of 10
$ Fold01: int [1:457] 1 2 3 4 5 6 7 8 9 10 ...
$ Fold02: int [1:454] 2 3 4 8 10 11 12 13 14 15 ...
$ Fold03: int [1:457] 1 2 3 4 5 6 7 8 9 10 ...
$ Fold04: int [1:455] 1 2 3 5 6 7 8 9 10 11 ...
$ Fold05: int [1:455] 1 2 3 4 5 6 7 8 9 10 ...
$ Fold06: int [1:455] 1 2 3 4 5 6 7 8 9 10 ...
$ Fold07: int [1:457] 1 3 4 5 6 7 8 9 10 13 ...
$ Fold08: int [1:455] 1 2 4 5 6 7 9 11 12 14 ...
$ Fold09: int [1:455] 1 2 3 4 5 6 7 8 9 10 ...
$ Fold10: int [1:454] 1 2 3 4 5 6 7 8 9 10 ...
如何使用这些信息以out_of_fold
与原始数据集相同的顺序将观察结果放入对象中BostonHousing
?