Caret confusionMatrix measures是错误的?

Caret confusionMatrix measures are wrong?

我做了一个函数来计算混淆矩阵的灵敏度和特异性,后来才发现 caret 包中有一个,confusionMatrix()。当我尝试时,事情变得非常混乱,因为 caret 使用了错误的公式??

示例数据:

dat <- data.frame(real = as.factor(c(1,1,1,0,0,1,1,1,1)),
                  pred = as.factor(c(1,1,0,1,0,1,1,1,0)))
cm <- table(dat$real, dat$pred)
cm
    0 1
  0 1 1
  1 2 5

我的函数:

model_metrics <- function(cm){
  acc <- (cm[1] + cm[4]) / sum(cm[1:4])
  # accuracy = ratio of the correctly labeled subjects to the whole pool of subjects = (TP+TN)/(TP+FP+FN+TN)
  sens <- cm[4] / (cm[4] + cm[3])
  # sensitivity/recall = ratio of the correctly +ve labeled to all who are +ve in reality = TP/(TP+FN)
  spec <- cm[1] / (cm[1] + cm[2])
  # specificity = ratio of the correctly -ve labeled cases to all who are -ve in reality = TN/(TN+FP)
  err <- (cm[2] + cm[3]) / sum(cm[1:4]) #(all incorrect / all)
  metrics <- data.frame(Accuracy = acc, Sensitivity = sens, Specificity = spec, Error = err)
  return(metrics)
}

现在将 confusionMatrix() 的结果与我的函数的结果进行比较:

library(caret)
c_cm <- confusionMatrix(dat$real, dat$pred)
c_cm
          Reference
Prediction 0 1
         0 1 1
         1 2 5
c_cm$byClass
Sensitivity          Specificity       Pos Pred Value       Neg Pred Value            Precision               Recall 
  0.3333333            0.8333333            0.5000000            0.7142857            0.5000000            0.3333333

model_metrics(cm)
  Accuracy Sensitivity Specificity     Error
1 0.6666667   0.8333333   0.3333333 0.3333333

灵敏度和特异性似乎在我的函数和 confusionMatrix() 之间交换了。我以为我使用了错误的公式,但我仔细检查了 Wiki and I was right. I also double-checked that I was calling the right values from the confusion matrix, and I'm pretty sure I am. The caret documentation 也表明它使用了正确的公式,所以我不知道发生了什么。

caret 功能有误,还是(更有可能)我犯了一些令人尴尬的明显错误?

插入符号函数没有错。

首先。考虑如何构建 table。 table(first, second) 将导致行 first 和列 second 的 table。

此外,当对 table 进行子集化时,应该按列计算单元格。例如,在您的函数中,计算灵敏度的正确方法是

 sens <- cm[4] / (cm[4] + cm[2])

最后,阅读未提供预期结果的函数的帮助页面始终是个好主意。 ?confusionMatrix 会给你帮助页面。

为此函数执行此操作时,您会发现可以指定将哪个因子水平视为阳性结果(使用 positive 参数)。

此外,请谨慎使用该功能。为避免混淆,我建议使用命名参数,而不是依赖于按位置指定参数。

第一个参数是数据(预测 类 的一个因素),第二个参数引用是观察到的一个因素 类 (dat$real 在你的情况下)。

得到你想要的结果:

confusionMatrix(data = dat$pred, reference = dat$real, positive = "1")

Confusion Matrix and Statistics

          Reference
Prediction 0 1
         0 1 2
         1 1 5
                                          
               Accuracy : 0.6667          
                 95% CI : (0.2993, 0.9251)
    No Information Rate : 0.7778          
    P-Value [Acc > NIR] : 0.8822          
                                          
                  Kappa : 0.1818          
                                          
 Mcnemar's Test P-Value : 1.0000          
                                          
            Sensitivity : 0.7143          
            Specificity : 0.5000          
         Pos Pred Value : 0.8333          
         Neg Pred Value : 0.3333          
             Prevalence : 0.7778          
         Detection Rate : 0.5556          
   Detection Prevalence : 0.6667          
      Balanced Accuracy : 0.6071          
                                          
       'Positive' Class : 1