以下是本包中常用的一些缩写
ROC: receiver operating characteristic,ROC曲线
AUC: area under the ROC curve,曲线下面积
pAUC: partial area under the ROC curve 部分曲线下面积
CI: confidence interval 可信区间
SP: specificity 特异度
SE: sensitivity 灵敏度
require(pROC)
## Loading required package: pROC
## Type 'citation("pROC")' for a citation.
##
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
##
## cov, smooth, var
data(aSAH)
if(!require(DT)) install.packages(DT)
## Loading required package: DT
DT::datatable(aSAH)
aSAH[1:5,1:5]
## gos6 outcome gender age wfns
## 29 5 Good Female 42 1
## 30 5 Good Female 37 1
## 31 5 Good Female 42 1
## 32 5 Good Female 27 1
## 33 1 Poor Female 42 3
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.6.1
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
aSAH %>%
filter(gender == "Female") %>%
roc(outcome, s100b)
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
##
## Call:
## roc.data.frame(data = ., response = outcome, predictor = s100b)
##
## Data: s100b in 50 controls (outcome Good) < 21 cases (outcome Poor).
## Area under the curve: 0.72
library(dplyr)
aSAH %>%
filter(gender == "Female") %>%
roc(outcome, s100b) %>%
coords(transpose=FALSE) %>%
filter(sensitivity > 0.6,
specificity > 0.6)
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
## threshold specificity sensitivity
## 1 0.155 0.68 0.6666667
## 2 0.165 0.74 0.6666667
## 3 0.175 0.76 0.6666667
## 4 0.185 0.78 0.6666667
## 5 0.215 0.80 0.6666667
## 6 0.245 0.82 0.6666667
## 7 0.255 0.82 0.6190476
# Build a ROC object and compute the AUC
roc(aSAH$outcome, aSAH$s100b)
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
##
## 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
roc(outcome ~ s100b, aSAH)
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
##
## Call:
## roc.formula(formula = outcome ~ s100b, data = aSAH)
##
## Data: s100b in 72 controls (outcome Good) < 41 cases (outcome Poor).
## Area under the curve: 0.7314
# Smooth ROC curve
roc(outcome ~ s100b, aSAH, smooth=TRUE)
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
##
## Call:
## roc.formula(formula = outcome ~ s100b, data = aSAH, smooth = TRUE)
##
## Data: s100b in 72 controls (outcome Good) < 41 cases (outcome Poor).
## Smoothing: binormal
## Area under the curve: 0.74
# more options, CI and plotting
roc1 <- roc(aSAH$outcome,
aSAH$s100b, percent=TRUE,
# arguments for auc
partial.auc=c(100, 90), partial.auc.correct=TRUE,
partial.auc.focus="sens",
# arguments for ci
ci=TRUE, boot.n=100, ci.alpha=0.9, stratified=FALSE,
# arguments for plot
plot=TRUE, auc.polygon=TRUE, max.auc.polygon=TRUE, grid=TRUE,
print.auc=TRUE, show.thres=TRUE)
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
## 在原有图形上继续绘制
roc2 <- roc(aSAH$outcome, aSAH$wfns,
plot=TRUE, add=TRUE, percent=roc1$percent)
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
## Coordinates of the curve ##
coords(roc1, "best", ret=c("threshold", "specificity", "1-npv"),transpose = FALSE
)
## threshold specificity 1-npv
## best 0.075 22.22222 20
coords(roc2, "local maximas", ret=c("threshold", "sens", "spec", "ppv", "npv"),transpose = FALSE)
## threshold sensitivity specificity ppv npv
## local.maximas -Inf 100.00000 0.00000 36.28319 NaN
## local.maximas.1 1.5 95.12195 51.38889 52.70270 94.87179
## local.maximas.2 2.5 65.85366 79.16667 64.28571 80.28169
## local.maximas.3 3.5 63.41463 83.33333 68.42105 80.00000
## local.maximas.4 4.5 43.90244 94.44444 81.81818 74.72527
## local.maximas.5 Inf 0.00000 100.00000 NaN 63.71681
# CI of the AUC
ci(roc2)
## 95% CI: 74.85%-89.88% (DeLong)
roc1 <- roc(aSAH$outcome,
aSAH$s100b, percent=TRUE,
# arguments for auc
partial.auc=c(100, 90), partial.auc.correct=TRUE,
partial.auc.focus="sens",
# arguments for ci
ci=TRUE, boot.n=100, ci.alpha=0.9, stratified=FALSE,
# arguments for plot
plot=TRUE, auc.polygon=TRUE, max.auc.polygon=TRUE, grid=TRUE,
print.auc=TRUE, show.thres=TRUE)
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
plot(roc2, add=TRUE)
# Test on the whole AUC
roc.test(roc1, roc2, reuse.auc=FALSE)
##
## DeLong's test for two correlated ROC curves
##
## data: roc1 and roc2
## Z = -2.209, p-value = 0.02718
## alternative hypothesis: true difference in AUC is not equal to 0
## sample estimates:
## AUC of roc1 AUC of roc2
## 73.13686 82.36789
# Create a basic roc object
data(aSAH)
rocobj <- roc(aSAH$outcome, aSAH$s100b)
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
rocobj2 <- roc(aSAH$outcome, aSAH$wfns)
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
library(ggplot2)
g <- ggroc(rocobj)
g
ggroc(rocobj, alpha = 0.5, colour = "red", linetype = 2, size = 2)
# You can then your own theme, etc.
g + theme_minimal() + ggtitle("My ROC curve") +
geom_segment(aes(x = 1, xend = 0, y = 0, yend = 1), color="grey", linetype="dashed")
# And change axis labels to FPR/FPR
gl <- ggroc(rocobj, legacy.axes = TRUE)
gl
gl + xlab("FPR") + ylab("TPR") +
geom_segment(aes(x = 0, xend = 1, y = 0, yend = 1), color="darkgrey", linetype="dashed")
# Multiple curves:
g2 <- ggroc(list(s100b=rocobj, wfns=rocobj2, ndka=roc(aSAH$outcome, aSAH$ndka)))
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
g2
# This is equivalent to using roc.formula:
roc.list <- roc(outcome ~ s100b + ndka + wfns, data = aSAH)
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
## Setting levels: control = Good, case = Poor
## Setting direction: controls < cases
g.list <- ggroc(roc.list)
g.list
# with additional aesthetics:
g3 <- ggroc(roc.list, size = 1.2,alpha=.6)
g3+ggsci::scale_color_lancet()
g4 <- ggroc(roc.list, aes="linetype", color="red")
g4
# changing multiple aesthetics:
g5 <- ggroc(roc.list, aes=c("linetype", "color"))
g5
# OR faceting
g.list + facet_grid(.~name) + theme(legend.position="none")
# To have all the curves of the same color, use aes="group":
g.group <- ggroc(roc.list, aes="group",color="red")
g.group
g.group + facet_grid(.~name)