Skip to contents

Creates a confusion matrix heatmap comparing true cluster assignments with estimated cluster assignments. This is the standard way to visualize clustering accuracy and identify systematic misclassification patterns.

Usage

plot_confusion_matrix(z_true, z_est, normalize = "true", theme_base_size = 14)

Arguments

z_true

Vector of true cluster assignments (length N, values 1 to K).

z_est

Vector of estimated cluster assignments (length N, values 1 to K).

normalize

Character string specifying normalization: "none" (raw counts), "true" (normalize by true cluster size), or "pred" (normalize by predicted cluster size). Default is "true".

theme_base_size

Base font size for the plot theme. Default is 14.

Value

A ggplot object showing the confusion matrix as a heatmap.

Details

The confusion matrix shows the correspondence between true and estimated cluster assignments. Rows represent true clusters, columns represent estimated clusters. Cell values indicate the number (or proportion) of subjects with that true-estimated cluster pair.

Perfect clustering produces a diagonal matrix (all subjects correctly assigned). Off-diagonal entries indicate misclassifications. This visualization complements the ARI metric by showing:

  • Which clusters are confused with each other

  • Whether misclassifications are systematic or random

  • Cluster size imbalances

  • Label permutation issues (if estimated clusters are numbered differently)

Normalization options:

  • "none": Raw counts (useful for seeing absolute numbers)

  • "true": Divide by true cluster size (shows recall/sensitivity)

  • "pred": Divide by predicted cluster size (shows precision)

Examples

if (FALSE) { # \dontrun{
# Simulate data
sim <- simulate_dynamic_synergy_data(N=30, K=3, r=3, M=8, T_each=200)

# Fit Two-step baseline
fit <- fit_two_step_baseline(
  sim$list_of_data, r=3, K=3, method="PCA",
  z_true=sim$z_true, align_basis=TRUE
)

# Plot confusion matrix
plot_confusion_matrix(sim$z_true, fit$z_est)

# Show raw counts instead of proportions
plot_confusion_matrix(sim$z_true, fit$z_est, normalize="none")

# Normalize by predicted cluster size
plot_confusion_matrix(sim$z_true, fit$z_est, normalize="pred")
} # }