我想知道如何恢复交叉验证预测 . 我有兴趣手动构建堆叠模型(like here in point 3.2.1),我需要模型的每个保持折叠的预测 . 我附上一个简短的例子 .
# load the library
library(caret)
# load the iris dataset
data(cars)
# define folds
cv_folds <- createFolds(cars$Price, k = 5, list = 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)
# looking at predictions
model$pred
# verifying the number of observations
nrow(model$pred[model$pred$Resample == "Fold1",])
nrow(cars)
我想知道在折叠1-4上估计模型和在折叠5等上评估的预测是什么 . 看看 model$pred
似乎没有给我我需要的东西 .
1 回答
当使用
createFolds
函数创建的折叠在插入符号中执行CV时,默认使用列车索引 . 所以当你这样做时:你收到了火车套装折叠
每个包含20%的数据
然后你在trainControl中指定了这些折叠:
在
trainControl
的帮助下:因此,每次模型构建在160行上并在其余部分上进行验证 . 这就是为什么
返回643
你应该做的是:
现在:
并在训练模型后: