Skip to contents

Agglomerative clustering via the Rust kodama crate, a port of fastcluster. Returns a standard stats::hclust object, so stats::cutree(), stats::as.dendrogram(), plot() and the rest of R's hierarchical clustering ecosystem work on the result unchanged.

Usage

shoal_hclust(
  d,
  method = c("complete", "single", "average", "weighted", "ward", "centroid", "median")
)

Arguments

d

A stats::dist object, or a numeric matrix or data frame, in which case Euclidean distances are computed on the way, into the single buffer the clustering works on: half the peak memory of calling shoal_dist() first, at the cost of not keeping the distances.

method

Linkage method. One of "complete", "single", "average", "weighted", "ward", "centroid" or "median".

Value

An object of class "hclust" with components merge, height, order, labels, method, call and dist.method.

Linkage methods

Dissimilarities are squared internally for "ward", "centroid" and "median", with the square root taken afterwards, the fastcluster and SciPy convention. Two consequences differ from stats::hclust():

  • "ward" corresponds to R's "ward.D2", not "ward.D".

  • "centroid" and "median" take plain distances here, whereas stats::hclust() expects them to be squared beforehand.

"weighted" is R's "mcquitty" (WPGMA); "average" is UPGMA.

"centroid" and "median" can produce inversions, a merge at a lower height than the one before it. This is a property of the methods, not a bug, but stats::cutree() rejects such trees, so a warning is issued when it happens.

See also

shoal_dist() for building the distance matrix.

Examples

d <- shoal_dist(as.matrix(iris[, 1:4]))
fit <- shoal_hclust(d, method = "ward")
cutree(fit, k = 3)
#>   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#>  [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
#>  [75] 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 3 3 3 3 2 3 3 3 3
#> [112] 3 3 2 2 3 3 3 3 2 3 2 3 2 3 3 2 2 3 3 3 3 3 2 2 3 3 3 2 3 3 3 2 3 3 3 2 3
#> [149] 3 2