--- title: "Clustering using dynamic programming, from one variable to several" author: "Karuna G. Reddy, Gabiriele Bulivou, M. G. M. Khan" date: "`r Sys.Date()`" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Clustering using dynamic programming, from one variable to several} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.5) ``` ## What clusta does `clusta` uses dynamic programming to find cluster boundaries on an ordered axis. The package starts with exact one-dimensional clustering. It then uses projection, geometry, sequence order, graph structure, or spectral unfolding to work with several variables. The package supports several objectives and method choices. Each result reports the method that was used. WCSS results also report a lower bound and an optimality gap when these quantities are available. ## Exact clustering for one variable Sort the observations on a line. A contiguous clustering divides this ordered line into adjacent segments. Dynamic programming compares the possible cut points and returns the best partition for the stated objective. ```{r one-d} library(clusta) set.seed(1) x <- c(rnorm(60, 0), rnorm(60, 6), rnorm(60, 13)) fit <- clusta(x, k = 3) fit ``` For one variable, `method = "wcss"` returns the global optimum for the within-cluster sum of squares. The result is deterministic for fixed data. ## Choosing an objective The usual WCSS objective favours compact clusters. `method = "estvar"` offers an alternative objective based on weighted within-cluster spread. The default is `objective = "neyman"`, which minimises the sum of `Wh * Sh`. ```{r estvar} skewed <- c(rexp(150, 1), rexp(150, 0.3) + 6) est <- clusta(skewed, k = 3, method = "estvar", objective = "neyman") summary(est) ``` This method works directly on the data. It does not fit a probability distribution. Use `method = "parametric"` when a fitted distribution is part of the analysis. ```{r parametric, eval = requireNamespace("fitdistrplus", quietly = TRUE)} par_fit <- clusta(skewed, k = 3, method = "parametric") par_fit$distr ``` The parametric method compares candidate distributions by AIC. It then places boundaries on the fitted model with dynamic programming. ## Two variables For two clusters in two variables, `method = "exact2d"` searches the possible separating directions. It projects the data onto each direction and solves the one-dimensional problem. The best result is the global two-cluster WCSS solution in the plane. ```{r exact2d, fig.height = 5} X <- rbind(matrix(rnorm(120, 0), 60, 2), matrix(rnorm(120, 5), 60, 2)) fit_2d <- clusta(X, k = 2, method = "exact2d") plot(fit_2d) ``` For more than two clusters, `method = "sweep"` finds the best partition formed by parallel separating lines. It is exact for two clusters. For larger values of `k`, it is a structured approximation. ## Several variables `method = "projection"` reduces the data to one score. The default score is the first principal component. The dynamic program is exact on that score. ```{r projection} iris_fit <- clusta(iris[, 1:4], k = 3, method = "projection") table(cluster = iris_fit$cluster, species = iris$Species) ``` Use `method = "sequence"` when the rows have a meaningful order, such as time or position. It finds the exact multivariate segment partition for that order. Use `method = "graph"` when observations have a neighbourhood structure. This method builds a minimum spanning tree and returns a connected clustering. It is a heuristic for a general graph. For non-convex multivariate groups, use `method = "spectral"` or call `spectral_mpdp()` directly. The S-MPDP vignette explains this method in detail. ## Reading the optimality gap For a multivariate WCSS problem, `clusta` solves each variable separately to obtain a lower bound. No joint clustering can have a smaller WCSS than this bound. The difference between the fitted solution and the bound gives `gap_pct`. ```{r gap} sweep_fit <- clusta(X, k = 2, method = "sweep") c(within_ss = sweep_fit$tot.withinss, lower_bound = sweep_fit$lower_bound, gap_percent = sweep_fit$gap_pct) ``` A gap of zero certifies the global WCSS optimum. A small gap shows that the solution is close to the best possible value. A large gap suggests trying a different method or reviewing the chosen number of clusters. ## Choosing the number of clusters `clusta_elbow()` compares candidate values of `k`. It reports WCSS, BIC, silhouette width, and the optimality gap. ```{r elbow, fig.width = 8, fig.height = 3.4} clusta_elbow(x, k_range = 1:7, method = "wcss") ``` No single diagnostic is best for every dataset. Compare the available measures and check whether the resulting clusters are useful for the analysis. ## References - Wang, H. and Song, M. (2011). Ckmeans.1d.dp: Optimal k-means clustering in one dimension by dynamic programming. *The R Journal*, 3(2), 29-33. - Assuncao, R. et al. (2006). Efficient regionalization techniques. *International Journal of Geographical Information Science*, 20(7). - Piccialli, V., Sudoso, A. M., and Wiegele, A. (2022). SOS-SDP: an exact solver for minimum sum-of-squares clustering. *INFORMS Journal on Computing*.