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.
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.
library(clusta)
set.seed(1)
x <- c(rnorm(60, 0), rnorm(60, 6), rnorm(60, 13))
fit <- clusta(x, k = 3)
fit
#> clusta clustering | method: wcss | k = 3 | n = 180, p = 1
#> Cluster sizes: 60, 60, 60
#>
#> Cluster centres:
#> V1
#> C1 0.1076
#> C2 6.1118
#> C3 12.9535
#>
#> Within-cluster sum of squares by cluster:
#> [1] 43.1475 49.3206 61.8346
#> (between_SS / total_SS = 97.0 %)
#>
#> Model diagnostics: BIC = 909.75 | avg. silhouette width = 0.825
#> Optimality: certified global optimum (gap = 0%).
#>
#> Available components:
#> [1] "cluster" "centers" "size" "withinss"
#> [5] "tot.withinss" "totss" "betweenss" "lower_bound"
#> [9] "gap_pct" "k" "p" "method"
#> [13] "objective" "obj_value" "distr" "fit_engine"
#> [17] "fit_params" "fit_ranking" "fit_compared" "data"
#> [21] "scores" "axis_label" "boundaries" "direction"
#> [25] "var_explained" "line" "order" "bic"
#> [29] "silinfo" "avg.silwidth"For one variable, method = "wcss" returns the global
optimum for the within-cluster sum of squares. The result is
deterministic for fixed data.
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.
skewed <- c(rexp(150, 1), rexp(150, 0.3) + 6)
est <- clusta(skewed, k = 3, method = "estvar", objective = "neyman")
summary(est)
#> clusta summary | method = estvar | k = 3
#> Total within SS = 566.7110 Between SS = 5490.5345 (R^2 = 0.906)
#> BIC = 1684.80 avg. silhouette width = 0.727 optimality gap <= 9.27%
#>
#> cluster size prop withinss sil.width V1
#> 1 148 0.4933 104.6510 0.846 0.9218
#> 2 102 0.3400 89.8285 0.749 7.2924
#> 3 50 0.1667 372.2315 0.330 12.0625This 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.
The parametric method compares candidate distributions by AIC. It then places boundaries on the fitted model with dynamic programming.
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.
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.
method = "projection" reduces the data to one score. The
default score is the first principal component. The dynamic program is
exact on that score.
iris_fit <- clusta(iris[, 1:4], k = 3, method = "projection")
table(cluster = iris_fit$cluster, species = iris$Species)
#> species
#> cluster setosa versicolor virginica
#> 1 50 0 0
#> 2 0 49 12
#> 3 0 1 38Use 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.
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.
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)
#> within_ss lower_bound gap_percent
#> 246.371207 245.114192 0.512828A 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.
clusta_elbow() compares candidate values of
k. It reports WCSS, BIC, silhouette width, and the
optimality gap.
No single diagnostic is best for every dataset. Compare the available measures and check whether the resulting clusters are useful for the analysis.