smooth.roc(pROC) | Spotfire S+ Documentation |
This function smoothes a ROC curve of numeric predictor. By default, a binormal smoothing is performed, but density or custom smoothings are supported.
smooth(x, ...) ## Default S3 method: smooth(x, ...) ## S3 method for class 'roc': smooth(roc, method=c("binormal", "density", "fitdistr"), n=512, bandwidth = "nrd", density=NULL, density.controls=density, density.cases=density, reuse.auc=TRUE, reuse.ci=FALSE, ...) ## S3 method for class 'smooth.roc': smooth(smooth.roc, ...)
x |
a roc object from the roc function (for smooth.roc), or a vector (for the regular (s+ default) smooth function). |
roc, smooth.roc |
a “roc” object from the
roc function, or a “smooth.roc” object from the
smooth.roc function.
|
method |
“binormal”, “density”, “fitdistr”, or a function returning a list of smoothed sensitivities and specificities. |
n |
the number of equally spaced points where the smoothed curve will be calculated. |
bandwidth |
if method="density" and density.controls and
density.cases are not provided, bandwidth is passed as
width to density to determine the bandwidth of the
density Can be a character string (“nrd”, “hb”,
“ucv”, “bcv” or “sj”, but any name
matching a function prefixed with “bandwidth.” is
supported) or a numeric value, as described in
density .
Defaults to “nrd”.
|
density, density.controls, density.cases |
if
method="density" , a numeric value of density (over the y
axis) or a function returning a density (such as
density . If method="fitdistr" , one of
“normal” (default), “exponential”, “log-normal”
(same as “lognormal”) or “uniform”, stating the shape of
the underlying distribution.
If the value is different for control and case observations,
density.controls and density.cases can be employed
instead, otherwise density will be propagated to both
density.controls and density.cases .
|
reuse.auc, reuse.ci |
if TRUE (default for reuse.auc) and the “roc” objects
contain “auc” or “ci” fields, re-use these
specifications to regenerate auc or ci
on the smoothed ROC curve with the original parameters. If
FALSE , the object returned will not contain
“auc” or “ci” fields. It is currently not possible to
redefine auc and ci options directly: you need to call auc or
ci later for that.
|
... |
further arguments passed to or from other methods, and
especially to density (only cut and
window , plus kernel and adjust for
compatibility with R). Also passed to to method if it
is a function.
|
If method="binormal"
, a linear model is fitted to the quantiles of
the sensitivities and specificities. Smoothed sensitivities and
specificities are then generated from this model on n
points.
This simple approach was found to work well for most ROC curves, but
it may produce hooked smooths in some situations (see in Hanley (1988)).
With method="density"
, the density
function is employed to generate a smooth kernel
density of the control and case observations as described by Zhou
et al. (1997), unless
density.controls
or density.cases
are provided
directly. Otherwise, bandwidth
can be given to
specify a bandwidth to use with density
. It can be a
numeric value or a character string (“nrd”, “hb”,
“ucv”, “bcv” or “sj”, but any name
matching a function prefixed with “bandwidth.” is
supported). In the case of a character
string, the whole predictor data is employed to determine the numeric
value to use on both controls and cases.
Note that the width
argument to density
is here called
bandwidth
to avoid clashes with the width
argument to
plot.roc
.
Depending on your data, it might be a good idea to specify the
window
argument for density
. By default,
“gaussian” is used, but “cosine”, “3gaussian”,
“rectangular” and “triangular” are supported. As all the
window kernels are symetrical, it might help to normalize the data first
(that is, before calling roc
), for example with quantile
normalization:
norm.x <- qnorm(rank(x)/(length(x)+1)) smooth(roc(response, norm.x, ...), ...)
Additionally, density
can be a function which must return
either a numeric vector of densities over the y axis or a list
with a “y” item like the density
function. It
must accept the following input:
density.fun(x, n, from, to, width, window, ...)It is important to honour
n
, from
and to
in order
to have the densities evaluated on the same points for controls and
cases. Failing to do so and returning densities of different length
will produce an error. It is also a good idea to use a constant
smoothing parameter (such as width
) especially when controls and
cases have a different number of observations, to avoid producing
smoother or rougher densities.
If method="fitdistr"
, a function similar to the fitdistr
function from the MASS package is employed to fit parameters for
the density function density
. The density function are fitted
separately in control (density.controls
)
and case observations (density.cases
). density
can be
one of the character values “normal” (default),
“exponential”, “log-normal” or “uniform”.
No start
parameter is supported, unlike
fitdistr
in the MASS package.
Finally, method
can also be a function. It must
return a list with exactly 2 elements named “sensitivities” and
“specificities”, which must be numeric vectors between 0 and 1
or 100 (depending on the percent
argument to
roc
). It is passed all the arguments to the
smooth
function.
smooth.default
forces the usage of the default
smooth
function, so
that other code relying on smooth
should continue to function
normally.
Smoothed ROC curves can be passed to smooth again. In this case, the smoothing is not re-applied on the smoothed ROC curve but the original “roc” object will be re-used.
A list of class “smooth.roc” with the following fields:
sensitivities |
the smoothed sensitivities defining the ROC curve. |
specificities |
the smoothed specificities defining the ROC curve. |
percent |
if the sensitivities, specificities and AUC are reported in percent, as defined in argument. |
direction |
the direction of the comparison, as defined in argument. |
thresholds |
the thresholds at which the sensitivities and specificities were computed. |
call |
how the function was called. See match.call for
more details.
|
smoothing.args |
a list of the arguments used for the smoothing. Will serve to apply the smoothing again in further bootstrap operations. |
fit.controls, fit.cases |
a list similar to a result of MASS's
fitdistr function for controls and cases, but with
only “estimate”, and an additional “densfun” item
indicating the density function, if possible as character.
|
auc |
if the original ROC curve contained an AUC, it is computed again on the smoothed ROC. |
ci |
if the original ROC curve contained a CI, it is computed again on the smoothed ROC. |
Additionally, the original roc
object is stored as a
“roc” attribute.
If method
is a function, the return values will be checked
thoroughly for validity (list with two numeric elements of the same
length named “sensitivities” and “specificities” with
values in the range of possible values for sensitivities and
specificities).
The message “The 'density function must return a numeric
vector or a list with a 'y' item.” will be displayed if the
density
function did not return a valid output. The message
“Length of 'density.controls' and 'density.cases' differ.”
will be displayed if the returned value differ in length.
Binormal smoothing cannot smooth ROC curve defined by only one point. Any such attempt will fail with the error “ROC curve not smoothable (not enough points).”. It will also fail if the points are poorly distributed and no model can be fit. In such a case, the error from 'lm' is printed within the message.
If the smooth ROC curve was generated by roc
with
density.controls
and density.cases
numeric arguments, it
cannot be smoothed and the error “Cannot smooth a ROC curve
generated directly with numeric 'density.controls' and
'density.cases'.” is produced.
All three smoothing methods require a numeric predictor
.
If the ROC curve to smooth was generated with an ordered factor smoothing
cannot be applied and the message “Only ROC curves of numeric
predictors can be smoothed.” is displayed.
James E. Hanley (1988) ``The robustness of the ``binormal'' assumptions used in fitting ROC curves''. Medical Decision Making 8, 197–203.
Xavier Robin, Natacha Turck, Alexandre Hainard, et al. (2011) ``pROC: an open-source package for R and S+ to analyze and compare ROC curves''. BMC Bioinformatics, 7, 77. DOI: 10.1186/1471-2105-12-77
Kelly H. Zou, W. J. Hall and David E. Shapiro (1997) ``Smooth non-parametric receiver operating characteristic (ROC) curves for continuous diagnostic tests''. Statistics in Medicine 18, 2143–2156. DOI: 10.1002/(SICI)1097-0258(19971015)16:19<2143::AID-SIM655>3.0.CO;2-3.
data(aSAH) ## Basic example rocobj <- roc(aSAH$outcome, aSAH$s100b) smooth(rocobj) # or directly with roc() roc(aSAH$outcome, aSAH$s100b, smooth=TRUE) # plotting plot(rocobj) rs <- smooth(rocobj, method="binormal") plot(rs, add=TRUE, col="green") rs2 <- smooth(rocobj, method="density") plot(rs2, add=TRUE, col="blue") rs3 <- smooth(rocobj, method="fitdistr", density="lognormal") plot(rs3, add=TRUE, col="magenta") legend(.6, .4, legend=c("Empirical", "Binormal", "Density", "Log-normal"), col=c("black", "green", "blue", "magenta"), lwd=2) ## Advanced smoothing # different distibutions for controls and cases: smooth(rocobj, method="fitdistr", density.controls="normal", density.cases="lognormal") # with densities width <- bandwidth.nrd(rocobj$predictor) density.controls <- density(rocobj$controls, from=min(rocobj$predictor) - 3 * width, to=max(rocobj$predictor) + 3*width, width=width, window="gaussian") density.cases <- density(rocobj$cases, from=min(rocobj$predictor) - 3 * width, to=max(rocobj$predictor) + 3*width, width=width, window="gaussian") smooth(rocobj, method="density", density.controls=density.controls$y, density.cases=density.cases$y) # which is roughly what is done by a simple: smooth(rocobj, method="density") ## Smoothing artificial ROC curves # two normals roc.norm <- roc(rep(c(0, 1), each=1000), c(rnorm(1000), rnorm(1000)+1), plot=TRUE) plot(smooth(roc.norm), col="green", lwd=1, add=TRUE) plot(smooth(roc.norm, method="density"), col="red", lwd=1, add=TRUE) plot(smooth(roc.norm, method="fitdistr"), col="blue", lwd=1, add=TRUE) legend(.6, .4, legend=c("empirical", "binormal", "density",, "fitdistr"), col=c(par("fg"), "green", "red", "blue"), lwd=c(2, 1, 1, 1, 1)) # deviation from the normality roc.norm.exp <- roc(rep(c(0, 1), each=1000), c(rnorm(1000), rexp(1000)), plot=TRUE) plot(smooth(roc.norm.exp), col="green", lwd=1, add=TRUE) plot(smooth(roc.norm.exp, method="density"), col="red", lwd=1, add=TRUE) # Wrong fitdistr: normality assumed by default plot(smooth(roc.norm.exp, method="fitdistr"), col="blue", lwd=1, add=TRUE) # Correct fitdistr plot(smooth(roc.norm.exp, method="fitdistr", density.controls="normal", density.cases="exponential"), col="purple", lwd=1, add=TRUE) legend(.6, .4, legend=c("empirical", "binormal", "density", "wrong fitdistr", "correct fitdistr"), col=c(par("fg"), "green", "red", "blue", "purple"), lwd=c(2, 1, 1, 1, 1)) # large deviation from the normality roc.unif.exp <- roc(rep(c(0, 1), each=1000), c(runif(1000, -1, 1), rexp(1000)), plot=TRUE) plot(smooth(roc.unif.exp), col="green", lwd=1, add=TRUE) plot(smooth(roc.unif.exp, method="density"), col="red", lwd=1, add=TRUE) # Wrong fitdistr: normality assumed by default plot(smooth(roc.unif.exp, method="fitdistr"), col="blue", lwd=1, add=TRUE) # Correct fitdistr plot(smooth(roc.unif.exp, method="fitdistr", density.controls="uniform", density.cases="exponential"), col="purple", lwd=1, add=TRUE) legend(.6, .4, legend=c("empirical", "binormal", "density", "density ucv", "wrong fitdistr", "correct fitdistr"), col=c(par("fg"), "green", "red", "magenta", "blue", "purple"), lwd=c(2, 1, 1, 1, 1)) # 2 uniform distributions with a custom density function unif.density <- function(x, n, from, to, bw, kernel, ...) { smooth.x <- seq(from=from, to=to, length.out=n) smooth.y <- dunif(smooth.x, min=min(x), max=max(x)) return(smooth.y) } roc.unif <- roc(rep(c(0, 1), each=1000), c(runif(1000, -1, 1), runif(1000, 0, 2)), plot=TRUE) s <- smooth(roc.unif, method="density", density=unif.density) plot(roc.unif) plot(s, add=TRUE, col="grey") ## Not run: # you can bootstrap a ROC curve smoothed with a density function: ci(s, boot.n=100) ## End(Not run)