roc
function.## S3 method for class 'roc':
plot(x, ...)
## S3 method for class 'smooth.roc':
plot(x, ...)
## S3 method for class 'roc':
plot.roc(x, add=FALSE, reuse.auc=TRUE,
# Generic arguments for par:
xlim=if(x$percent){c(100, 0)} else{c(1, 0)},
ylim=if(x$percent){c(0, 100)} else{c(0, 1)},
xlab=ifelse(x$percent, "Specificity (%)", "Specificity"),
ylab=ifelse(x$percent, "Sensitivity (%)", "Sensitivity"),
asp=1,
mar=c(4, 4, 2, 2)+.1,
mgp=c(2.5, 1, 0),
# col, lty and lwd for the ROC line only
col=par("col"),
lty=par("lty"),
lwd=2,
type="l",
# Identity line
identity=!add,
identity.col="darkgrey",
identity.lty=1,
identity.lwd=1,
# Print the thresholds on the plot
print.thres=FALSE,
print.thres.pch=20,
print.thres.adj=c(-.05,1.25),
print.thres.col="black",
print.thres.pattern=ifelse(x$percent, "%.1f (%.1f%%, %.1f%%)", "%.3f (%.3f, %.3f)"),
print.thres.cex=par("cex"),
print.thres.pattern.cex=print.thres.cex,
# Print the AUC on the plot
print.auc=FALSE,
print.auc.pattern=NULL,
print.auc.x=ifelse(x$percent, 50, .5),
print.auc.y=ifelse(x$percent, 50, .5),
print.auc.adj=c(0,1),
print.auc.col=col,
print.auc.cex=par("cex"),
# Grid
grid=FALSE,
grid.v={if(is.logical(grid) && grid[1]==TRUE){seq(0, 1, 0.1) * ifelse(x$percent, 100, 1)} else if(is.numeric(grid)) {seq(0, ifelse(x$percent, 100, 1), grid[1])} else {NULL}},
grid.h={if (length(grid) == 1) {grid.v} else if (is.logical(grid) && grid[2]==TRUE){seq(0, 1, 0.1) * ifelse(x$percent, 100, 1)} else if(is.numeric(grid)) {seq(0, ifelse(x$percent, 100, 1), grid[2])} else {NULL}},
grid.lty=3,
grid.lwd=1,
grid.col="#DDDDDD",
# Polygon for the AUC
auc.polygon=FALSE,
auc.polygon.col="gainsboro",
auc.polygon.lty=par("lty"),
auc.polygon.density=NULL,
auc.polygon.angle=45,
auc.polygon.border=NULL,
# Polygon for the maximal AUC possible
max.auc.polygon=FALSE,
max.auc.polygon.col="#EEEEEE",
max.auc.polygon.lty=par("lty"),
max.auc.polygon.density=NULL,
max.auc.polygon.angle=45,
max.auc.polygon.border=NULL,
# Confidence interval
ci=!is.null(x$ci),
ci.type=c("bars", "shape", "no"),
ci.col=ifelse(ci.type=="bars", par("fg"), "gainsboro"),
...)
## S3 method for class 'formula':
plot.roc(x, data, ...)
## S3 method for class 'default':
plot.roc(x, predictor, ...)
## S3 method for class 'smooth.roc':
plot.roc(x, ...)
TRUE
(default) and the add=FALSE
.plot
.FALSE
, NULL
or TRUE
or type
and col
arguments for
plot.ci
. The special value roc
and plot.roc.roc
when calling
plot.roc.default
or plot.roc.formula
. Note that the
print.auc
, auc.polygon
and max.auc.polygon
arguments, an AUC specification is
required. By default, the total AUC is plotted, but you may want a
partial AUCs. The specification is defined by:
reuse.auc
is set toTRUE
(default). It is naturally
inherited from any call toroc
and fits most cases.auc
with...(argumentspartial.auc
,partial.auc.correct
andpartial.auc.focus
). In this case, you must ensure either that
theroc
object do not contain anauc
field (if
you calledroc
withauc=FALSE
), or setreuse.auc=FALSE
. If reuse.auc=FALSE
the auc
function will always
be called with ...
to determine the specification, even if
the auc
field.
As well if the auc
field, the auc
function will always be called with
...
to determine the specification.
Warning: if the roc object passed to plot.roc contains an auc
field and reuse.auc=TRUE
, auc is not called and
arguments such as partial.auc
are silently ignored.
roc
when plot=TRUE (not by
default). plot.roc.formula
and plot.roc.default
are convenience methods
that build the ROC curve (with the roc
function) before
calling plot.roc.roc
. You can pass them arguments for both
roc
and plot.roc.roc
. Simply use plot.roc
that will dispatch to the correct method.The plotting is done in the following order:
add=FALSE
.grid.v
andgrid.h
are not NULL.max.auc.polygon=TRUE
.ci=TRUE
,ci.type="shape"
andx$ci
isn't aauc.polygon=TRUE
.identity=TRUE
.ci=TRUE
,ci.type="bars"
andx$ci
isn't aprint.thres
isTRUE
or numeric.print.auc=TRUE
.Graphical functions are called with suppressWarnings.
roc
, auc
, ci
data(aSAH)
# Syntax (response, predictor):
plot.roc(aSAH$outcome, aSAH$s100b)
# With a roc object:
rocobj <- roc(aSAH$outcome, aSAH$s100b)
# identical:
plot(rocobj)
plot.roc(rocobj)
# Add a smoothed ROC:
plot.roc(smooth(rocobj), add=TRUE, col="blue")
legend("bottomright", legend=c("Empirical", "Smoothed"),
col=c(par("fg"), "blue"), lwd=2)
# With more options:
plot(rocobj, print.auc=TRUE, auc.polygon=TRUE, grid=c(0.1, 0.2),
grid.col=c("green", "red"), max.auc.polygon=TRUE,
auc.polygon.col="blue", print.thres=TRUE)
# To plot a different partial AUC, we need to ignore the existing value
# with reuse.auc=FALSE:
plot(rocobj, print.auc=TRUE, auc.polygon=TRUE, partial.auc=c(1, 0.8),
partial.auc.focus="se", grid=c(0.1, 0.2), grid.col=c("green", "red"),
max.auc.polygon=TRUE, auc.polygon.col="blue", print.thres=TRUE,
reuse.auc=FALSE)
# Add a line to the previous plot:
plot.roc(aSAH$outcome, aSAH$wfns, add=TRUE)
# Alternatively, you can get the plot directly from roc():
roc(aSAH$outcome, aSAH$s100b, plot=TRUE)
Run the code above in your browser using DataLab