簡體   English   中英

R 和隨機森林:caret 和 pROC 如何處理正類和負類?

[英]R and Random Forest: How caret and pROC deal with positive and negative class?

在過去的幾天里,我一直在分析 R 實現隨機森林的性能以及可用的不同工具,以獲得:

  • AUC
  • 靈敏度
  • 特異性

因此,我使用了兩種不同的方法:

  • 來自pROC庫的 mroc 和 coords 以獲得模型在不同截止點的性能。
  • 插入符號庫中的混淆矩陣以獲得模型的最佳性能(AUC、准確度、靈敏度、特異性等)

關鍵是我已經意識到兩種方法之間存在一些差異。

我開發了以下代碼:

suppressMessages(library(randomForest))
suppressMessages(library(pROC))
suppressMessages(library(caret))

set.seed(100)

t_x <- as.data.frame(matrix(runif(100),ncol=10))
t_y <- factor(sample(c("A","B"), 10, replace = T), levels=c("A","B"))

v_x  <- as.data.frame(matrix(runif(50),ncol=10))
v_y <- factor(sample(c("A","B"), 5, replace = T), levels=c("A","B"))

model <- randomForest(t_x, t_y, ntree=1000, importance=T);
prob.out <- predict(model, v_x, type="prob")[,1];
prediction.out <- predict(model, v_x, type="response");

mroc <- roc(v_y,prob.out,plot=F)

results <- coords(mroc,seq(0, 1, by = 0.01),input=c("threshold"),ret=c("sensitivity","specificity","ppv","npv"))

accuracyData <- confusionMatrix(prediction.out,v_y)

如果比較結果accuracyData變量,可以看到敏感性和特異性之間的關系是相反的。

也就是說,confusionMatrix 結果是:

Confusion Matrix and Statistics

          Reference
Prediction A B
         A 1 1
         B 2 1

               Accuracy : 0.4             
                 95% CI : (0.0527, 0.8534)
    No Information Rate : 0.6             
    P-Value [Acc > NIR] : 0.913           

                  Kappa : -0.1538         
 Mcnemar's Test P-Value : 1.000           

            Sensitivity : 0.3333          
            Specificity : 0.5000          
         Pos Pred Value : 0.5000          
         Neg Pred Value : 0.3333          
             Prevalence : 0.6000          
         Detection Rate : 0.2000          
   Detection Prevalence : 0.4000          
      Balanced Accuracy : 0.4167          

       'Positive' Class : A 

但是如果我在坐標計算中尋找這樣的靈敏度和特異性,我發現它們交換了:

     sensitivity specificity       ppv       npv
0.32         0.5   0.3333333 0.3333333 0.5000000

顯然,靈敏度和特異性在坐標和混淆矩陣中是相反的。

考慮到混淆矩陣正確識別了正類,我認為這是對敏感性和特異性的良好解釋。

我的問題是:有沒有辦法強制坐標以我想要的方式解釋正類和負類?

如果你看一下confusionMatrix的輸出,你會看到:

       'Positive' Class : A 

現在查看mroc ,B 類被視為正類:

Data: prob.out in 3 controls (v_y A) < 2 cases (v_y B).

基本上, pROC將您的因子水平視為 Negative、Positive 和caret正好相反。 您可以使用pROC明確指定您的級別以獲得相同的行為:

mroc <- roc(v_y,prob.out,plot=F, levels = c("B", "A"))

或者根據您的首選行為,使用confusionMatrixpositive論證:

accuracyData <- confusionMatrix(prediction.out,v_y, positive = "B")

試試這個,你會用這兩種方法得到相同的結果(這都是關於正和負類因子水平):

accuracyData <- confusionMatrix(prediction.out,v_y, positive='A')
accuracyData



Confusion Matrix and Statistics

             Reference
    Prediction A B
             A 1 0
             B 2 2                         

             Accuracy : 0.6             
                 95% CI : (0.1466, 0.9473)
    No Information Rate : 0.6             
    P-Value [Acc > NIR] : 0.6826          

                  Kappa : 0.2857          
 Mcnemar's Test P-Value : 0.4795          

            Sensitivity : 0.3333          
            Specificity : 1.0000          
         Pos Pred Value : 1.0000          
         Neg Pred Value : 0.5000          
             Prevalence : 0.6000          
         Detection Rate : 0.2000          
   Detection Prevalence : 0.2000          
      Balanced Accuracy : 0.6667          

       'Positive' Class : A   


mroc <- roc(v_y,prob.out,plot=F, levels=c("B", "A"))
results <- coords(mroc, 0.49, "threshold", ret=c("specificity", "sensitivity", "accuracy",
                                      "tn", "tp", "fn", "fp", "npv", "ppv", "1-specificity",
                                      "1-sensitivity", "1-accuracy", "1-npv", "1-ppv"))
results 

specificity   sensitivity      accuracy            tn            tp            fn            fp           npv           ppv 1-specificity 
    1.0000000     0.3333333     0.6000000     2.0000000     1.0000000     2.0000000     0.0000000     0.5000000     1.0000000     0.0000000 
1-sensitivity    1-accuracy         1-npv         1-ppv 
    0.6666667     0.4000000     0.5000000     0.0000000 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM