# \donttest{
# Attach cvms
library(cvms)
library(ggplot2)
# Two classes
# Create targets and predictions data frame
data <- data.frame(
"target" = c("A", "B", "A", "B", "A", "B", "A", "B",
"A", "B", "A", "B", "A", "B", "A", "A"),
"prediction" = c("B", "B", "A", "A", "A", "B", "B", "B",
"B", "B", "A", "B", "A", "A", "A", "A"),
stringsAsFactors = FALSE
)
# Evaluate predictions and create confusion matrix
evaluation <- evaluate(
data = data,
target_col = "target",
prediction_cols = "prediction",
type = "binomial"
)
# Inspect confusion matrix tibble
evaluation[["Confusion Matrix"]][[1]]
# Plot confusion matrix
# Supply confusion matrix tibble directly
plot_confusion_matrix(evaluation[["Confusion Matrix"]][[1]])
# Plot first confusion matrix in evaluate() output
plot_confusion_matrix(evaluation)
# }
if (FALSE) {
# Add sum tiles
plot_confusion_matrix(evaluation, add_sums = TRUE)
}
# \donttest{
# Add labels to diagonal row and column percentages
# This example assumes "B" is the positive class
# but you could write anything as prefix to the percentages
plot_confusion_matrix(
evaluation,
font_row_percentages = font(prefix=c("NPV = ", "", "", "PPV = ")),
font_col_percentages = font(prefix=c("Spec = ", "", "", "Sens = "))
)
# Three (or more) classes
# Create targets and predictions data frame
data <- data.frame(
"target" = c("A", "B", "C", "B", "A", "B", "C",
"B", "A", "B", "C", "B", "A"),
"prediction" = c("C", "B", "A", "C", "A", "B", "B",
"C", "A", "B", "C", "A", "C"),
stringsAsFactors = FALSE
)
# Evaluate predictions and create confusion matrix
evaluation <- evaluate(
data = data,
target_col = "target",
prediction_cols = "prediction",
type = "multinomial"
)
# Inspect confusion matrix tibble
evaluation[["Confusion Matrix"]][[1]]
# Plot confusion matrix
# Supply confusion matrix tibble directly
plot_confusion_matrix(evaluation[["Confusion Matrix"]][[1]])
# Plot first confusion matrix in evaluate() output
plot_confusion_matrix(evaluation)
# }
if (FALSE) {
# Add sum tiles
plot_confusion_matrix(evaluation, add_sums = TRUE)
}
# \donttest{
# Counts only
plot_confusion_matrix(
evaluation[["Confusion Matrix"]][[1]],
add_normalized = FALSE,
add_row_percentages = FALSE,
add_col_percentages = FALSE
)
# Change color palette to green
# Change theme to `theme_light`.
plot_confusion_matrix(
evaluation[["Confusion Matrix"]][[1]],
palette = "Greens",
theme_fn = ggplot2::theme_light
)
# }
if (FALSE) {
# Change colors palette to custom gradient
# with a different gradient for sum tiles
plot_confusion_matrix(
evaluation[["Confusion Matrix"]][[1]],
palette = list("low" = "#B1F9E8", "high" = "#239895"),
sums_settings = sum_tile_settings(
palette = list("low" = "#e9e1fc", "high" = "#BE94E6")
),
add_sums = TRUE
)
}
# \donttest{
# The output is a ggplot2 object
# that you can add layers to
# Here we change the axis labels
plot_confusion_matrix(evaluation[["Confusion Matrix"]][[1]]) +
ggplot2::labs(x = "True", y = "Guess")
# Replace the bottom tile text
# with some information
# First extract confusion matrix
# Then add new column with text
cm <- evaluation[["Confusion Matrix"]][[1]]
cm[["Trials"]] <- c(
"(8/9)", "(3/9)", "(1/9)",
"(3/9)", "(7/9)", "(4/9)",
"(1/9)", "(2/9)", "(8/9)"
)
# Now plot with the `sub_col` argument specified
plot_confusion_matrix(cm, sub_col="Trials")
# }
Run the code above in your browser using DataLab