sachsmc / plotROC

Interactive ROC plots with ggplot + d3.js.
http://sachsmc.github.io/plotROC
Other
86 stars 15 forks source link

Allow arbitrary class labels in calculate_roc and calculate_multi_roc #16

Closed sachsmc closed 9 years ago

sachsmc commented 9 years ago

Maybe follow this approach from ROCR

## convert 'labels' into ordered factors, aborting if the number
## of classes is not equal to 2.
levels <- c()
if ( label.format == "ordered" ) {
    if (!is.null(label.ordering)) {
        stop(paste("'labels' is already ordered. No additional",
                   "'label.ordering' must be supplied."))
    } else {
        levels <- levels(labels[[1]])
    }
} else {
    if ( is.null( label.ordering )) {
        if ( label.format == "factor" ) levels <- sort(levels(labels[[1]]))
        else levels <- sort( unique( unlist( labels)))
    } else {
      ## if (!setequal( levels, label.ordering)) {
      if (!setequal( unique(unlist(labels)), label.ordering )) {
        stop("Label ordering does not match class labels.")
      }
      levels <- label.ordering
    }
    for (i in 1:length(labels)) {
        if (is.factor(labels))
          labels[[i]] <- ordered(as.character(labels[[i]]),
                                 levels=levels)
        else labels[[i]] <- ordered( labels[[i]], levels=levels)
    }

}

if (length(levels) != 2) {
    message <- paste("Number of classes is not equal to 2.\n",
                     "ROCR currently supports only evaluation of ",
                     "binary classification tasks.",sep="")
    stop(message)
}
sachsmc commented 9 years ago

Took a slightly simpler approach