我认为也许您想使用 1/5 的数据作为测试集并使用其他 4/5 进行训练?
如果是这种情况,您应该createDataPartition
先使用,然后train
再做其余的事情。例如:
> library(caret)
> library(mlbench)
> data(Sonar)
>
> set.seed(1)
> in_train <- createDataPartition(Sonar$Class, p = 4/5, list = FALSE)
>
> training <- Sonar[ in_train,]
> testing <- Sonar[-in_train,]
>
> nrow(Sonar)
[1] 208
> nrow(training)
[1] 167
> nrow(testing)
[1] 41
>
> lda_fit <- train(Class ~ ., data = training, method = "lda")
> lda_fit
Linear Discriminant Analysis
167 samples
60 predictors
2 classes: 'M', 'R'
No pre-processing
Resampling: Bootstrapped (25 reps)
Summary of sample sizes: 167, 167, 167, 167, 167, 167, ...
Resampling results
Accuracy Kappa Accuracy SD Kappa SD
0.71 0.416 0.0532 0.108