任务描述:采取R语言绘制ROC曲线
参考文献:link.
专业背景:临床医学(心血管病学)
数据库:R语言的公共数据库
软件环境
R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
> library(pROC)
Type 'citation("pROC")' for a citation.
载入程辑包:‘pROC’
The following objects are masked from ‘package:stats’:
cov, smooth, var
> aSAH
gos6 outcome gender age wfns s100b ndka
29 5 Good Female 42 1 0.13 3.01
30 5 Good Female 37 1 0.14 8.54
31 5 Good Female 42 1 0.10 8.09
32 5 Good Female 27 1 0.04 10.42
33 1 Poor Female 42 3 0.13 17.40
34 1 Poor Male 48 2 0.10 12.75
##### Build a ROC object and compute the AUC
> roc(aSAH$outcome,aSAH$s100b)
Call:
roc.default(response = aSAH$outcome, predictor = aSAH$s100b)
Data: aSAH$s100b in 72 controls (aSAH$outcome Good) < 41 cases (aSAH$outcome Poor).
Area under the curve: 0.7314
//# Smooth ROC curve
> roc(aSAH$outcome,aSAH$s100b,smooth = TRUE)
Call:
roc.default(response = aSAH$outcome, predictor = aSAH$s100b, smooth = TRUE)
Data: aSAH$s100b in 72 controls (aSAH$outcome Good) < 41 cases (aSAH$outcome Poor).
Smoothing: binormal
Area under the curve: 0.74
> levels(aSAH$outcome)
[1] "Good" "Poor"
//# more options, CI and plotting
> roc1 <- roc(aSAH$outcome,aSAH$s100b,smooth = TRUE,percent = TRUE,partial.auc = c(100,90),partial.auc.correct = TRUE,partial.auc.focus = "sens",ci = TRUE,boot.n = 100,ci.alpha = 0.9,stratified = FALSE,plot = TRUE,auc.polygon = TRUE,max.auc.polygon = TRUE,grid = TRUE)
Call:
roc.default(response = aSAH$outcome, predictor = aSAH$s100b, percent = TRUE, smooth = TRUE, ci = TRUE, plot = TRUE, partial.auc = c(100, 90), partial.auc.correct = TRUE, partial.auc.focus = "sens", boot.n = 100, ci.alpha = 0.9, stratified = FALSE, auc.polygon = TRUE, max.auc.polygon = TRUE, grid = TRUE)
Data: aSAH$s100b in 72 controls (aSAH$outcome Good) < 41 cases (aSAH$outcome Poor).
Smoothing: binormal
Corrected partial area under the curve (sensitivity 100%-90%): 55.63%
95% CI: 50.02%-66.08% (100 stratified bootstrap replicates)
//计算曲线下面积和部分曲线下面积
> auc(roc1)
Area under the curve: 74%>
> auc(roc1,partial.auc =c(1,10))
Partial area under the curve (specificity 10%-1%): 8.857%
> auc(roc1,partial.auc =c(1,0))
Partial area under the curve (specificity 1%-0%): 0.9985%
> auc(roc1,partial.auc =c(0,100))
Partial area under the curve (specificity 100%-0%): 74%
绘图部分
####### 加载程序包 ########
> library(pROC)
> library(ggplot2)
###### 查看数据库 #######
> summary(aSAH$outcome)
Good Poor
72 41
###### 构建方程 #########
> rocs100b <- roc(aSAH$outcome, aSAH$s100b)
Setting levels: control = Good, case = Poor
Setting direction: controls < cases
> rocage <- roc(aSAH$outcome, aSAH$age)
Setting levels: control = Good, case = Poor
Setting direction: controls < cases
######### 1.0版本的图像 ############
> g <- ggroc(rocs100b)
######### 2.0版本的图像 ############
> ggroc(rocs100b, alpha = 0.5, colour = "red", linetype = 2, size = 2)
######### 3.0版本的图像 ############
> g + theme_minimal() + ggtitle("My ROC curve") +
geom_segment(aes(x = 1, xend = 0, y = 0, yend = 1), colour = "grey", linetype = "dashed")
######### 4.0版本的图像(改变X轴) ########
> g1 <- ggroc(rocs100b, legacy.axes = TRUE)
> g1
> g1 + xlab("FRR") + ylab("TRR") +
geom_segment(aes(x = 0, xend = 1, y = 0, yend = 1), colour = "darkgrey", linetype = "dashed")
######### 绘制多条曲线 ##########
> g2 <- ggroc(list(s100b = rocs100b, age = rocage), legacy.axes = TRUE)
> g2