【问题标题】:Sensitivity and Specificity Calculation and Decision Matrix for the One-R Classification Model in RR中One-R分类模型的敏感性和特异性计算和决策矩阵
【发布时间】:2021-11-13 18:13:54
【问题描述】:

我没有看到类似的问题,在this question 中没有指定阳性结果。 This question 是相似的,是我自己问的,但这是一个不同的问题,一个是零 R 数据集,我似乎对 One R 有同样的问题,这个可能更清楚。我的问题是为什么我的结果与我的预期不同,以及我的 One Rule 模型是否正常运行 - 有一条警告消息我不确定是否需要解决,但具体来说有两个相互冲突的混淆矩阵相关,敏感性和特异性的手动计算与插入符号包中的confusionMatrix() 函数的特异性和敏感性计算不匹配。看起来有些东西颠倒了,但我会继续检查。非常感谢任何建议!

对于上下文,One Rule 模型会测试癌症数据集的每个属性或列,例如纹理是否在混淆矩阵中对良性 (B) 预测与恶性 (M) 预测产生了最准确的结果,或者是平滑度、面积还是其他一些因素,每个因素都表示为每列中的原始数据。

有这个警告,我的假设是我可以添加更多参数,但我没有完全理解它们:

oneRModel <- OneR(as.factor(Diagnosis)~., cancersamp)
#> Warning message:
#> In OneR.data.frame(x = data, ties.method = ties.method, verbose = verbose
#> data contains unused factor levels

这里有两个单独的混淆矩阵,它们可能有倒置标签,每个给出不同的特异性和敏感性结果,一个是我手动完成的,另一个是使用 caret 包中的confusionMatrix() 函数完成的:

table(dataTest$Diagnosis, dataTest.pred)
#> dataTest.pred
#>     B  M
#>  B 28  1
#>  M  5 12
 
 #OneR(formula, data, subset, na.action,
 #     control = Weka_control(), options = NULL)
 
 
confusionMatrix(dataTest.pred, as.factor(dataTest$Diagnosis), positive="B") 
#> Confusion Matrix and Statistics
#> 
#>           Reference
#> Prediction  B  M
#>          B 28  5
#>          M  1 12
#>                                          
#>                Accuracy : 0.8696          
#>                  95% CI : (0.7374, 0.9506)
#>     No Information Rate : 0.6304          
#>     P-Value [Acc > NIR] : 0.0003023       
#>                                          
#>                   Kappa : 0.7058          
#>                                          
#>  Mcnemar's Test P-Value : 0.2206714       
#>                                           
#>             Sensitivity : 0.9655          
#>             Specificity : 0.7059          
#>          Pos Pred Value : 0.8485          
#>          Neg Pred Value : 0.9231          
#>              Prevalence : 0.6304          
#>          Detection Rate : 0.6087          
#>    Detection Prevalence : 0.7174          
#>       Balanced Accuracy : 0.8357          
#>                                          
#>        'Positive' Class : B               
#>                                          
 
sensitivity1 = 28/(28+5)
specificity1 = 12/(12+1)
specificity1
#> [1] 0.9230769
sensitivity1
#> [1] 0.8484848

这是伪代码,我的假设是 OneR 函数已经这样做了,我不应该手动执行此操作:

For each attribute, 
  For each value of the attribute, make a rule as follows:
      count how often each class appears 
      find the most frequent class 
      make the rule assign that class to this attribute-value
  Calculate the error rate of the rules Choose the rules with the smallest error rate

这是我的 One R 模型的其余代码:

 #--------------------------------------------------
 #     One R Model
 #--------------------------------------------------
 
 
 set.seed(23)
 randsamp <- sample(nrow(cancerdata), 150, replace=FALSE)
 #randsamp
 
 cancersamp <- cancerdata[randsamp,]
 #cancersamp
 
 #?sample.split
 
 spl = sample.split(cancersamp$Diagnosis, SplitRatio = 0.7)
 #spl

dataTrain = subset(cancersamp, spl==TRUE)
dataTest = subset(cancersamp, spl==FALSE)
 
 oneRModel <- OneR(as.factor(Diagnosis)~., cancersamp)
#> Warning message:
#> In OneR.data.frame(x = data, ties.method = ties.method, verbose = #> verbose,  :
#>   data contains unused factor levels
summary(oneRModel)

#> Call:
#> OneR.formula(formula = as.factor(Diagnosis) ~ ., data = cancersamp)

#> Rules:
#> If perimeter = (53.2,75.7] then as.factor(Diagnosis) = B
#> If perimeter = (75.7,98.2] then as.factor(Diagnosis) = B
#> If perimeter = (98.2,121]  then as.factor(Diagnosis) = M
#> If perimeter = (121,143]   then as.factor(Diagnosis) = M
#> If perimeter = (143,166]   then as.factor(Diagnosis) = M

#> Accuracy:
#> 134 of 150 instances classified correctly (89.33%)

#> Contingency table:
#>                     perimeter
#> as.factor(Diagnosis) (53.2,75.7] (75.7,98.2] (98.2,121] (121,143] #> (143,166] Sum
#>                 B          * 31        * 63          1         0         0  95
#>                 M             1          14       * 19      * 18       * 3  55
#>                 Sum          32          77         20        18         3 150
#> ---
#> Maximum in each column: '*'

#> Pearson's Chi-squared test:
#> X-squared = 92.412, df = 4, p-value < 2.2e-16

