categorical_summary: Categorical summary

View source: R/metrics.R

categorical_summaryR Documentation

Categorical summary

Description

Given the observed and predicted values of categorical data (of any number of classes) computes the confusion matrix, Kappa's coefficient, Matthews' correlation coefficient, accuracy, sensitivity, specificity, precision, F1 score and Brier's score.

Usage

categorical_summary(
  observed,
  predicted,
  probabilities = NULL,
  positive_class = NULL,
  remove_na = TRUE
)

Arguments

observed

(factor) The observed values. It has to have the same length as predicted.

predicted

(factor) The observed values. It has to have the same length as observed.

probabilities

(data.frame) The probability of each class for each individual. It is required the columns names of probabilities corresponds to all classes (levels) in observed and that probabilities has as many rows as records observed.

remove_na

(logical(1)) Should NA values be removed?. TRUE by default.

Value

A list with the confusion_matrix and all metrics. Matthews' correlation coefficient is only returned for binary data and Brier's score when the probabilities matrix is provided.

See Also

Other categorical_metrics: accuracy(), brier_score(), confusion_matrix(), f1_score(), kappa_coeff(), math_mode(), matthews_coeff(), pccc(), pcic(), pr_auc(), precision(), recall(), roc_auc(), sensitivity(), specificity()

Examples

## Not run: 
categorical_summary(c("a", "b"), c("a", "b"))
categorical_summary(c("a", "b"), c("b", "a"))
categorical_summary(c("a", "b", "a"), c("b", "a", "c"))

example <- data.frame(
  observed = c("a", "a", "a", "a", "b", "b", "b", "b", "c", "c", "c", "c"),
  predicted = c("a", "a", "b", "c", "a", "b", "b", "c", "a", "b", "b", "c"),
  # Probabilities
  a = c(
    0.2377, 0.2924, 0.0406, 0.1893, 0.3978, 0.1965,
    0.0673, 0.2796, 0.1921, 0.2020, 0.1752, 0.3428
  ),
  b = c(
    0.0432, 0.1948, 0.0835, 0.3969, 0.0749, 0.0250,
    0.1507, 0.0752, 0.3952, 0.0807, 0.3097, 0.1282
  ),
  c = c(
    0.7190, 0.5126, 0.8757, 0.4136, 0.5272, 0.7783,
    0.7818, 0.6451, 0.4125, 0.7172, 0.5150, 0.5288
  )
)
categorical_summary(
  example$observed,
  example$predicted,
  probabilities = example[, c("a", "b", "c")]
)

## End(Not run)


brandon-mosqueda/SKM documentation built on Feb. 8, 2025, 5:24 p.m.