我使用插入符号包训练10x10CV的randomForest对象。
library(caret)
tc <- trainControl("repeatedcv", number=10, repeats=10, classProbs=TRUE, savePred=T) 
RFFit <- train(Defect ~., data=trainingSet, method="rf", trControl=tc, preProc=c("center", "scale"))
之后,我在testSet上测试randomForest(新数据)
RF.testSet$Prediction <- predict(RFFit, newdata=testSet)
混乱矩阵向我展示了该模型还不错。
confusionMatrix(data=RF.testSet$Prediction, RF.testSet$Defect)
              Reference
    Prediction   0   1
             0 886 179
             1  53 126  
      Accuracy : 0.8135          
             95% CI : (0.7907, 0.8348)
No Information Rate : 0.7548          
P-Value [Acc > NIR] : 4.369e-07       
              Kappa : 0.4145 
我现在想测试$ finalModel,我认为它应该给我相同的结果,但是以某种方式我收到了
> RF.testSet$Prediction <- predict(RFFit$finalModel, newdata=RF.testSet)
>  confusionMatrix(data=RF.testSet$Prediction, RF.testSet$Defect)
Confusion Matrix and Statistics
          Reference
Prediction   0   1
         0 323  66
         1 616 239
               Accuracy : 0.4518          
                 95% CI : (0.4239, 0.4799)
    No Information Rate : 0.7548          
    P-Value [Acc > NIR] : 1               
                  Kappa : 0.0793 
我想念什么?
编辑@topepo:
我还学习了另一个没有preProcessed选项的randomForest,并得到了另一个结果:
RFFit2 <- train(Defect ~., data=trainingSet, method="rf", trControl=tc)
testSet$Prediction2 <- predict(RFFit2, newdata=testSet)
confusionMatrix(data=testSet$Prediction2, testSet$Defect)
Confusion Matrix and Statistics
          Reference
Prediction   0   1
         0 878 174
         1  61 131
               Accuracy : 0.8111          
                 95% CI : (0.7882, 0.8325)
    No Information Rate : 0.7548          
    P-Value [Acc > NIR] : 1.252e-06       
                  Kappa : 0.4167     
          
                  对于第二个
                
                  
                    —
                    topepo 2014年
                    
                  
                
              train模型,除非您在运行随机种子之前对其进行设置(请参阅参考资料?set.seed),否则结果会略有不同。精度值为0.8135和0.8111,这非常接近,并且仅是由于重采样和模型计算的随机性。
                
RFFit我猜是第一种情况,您使用称为的火车对象进行了预测,第二种情况是使用模型对象进行了预测。因此,区别可能在于将其他事物与处理新测试数据的火车对象一起传递的方式与不使用火车对象的传递方式有所不同。