1

在过去的几天里,我一直在分析 R 实现随机森林的性能以及可用的不同工具,以获得:

  • 曲线下面积
  • 灵敏度
  • 特异性

因此,我使用了两种不同的方法:

  • 来自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)

如果比较结果准确度数据变量,可以看到敏感性和特异性之间的关系是相反的。

也就是说,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 正确识别了正类,我假设这是对敏感性和特异性的良好解释。

我的问题是:有什么方法可以强制坐标以我想要的方式解释正负类?

4

2 回答 2

4

如果您查看 的输出confusionMatrix,您可以看到:

       'Positive' Class : A 

现在看mroc,B 类被视为正类:

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

基本上,pROC将您的因素水平视为负面,正面并且caret完全相反。您可以明确指定您的级别pROC以获得相同的行为:

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

或者根据您的首选行为,使用以下positive参数confusionMatrix

accuracyData <- confusionMatrix(prediction.out,v_y, positive = "B")
于 2016-10-04T13:18:30.997 回答
0

试试这个,你会使用这两种方法得到相同的结果(这都是关于正面和负面的类因子水平):

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 
于 2016-10-04T13:42:28.580 回答