| 1 |
#' @name prediction |
| 2 |
#' |
| 3 |
#' @title Function to create prediction objects |
| 4 |
#' |
| 5 |
#' @description |
| 6 |
#' Every classifier evaluation using ROCR starts with creating a |
| 7 |
#' \code{prediction} object. This function is used to transform the input data |
| 8 |
#' (which can be in vector, matrix, data frame, or list form) into a |
| 9 |
#' standardized format. |
| 10 |
#' |
| 11 |
#' @details |
| 12 |
#' \code{predictions} and \code{labels} can simply be vectors of the same |
| 13 |
#' length. However, in the case of cross-validation data, different |
| 14 |
#' cross-validation runs can be provided as the *columns* of a matrix or |
| 15 |
#' data frame, or as the entries of a list. In the case of a matrix or |
| 16 |
#' data frame, all cross-validation runs must have the same length, whereas |
| 17 |
#' in the case of a list, the lengths can vary across the cross-validation |
| 18 |
#' runs. Internally, as described in section 'Value', all of these input |
| 19 |
#' formats are converted to list representation. |
| 20 |
#' |
| 21 |
#' Since scoring classifiers give relative tendencies towards a negative |
| 22 |
#' (low scores) or positive (high scores) class, it has to be declared |
| 23 |
#' which class label denotes the negative, and which the positive class. |
| 24 |
#' Ideally, labels should be supplied as ordered factor(s), the lower |
| 25 |
#' level corresponding to the negative class, the upper level to the |
| 26 |
#' positive class. If the labels are factors (unordered), numeric, |
| 27 |
#' logical or characters, ordering of the labels is inferred from |
| 28 |
#' R's built-in \code{<} relation (e.g. 0 < 1, -1 < 1, 'a' < 'b', |
| 29 |
#' FALSE < TRUE). Use \code{label.ordering} to override this default |
| 30 |
#' ordering. Please note that the ordering can be locale-dependent |
| 31 |
#' e.g. for character labels '-1' and '1'. |
| 32 |
#' |
| 33 |
#' Currently, ROCR supports only binary classification (extensions toward |
| 34 |
#' multiclass classification are scheduled for the next release, |
| 35 |
#' however). If there are more than two distinct label symbols, execution |
| 36 |
#' stops with an error message. If all predictions use the same two |
| 37 |
#' symbols that are used for the labels, categorical predictions are |
| 38 |
#' assumed. If there are more than two predicted values, but all numeric, |
| 39 |
#' continuous predictions are assumed (i.e. a scoring |
| 40 |
#' classifier). Otherwise, if more than two symbols occur in the |
| 41 |
#' predictions, and not all of them are numeric, execution stops with an |
| 42 |
#' error message. |
| 43 |
#' |
| 44 |
#' @param predictions A vector, matrix, list, or data frame containing the |
| 45 |
#' predictions. |
| 46 |
#' @param labels A vector, matrix, list, or data frame containing the true class |
| 47 |
#' labels. Must have the same dimensions as \code{predictions}. |
| 48 |
#' @param label.ordering The default ordering (cf.details) of the classes can |
| 49 |
#' be changed by supplying a vector containing the negative and the positive |
| 50 |
#' class label. |
| 51 |
#' |
| 52 |
#' @return An S4 object of class \code{prediction}. |
| 53 |
#' |
| 54 |
#' @references |
| 55 |
#' A detailed list of references can be found on the ROCR homepage at |
| 56 |
#' \url{https://ipa-tys.github.io/ROCR/}. |
| 57 |
#' |
| 58 |
#' @author |
| 59 |
#' Tobias Sing \email{[email protected]}, Oliver Sander |
| 60 |
#' \email{[email protected]} |
| 61 |
#' |
| 62 |
#' @seealso |
| 63 |
#' \code{\link{prediction-class}}, |
| 64 |
#' \code{\link{performance}}, |
| 65 |
#' \code{\link{performance-class}}, |
| 66 |
#' \code{\link{plot.performance}} |
| 67 |
#' |
| 68 |
#' @export |
| 69 |
#' |
| 70 |
#' @examples |
| 71 |
#' # create a simple prediction object |
| 72 |
#' library(ROCR) |
| 73 |
#' data(ROCR.simple) |
| 74 |
#' pred <- prediction(ROCR.simple$predictions,ROCR.simple$labels) |
| 75 |
#' pred |
| 76 |
prediction <- function(predictions, labels, label.ordering=NULL) { |
| 77 |
|
| 78 |
## bring 'predictions' and 'labels' into list format, |
| 79 |
## each list entry representing one x-validation run |
| 80 |
|
| 81 |
## convert predictions into canonical list format |
| 82 |
if (is.data.frame(predictions)) { |
| 83 |
names(predictions) <- c() |
| 84 |
predictions <- as.list(predictions) |
| 85 |
} else if (is.matrix(predictions)) { |
| 86 |
predictions <- as.list(data.frame(predictions)) |
| 87 |
names(predictions) <- c() |
| 88 |
} else if (is.vector(predictions) && !is.list(predictions)) { |
| 89 |
predictions <- list(predictions) |
| 90 |
} else if (!is.list(predictions)) { |
| 91 |
stop("Format of predictions is invalid. It couldn't be coerced to a list.", |
| 92 |
call. = FALSE) |
| 93 |
} |
| 94 |
## if predictions is a list -> keep unaltered |
| 95 |
if(any(vapply(predictions,anyNA,logical(1)))){ |
| 96 |
stop("'predictions' contains NA.", call. = FALSE) |
| 97 |
} |
| 98 |
|
| 99 |
## convert labels into canonical list format |
| 100 |
if (is.data.frame(labels)) { |
| 101 |
names(labels) <- c() |
| 102 |
labels <- as.list( labels) |
| 103 |
} else if (is.matrix(labels)) { |
| 104 |
labels <- as.list( data.frame( labels)) |
| 105 |
names(labels) <- c() |
| 106 |
} else if ((is.vector(labels) || |
| 107 |
is.ordered(labels) || |
| 108 |
is.factor(labels)) && |
| 109 |
!is.list(labels)) { |
| 110 |
labels <- list( labels) |
| 111 |
} else if (!is.list(labels)) { |
| 112 |
stop("Format of labels is invalid. It couldn't be coerced to a list.", |
| 113 |
call. = FALSE) |
| 114 |
} |
| 115 |
## if labels is a list -> keep unaltered |
| 116 |
|
| 117 |
## Length consistency checks |
| 118 |
if (length(predictions) != length(labels)) |
| 119 |
stop(paste("Number of cross-validation runs must be equal", |
| 120 |
"for predictions and labels.")) |
| 121 |
if (! all(sapply(predictions, length) == sapply(labels, length))) |
| 122 |
stop(paste("Number of predictions in each run must be equal", |
| 123 |
"to the number of labels for each run.")) |
| 124 |
|
| 125 |
## only keep prediction/label pairs that are finite numbers |
| 126 |
for (i in 1:length(predictions)) { |
| 127 |
finite.bool <- is.finite( predictions[[i]] ) |
| 128 |
predictions[[i]] <- predictions[[i]][ finite.bool ] |
| 129 |
labels[[i]] <- labels[[i]][ finite.bool ] |
| 130 |
} |
| 131 |
|
| 132 |
## abort if 'labels' format is inconsistent across |
| 133 |
## different cross-validation runs |
| 134 |
label.format="" ## one of 'normal','factor','ordered' |
| 135 |
if (all(sapply( labels, is.factor)) && |
| 136 |
!any(sapply(labels, is.ordered))) { |
| 137 |
label.format <- "factor" |
| 138 |
} else if (all(sapply( labels, is.ordered))) { |
| 139 |
label.format <- "ordered" |
| 140 |
} else if (all(sapply( labels, is.character)) || |
| 141 |
all(sapply( labels, is.numeric)) || |
| 142 |
all(sapply( labels, is.logical))) { |
| 143 |
label.format <- "normal" |
| 144 |
} else { |
| 145 |
stop(paste("Inconsistent label data type across different", |
| 146 |
"cross-validation runs.")) |
| 147 |
} |
| 148 |
|
| 149 |
## abort if levels are not consistent across different |
| 150 |
## cross-validation runs |
| 151 |
if (! all(sapply(labels, levels)==levels(labels[[1]])) ) { |
| 152 |
stop(paste("Inconsistent factor levels across different", |
| 153 |
"cross-validation runs.")) |
| 154 |
} |
| 155 |
|
| 156 |
## convert 'labels' into ordered factors, aborting if the number |
| 157 |
## of classes is not equal to 2. |
| 158 |
levels <- c() |
| 159 |
if ( label.format == "ordered" ) { |
| 160 |
if (!is.null(label.ordering)) { |
| 161 |
stop(paste("'labels' is already ordered. No additional", |
| 162 |
"'label.ordering' must be supplied.")) |
| 163 |
} else { |
| 164 |
levels <- levels(labels[[1]]) |
| 165 |
} |
| 166 |
} else { |
| 167 |
if ( is.null( label.ordering )) { |
| 168 |
if ( label.format == "factor" ) levels <- sort(levels(labels[[1]])) |
| 169 |
else levels <- sort( unique( unlist( labels))) |
| 170 |
} else { |
| 171 |
## if (!setequal( levels, label.ordering)) { |
| 172 |
if (!setequal( unique(unlist(labels)), label.ordering )) { |
| 173 |
stop("Label ordering does not match class labels.") |
| 174 |
} |
| 175 |
levels <- label.ordering |
| 176 |
} |
| 177 |
for (i in 1:length(labels)) { |
| 178 |
if (is.factor(labels)) |
| 179 |
labels[[i]] <- ordered(as.character(labels[[i]]), |
| 180 |
levels=levels) |
| 181 |
else labels[[i]] <- ordered( labels[[i]], levels=levels) |
| 182 |
} |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
if (length(levels) != 2) { |
| 187 |
message <- paste("Number of classes is not equal to 2.\n", |
| 188 |
"ROCR currently supports only evaluation of ", |
| 189 |
"binary classification tasks.",sep="") |
| 190 |
stop(message) |
| 191 |
} |
| 192 |
|
| 193 |
## determine whether predictions are continuous or categorical |
| 194 |
## (in the latter case stop; scheduled for the next ROCR version) |
| 195 |
if (!is.numeric( unlist( predictions ))) { |
| 196 |
stop("Currently, only continuous predictions are supported by ROCR.") |
| 197 |
} |
| 198 |
|
| 199 |
## compute cutoff/fp/tp data |
| 200 |
|
| 201 |
cutoffs <- list() |
| 202 |
fp <- list() |
| 203 |
tp <- list() |
| 204 |
fn <- list() |
| 205 |
tn <- list() |
| 206 |
n.pos <- list() |
| 207 |
n.neg <- list() |
| 208 |
n.pos.pred <- list() |
| 209 |
n.neg.pred <- list() |
| 210 |
for (i in 1:length(predictions)) { |
| 211 |
n.pos <- c( n.pos, sum( labels[[i]] == levels[2] )) |
| 212 |
n.neg <- c( n.neg, sum( labels[[i]] == levels[1] )) |
| 213 |
ans <- .compute.unnormalized.roc.curve( predictions[[i]], labels[[i]] ) |
| 214 |
cutoffs <- c( cutoffs, list( ans$cutoffs )) |
| 215 |
fp <- c( fp, list( ans$fp )) |
| 216 |
tp <- c( tp, list( ans$tp )) |
| 217 |
fn <- c( fn, list( n.pos[[i]] - tp[[i]] )) |
| 218 |
tn <- c( tn, list( n.neg[[i]] - fp[[i]] )) |
| 219 |
n.pos.pred <- c(n.pos.pred, list(tp[[i]] + fp[[i]]) ) |
| 220 |
n.neg.pred <- c(n.neg.pred, list(tn[[i]] + fn[[i]]) ) |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
return( new("prediction", predictions=predictions, |
| 225 |
labels=labels, |
| 226 |
cutoffs=cutoffs, |
| 227 |
fp=fp, |
| 228 |
tp=tp, |
| 229 |
fn=fn, |
| 230 |
tn=tn, |
| 231 |
n.pos=n.pos, |
| 232 |
n.neg=n.neg, |
| 233 |
n.pos.pred=n.pos.pred, |
| 234 |
n.neg.pred=n.neg.pred)) |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
## fast fp/tp computation based on cumulative summing |
| 239 |
.compute.unnormalized.roc.curve <- function( predictions, labels ) { |
| 240 |
## determine the labels that are used for the pos. resp. neg. class : |
| 241 |
pos.label <- levels(labels)[2] |
| 242 |
neg.label <- levels(labels)[1] |
| 243 |
|
| 244 |
pred.order <- order(predictions, decreasing=TRUE) |
| 245 |
predictions.sorted <- predictions[pred.order] |
| 246 |
tp <- cumsum(labels[pred.order]==pos.label) |
| 247 |
fp <- cumsum(labels[pred.order]==neg.label) |
| 248 |
|
| 249 |
## remove fp & tp for duplicated predictions |
| 250 |
## as duplicated keeps the first occurrence, but we want the last, two |
| 251 |
## rev are used. |
| 252 |
## Highest cutoff (Infinity) corresponds to tp=0, fp=0 |
| 253 |
dups <- rev(duplicated(rev(predictions.sorted))) |
| 254 |
tp <- c(0, tp[!dups]) |
| 255 |
fp <- c(0, fp[!dups]) |
| 256 |
cutoffs <- c(Inf, predictions.sorted[!dups]) |
| 257 |
|
| 258 |
return(list( cutoffs=cutoffs, fp=fp, tp=tp )) |
| 259 |
} |