R- ROC曲線


R 語言繪制ROC 曲線代碼。

 

  1 # install.packages("pROC")     # 下載 pROC 包
  2 # install.packages("ggplot2")  # 下載 ggplot2 包
  3 # install.packages("Rcpp")
  4 
  5 
  6 library(pROC)      # 加載pROC包
  7 library(ggplot2)  # 調用ggplot2包以利用ggroc函數
  8 library(Rcpp)      # 加載Rcpp包
  9 
 10 # dev.new()
 11 data(aSAH)   # 加載數據集
 12 head(aSAH)   # 查看數據集前6行
 13 roc1 <- roc(aSAH$outcome,aSAH$s100b,
 14             percent=T,  #設置ROC曲線顯示為百分比形式
 15             # # arguments for auc   繪制置信區域
 16             # partial.auc=c(100, 90), partial.auc.correct=TRUE,
 17             # partial.auc.focus="sens"
 18             );roc1  # Build a ROC object and compute the AUC
 19 roc2 <- roc(aSAH$outcome, aSAH$ndka, 
 20             percent=T,  #設置ROC曲線顯示為百分比形式
 21             );roc2  # Create a few more curves for the next examples
 22 # smooth=TRUE 繪制光滑曲線,默認為F,可省略
 23 auc(roc1);ci(roc1)
 24 auc(roc2);ci(roc2)
 25 
 26 roc.test(roc1,roc2,method = "delong") # 其他兩種方法 “bootstrap”或“venkatraman”
 27 # roc.test(roc1,roc2,method = "bootstrap") # 其他兩種方法 “bootstrap”或“venkatraman”
 28 # roc.test(roc1,roc2,method = "venkatraman") # 其他兩種方法 “bootstrap”或“venkatraman”
 29 
 30 # plot(roc1,
 31 #      print.auc=TRUE,print.auc.x=0.5,print.auc.y=0.5,  # 圖像上輸出AUC值,坐標為(x,y)
 32 #      auc.polygon=TRUE, auc.polygon.col="skyblue",  # 設置ROC曲線下填充色
 33 #      max.auc.polygon=TRUE,   # 填充整個圖像
 34 #      partial.auc=c(1, 0.8), partial.auc.focus="sp",  # sp改為se試試
 35 #      grid=c(0.1, 0.2),grid.col=c("green", "red"), # 設置間距為0.1,0.2,線條顏色
 36 #      print.thres=TRUE,   # 圖像上輸出最佳截斷值
 37 #      reuse.auc=F)
 38 
 39 # plot(roc1, # roc1換為roc2,更改參數可繪制roc2曲線
 40 #      print.auc=TRUE,print.auc.x=0.5,print.auc.y=0.5, # 圖像上輸出AUC值,坐標為(x,y)
 41 #      auc.polygon=TRUE, auc.polygon.col="skyblue", # 設置ROC曲線下填充色
 42 #      max.auc.polygon=TRUE, # 填充整個圖像
 43 #      grid=c(0.1,0.2), grid.col=c("green", "red"), # 設置間距為0.1,0.2,線條顏色
 44 #      print.thres=TRUE, print.thres.cex=0.8,  # 圖像上輸出最佳截斷值,字體縮放0.8倍
 45 #      legacy.axes=TRUE)  # 使橫軸從0到1,表示為1-特異度
 46 
 47 opar<-par(no.readonly = T)
 48 par(xaxs = 'i', yaxs = "i",
 49     mar=c(4, 4, 0, 0)+.1,
 50     mgp=c(2.5, 1, 0),
 51     # pin = c(2,3),#繪圖尺寸
 52     omd=c(0, 0.95, 0, 0.95)
 53     )
 54 plot(roc1, 
 55      print.auc=TRUE, print.auc.x=40, print.auc.y=45,
 56      # 圖像上輸出AUC值,坐標為(x,y)
 57      auc.polygon=TRUE, auc.polygon.col="#fff7f7", # 設置ROC曲線下填充色
 58      max.auc.polygon=FALSE,  # 填充整個圖像
 59      grid=c(20, 30), grid.col=c("black", "black"),  # 設置間距為0.1,0.2,線條顏色
 60      # print.thres=TRUE, print.thres.cex=0.9, # 圖像上輸出最佳截斷值,字體縮放倍數
 61      smooth=F, # 繪制不平滑曲線
 62 
 63      main="Comparison of two ROC curves", # 添加標題
 64      col="#FF2E63",  # 曲線顏色
 65      legacy.axes=T,   # 使橫軸從0到1,表示為1-特異度,否則橫軸從1-0
 66      # xlim = c(1,0),ylim = c(0.108,1),
 67      # xlim=if(roc1$percent){c(100, 0)} else{c(1, 0)},
 68      # ylim=if(roc1$percent){c(0, 100)} else{c(0, 1)},
 69      
 70      xlab=ifelse(roc1$percent, ifelse(T, "Specificity (%)", "Specificity (%)"),
 71                  ifelse(T, "1 - Specificity", "Specificity")),
 72      ylab=ifelse(roc1$percent, "Sensitivity (%)", "Sensitivity"),
 73      #設置最佳截斷值顯示為百分比格式
 74      # print.thres.pattern=ifelse(roc1$percent, "%.1f%% (%.1f%%, %.1f%%)", "%.3f (%.3f, %.3f)"),
 75      xaxt="n", yaxt="n",  #先不顯示坐標軸刻度
 76 
 77      )
 78 axis(1,at = c(0,20,40,60,80,100),labels = c(0,20,40,60,80,100))
 79 axis(2,at = c(0,20,40,60,80,100),labels = c(0,20,40,60,80,100))
 80 # box("plot",lty=2,lwd=4,col="red")
 81 # box("inner",lty=2,lwd=4,col="green")
 82 # box("outer",lty=2,lwd=4,col="yellow")
 83 # box("figure",lty=2,lwd=4,col="blue")
 84 
 85 plot.roc(roc2,
 86          add=T,  # 增加曲線
 87          col="#252A34", # 曲線顏色為紅色
 88          # print.thres.pattern=ifelse(roc2$percent, "%.1f%% (%.1f%%, %.1f%%)", "%.3f (%.3f, %.3f)"),
 89          # print.thres=TRUE, print.thres.cex=0.9,  # 圖像上輸出最佳截斷值,字體縮放倍數
 90          print.auc=TRUE, print.auc.x=40,print.auc.y=40,
 91          # 圖像上輸出AUC值,坐標為(x,y)
 92          smooth = F,   # 繪制不平滑曲線
 93          legacy.axes=F,   # 使橫軸從0到1,表示為1-特異度
 94          xaxs = 'i', yaxs = "i",   #圖形與邊框間距
 95          )  
 96 
 97 
 98 
 99 # dev.off()
100 testobj <- roc.test(roc1,roc2)   # 檢驗兩條曲線
101 text(40, 33.5, labels=paste("P value =", format.pval(testobj$p.value)), adj=c(0, .5)) # 在圖上添加P值
102 legend(95,95,  # 圖例位置
103        bty = "n",  # 圖例樣式,默認為 "o"
104        title="",   # 引號內添加圖例標題
105        legend=c("roc1","roc2"),  # 添加分組
106        col=c("#FF2E63","#252A34"),  # 顏色跟前面一致
107        lwd=2)  # 線條粗細
108 
109 # par(opar)
110 # ggroc函數繪制多條曲線
111 # roc.list <- roc(outcome ~ wfns + s100b + ndka, data = aSAH)
112 # ggroc1 <- ggroc(roc.list,
113 #                 legacy.axes = TRUE); ggroc1
114 # ggroc1 + xlab("1-specificity") + ylab("Sensitivity") +
115 #   theme_minimal() + ggtitle("My ROC curve") +
116 #   geom_segment(aes(x = 0,xend = 1,y = 0,yend = 1),col = "darkgrey",linetype = "dashed")

繪制曲線示例數據。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM