pROC是一個專門用來計(jì)算和繪制ROC曲線的R包坏挠,目前已被CRAN收錄,因此安裝也非常簡單邪乍,同時該包也兼容ggplot2函數(shù)繪圖降狠,本次就教大家怎么用pROC來快速畫出ROC圖对竣。在醫(yī)學(xué)領(lǐng)域主要用于判斷某種因素對于某種疾病的診斷是否有診斷價值。什么是ROC曲線和AUC榜配,以及如何去看ROC曲線的結(jié)果否纬,可以這樣總結(jié):ROC曲線呢,其實(shí)就是每個對應(yīng)的cutoff值都有一個對應(yīng)的真陽性率(縱坐標(biāo))和假陽性率(橫坐標(biāo))蛋褥,比如選擇了10個cutoff值临燃,那就相當(dāng)于有個10個點(diǎn),把這些點(diǎn)連成一條線就是ROC曲線烙心。AUC值就是ROC曲線下的面積,一般認(rèn)為AUC值在0.7~1之間膜廊,模型預(yù)測的結(jié)果才有效。TPR(真陽性率) = TP(真陽)/(TP(真陽) + FN(假陰))弃理,F(xiàn)PR(假陽性率) = FP(假陽) / (FP(假陽) + TN(真陰))溃论。比如下面的一個模型預(yù)測后的數(shù)據(jù)結(jié)果:
image.png
上圖中如果選cutoff值為0.5時
TPR = 5 /(5 + 0)= 1,
FPR = 2 / (2 + 3) = 0.4,
預(yù)測的準(zhǔn)確性 = (TP + TN )/ 總的樣本數(shù) = (5 + 3)/10 = 0.8
好了痘昌,話不多說,我們直接上代碼
1.讀取數(shù)據(jù)
library(openxlsx)
ROC <- read.xlsx("ROC曲線.xlsx")
2.AUC和CI的計(jì)算
library(pROC)
## roc的計(jì)算炬转,可以一次性批量計(jì)算a辆苔、b、c三組數(shù)據(jù)
res<-roc(outcome~a+b+c,data=ROC,aur=TRUE,
ci=TRUE, # 顯示95%CI
# percent=TRUE, ##是否需要以百分比顯示
levels=c('group1','group2'),direction=">" #設(shè)置分組方向
)
## 平滑曲線的ROC結(jié)果
smooth<-roc(outcome~a+b+c,data=ROC,aur=TRUE,
ci=TRUE, # 顯示95%CI
# percent=TRUE, ##是否需要以百分比顯示
smooth=TRUE,
levels=c('group1','group2'),direction=">" #設(shè)置分組方向
)
顯示非平滑ROC曲線的結(jié)果
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扼劈,(默認(rèn)是1-0)
)+
geom_segment(aes(x = 0, xend = 1, y = 0, yend = 1),
color="darkgrey", linetype=4)+
theme_bw() +# 設(shè)置背景
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)
image.png
4.合并多個ROC曲線結(jié)果
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)))
image.png
需要代碼中案例數(shù)據(jù)的可以從我的博客中下載:
http://81.69.237.191/2022/05/14/R-plot-paper2/