Spectral MP-DP clustering with clusta

Overview

S-MPDP is the main multivariate method in clusta. It has two stages.

Stage 1 builds a locally scaled mutual k-nearest-neighbour graph. It uses the normalised Laplacian and the leading eigenvectors to represent the data. PCA then reduces this representation to one ordered signal.

Stage 2 fits candidate probability distributions to the signal. It selects a distribution by AIC. Dynamic programming then places the cluster boundaries to minimise the total weighted within-cluster spread, \(\sum_h W_h \sigma_h\).

The optimality statement applies to the one-dimensional signal and fitted model. It is not a claim of global optimality for the original multivariate problem.

Worked example

The package includes the eight-point dataset smpdp_toy.

data(smpdp_toy)
res <- spectral_mpdp(smpdp_toy[, 1:2], h = 2,
                     k_neighbors = 2, mutual = FALSE)
table(true = smpdp_toy$class,
      predicted = res$cluster_results$cluster)
#>     predicted
#> true 1 2
#>    1 4 0
#>    2 0 4

The method separates the two crescent-shaped groups in this example. The small dataset uses the symmetric graph rule through mutual = FALSE. Larger examples normally use the default mutual graph.

Using the clusta interface

S-MPDP is also available through clusta().

fit <- clusta(smpdp_toy[, 1:2], k = 2,
              method = "spectral", k_neighbors = 2)
#> Warning: 2 observation(s) have no neighbours in the Stage 1 graph at
#> k_neighbors = 2. Isolated points contribute indicator eigenvectors that can
#> dominate the 1-D signal and produce a degenerate split. Increase 'k_neighbors',
#> or use mutual = FALSE for the looser union rule.
fit$distr
#> [1] "unif"
fit$cluster
#> [1] 1 1 1 1 2 2 1 2

This returns a standard clusta object. Use spectral_mpdp() directly when you want the full S-MPDP result and its detailed diagnostics.

Real data example

The ceramic dataset contains chemical measurements from two ceramic body parts.

data(ceramic)
X <- ceramic[, setdiff(names(ceramic), "class")]
set.seed(12345)
pred <- spectral_mpdp(X, h = 2, k_neighbors = 41)$cluster_results$cluster
cluster_metrics(X, ceramic$class, pred)
#>        ARI        NMI Silhouette     Purity   Accuracy         CH         DB 
#>      1.000      1.000      0.527      1.000      1.000    109.700      0.656 
#>       Dunn 
#>      0.036

Result contents

An smpdp object contains labels, the one-dimensional score, assignment confidence, fitted boundaries, graph information, and run settings.

res <- spectral_mpdp(X, h = 2, k_neighbors = 41)
res
#> S-MPDP clustering  |  n = 88, p = 17  |  h = 2  |  k_neighbors = 41 (mutual k-NN)
#> Stage 2 distribution: unif (Akaike weight 100.00%)
#> Cluster sizes: 44, 44
#> Boundaries on the 1-D signal: 50
#> Mean assignment confidence: 0.963
#> Stage 1 graph: 1 component(s), fully connected within clusters
names(attributes(res))
#> [1] "names"       "mpdp"        "signal"      "affinity"    "eigenvalues"
#> [6] "boundaries"  "settings"    "class"
head(res$cluster_results[, c("score", "cluster", "confidence")], 3)
#>   score cluster confidence
#> 1   2.1       1      0.958
#> 2   0.2       1      0.995
#> 3   0.8       1      0.983

Confidence ranges from 0 to 1. A value near 0 means the observation is close to a fitted boundary. A value near 1 means it lies well inside its assigned cluster.

Six standard views are available.

plot(res, "clusters")
plot(res, "signal")
plot(res, "confidence")
plot(res, "eigengap")
plot(res, "similarity")
plot(res, "profile")

ggplot2::autoplot(res, "clusters")

Choosing the number of clusters

suggest_k() uses the eigengap in the Stage 1 spectrum. A clear gap can support a candidate value of k, but it should be considered with other diagnostics.

suggest_k(X, k_neighbors = 41, k_max = 8)
#> Eigen-gap suggestion  |  k_neighbors = 41 (mutual k-NN)
#> Largest gap follows eigenvalue 2, so the spectrum suggests k = 2.
#> Gaps after eigenvalue 1, 2, ...: 0.0114, 0.3164, 0.2072, 0.2095, 0.0515, 0.0308, 2e-04, 0.0047

Checking stability

cluster_stability() resamples the observations and runs S-MPDP again. It measures the best Jaccard overlap for each original cluster.

cluster_stability(X, h = 2, k_neighbors = 41, B = 10, seed = 12345)
#> Bootstrap cluster stability  |  h = 2  |  B = 10 resamples
#> Overall mean Jaccard: 0.771 (stable)
#>  cluster jaccard    grade
#>        1   0.801   stable
#>        2   0.741 doubtful
#> Guide: >0.85 highly stable, 0.75-0.85 stable, 0.60-0.75 doubtful, <0.60 unstable.

Scores above 0.85 indicate strong stability. Scores from 0.75 to 0.85 indicate good stability. Scores from 0.60 to 0.75 need care. Scores below 0.60 indicate an unstable cluster.

Checking sensitivity

The main tuning setting is k_neighbors. Use smpdp_sensitivity() to compare several neighbourhood sizes and cluster counts.

d <- gen_two_moons(300)
s <- smpdp_sensitivity(d[, c("x", "y")], d$class,
                       k_grid = c(5, 10, 15, 20, 30),
                       h_grid = 2:3)
s
plot(s)

S-MPDP checks graph connectivity during fitting. A disconnected graph or an isolated observation can distort the one-dimensional signal. Review any graph warning before interpreting the clusters.

Reference labels must not be included among the clustering features. They are for evaluation only. This point needs special care when a CSV stores its class column as numbers, because the class column can otherwise look like an ordinary numeric feature. The bundled app removes the selected label column from the feature selector automatically. Its standardised PCA display also omits and reports constant variables, which cannot be divided by a zero standard deviation.

Comparing methods

run_clustering_benchmarks() compares a selected set of methods. The optional MSC choice uses sClust::MSC() when the sClust package is installed. MSC may return a final number of clusters different from L, and clusta warns when this occurs.

preds <- run_clustering_benchmarks(
  iris[, 1:4], L = 3, k_neb = 50,
  methods = c("S-MPDP", "K-Means", "Spectral", "Agglomerative")
)
metrics <- t(sapply(preds, function(p) {
  cluster_metrics(iris[, 1:4], iris$Species, p,
                  metrics = c("ARI", "NMI", "Silhouette"))
}))
round(metrics, 3)

cluster_metrics() returns external and internal measures. Use its metrics argument to skip measures that are not required. This can save memory on large datasets because Silhouette and Dunn use pairwise distances. Most measures are better when large. Davies-Bouldin is better when small.

Use friedman_ranks() when the same methods are compared across several datasets.

friedman_ranks(ari_table)

Average ranks place results from different datasets on a common scale. The Friedman test evaluates whether the rank differences are larger than expected from sampling variation.