Mapping the confusion matrix

The ‘confusionLabel‘ function below labels the predictions of a binary response model according to their confusion matrix categories, i.e., it classifies each prediction into a false positive, false negative, true positive or true negative, given a user-defined threshold value:

confusionLabel <- function(model, # placeholder, not yet implemented
                           obs,  # vector of observed 0 and 1 values
                           pred, # vector of predicted probabilities
                           thresh) # threshold to separate predictions
  if (length(obs) != length(pred)) stop("'obs' and 'pred' must be of the same length (and in the same order)")
  res <- rep("", length(obs))
  res[pred >= thresh & obs == 1] <- "TruePos"
  res[pred < thresh & obs == 0] <- "TrueNeg"
  res[pred >= thresh & obs == 0] <- "FalsePos"
  res[pred < thresh & obs == 1] <- "FalseNeg"

Usage example:

confusionLabel(obs = myspecies_presabs, pred = myspecies_P, thresh = 0.23)

The result is a vector with the same length as ‘obs’ and ‘pred’, containing the label for each value. This allows you to then map the confusion matrix, like this:

This function will be included in the next version of the ‘modEvA‘ package, which is soon to be released.

3 thoughts on “Mapping the confusion matrix

  1. Pingback: Package modEvA 3.0 is now on CRAN! | modTools

  2. Pingback: Mapping the confusion matrix | R-bloggers

  3. Pingback: Mapping the confusion matrix – Data Science Austria


Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s