# NOT RUN {
data(aSAH)
# Print a roc object:
rocobj <- roc(aSAH$outcome, aSAH$s100b, percent = TRUE)
# Get the coordinates of S100B threshold 0.55
coords(rocobj, 0.55, transpose = FALSE)
# Get the coordinates at 50% sensitivity
coords(roc=rocobj, x=50, input="sensitivity", transpose = FALSE)
# Can be abbreviated:
coords(rocobj, 50, "se", transpose = FALSE)
# Works with smoothed ROC curves
coords(smooth(rocobj), 90, "specificity", transpose = FALSE)
# Get the sensitivities for all thresholds
sensitivities <- coords(rocobj, rocobj$thresholds, "thr", "se", transpose = FALSE)
print(sensitivities)
# This is equivalent to taking sensitivities from rocobj directly
stopifnot(all.equal(as.vector(rocobj$sensitivities), as.vector(sensitivities)))
# You could also write:
sensitivities <- coords(rocobj, "all", ret="se", transpose = FALSE)
stopifnot(all.equal(as.vector(rocobj$sensitivities), as.vector(sensitivities)))
# Get the best threshold
coords(rocobj, "b", ret="t", transpose = FALSE)
# Get the best threshold according to different methods
rocobj <- roc(aSAH$outcome, aSAH$ndka, percent=TRUE)
coords(rocobj, "best", ret="threshold", transpose = FALSE,
best.method="youden") # default
coords(rocobj, "best", ret="threshold", transpose = FALSE,
best.method="closest.topleft")
# and with different weights
coords(rocobj, "best", ret="threshold", transpose = FALSE,
best.method="youden", best.weights=c(50, 0.2))
coords(rocobj, "best", ret="threshold", transpose = FALSE,
best.method="closest.topleft", best.weights=c(5, 0.2))
# and plot them
plot(rocobj, print.thres="best", print.thres.best.method="youden")
plot(rocobj, print.thres="best", print.thres.best.method="closest.topleft")
plot(rocobj, print.thres="best", print.thres.best.method="youden",
print.thres.best.weights=c(50, 0.2))
plot(rocobj, print.thres="best", print.thres.best.method="closest.topleft",
print.thres.best.weights=c(5, 0.2))
# Return more values:
coords(rocobj, "best", ret=c("threshold", "specificity", "sensitivity", "accuracy",
"precision", "recall"), transpose = FALSE)
# Return all values
coords(rocobj, "best", ret = "all", transpose = FALSE)
# You can use coords to plot for instance a sensitivity + specificity vs. cut-off diagram
plot(specificity + sensitivity ~ threshold,
coords(rocobj, "all", transpose = FALSE),
type = "l", log="x",
subset = is.finite(threshold))
# Plot the Precision-Recall curve
plot(precision ~ recall,
coords(rocobj, "all", ret = c("recall", "precision"), transpose = FALSE),
type="l", ylim = c(0, 100))
# Alternatively plot the curve with TPR and FPR instead of SE/SP
# (identical curve, only the axis change)
plot(tpr ~ fpr,
coords(rocobj, "all", ret = c("tpr", "fpr"), transpose = FALSE),
type="l")
# }
Run the code above in your browser using DataLab