Learn R Programming

caret (version 3.21)

confusionMatrix: Create a confusion matrix

Description

Calculates a cross-tabulation of observed and predicted classes with associated statistics.

Usage

## S3 method for class 'default':
confusionMatrix(data, reference, positive = NULL, dnn = c("Prediction", "Reference"), ...)

Arguments

data
a factor of predicted classes
reference
a factor of classes to be used as the true results
positive
an optional character string for the factor level that corresponds to a "positive" result (if that makes sense for your data). If there are only two factor levels, the first level will be used as the "positive" result.
dnn
a character vector of dimnames for the table
...
options to be passed to table. NOTE: do not include dnn here

Value

  • a list with elements
  • tablethe results of table on data and reference
  • positivethe positive result level
  • overalla numeric vector with overall accuracy and Kappa statistic values
  • byClassthe sensitivity, specificity, positive predictive value and negative predictive value for each class. For two class systems, this is calculated once using the positive argument

Details

The functions requires that the factors have exactly the same levels.

For two class problems, the sensitivity, specificity, positive predictive value and negative predictive value is calculated using the positive argument. For more than two classes, these results are calculated comparing each factor level to the remaining levels (i.e. a "one versus all" approach). In each case, the overall accuracy and Kappa statistic are calculated.

The overall accuracy rate is computed along with a 95 percent confidence interval for this rate (using binom.test) and a one-sided test to see if the accuracy is better than the "no information rate," which is taken to be the largest class percentage in the data.

See Also

sensitivity, specificity, posPredValue, negPredValue, print.confusionMatrix, binom.test

Examples

Run this code
numLlvs <- 4
confusionMatrix(
   factor(sample(rep(letters[1:numLlvs], 200), 50)),
   factor(sample(rep(letters[1:numLlvs], 200), 50)))  
   
numLlvs <- 2
confusionMatrix(
   factor(sample(rep(letters[1:numLlvs], 200), 50)),
   factor(sample(rep(letters[1:numLlvs], 200), 50)))

Run the code above in your browser using DataLab