目录
proc是一个专门用来计算和绘制roc曲线的r包,目前已被cran收录,因此安装也非常简单,同时该包也兼容ggplot2函数绘图,本次就教大家怎么用proc来快速画出roc图。在医学领域主要用于判断某种因素对于某种疾病的诊断是否有诊断价值。什么是roc曲线和auc,以及如何去看roc曲线的结果,可以这样总结:roc曲线呢,其实就是每个对应的cutoff值都有一个对应的真阳性率(纵坐标)和假阳性率(横坐标),比如选择了10个cutoff值,那就相当于有个10个点,把这些点连成一条线就是roc曲线。auc值就是roc曲线下的面积,一般认为auc值在0.7~1之间,模型预测的结果才有效。tpr(真阳性率) = tp(真阳)/(tp(真阳) fn(假阴)),fpr(假阳性率) = fp(假阳) / (fp(假阳) tn(真阴))。 比如下面的一个模型预测后的数据结果:
上图中如果选cutoff值为0.5时 tpr = 5 /(5 0)= 1, fpr = 2 / (2 3) = 0.4, 预测的准确性 = (tp tn )/ 总的样本数 = (5 3)/10 = 0.8
好了,话不多说,我们直接上代码
1.读取数据
library(openxlsx) roc <- read.xlsx("roc曲线.xlsx")
2.auc和ci的计算
library(proc) ## roc的计算,可以一次性批量计算a、b、c三组数据 res<-roc(outcome~a b c,data=roc,aur=true, ci=true, # 显示95%ci # percent=true, ##是否需要以百分比显示 levels=c('group1','group2'),direction=">" #设置分组方向 ) ## 平滑曲线的roc结果 smooth<-roc(outcome~a b c,data=roc,aur=true, ci=true, # 显示95%ci # percent=true, ##是否需要以百分比显示 smooth=true, levels=c('group1','group2'),direction=">" #设置分组方向 )
显示非平滑roc曲线的结果
res call: roc.formula(formula = outcome ~ a, data = roc, aur = true, ci = true, levels = c("group1", "group2"), direction = ">") data: a in 40 controls (outcome group1) > 32 cases (outcome group2). area under the curve: 0.7328 95% ci: 0.6171-0.8485 (delong) $b call: roc.formula(formula = outcome ~ b, data = roc, aur = true, ci = true, levels = c("group1", "group2"), direction = ">") data: b in 40 controls (outcome group1) > 32 cases (outcome group2). area under the curve: 0.8234 95% ci: 0.7303-0.9165 (delong) $c call: roc.formula(formula = outcome ~ c, data = roc, aur = true, ci = true, levels = c("group1", "group2"), direction = ">") data: c in 40 controls (outcome group1) > 32 cases (outcome group2). area under the curve: 0.9242 95% ci: 0.8679-0.9805 (delong)
3.利用ggplot2绘图
library(ggplot2) pa<- ggroc(smooth$a, legacy.axes = true # 将x轴改为0-1,(默认是1-0) ) geom_segment(aes(x = 0, xend = 1, y = 0, yend = 1), color="darkgrey", linetype=4) theme_bw() # 设置背景 ggtitle('a-roc') pb<- ggroc(smooth$b, legacy.axes = true) geom_segment(aes(x = 0, xend = 1, y = 0, yend = 1), color="darkgrey", linetype=4) theme_bw() ggtitle('b-roc') pc<- ggroc(smooth$c, legacy.axes = true) geom_segment(aes(x = 0, xend = 1, y = 0, yend = 1), color="darkgrey", linetype=4) theme_bw() ggtitle('c-roc') cowplot::plot_grid(pa,pb,pc,labels = "auto",nrow = 1)
4.合并多个roc曲线结果
ggroc(smooth, legacy.axes = true) geom_segment(aes(x = 0, xend = 1, y = 0, yend = 1), color="darkgrey", linetype=4) theme_bw() ggtitle('roc') ggsci::scale_color_lancet() annotate("text",x=0.75,y=0.125,label=paste("a-auc = ", round(res$a$auc,3))) annotate("text",x=0.75,y=0.25,label=paste("b-auc = ", round(res$b$auc,3))) annotate("text",x=0.75,y=0.375,label=paste("c-auc = ", round(res$c$auc,3)))