Learn R Programming

OptimClassifier (version 0.1.5)

MC: Confusion Matrix

Description

Confusion Matrix is a contingency table that gives a visualization of the performance of an algorithm

Usage

MC(yhat, y, metrics = FALSE)

Arguments

yhat

A predicted value vector.

y

A real value vector.

metrics

Calculate all metrics. See details for more information.

Details

Also it known as an error matrix. Normally, you can identify 4 elements, they known as true positive (TP), true negative (TN), false positive (FP) and false negative (FN). To understand it, a simple example is presented:

Real Values
Estimated Class 1 Class 2
Class 1 TP FP
Class 2 FN TN

The problem arises that there is not always a clear relationship between which is the positive class or there may be different classes so it is also common to use the terms Type I error (FP), Type II error (FN) and unify the success or accuracy (TP+TN) in a single value.

Suppose a 3x3 table with notation

Real Values Estimated
Class 1 Class 2 Class 3 Class 1
A B C Class 2
D E F Class 3
G H I

where N = A+B+C+D+E+F+G+H+I The formulas used here are: $$Success rate = (A+E+I)/N$$ $$Type I error = (B+F+C)/N$$ $$Type II error = (D+H+G)/N$$

Other indicators depends of one class and in the case choose Class 1 $$Sensitivity Class 1 = A/(A+D+G)$$ $$Specificity Class 1 = (E+I)/(B+E+H+C+F+I)$$ $$Precision Class 1 = A/(A+E+I),$$ also it is called Positive Predictive Value (PPV) $$Prevalence Class 1 = (A+D+G)/N$$

References

Stehman, Stephen V. (1997). "Selecting and interpreting measures of thematic classification accuracy". Remote Sensing of Environment. 62 (1): 77<U+2013>89. doi:10.1016/S0034-4257(97)00083-7.

Examples

Run this code
# NOT RUN {
if(interactive()){
 # You can create a confusion Matrix like a table

 RealValue <- c(1,0,1,0)
 Predicted <- c(1,1,1,0)

 MC(y = RealValue, yhat=Predicted ,metrics=TRUE)


}
# }

Run the code above in your browser using DataLab