dataTest.pred <- predict(oneRModel, newdata = dataTest)
table(dataTest$Diagnosis, dataTest.pred)
#>   dataTest.pred
#>      B  M
#>   B 28  1
#>   M  5 12

这是数据集的一个小 sn-p,正如您所见,周长是选择的单一规则因素,但我希望结果与研究对纹理、面积和平滑度的预测相关,从而提供最佳结果,但我不知道研究中围绕该变量的所有变量,这些是随机样本,所以我总是可以继续测试。

head(cancerdata)
  PatientID radius texture perimeter   area smoothness compactness concavity concavePoints symmetry  fractalDimension Diagnosis
1    842302  17.99   10.38    122.80 1001.0    0.11840     0.27760    0.3001       0.14710   0.2419          0.07871         M
2    842517  20.57   17.77    132.90 1326.0    0.08474     0.07864    0.0869       0.07017   0.1812          0.05667         M
3  84300903  19.69   21.25    130.00 1203.0    0.10960     0.15990    0.1974       0.12790   0.2069          0.05999         M
4  84348301  11.42   20.38     77.58  386.1    0.14250     0.28390    0.2414       0.10520   0.2597          0.09744         M
5  84358402  20.29   14.34    135.10 1297.0    0.10030     0.13280    0.1980       0.10430   0.1809          0.05883         M
6    843786  12.45   15.70     82.57  477.1    0.12780     0.17000    0.1578       0.08089   0.2087          0.07613         M

【问题讨论】:

  • 正在使用什么OneR 函数?什么包?什么文件?
  • 嗨@IRTFM!实际上,我在前三句话中指定了包,请继续阅读并在您阅读后发表评论,并告诉我您看到的包是什么。在同一个句子中,您会发现正在使用的函数,然后在前两个代码 sn-ps 中指定了该函数,包括警告消息,当您找到它时告诉我。你能澄清你的问题吗,你想要什么文件?
  • 我没有看到任何提到的包,也没有看到 library 或 require 电话。投票结束,需要更多细节。
  • @IRTFM 在第三句中阐明的包是插入符号,在同一句中它说confusionMatrix() 函数是问题所在。它还指定了刚刚称为 OneR() 的 OneR 函数。但是这个问题已经得到了回答,因为我用两个不同的模型两次遇到同样的事情,并推断出共同点是什么。

标签: r model classification


【解决方案1】:

根据https://topepo.github.io/caret/measuring-performance.html

灵敏度是真阳性率(预测阳性/总阳性);在这种情况下,当您告诉confusionMatrix()“正”类是“B”时:28/(28 + 1) = 0.9655

特异性是真阴性率(预测阴性/总阴性);在这种情况下,当您告诉confusionMatrix()“正”类是“B”时:12/(12 + 5) = 0.7059

看起来不一致是因为 OneR/手动混淆矩阵列表相对于混淆矩阵()产生的矩阵是倒置的。您的手动计算似乎也不正确,因为您除以总的真/假预测值,而不是总的真/假值。

【讨论】:

    【解决方案2】:

    This website 提供了一些信息,但对于 OneR 模型,很难确定要使用哪个矩阵,两者都有相似的特异性和敏感性计算,并且都有相似的混淆矩阵表。

    但是,my Zero-R question 是混淆矩阵问题的另一个问题,只是弄清楚了哪个是正确的。这个零 R 矩阵看起来是错误的,因为它表示灵敏度为 1.00,特异性为 0.00,而我的结果是,在多个试验中灵敏度为 0.6246334,特异性为 0.00。但是this website其实是清除了它,因为Zero-R模型的因子为零,灵敏度真的只有1.00,特异性是0.00。它给出了一个预测,这只是基于大多数。

    将 Zero-R 模型上正确的表交叉应用到 One-R 模型,正确的表基于以相同方式完成的相同的confusionMatrix() 函数:

    > confusionMatrix(dataTest.pred, as.factor(dataTest$Diagnosis), positive="B") 
    Confusion Matrix and Statistics
    
              Reference
    Prediction  B  M
             B 28  5
             M  1 12
    

    这些是正确的计算,与零 R 模型的 1.00 灵敏度和 0.00 特异性相关:

    Sensitivity : 0.9655          
    Specificity : 0.7059
    

    这一个在我的两个问题上都做错了,对于 Zero-R 和 One-R,大概是因为参数没有正确完成:

    > dataTest.pred <- predict(oneRModel, newdata = dataTest)
    > table(dataTest$Diagnosis, dataTest.pred)
       dataTest.pred
         B  M
      B 28  1
      M  5 12
    

    【讨论】:

    • 第一个链接似乎已损坏。你应该修复它。您还应该解释这是在什么背景下使用的。这是课堂作业吗?您是否隶属于开发人员?
    • 嗨@IRTFM,感谢您让我知道那个损坏的链接,它实际上与另一个链接相同的网站,只是在不同的页面上显示“这个网站”,很抱歉造成混淆。上下文是基于一项研究的癌症数据集,其目的是确定两个非常基本的分类模型,ZeroR 和 OneR 分类模型。这是出于教育目的,我在上课,不明白为什么一个混淆矩阵说一件事而另一个说另一件事哈哈
    猜你喜欢
    • 2017-05-17
    • 2020-08-23
    • 1970-01-01
    • 2021-04-01
    • 2021-11-12
    • 1970-01-01
    • 2014-09-22
    • 2019-01-01
    • 1970-01-01
    相关资源
    最近更新 更多