ROC分析与统计检验

| November 5, 2017

{% include toc title=“Table” icon=“file-text” %}

pROC is a R package for visualizing, smoothing and comparing receiver operating characteristic (ROC) curves.1 2

The (Partial) area under the curve (AUC) can be compared with statistical tests based on U-statistics or bootstrap. 3 Confidence intervals can be computed for (p)AUC or ROC curves. Sample size / power computation for one or two ROC curves are available.

百度百科:敏感度、特异度

考虑一个二分问题,即将实例分成正类(positive)或负类(negative)。对一个二分问题来说,会出现四种情况。如果一个实例是正类并且也被 预测成正类,即为真正类(True positive),如果实例是负类被预测成正类,称之为假正类(False positive)。相应地,如果实例是负类被预测成负类,称之为真负类(True negative),正类被预测成负类则为假负类(false negative)。

列联表如下表所示,1代表正类,0代表负类。

预测
10合计
实际1True Positive(TP)False Negative(FN)Actual Positive(TP+FN)
0False Positive(FP)True Negative(TN)Actual Negative(FP+TN)
合计Predicted Positive(TP+FP)Predicted Negative(FN+TN)TP+FP+FN+TN

从列联表引入两个新名词。其一是真正类率(true positive rate ,TPR), 计算公式为TPR=TP/ (TP+ FN),刻画的是分类器所识别出的 正实例占所有正实例的比例。另外一个是假正类率(false positive rate, FPR),计算公式为*FPR= FP / (FP + TN),*计算的是分类器错认为正类的负实例占所有负实例的比例。还有一个真负类率(True Negative Rate,TNR),也称为specificity,计算公式为TNR=TN/ (FP+ TN) = 1 - FPR

在一个二分类模型中,对于所得到的连续结果,假设已确定一个阈值,比如说 0.6,大于这个值的实例划归为正类,小于这个值则划到负类中。如果减小阈值,减到0.5,固然能识别出更多的正类,也就是提高了识别出的正例占所有正例的比例,即TPR,但同时也将更多的负实例当作了正实例,即提高了FPR。为了形象化这一变化,在此引入ROC。

Receiver Operating Characteristic,翻译为"接受者操作特性曲线",够拗口的。曲线由两个变量1-specificity 和 Sensitivity绘制. 1-specificity=FPR,即假正类率。Sensitivity即是真正类率,TPR(True positive rate),反映了正类覆盖程度。这个组合以1-specificity对sensitivity,即是以代价(costs)对收益(benefits)。

下表是一个逻辑回归得到的结果。将得到的实数值按大到小划分成10个个数 相同的部分。

Percentile实例数正例数1-特异度(%)敏感度(%)
106179748792.7334.64
206179728049.8054.55
3061797216518.2269.92
4061797150628.0180.62
506179798738.9087.62
606179752950.7491.38
706179736562.9393.97
806179729475.2696.06
906179729787.5998.17
10061797258100.00100.00

其正例数为此部分里实际的正类数。也就是说,将逻辑回归得到的结 果按从大到小排列,倘若以前10%的数值作为阈值,即将前10%的实例都划归为正类,6180个。其中,正确的个数为4879个,占所有正类的 4879/14084100%=34.64%,即敏感度;另外,有6180-4879=1301个负实例被错划为正类,占所有负类的1301 /47713100%=2.73%,即1-特异度。以这两组值分别作为y值(敏感度)和x值(1-特异度),在excel中作散点图,就可以得到ROC曲线。

mtcars数据

head(mtcars)
mpgcyldisphpdratwtqsecvsam
Mazda RX421.061601103.902.62016.4601
Mazda RX4 Wag21.061601103.902.87517.0201
Datsun 71022.84108933.852.32018.6111
Hornet 4 Drive21.462581103.083.21519.4410
Hornet Sportabout18.783601753.153.44017.0200
Valiant18.162251052.763.46020.2210

ROC曲线

library(pROC)

par(mfrow=c(1,2))
plot(vs~wt, mtcars)
plot(vs~am, mtcars)

![png]({{ site.url }}{{ site.baseurl }}/assets//img2017/Rplot1.jpeg)

par(mfrow=c(1,1))
plot.roc(vs~wt,mtcars,col="1")
lines.roc(vs~am,mtcars,col='2')

![png]({{ site.url }}{{ site.baseurl }}/assets//img2017/Rplot2.jpeg)

显著性检验

r1 = roc(vs~wt,mtcars)
r2 = roc(vs~am,mtcars)
roc.test(r1,r2, boot.n = 2000)
|===========================================| 100%

	Bootstrap test for two correlated ROC curves

data:  r1 and r2
D = 4.5831, boot.n = 2000, boot.stratified =
1, p-value = 4.581e-06
alternative hypothesis: true difference in AUC is not equal to 0
sample estimates:
AUC of roc1 AUC of roc2
  0.8412698   0.5833333
roc.test(roc1, roc2, method="venkatraman")
|===========================================| 100%

Venkatraman's test for two paired ROC curves

data:  roc1 and roc2
E = 578, boot.n = 2000, p-value = 0.024
alternative hypothesis: true difference in AUC is not equal to 0

Reference


  1. Tom Fawcett (2006) “An introduction to ROC analysis”. Pattern Recognition Letters 27, 861–874. DOI: 10.1016/j.patrec.2005.10.010. ↩︎

  2. Xavier Robin, Natacha Turck, Alexandre Hainard, et al. (2011) “pROC: an open-source package for R and S+ to analyze and compare ROC curves”. BMC Bioinformatics, 7, 77. DOI: 10.1186/1471-2105-12-77. ↩︎

  3. Elisabeth R. DeLong, David M. DeLong and Daniel L. Clarke-Pearson (1988) “Comparing the areas under two or more correlated receiver operating characteristic curves: a nonparametric approach”. Biometrics 44, 837–845. ↩︎