【问题标题】:"coords" function of "pROC" package returns different sensibility and specificity values than "confusionMatrix" function of "caret" package“pROC”包的“coords”函数返回与“caret”包的“confusionMatrix”函数不同的敏感性和特异性值
【发布时间】:2019-04-21 13:56:48
【问题描述】:

大家好,非常感谢您的帮助。

我已经执行了一个随机森林模型进行分类。现在我想确定优化特异性和敏感性的最佳阈值。

我很困惑,因为如标题所述,“pROC”包的“coords”函数返回的值与“caret”包的“confusionMatrix”函数返回的值不同。

下面是代码:

# package import

library(caret)
library(pROC)

# data import

data <- read.csv2("denonciation.csv", check.names = F)

# data partition

validation_index <- createDataPartition(data$Denonc, p=0.80,list=FALSE)
validation <- data[-validation_index,]
entrainement <- data[validation_index,]

# handling class imbalance

set.seed (7)
up_entrainement <- upSample(x=entrainement[,-ncol(entrainement)],y=entrainement$Denonc)

# Cross validation setting

control <- trainControl(method ="cv", number=10, classProbs = TRUE)

# Model training

fit.rf_up <-train(Denonc~EMOTION+Agreabilite_classe+Conscienciosite_classe, data = up_entrainement, method="rf", trControl = control)

# Best threshold determination

roc <- roc(up_entrainement$Denonc, predict(fit.rf_up, up_entrainement, type = "prob")[,2])
    coords(roc, x="best", input = "threshold", best.method = "closest.topleft")

### The best threshold seems to be .36 with a specificity of .79 and a sensitivity of .73 ###

# Confusion matrix with the best threshold returned by "coords"

probsTest <- predict(fit.rf_up, validation, type = "prob")
threshold <- 0.36
predictions <- factor(ifelse(probsTest[, "denoncant"] > threshold, "denoncant", "non_denoncant"))
confusionMatrix(predictions, validation$Denonc)

这里的值是不同的:

Confusion Matrix and Statistics

                Reference
Prediction      denoncant non_denoncant
  denoncant           433          1380
  non_denoncant       386          1671

           Accuracy : 0.5437          
             95% CI : (0.5278, 0.5595)
No Information Rate : 0.7884          
P-Value [Acc > NIR] : 1               

              Kappa : 0.0529          
 Mcnemar's Test P-Value : <2e-16          

        Sensitivity : 0.5287          
        Specificity : 0.5477          
     Pos Pred Value : 0.2388          
     Neg Pred Value : 0.8123          
         Prevalence : 0.2116          
     Detection Rate : 0.1119          
   Detection Prevalence : 0.4685          
    Balanced Accuracy : 0.5382          

   'Positive' Class : denoncant    

请告诉我为什么“pROC”包的“coords”函数返回错误值?

非常感谢,

巴本

【问题讨论】:

  • 如果我没记错的话,你选择了基于训练数据(模型看到的数据)的截止值。您应该做的是根据重新采样中的预测来选择截止值。

标签: r machine-learning r-caret proc confusion-matrix


【解决方案1】:

我可以看到这里有 2 个可能的问题:

  1. 在训练模型时,来自 2 个类别的样本通过对数量较少的类别进行上采样来平衡:模型产生的最佳阈值也在同一个上采样数据集上进行校准。据我所知,验证数据集并非如此。

  2. 这两个结果给出了不同集合(训练和验证)上的模型指标:虽然它们对于 RandomForest 模型应该是接近的,但考虑到所有在后台发生的平均,这并不意味着结果将完全相同。 RandomForest 模型不太可能过度拟合数据,但如果数据由具有不同特征向量分布和/或不同特征响应关系的几个不同群体的混合组成,则有可能,这可能并不总是均匀分布在训练集和验证集中,即使您确实对数据进行了随机抽样(即平均分布可能相同,但对于特定的训练-验证划分则不同)。

我认为第一个问题出在哪里,但不幸的是,我无法测试您的代码,因为它取决于文件denonciation.csv。

【讨论】:

    猜你喜欢
    • 2019-12-22
    • 2014-05-23
    • 2021-04-21
    • 2020-08-23
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 2019-12-08
    相关资源
    最近更新 更多