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")
绘制曲线示例数据。
标签:曲线,ROC,library,ggplot2,install,Rcpp,packages,pROC 来源: https://www.cnblogs.com/ak422/p/16115148.html