| 1 |
#' @templateVar class confusionMatrix |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x An object of class `confusionMatrix` created by a call to |
| 5 |
#' [caret::confusionMatrix()]. |
| 6 |
#' @param by_class Logical indicating whether or not to show performance |
| 7 |
#' measures broken down by class. Defaults to `TRUE`. When `by_class = FALSE` |
| 8 |
#' only returns a tibble with accuracy, kappa, and McNemar statistics. |
| 9 |
#' @template param_unused_dots |
| 10 |
#' |
| 11 |
#' @evalRd return_tidy( |
| 12 |
#' "term", |
| 13 |
#' "estimate", |
| 14 |
#' "conf.low", |
| 15 |
#' "conf.high", |
| 16 |
#' "class", |
| 17 |
#' p.value = "P-value for accuracy and kappa statistics." |
| 18 |
#' ) |
| 19 |
#' |
| 20 |
#' @examplesIf rlang::is_installed("caret") |
| 21 |
#' |
| 22 |
#' # load libraries for models and data |
| 23 |
#' library(caret) |
| 24 |
#' |
| 25 |
#' set.seed(27) |
| 26 |
#' |
| 27 |
#' # generate data |
| 28 |
#' two_class_sample1 <- as.factor(sample(letters[1:2], 100, TRUE)) |
| 29 |
#' two_class_sample2 <- as.factor(sample(letters[1:2], 100, TRUE)) |
| 30 |
#' |
| 31 |
#' two_class_cm <- confusionMatrix( |
| 32 |
#' two_class_sample1, |
| 33 |
#' two_class_sample2 |
| 34 |
#' ) |
| 35 |
#' |
| 36 |
#' # summarize model fit with tidiers |
| 37 |
#' tidy(two_class_cm) |
| 38 |
#' tidy(two_class_cm, by_class = FALSE) |
| 39 |
#' |
| 40 |
#' # multiclass example |
| 41 |
#' six_class_sample1 <- as.factor(sample(letters[1:6], 100, TRUE)) |
| 42 |
#' six_class_sample2 <- as.factor(sample(letters[1:6], 100, TRUE)) |
| 43 |
#' |
| 44 |
#' six_class_cm <- confusionMatrix( |
| 45 |
#' six_class_sample1, |
| 46 |
#' six_class_sample2 |
| 47 |
#' ) |
| 48 |
#' |
| 49 |
#' # summarize model fit with tidiers |
| 50 |
#' tidy(six_class_cm) |
| 51 |
#' tidy(six_class_cm, by_class = FALSE) |
| 52 |
#' |
| 53 |
#' @aliases caret_tidiers confusionMatrix_tidiers |
| 54 |
#' @export |
| 55 |
#' @seealso [tidy()], [caret::confusionMatrix()] |
| 56 |
tidy.confusionMatrix <- function(x, by_class = TRUE, ...) { |
| 57 |
cm <- as.list(x$overall) |
| 58 |
nms_cm <- stringr::str_to_lower(c(names(cm)[1:2], "McNemar")) |
| 59 |
|
| 60 |
if (by_class) { |
| 61 |
# case when only 2 classes |
| 62 |
if (!inherits(x$byClass, "matrix")) { |
| 63 |
classes <- |
| 64 |
x$byClass |> |
| 65 |
as.data.frame() |> |
| 66 |
rename_at(1, ~"value") |> |
| 67 |
tibble::rownames_to_column("var") |> |
| 68 |
mutate(var = stringr::str_to_lower(gsub(" ", "_", var))) |
| 69 |
|
| 70 |
terms <- c(nms_cm, classes$var) |
| 71 |
class <- c(rep(NA_character_, 3), rep(x$positive, length(terms) - 3)) |
| 72 |
estimates <- c(cm$Accuracy, cm$Kappa, NA, classes$value) |
| 73 |
conf.low <- c(cm$AccuracyLower, rep(NA, length(terms) - 1)) |
| 74 |
conf.high <- c(cm$AccuracyUpper, rep(NA, length(terms) - 1)) |
| 75 |
p.value <- c( |
| 76 |
cm$AccuracyPValue, |
| 77 |
NA, |
| 78 |
cm$McnemarPValue, |
| 79 |
rep(NA, length(terms) - 3) |
| 80 |
) |
| 81 |
} else { |
| 82 |
# case when there are more than 2 classes |
| 83 |
classes <- |
| 84 |
x$byClass |> |
| 85 |
as.data.frame() |> |
| 86 |
tibble::rownames_to_column("class") |> |
| 87 |
pivot_longer( |
| 88 |
cols = c(dplyr::everything(), -class), |
| 89 |
names_to = "var", |
| 90 |
values_to = "value" |
| 91 |
) |> |
| 92 |
mutate( |
| 93 |
var = stringr::str_to_lower(gsub(" ", "_", var)), |
| 94 |
class = gsub("Class: ", "", class) |
| 95 |
) |
| 96 |
|
| 97 |
terms <- c(nms_cm, classes$var) |
| 98 |
class <- c(rep(NA_character_, 3), classes$class) |
| 99 |
estimates <- c(cm$Accuracy, cm$Kappa, NA, classes$value) |
| 100 |
conf.low <- c(cm$AccuracyLower, rep(NA, length(terms) - 1)) |
| 101 |
conf.high <- c(cm$AccuracyUpper, rep(NA, length(terms) - 1)) |
| 102 |
p.value <- c( |
| 103 |
cm$AccuracyPValue, |
| 104 |
NA, |
| 105 |
cm$McnemarPValue, |
| 106 |
rep(NA, length(terms) - 3) |
| 107 |
) |
| 108 |
} |
| 109 |
df <- tibble( |
| 110 |
term = terms, |
| 111 |
class = class, |
| 112 |
estimate = estimates, |
| 113 |
conf.low = conf.low, |
| 114 |
conf.high = conf.high, |
| 115 |
p.value = p.value |
| 116 |
) |
| 117 |
} else { |
| 118 |
# only show alpha, kappa, and mcnamara, when show_class = FALSE |
| 119 |
terms <- c(nms_cm) |
| 120 |
estimates <- c(cm$Accuracy, cm$Kappa, NA) |
| 121 |
conf.low <- c(cm$AccuracyLower, NA, NA) |
| 122 |
conf.high <- c(cm$AccuracyUpper, NA, NA) |
| 123 |
p.value <- c(cm$AccuracyPValue, NA, cm$McnemarPValue) |
| 124 |
|
| 125 |
df <- tibble( |
| 126 |
term = terms, |
| 127 |
estimate = estimates, |
| 128 |
conf.low = conf.low, |
| 129 |
conf.high = conf.high, |
| 130 |
p.value = p.value |
| 131 |
) |
| 132 |
} |
| 133 |
|
| 134 |
as_tidy_tibble(df) |
| 135 |
} |