Calculates the confusion matrix for a binary classification problem once and then calculates all confusion measures of this package.

confusion_matrix(truth, response, positive, na_value = NaN, relative = FALSE)

Arguments

truth

:: factor()
True (observed) labels. Must have the exactly same two levels and the same length as response.

response

:: factor()
Predicted response labels. Must have the exactly same two levels and the same length as truth.

positive

:: character(1)
Name of the positive class.

na_value

:: numeric(1)
Value that should be returned if the measure is not defined for the input (as described in the note). Default is NaN.

relative

:: logical(1)
If TRUE, the returned confusion matrix contains relative frequencies instead of absolute frequencies.

Value

List with two elements:

  • matrix stores the calculated confusion matrix.

  • measures stores the metrics as named numeric vector.

Examples

set.seed(123) lvls = c("a", "b") truth = factor(sample(lvls, 20, replace = TRUE), levels = lvls) response = factor(sample(lvls, 20, replace = TRUE), levels = lvls) confusion_matrix(truth, response, positive = "a")
#> $matrix #> truth #> response a b #> a 7 5 #> b 4 4 #> #> $measures #> acc ce dor f1 fdr fnr fomr #> 0.55000000 0.45000000 1.40000000 0.60869565 0.41666667 0.36363636 0.50000000 #> fpr mcc npv ppv tnr tpr #> 0.55555556 0.08206099 0.50000000 0.58333333 0.44444444 0.63636364 #> #> attr(,"class") #> [1] "confusion_matrix"
confusion_matrix(truth, response, positive = "a", relative = TRUE)
#> $matrix #> truth #> response a b #> a 0.35 0.25 #> b 0.20 0.20 #> #> $measures #> acc ce dor f1 fdr fnr fomr #> 0.55000000 0.45000000 1.40000000 0.60869565 0.41666667 0.36363636 0.50000000 #> fpr mcc npv ppv tnr tpr #> 0.55555556 0.08206099 0.50000000 0.58333333 0.44444444 0.63636364 #> #> attr(,"class") #> [1] "confusion_matrix"
confusion_matrix(truth, response, positive = "b")
#> $matrix #> truth #> response b a #> b 4 4 #> a 5 7 #> #> $measures #> acc ce dor f1 fdr fnr fomr #> 0.55000000 0.45000000 1.40000000 0.47058824 0.50000000 0.55555556 0.41666667 #> fpr mcc npv ppv tnr tpr #> 0.36363636 0.08206099 0.58333333 0.50000000 0.63636364 0.44444444 #> #> attr(,"class") #> [1] "confusion_matrix"