data(aSAH)
## Build a ROC object and compute the AUC ##
roc1 <- roc(aSAH$outcome, aSAH$s100b)
print(roc1)
# With a formula
roc(outcome ~ s100b, aSAH)
# With pipes, dplyr-style:
if (FALSE) {
library(dplyr)
aSAH %>% roc(outcome, s100b)}
# Create a few more curves for the next examples
roc2 <- roc(aSAH$outcome, aSAH$wfns)
roc3 <- roc(aSAH$outcome, aSAH$ndka)
## AUC ##
auc(roc1, partial.auc = c(1, .9))
## Smooth ROC curve ##
smooth(roc1)
## Summary statistics
var(roc1)
cov(roc1, roc3)
## Plot the curve ##
plot(roc1)
# More plotting options, CI and plotting
# with all-in-one syntax:
roc4 <- 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)
# Add to an existing plot. Beware of 'percent' specification!
roc5 <- roc(aSAH$outcome, aSAH$wfns,
plot=TRUE, add=TRUE, percent=roc4$percent)
## With ggplot2 ##
if (require(ggplot2)) {
# Create multiple curves to plot
rocs <- roc(outcome ~ wfns + s100b + ndka, data = aSAH)
ggroc(rocs)
}
## Coordinates of the curve ##
coords(roc1, "best", ret=c("threshold", "specificity", "1-npv"))
coords(roc2, "local maximas", ret=c("threshold", "sens", "spec", "ppv", "npv"))
## Confidence intervals ##
# CI of the AUC
ci(roc2)
if (FALSE) {
# CI of the curve
sens.ci <- ci.se(roc1, specificities=seq(0, 100, 5))
plot(sens.ci, type="shape", col="lightblue")
plot(sens.ci, type="bars")}
# need to re-add roc2 over the shape
plot(roc2, add=TRUE)
if (FALSE) {
# CI of thresholds
plot(ci.thresholds(roc2))}
# In parallel
if (require(doParallel)) {
registerDoParallel(cl <- makeCluster(getOption("mc.cores", 2L)))
if (FALSE) ci(roc2, method="bootstrap", parallel=TRUE)
ci(roc2, method="bootstrap", parallel=TRUE, boot.n=20)
stopCluster(cl)
}
## Comparisons ##
# Test on the whole AUC
roc.test(roc1, roc2, reuse.auc=FALSE)
if (FALSE) {
# Test on a portion of the whole AUC
roc.test(roc1, roc2, reuse.auc=FALSE, partial.auc=c(100, 90),
partial.auc.focus="se", partial.auc.correct=TRUE)
# With modified bootstrap parameters
roc.test(roc1, roc2, reuse.auc=FALSE, partial.auc=c(100, 90),
partial.auc.correct=TRUE, boot.n=1000, boot.stratified=FALSE)}
## Power & sample size ##
# Power
# 1 curve
power.roc.test(roc1)
# 2 curves
power.roc.test(roc3, roc2)
# Sample size
# 1 curve
power.roc.test(roc3, power = 0.9)
# 2 curves
power.roc.test(roc1, roc2, power = 0.9)
# Also without ROC objects.
# For instance what AUC would be significantly different from 0.5?
power.roc.test(ncases=41, ncontrols=72, sig.level=0.05, power=0.95)
Run the code above in your browser using DataLab