【发布时间】: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