绘制ROC曲线并在特定截止信息处计算R中的AUC [英] Plot ROC curve and calculate AUC in R at specific cutoff info

查看:221
本文介绍了绘制ROC曲线并在特定截止信息处计算R中的AUC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出这样的数据:
SN =灵敏度;
SP =特异性

Given such data: SN = Sensitivity; SP = Specificity

Cutpoint        SN   1-SP
       1       0.5    0.1
       2       0.7    0.2
       3       0.9    0.6

我如何绘制ROC曲线并计算AUC。并比较两个不同ROC曲线之间的AUC。在大多数pROC或ROCR软件包中,数据输入与上面显示的不同。有人可以建议用R或其他方式解决此问题的方法吗?

How can i plot the ROC curve and calculate AUC. And compare the AUC between two different ROC curves. In the most of the packages such pROC or ROCR, the input of the data is different from those shown above. Can anybody suggest the way to solve this problem in R or by something else?

ROCsdat <- data.frame(cutpoint = c(5, 7, 9), TPR = c(0.56, 0.78, 0.91), FPR = c(0.01, 0.19, 0.58))

## plot version 1
op <- par(xaxs = "i", yaxs = "i")
plot(TPR ~ FPR, data = dat, xlim = c(0,1), ylim = c(0,1), type = "n")
with(dat, lines(c(0, FPR, 1), c(0, TPR, 1), type = "o", pch = 25, bg = "black"))
text(TPR ~ FPR, data = dat, pos = 3, labels = dat$cutpoint)
abline(0, 1)
par(op)


推荐答案

首先,我建议您访问本地图书馆并找到有关R的入门书籍。在编写自己的代码之前拥有坚实的基础非常重要,并且在互联网上找到的粘贴粘贴的代码充其量不会冒什么风险。

First off, I would recommend to visit your local library and find an introductory book on R. It is important to have a solid base before you can write your own code, and copy-pasting code found on the internet without really understanding what is means is risky at best.

关于您的问题,我相信(0,0)和(1,1)坐标是ROC曲线的一部分,因此我将它们包括在t他的数据:

Regarding your question, I believe the (0,0) and (1,1) cooordinates are part of the ROC curve so I included them in the data:

ROCsdat <- data.frame(cutpoint = c(-Inf, 5, 7, 9, Inf), TPR = c(0, 0.56, 0.78, 0.91, 1), FPR = c(0, 0.01, 0.19, 0.58, 1)) 



AUC



我强烈建议您在R训练的这个阶段不要建立自己的梯形积分功能。这太错误了-

AUC

I strongly recommend against setting up your own trapezoid integration function at this stage of your training in R. It's too error-prone and easy to screw up with a small (syntax) mistake.

相反,请使用完善的集成代码,例如 trapz 函数中的$ c>函数:

Instead, use a well established integration code like the trapz function in pracma:

library(pracma)
trapz(ROCsdat$FPR, ROCsdat$TPR)



绘图



我想您大部分都可以得到绘图,尽管我会写得稍有不同:

Plotting

I think you mostly got the plotting, although I would write it slightly differently:

plot(TPR ~ FPR, data = ROCsdat, xlim = c(0,1), ylim = c(0,1), type="b", pch = 25, bg = "black")
text(TPR ~ FPR, data = ROCsdat, pos = 3, labels = ROCsdat$cutpoint)
abline(0, 1, col="lightgrey")



比较



为了比较,假设您有 auc1 auc2 中的两个AUC。 if / else语法如下:

Comparison

For the comparison, let's say you have two AUCs in auc1 and auc2. The if/else syntax looks like this:

if (auc1 < auc2) {
    cat("auc1 < auc2!\n")
} else if (auc1 == auc2) {
    cat("aucs are identical!\n")
} else {
    cat("auc1 > auc2!\n")
}

这篇关于绘制ROC曲线并在特定截止信息处计算R中的AUC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