| 1 |
|
| 2 |
#' @name performance |
| 3 |
#' |
| 4 |
#' @title Function to create performance objects |
| 5 |
#' |
| 6 |
#' @description |
| 7 |
#' All kinds of predictor evaluations are performed using this function. |
| 8 |
#' |
| 9 |
#' @details |
| 10 |
#' Here is the list of available performance measures. Let Y and |
| 11 |
#' \eqn{\hat{Y}}{Yhat} be random variables representing the class and the prediction for |
| 12 |
#' a randomly drawn sample, respectively. We denote by |
| 13 |
#' \eqn{\oplus}{+} and \eqn{\ominus}{-} the positive and |
| 14 |
#' negative class, respectively. Further, we use the following |
| 15 |
#' abbreviations for empirical quantities: P (# positive |
| 16 |
#' samples), N (# negative samples), TP (# true positives), TN (# true |
| 17 |
#' negatives), FP (# false positives), FN (# false negatives). |
| 18 |
#' \describe{ |
| 19 |
#' \item{\code{acc}:}{Accuracy. \eqn{P(\hat{Y}=Y)}{P(Yhat = Y)}. Estimated |
| 20 |
#' as: \eqn{\frac{TP+TN}{P+N}}{(TP+TN)/(P+N)}.} |
| 21 |
#' \item{\code{err}:}{Error rate. \eqn{P(\hat{Y}\ne Y)}{P(Yhat != |
| 22 |
#' Y)}. Estimated as: \eqn{\frac{FP+FN}{P+N}}{(FP+FN)/(P+N)}.} |
| 23 |
#' \item{\code{fpr}:}{False positive rate. \eqn{P(\hat{Y}=\oplus | Y = |
| 24 |
#' \ominus)}{P(Yhat = + | Y = -)}. Estimated as: |
| 25 |
#' \eqn{\frac{FP}{N}}{FP/N}.} |
| 26 |
#' \item{\code{fall}:}{Fallout. Same as \code{fpr}.} |
| 27 |
#' \item{\code{tpr}:}{True positive |
| 28 |
#' rate. \eqn{P(\hat{Y}=\oplus|Y=\oplus)}{P(Yhat = + | Y = +)}. Estimated |
| 29 |
#' as: \eqn{\frac{TP}{P}}{TP/P}.} |
| 30 |
#' \item{\code{rec}:}{Recall. Same as \code{tpr}.} |
| 31 |
#' \item{\code{sens}:}{Sensitivity. Same as \code{tpr}.} |
| 32 |
#' \item{\code{fnr}:}{False negative |
| 33 |
#' rate. \eqn{P(\hat{Y}=\ominus|Y=\oplus)}{P(Yhat = - | Y = |
| 34 |
#' +)}. Estimated as: \eqn{\frac{FN}{P}}{FN/P}.} |
| 35 |
#' \item{\code{miss}:}{Miss. Same as \code{fnr}.} |
| 36 |
#' \item{\code{tnr}:}{True negative rate. \eqn{P(\hat{Y} = |
| 37 |
#' \ominus|Y=\ominus)}{P(Yhat = - | Y = -)}.} |
| 38 |
#' \item{\code{spec}:}{Specificity. Same as \code{tnr}.} |
| 39 |
#' \item{\code{ppv}:}{Positive predictive |
| 40 |
#' value. \eqn{P(Y=\oplus|\hat{Y}=\oplus)}{P(Y = + | Yhat = |
| 41 |
#' +)}. Estimated as: \eqn{\frac{TP}{TP+FP}}{TP/(TP+FP)}.} |
| 42 |
#' \item{\code{prec}:}{Precision. Same as \code{ppv}.} |
| 43 |
#' \item{\code{npv}:}{Negative predictive |
| 44 |
#' value. \eqn{P(Y=\ominus|\hat{Y}=\ominus)}{P(Y = - | Yhat = |
| 45 |
#' -)}. Estimated as: \eqn{\frac{TN}{TN+FN}}{TN/(TN+FN)}.} |
| 46 |
#' \item{\code{pcfall}:}{Prediction-conditioned |
| 47 |
#' fallout. \eqn{P(Y=\ominus|\hat{Y}=\oplus)}{P(Y = - | Yhat = |
| 48 |
#' +)}. Estimated as: \eqn{\frac{FP}{TP+FP}}{FP/(TP+FP)}.} |
| 49 |
#' \item{\code{pcmiss}:}{Prediction-conditioned |
| 50 |
#' miss. \eqn{P(Y=\oplus|\hat{Y}=\ominus)}{P(Y = + | Yhat = |
| 51 |
#' -)}. Estimated as: \eqn{\frac{FN}{TN+FN}}{FN/(TN+FN)}.} |
| 52 |
#' \item{\code{rpp}:}{Rate of positive predictions. \eqn{P( \hat{Y} = |
| 53 |
#' \oplus)}{P(Yhat = +)}. Estimated as: (TP+FP)/(TP+FP+TN+FN).} |
| 54 |
#' \item{\code{rnp}:}{Rate of negative predictions. \eqn{P( \hat{Y} = |
| 55 |
#' \ominus)}{P(Yhat = -)}. Estimated as: (TN+FN)/(TP+FP+TN+FN).} |
| 56 |
#' \item{\code{phi}:}{Phi correlation coefficient. \eqn{\frac{TP \cdot |
| 57 |
#' TN - FP \cdot FN}{\sqrt{ (TP+FN) \cdot (TN+FP) \cdot (TP+FP) |
| 58 |
#' \cdot (TN+FN)}}}{(TP*TN - |
| 59 |
#' FP*FN)/(sqrt((TP+FN)*(TN+FP)*(TP+FP)*(TN+FN)))}. Yields a |
| 60 |
#' number between -1 and 1, with 1 indicating a perfect |
| 61 |
#' prediction, 0 indicating a random prediction. Values below 0 |
| 62 |
#' indicate a worse than random prediction.} |
| 63 |
#' \item{\code{mat}:}{Matthews correlation coefficient. Same as \code{phi}.} |
| 64 |
#' \item{\code{mi}:}{Mutual information. \eqn{I(\hat{Y},Y) := H(Y) - |
| 65 |
#' H(Y|\hat{Y})}{I(Yhat, Y) := H(Y) - H(Y | Yhat)}, where H is the |
| 66 |
#' (conditional) entropy. Entropies are estimated naively (no bias |
| 67 |
#' correction).} |
| 68 |
#' \item{\code{chisq}:}{Chi square test statistic. \code{?chisq.test} |
| 69 |
#' for details. Note that R might raise a warning if the sample size |
| 70 |
#' is too small.} |
| 71 |
#' \item{\code{odds}:}{Odds ratio. \eqn{\frac{TP \cdot TN}{FN \cdot |
| 72 |
#' FP}}{(TP*TN)/(FN*FP)}. Note that odds ratio produces |
| 73 |
#' Inf or NA values for all cutoffs corresponding to FN=0 or |
| 74 |
#' FP=0. This can substantially decrease the plotted cutoff region.} |
| 75 |
#' \item{\code{lift}:}{Lift |
| 76 |
#' value. \eqn{\frac{P(\hat{Y}=\oplus|Y=\oplus)}{P(\hat{Y}=\oplus)}}{P(Yhat = + | |
| 77 |
#' Y = +)/P(Yhat = +)}.} |
| 78 |
#' \item{\code{f}:}{Precision-recall F measure (van Rijsbergen, 1979). Weighted |
| 79 |
#' harmonic mean of precision (P) and recall (R). \eqn{F = |
| 80 |
#' \frac{1}{\alpha \frac{1}{P} + (1-\alpha)\frac{1}{R}}}{F = 1/ |
| 81 |
#' (alpha*1/P + (1-alpha)*1/R)}. If |
| 82 |
#' \eqn{\alpha=\frac{1}{2}}{alpha=1/2}, the mean is balanced. A |
| 83 |
#' frequent equivalent formulation is |
| 84 |
#' \eqn{F = \frac{(\beta^2+1) \cdot P \cdot R}{R + \beta^2 \cdot |
| 85 |
#' P}}{F = (beta^2+1) * P * R / (R + beta^2 * P)}. In this formulation, the |
| 86 |
#' mean is balanced if \eqn{\beta=1}{beta=1}. Currently, ROCR only accepts |
| 87 |
#' the alpha version as input (e.g. \eqn{\alpha=0.5}{alpha=0.5}). If no |
| 88 |
#' value for alpha is given, the mean will be balanced by default.} |
| 89 |
#' \item{\code{rch}:}{ROC convex hull. A ROC (=\code{tpr} vs \code{fpr}) curve |
| 90 |
#' with concavities (which represent suboptimal choices of cutoff) removed |
| 91 |
#' (Fawcett 2001). Since the result is already a parametric performance |
| 92 |
#' curve, it cannot be used in combination with other measures.} |
| 93 |
#' \item{\code{auc}:}{Area under the ROC curve. This is equal to the value of the |
| 94 |
#' Wilcoxon-Mann-Whitney test statistic and also the probability that the |
| 95 |
#' classifier will score are randomly drawn positive sample higher than a |
| 96 |
#' randomly drawn negative sample. Since the output of |
| 97 |
#' \code{auc} is cutoff-independent, this |
| 98 |
#' measure cannot be combined with other measures into a parametric |
| 99 |
#' curve. The partial area under the ROC curve up to a given false |
| 100 |
#' positive rate can be calculated by passing the optional parameter |
| 101 |
#' \code{fpr.stop=0.5} (or any other value between 0 and 1) to |
| 102 |
#' \code{performance}.} |
| 103 |
#' \item{\code{aucpr}:}{Area under the Precision/Recall curve. Since the output |
| 104 |
#' of \code{aucpr} is cutoff-independent, this measure cannot be combined |
| 105 |
#' with other measures into a parametric curve.} |
| 106 |
#' \item{\code{prbe}:}{Precision-recall break-even point. The cutoff(s) where |
| 107 |
#' precision and recall are equal. At this point, positive and negative |
| 108 |
#' predictions are made at the same rate as their prevalence in the |
| 109 |
#' data. Since the output of |
| 110 |
#' \code{prbe} is just a cutoff-independent scalar, this |
| 111 |
#' measure cannot be combined with other measures into a parametric curve.} |
| 112 |
#' \item{\code{cal}:}{Calibration error. The calibration error is the |
| 113 |
#' absolute difference between predicted confidence and actual reliability. This |
| 114 |
#' error is estimated at all cutoffs by sliding a window across the |
| 115 |
#' range of possible cutoffs. The default window size of 100 can be |
| 116 |
#' adjusted by passing the optional parameter \code{window.size=200} |
| 117 |
#' to \code{performance}. E.g., if for several |
| 118 |
#' positive samples the output of the classifier is around 0.75, you might |
| 119 |
#' expect from a well-calibrated classifier that the fraction of them |
| 120 |
#' which is correctly predicted as positive is also around 0.75. In a |
| 121 |
#' well-calibrated classifier, the probabilistic confidence estimates |
| 122 |
#' are realistic. Only for use with |
| 123 |
#' probabilistic output (i.e. scores between 0 and 1).} |
| 124 |
#' \item{\code{mxe}:}{Mean cross-entropy. Only for use with |
| 125 |
#' probabilistic output. \eqn{MXE :=-\frac{1}{P+N}( \sum_{y_i=\oplus} |
| 126 |
#' ln(\hat{y}_i) + \sum_{y_i=\ominus} ln(1-\hat{y}_i))}{MXE := - 1/(P+N) \sum_{y_i=+} |
| 127 |
#' ln(yhat_i) + \sum_{y_i=-} ln(1-yhat_i)}. Since the output of |
| 128 |
#' \code{mxe} is just a cutoff-independent scalar, this |
| 129 |
#' measure cannot be combined with other measures into a parametric curve.} |
| 130 |
#' \item{\code{rmse}:}{Root-mean-squared error. Only for use with |
| 131 |
#' numerical class labels. \eqn{RMSE:=\sqrt{\frac{1}{P+N}\sum_i (y_i |
| 132 |
#' - \hat{y}_i)^2}}{RMSE := sqrt(1/(P+N) \sum_i (y_i - |
| 133 |
#' yhat_i)^2)}. Since the output of |
| 134 |
#' \code{rmse} is just a cutoff-independent scalar, this |
| 135 |
#' measure cannot be combined with other measures into a parametric curve.} |
| 136 |
#' \item{\code{sar}:}{Score combinining performance measures of different |
| 137 |
#' characteristics, in the attempt of creating a more "robust" |
| 138 |
#' measure (cf. Caruana R., ROCAI2004): |
| 139 |
#' SAR = 1/3 * ( Accuracy + Area under the ROC curve + Root |
| 140 |
#' mean-squared error ).} |
| 141 |
#' \item{\code{ecost}:}{Expected cost. For details on cost curves, |
| 142 |
#' cf. Drummond&Holte 2000,2004. \code{ecost} has an obligatory x |
| 143 |
#' axis, the so-called 'probability-cost function'; thus it cannot be |
| 144 |
#' combined with other measures. While using \code{ecost} one is |
| 145 |
#' interested in the lower envelope of a set of lines, it might be |
| 146 |
#' instructive to plot the whole set of lines in addition to the lower |
| 147 |
#' envelope. An example is given in \code{demo(ROCR)}.} |
| 148 |
#' \item{\code{cost}:}{Cost of a classifier when |
| 149 |
#' class-conditional misclassification costs are explicitly given. |
| 150 |
#' Accepts the optional parameters \code{cost.fp} and |
| 151 |
#' \code{cost.fn}, by which the costs for false positives and |
| 152 |
#' negatives can be adjusted, respectively. By default, both are set |
| 153 |
#' to 1.} |
| 154 |
#' } |
| 155 |
#' |
| 156 |
#' @note |
| 157 |
#' Here is how to call \code{performance()} to create some standard |
| 158 |
#' evaluation plots: |
| 159 |
#' \describe{ |
| 160 |
#' \item{ROC curves:}{measure="tpr", x.measure="fpr".} |
| 161 |
#' \item{Precision/recall graphs:}{measure="prec", x.measure="rec".} |
| 162 |
#' \item{Sensitivity/specificity plots:}{measure="sens", x.measure="spec".} |
| 163 |
#' \item{Lift charts:}{measure="lift", x.measure="rpp".} |
| 164 |
#' } |
| 165 |
#' |
| 166 |
#' @param prediction.obj An object of class \code{prediction}. |
| 167 |
#' @param measure Performance measure to use for the evaluation. A complete list |
| 168 |
#' of the performance measures that are available for \code{measure} and |
| 169 |
#' \code{x.measure} is given in the 'Details' section. |
| 170 |
#' @param x.measure A second performance measure. If different from the default, |
| 171 |
#' a two-dimensional curve, with \code{x.measure} taken to be the unit in |
| 172 |
#' direction of the x axis, and \code{measure} to be the unit in direction of |
| 173 |
#' the y axis, is created. This curve is parametrized with the cutoff. |
| 174 |
#' @param ... Optional arguments (specific to individual performance measures). |
| 175 |
#' |
| 176 |
#' @return An S4 object of class \code{performance}. |
| 177 |
#' |
| 178 |
#' @references |
| 179 |
#' A detailed list of references can be found on the ROCR homepage at |
| 180 |
#' \url{https://ipa-tys.github.io/ROCR/}. |
| 181 |
#' |
| 182 |
#' @author |
| 183 |
#' Tobias Sing \email{[email protected]}, Oliver Sander |
| 184 |
#' \email{[email protected]} |
| 185 |
#' |
| 186 |
#' @seealso |
| 187 |
#' \code{\link{prediction}}, |
| 188 |
#' \code{\link{prediction-class}}, |
| 189 |
#' \code{\link{performance-class}}, |
| 190 |
#' \code{\link{plot.performance}} |
| 191 |
#' |
| 192 |
#' @export |
| 193 |
#' |
| 194 |
#' @examples |
| 195 |
#' # computing a simple ROC curve (x-axis: fpr, y-axis: tpr) |
| 196 |
#' library(ROCR) |
| 197 |
#' data(ROCR.simple) |
| 198 |
#' pred <- prediction( ROCR.simple$predictions, ROCR.simple$labels) |
| 199 |
#' pred |
| 200 |
#' perf <- performance(pred,"tpr","fpr") |
| 201 |
#' perf |
| 202 |
#' plot(perf) |
| 203 |
#' |
| 204 |
#' # precision/recall curve (x-axis: recall, y-axis: precision) |
| 205 |
#' perf <- performance(pred, "prec", "rec") |
| 206 |
#' perf |
| 207 |
#' plot(perf) |
| 208 |
#' |
| 209 |
#' # sensitivity/specificity curve (x-axis: specificity, |
| 210 |
#' # y-axis: sensitivity) |
| 211 |
#' perf <- performance(pred, "sens", "spec") |
| 212 |
#' perf |
| 213 |
#' plot(perf) |
| 214 |
performance <- function(prediction.obj, |
| 215 |
measure, |
| 216 |
x.measure="cutoff", |
| 217 |
...) { |
| 218 |
|
| 219 |
## define the needed environments |
| 220 |
envir.list <- .define.environments() |
| 221 |
long.unit.names <- envir.list$long.unit.names |
| 222 |
function.names <- envir.list$function.names |
| 223 |
obligatory.x.axis <- envir.list$obligatory.x.axis |
| 224 |
optional.arguments <- envir.list$optional.arguments |
| 225 |
default.values <- envir.list$default.values |
| 226 |
|
| 227 |
## abort in case of misuse |
| 228 |
if (!is(prediction.obj, "prediction") || |
| 229 |
!exists(measure, where=long.unit.names, inherits=FALSE) || |
| 230 |
!exists(x.measure, where=long.unit.names, inherits=FALSE)) { |
| 231 |
stop(paste("Wrong argument types: First argument must be of type", |
| 232 |
"'prediction'; second and optional third argument must", |
| 233 |
"be available performance measures!")) |
| 234 |
} |
| 235 |
|
| 236 |
## abort, if attempt is made to use a measure that has an obligatory |
| 237 |
## x.axis as the x.measure (cannot be combined) |
| 238 |
if (exists( x.measure, where=obligatory.x.axis, inherits=FALSE )) { |
| 239 |
message <- paste("The performance measure", |
| 240 |
x.measure, |
| 241 |
"can only be used as 'measure', because it has", |
| 242 |
"the following obligatory 'x.measure':\n", |
| 243 |
get( x.measure, envir=obligatory.x.axis)) |
| 244 |
stop(message) |
| 245 |
} |
| 246 |
|
| 247 |
## if measure is a performance measure with obligatory x.axis, then |
| 248 |
## enforce this axis: |
| 249 |
if (exists( measure, where=obligatory.x.axis, inherits=FALSE )) { |
| 250 |
x.measure <- get( measure, envir=obligatory.x.axis ) |
| 251 |
} |
| 252 |
|
| 253 |
if (x.measure == "cutoff" || |
| 254 |
exists( measure, where=obligatory.x.axis, inherits=FALSE )) { |
| 255 |
|
| 256 |
## fetch from '...' any optional arguments for the performance |
| 257 |
## measure at hand that are given, otherwise fill up the default values |
| 258 |
optional.args <- list(...) |
| 259 |
argnames <- c() |
| 260 |
if ( exists( measure, where=optional.arguments, inherits=FALSE )) { |
| 261 |
argnames <- get( measure, envir=optional.arguments ) |
| 262 |
default.arglist <- list() |
| 263 |
for (i in 1:length(argnames)) { |
| 264 |
default.arglist <- c(default.arglist, |
| 265 |
get(paste(measure,":",argnames[i],sep=""), |
| 266 |
envir=default.values, inherits=FALSE)) |
| 267 |
} |
| 268 |
names(default.arglist) <- argnames |
| 269 |
|
| 270 |
for (i in 1:length(argnames)) { |
| 271 |
templist <- list(optional.args, |
| 272 |
default.arglist[[i]]) |
| 273 |
names(templist) <- c('arglist', argnames[i]) |
| 274 |
|
| 275 |
optional.args <- do.call('.farg', templist) |
| 276 |
} |
| 277 |
} |
| 278 |
optional.args <- .select.args( optional.args, argnames ) |
| 279 |
|
| 280 |
## determine function name |
| 281 |
function.name <- get( measure, envir=function.names ) |
| 282 |
|
| 283 |
## for each x-validation run, compute the requested performance measure |
| 284 |
x.values <- list() |
| 285 |
y.values <- list() |
| 286 |
for (i in 1:length( prediction.obj@predictions )) { |
| 287 |
argumentlist <- .sarg(optional.args, |
| 288 |
predictions= prediction.obj@predictions[[i]], |
| 289 |
labels= prediction.obj@labels[[i]], |
| 290 |
cutoffs= prediction.obj@cutoffs[[i]], |
| 291 |
fp= prediction.obj@fp[[i]], |
| 292 |
tp= prediction.obj@tp[[i]], |
| 293 |
fn= prediction.obj@fn[[i]], |
| 294 |
tn= prediction.obj@tn[[i]], |
| 295 |
n.pos= [email protected][[i]], |
| 296 |
n.neg= [email protected][[i]], |
| 297 |
n.pos.pred= [email protected][[i]], |
| 298 |
n.neg.pred= [email protected][[i]]) |
| 299 |
|
| 300 |
ans <- do.call( function.name, argumentlist ) |
| 301 |
|
| 302 |
if (!is.null(ans[[1]])) x.values <- c( x.values, list( ans[[1]] )) |
| 303 |
y.values <- c( y.values, list( ans[[2]] )) |
| 304 |
} |
| 305 |
|
| 306 |
if (! (length(x.values)==0 || length(x.values)==length(y.values)) ) { |
| 307 |
stop("Consistency error.") |
| 308 |
} |
| 309 |
|
| 310 |
## create a new performance object |
| 311 |
return( new("performance", |
| 312 |
x.name = get( x.measure, envir=long.unit.names ), |
| 313 |
y.name = get( measure, envir=long.unit.names ), |
| 314 |
alpha.name = "none", |
| 315 |
x.values = x.values, |
| 316 |
y.values = y.values, |
| 317 |
alpha.values = list() )) |
| 318 |
} else { |
| 319 |
perf.obj.1 <- performance( prediction.obj, measure=x.measure, ... ) |
| 320 |
perf.obj.2 <- performance( prediction.obj, measure=measure, ... ) |
| 321 |
return( .combine.performance.objects( perf.obj.1, perf.obj.2 ) ) |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
#' @importFrom stats approxfun |
| 326 |
.combine.performance.objects <- function( p.obj.1, p.obj.2 ) { |
| 327 |
## some checks for misusage (in any way, this function is |
| 328 |
## only for internal use) |
| 329 |
if ( [email protected] != [email protected] ) { |
| 330 |
stop("Error: Objects need to have identical x axis.") |
| 331 |
} |
| 332 |
if ( [email protected] != "none" || [email protected] != "none") { |
| 333 |
stop("Error: At least one of the two objects has already been merged.") |
| 334 |
} |
| 335 |
if (length([email protected]) != length([email protected])) { |
| 336 |
stop(paste("Only performance objects with identical number of", |
| 337 |
"cross-validation runs can be combined.")) |
| 338 |
} |
| 339 |
|
| 340 |
x.values <- list() |
| 341 |
x.name <- [email protected] |
| 342 |
y.values <- list() |
| 343 |
y.name <- [email protected] |
| 344 |
alpha.values <- list() |
| 345 |
alpha.name <- [email protected] |
| 346 |
|
| 347 |
for (i in 1:length( [email protected] )) { |
| 348 |
x.values.1 <- [email protected][[i]] |
| 349 |
y.values.1 <- [email protected][[i]] |
| 350 |
x.values.2 <- [email protected][[i]] |
| 351 |
y.values.2 <- [email protected][[i]] |
| 352 |
|
| 353 |
## cutoffs of combined object = merged cutoffs of simple objects |
| 354 |
cutoffs <- sort( unique( c(x.values.1, x.values.2)), decreasing=TRUE ) |
| 355 |
|
| 356 |
## calculate y.values at cutoffs using step function |
| 357 |
y.values.int.1 <- stats::approxfun(x.values.1, y.values.1, |
| 358 |
method="constant",f=1,rule=2)(cutoffs) |
| 359 |
y.values.int.2 <- stats::approxfun(x.values.2, y.values.2, |
| 360 |
method="constant",f=1,rule=2)(cutoffs) |
| 361 |
|
| 362 |
## 'approxfun' ignores NA and NaN |
| 363 |
objs <- list( y.values.int.1, y.values.int.2) |
| 364 |
objs.x <- list( x.values.1, x.values.2 ) |
| 365 |
na.cutoffs.1.bool <- is.na( y.values.1) & !is.nan( y.values.1 ) |
| 366 |
nan.cutoffs.1.bool <- is.nan( y.values.1) |
| 367 |
na.cutoffs.2.bool <- is.na( y.values.2) & !is.nan( y.values.2 ) |
| 368 |
nan.cutoffs.2.bool <- is.nan( y.values.2) |
| 369 |
bools <- list(na.cutoffs.1.bool, nan.cutoffs.1.bool, |
| 370 |
na.cutoffs.2.bool, nan.cutoffs.2.bool) |
| 371 |
values <- c(NA,NaN,NA,NaN) |
| 372 |
|
| 373 |
for (j in 1:4) { |
| 374 |
for (k in which(bools[[j]])) { |
| 375 |
interval.max <- objs.x[[ ceiling(j/2) ]][k] |
| 376 |
interval.min <- -Inf |
| 377 |
if (k < length(objs.x[[ ceiling(j/2) ]])) { |
| 378 |
interval.min <- objs.x[[ ceiling(j/2) ]][k+1] |
| 379 |
} |
| 380 |
objs[[ ceiling(j/2) ]][cutoffs <= interval.max & |
| 381 |
cutoffs > interval.min ] <- values[j] |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
alpha.values <- c(alpha.values, list(cutoffs)) |
| 386 |
x.values <- c(x.values, list(objs[[1]])) |
| 387 |
y.values <- c(y.values, list(objs[[2]])) |
| 388 |
} |
| 389 |
|
| 390 |
return( new("performance", |
| 391 |
x.name=x.name, y.name=y.name, |
| 392 |
alpha.name=alpha.name, x.values=x.values, |
| 393 |
y.values=y.values, alpha.values=alpha.values)) |
| 394 |
} |
| 395 |
|
| 396 |
.define.environments <- function() { |
| 397 |
## There are five environments: long.unit.names, function.names, |
| 398 |
## obligatory.x.axis, optional.arguments, default.values |
| 399 |
|
| 400 |
## Define long names corresponding to the measure abbreviations. |
| 401 |
long.unit.names <- new.env() |
| 402 |
assign("none","None", envir=long.unit.names) |
| 403 |
assign("cutoff", "Cutoff", envir=long.unit.names) |
| 404 |
assign("acc", "Accuracy", envir=long.unit.names) |
| 405 |
assign("err", "Error Rate", envir=long.unit.names) |
| 406 |
assign("fpr", "False positive rate", envir=long.unit.names) |
| 407 |
assign("tpr", "True positive rate", envir=long.unit.names) |
| 408 |
assign("rec", "Recall", envir=long.unit.names) |
| 409 |
assign("sens", "Sensitivity", envir=long.unit.names) |
| 410 |
assign("fnr", "False negative rate", envir=long.unit.names) |
| 411 |
assign("tnr", "True negative rate", envir=long.unit.names) |
| 412 |
assign("spec", "Specificity", envir=long.unit.names) |
| 413 |
assign("ppv", "Positive predictive value", envir=long.unit.names) |
| 414 |
assign("prec", "Precision", envir=long.unit.names) |
| 415 |
assign("npv", "Negative predictive value", envir=long.unit.names) |
| 416 |
assign("fall", "Fallout", envir=long.unit.names) |
| 417 |
assign("miss", "Miss", envir=long.unit.names) |
| 418 |
assign("pcfall", "Prediction-conditioned fallout", envir=long.unit.names) |
| 419 |
assign("pcmiss", "Prediction-conditioned miss", envir=long.unit.names) |
| 420 |
assign("rpp", "Rate of positive predictions", envir=long.unit.names) |
| 421 |
assign("rnp", "Rate of negative predictions", envir=long.unit.names) |
| 422 |
assign("auc","Area under the ROC curve", envir=long.unit.names) |
| 423 |
assign("aucpr","Area under the Precision/Recall curve", envir=long.unit.names) |
| 424 |
assign("cal", "Calibration error", envir=long.unit.names) |
| 425 |
assign("mwp", "Median window position", envir=long.unit.names) |
| 426 |
assign("prbe","Precision/recall break-even point", envir=long.unit.names) |
| 427 |
assign("rch", "ROC convex hull", envir=long.unit.names) |
| 428 |
assign("mxe", "Mean cross-entropy", envir=long.unit.names) |
| 429 |
assign("rmse","Root-mean-square error", envir=long.unit.names) |
| 430 |
assign("phi", "Phi correlation coefficient", envir=long.unit.names) |
| 431 |
assign("mat","Matthews correlation coefficient", envir=long.unit.names) |
| 432 |
assign("mi", "Mutual information", envir=long.unit.names) |
| 433 |
assign("chisq", "Chi-square test statistic", envir=long.unit.names) |
| 434 |
assign("odds","Odds ratio", envir=long.unit.names) |
| 435 |
assign("lift", "Lift value", envir=long.unit.names) |
| 436 |
assign("f","Precision-Recall F measure", envir=long.unit.names) |
| 437 |
assign("sar", "SAR", envir=long.unit.names) |
| 438 |
assign("ecost", "Expected cost", envir=long.unit.names) |
| 439 |
assign("cost", "Explicit cost", envir=long.unit.names) |
| 440 |
|
| 441 |
## Define function names corresponding to the measure abbreviations. |
| 442 |
function.names <- new.env() |
| 443 |
assign("acc", ".performance.accuracy", envir=function.names) |
| 444 |
assign("err", ".performance.error.rate", envir=function.names) |
| 445 |
assign("fpr", ".performance.false.positive.rate", envir=function.names) |
| 446 |
assign("tpr", ".performance.true.positive.rate", envir=function.names) |
| 447 |
assign("rec", ".performance.true.positive.rate", envir=function.names) |
| 448 |
assign("sens", ".performance.true.positive.rate", envir=function.names) |
| 449 |
assign("fnr", ".performance.false.negative.rate", envir=function.names) |
| 450 |
assign("tnr", ".performance.true.negative.rate", envir=function.names) |
| 451 |
assign("spec", ".performance.true.negative.rate", envir=function.names) |
| 452 |
assign("ppv", ".performance.positive.predictive.value", |
| 453 |
envir=function.names) |
| 454 |
assign("prec", ".performance.positive.predictive.value", |
| 455 |
envir=function.names) |
| 456 |
assign("npv", ".performance.negative.predictive.value", |
| 457 |
envir=function.names) |
| 458 |
assign("fall", ".performance.false.positive.rate", envir=function.names) |
| 459 |
assign("miss", ".performance.false.negative.rate", envir=function.names) |
| 460 |
assign("pcfall", ".performance.prediction.conditioned.fallout", |
| 461 |
envir=function.names) |
| 462 |
assign("pcmiss", ".performance.prediction.conditioned.miss", |
| 463 |
envir=function.names) |
| 464 |
assign("rpp", ".performance.rate.of.positive.predictions", |
| 465 |
envir=function.names) |
| 466 |
assign("rnp", ".performance.rate.of.negative.predictions", |
| 467 |
envir=function.names) |
| 468 |
assign("auc", ".performance.auc", envir=function.names) |
| 469 |
assign("aucpr", ".performance.aucpr", envir=function.names) |
| 470 |
assign("cal", ".performance.calibration.error", envir=function.names) |
| 471 |
assign("prbe", ".performance.precision.recall.break.even.point", |
| 472 |
envir=function.names) |
| 473 |
assign("rch", ".performance.rocconvexhull", envir=function.names) |
| 474 |
assign("mxe", ".performance.mean.cross.entropy", envir=function.names) |
| 475 |
assign("rmse", ".performance.root.mean.squared.error", |
| 476 |
envir=function.names) |
| 477 |
assign("phi", ".performance.phi", envir=function.names) |
| 478 |
assign("mat", ".performance.phi", envir=function.names) |
| 479 |
assign("mi", ".performance.mutual.information", envir=function.names) |
| 480 |
assign("chisq", ".performance.chisq", envir=function.names) |
| 481 |
assign("odds", ".performance.odds.ratio", envir=function.names) |
| 482 |
assign("lift", ".performance.lift", envir=function.names) |
| 483 |
assign("f", ".performance.f", envir=function.names) |
| 484 |
assign("sar", ".performance.sar", envir=function.names) |
| 485 |
assign("ecost", ".performance.expected.cost", envir=function.names) |
| 486 |
assign("cost", ".performance.cost", envir=function.names) |
| 487 |
|
| 488 |
## If a measure comes along with an obligatory x axis (including "none"), |
| 489 |
## list it here. |
| 490 |
obligatory.x.axis <- new.env() |
| 491 |
assign("mxe", "none", envir=obligatory.x.axis) |
| 492 |
assign("rmse", "none", envir=obligatory.x.axis) |
| 493 |
assign("prbe", "none", envir=obligatory.x.axis) |
| 494 |
assign("auc", "none", envir=obligatory.x.axis) |
| 495 |
assign("aucpr", "none", envir=obligatory.x.axis) |
| 496 |
assign("rch","none", envir=obligatory.x.axis) |
| 497 |
## ecost requires probability cost function as x axis, which is handled |
| 498 |
## implicitly, not as an explicit performance measure. |
| 499 |
assign("ecost","none", envir=obligatory.x.axis) |
| 500 |
|
| 501 |
## If a measure has optional arguments, list the names of the |
| 502 |
## arguments here. |
| 503 |
optional.arguments <- new.env() |
| 504 |
assign("cal", "window.size", envir=optional.arguments) |
| 505 |
assign("f", "alpha", envir=optional.arguments) |
| 506 |
assign("cost", c("cost.fp", "cost.fn"), envir=optional.arguments) |
| 507 |
assign("auc", "fpr.stop", envir=optional.arguments) |
| 508 |
|
| 509 |
## If a measure has additional arguments, list the default values |
| 510 |
## for them here. Naming convention: e.g. "cal" has an optional |
| 511 |
## argument "window.size" the key to use here is "cal:window.size" |
| 512 |
## (colon as separator) |
| 513 |
default.values <- new.env() |
| 514 |
assign("cal:window.size", 100, envir=default.values) |
| 515 |
assign("f:alpha", 0.5, envir=default.values) |
| 516 |
assign("cost:cost.fp", 1, envir=default.values) |
| 517 |
assign("cost:cost.fn", 1, envir=default.values) |
| 518 |
assign("auc:fpr.stop", 1, envir=default.values) |
| 519 |
|
| 520 |
list(long.unit.names=long.unit.names, function.names=function.names, |
| 521 |
obligatory.x.axis=obligatory.x.axis, |
| 522 |
optional.arguments=optional.arguments, |
| 523 |
default.values=default.values) |
| 524 |
} |