--- title: "Spectral MP-DP clustering with clusta" author: "Karuna G. Reddy, Gabiriele Bulivou, M. G. M. Khan" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Spectral MP-DP clustering with clusta} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>") library(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`. ```{r 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) ``` 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()`. ```{r clusta-interface} fit <- clusta(smpdp_toy[, 1:2], k = 2, method = "spectral", k_neighbors = 2) fit$distr fit$cluster ``` 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. ```{r ceramic} 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) ``` ## Result contents An `smpdp` object contains labels, the one-dimensional score, assignment confidence, fitted boundaries, graph information, and run settings. ```{r object} res <- spectral_mpdp(X, h = 2, k_neighbors = 41) res names(attributes(res)) head(res$cluster_results[, c("score", "cluster", "confidence")], 3) ``` 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. ```{r figs, eval = FALSE} 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. ```{r suggestk} suggest_k(X, k_neighbors = 41, k_max = 8) ``` ## Checking stability `cluster_stability()` resamples the observations and runs S-MPDP again. It measures the best Jaccard overlap for each original cluster. ```{r stability} cluster_stability(X, h = 2, k_neighbors = 41, B = 10, seed = 12345) ``` 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. ```{r sens, eval = FALSE} 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. ```{r bench, eval = FALSE} 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. ```{r ranks, eval = FALSE} 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.