[英]Plot ROC curve and calculate AUC in R at specific cutoff info
Given such data: SN = Sensitivity; 给定这样的数据:SN =灵敏度; SP = Specificity SP =特异性
Cutpoint SN 1-SP
1 0.5 0.1
2 0.7 0.2
3 0.9 0.6
How can i plot the ROC curve and calculate AUC. 如何绘制ROC曲线并计算AUC。 And compare the AUC between two different ROC curves. 并比较两个不同ROC曲线之间的AUC。 In the most of the packages such pROC or ROCR, the input of the data is different from those shown above. 在大多数pROC或ROCR软件包中,数据输入与上面显示的不同。 Can anybody suggest the way to solve this problem in R or by something else? 有人可以建议用R或其他方法解决此问题的方法吗?
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)
I suppose you could just compute it manually: 我想您可以手动计算它:
dat <- data.frame(tpr=c(0, .5, .7, .9, 1), fpr=c(0, .1, .2, .6, 1))
sum(diff(dat$fpr) * (dat$tpr[-1] + dat$tpr[-length(dat$tpr)]) / 2)
# [1] 0.785
You need to have the tpr
and fpr
vectors begin with 0 and end with 1 to compute the AUC properly. 您需要让tpr
和fpr
向量以0开头并以1结尾才能正确计算AUC。
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. 首先,我建议您访问当地的图书馆,并找到有关R的入门书籍。拥有坚实的基础很重要,然后您才能编写自己的代码,并在互联网上找到粘贴粘贴的代码而不真正理解其含义。风险最大。
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: 关于您的问题,我相信(0,0)和(1,1)坐标是ROC曲线的一部分,因此我将它们包括在数据中:
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))
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. 我强烈建议您在R的此阶段培训中不要设置自己的梯形积分功能。它太容易出错,容易被小(语法)错误搞乱。
Instead, use a well established integration code like the trapz
function in pracma
: 相反,请使用完善的集成代码,例如pracma
的trapz
函数:
library(pracma)
trapz(ROCsdat$FPR, ROCsdat$TPR)
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")
For the comparison, let's say you have two AUCs in auc1
and auc2
. 为了进行比较,假设您在auc1
和auc2
有两个AUC。 The if/else syntax looks like this: if / else语法如下所示:
if (auc1 < auc2) {
cat("auc1 < auc2!\n")
} else if (auc1 == auc2) {
cat("aucs are identical!\n")
} else {
cat("auc1 > auc2!\n")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.