我有二進制YES/NO Class響應的數據。使用以下代碼來運行RF模型。我在獲取混淆矩陣結果時遇到了問題。R中的隨機森林混亂矩陣Caret
dataR <- read_excel("*:/*.xlsx")
Train <- createDataPartition(dataR$Class, p=0.7, list=FALSE)
training <- dataR[ Train, ]
testing <- dataR[ -Train, ]
model_rf <- train( Class~., tuneLength=3, data = training, method =
"rf", importance=TRUE, trControl = trainControl (method = "cv", number =
5))
結果:
Random Forest
3006 samples
82 predictor
2 classes: 'NO', 'YES'
No pre-processing
Resampling: Cross-Validated (5 fold)
Summary of sample sizes: 2405, 2406, 2405, 2404, 2404
Addtional sampling using SMOTE
Resampling results across tuning parameters:
mtry Accuracy Kappa
2 0.7870921 0.2750655
44 0.7787721 0.2419762
87 0.7767760 0.2524898
Accuracy was used to select the optimal model using the largest value.
The final value used for the model was mtry = 2.
到目前爲止很好,但是當我運行此代碼:
# Apply threshold of 0.50: p_class
class_log <- ifelse(model_rf[,1] > 0.50, "YES", "NO")
# Create confusion matrix
p <-confusionMatrix(class_log, testing[["Class"]])
##gives the accuracy
p$overall[1]
我得到這個錯誤:
Error in model_rf[, 1] : incorrect number of dimensions
我很感激,如果你傢伙可以幫助我得到混淆矩陣結果。
將'model_rf [,1]'打印到控制檯並查看它。 – jsb
如果你在你的問題中包含一個[最小可重現的例子](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example),它會更容易幫助你。 – jsb