Skip to contents

Deterministic by construction so the two language arms agree exactly: initial centres are the FIRST k rows, assignment ties go to the lowest cluster index, and iteration stops when no label changes.

Usage

Compkm(X, k = 2, max_iter = 50)

Arguments

X

One composition per row; strictly positive.

k

Number of clusters (2 <= k <= n).

max_iter

Maximum Lloyd sweeps.

Value

List with cluster (one-based), centers, clr_centers, withinss, tot_withinss, iterations, n, D, k.

Details

Formula: assign i to argmin_c ||clr(x_i) - m_c||^2; m_c <- mean of the clr coordinates assigned to c; centre_c = clr^-1(m_c)

References

Aitchison (1986), The Statistical Analysis of Compositional Data, Chapter 8 (the log-ratio distance being clustered on); Lloyd (1982), Least squares quantization in PCM, IEEE Transactions on Information Theory 28(2), 129-137 (the algorithm itself).

Examples

V <- c(1, 2, 3, 4, 5, 6, 7, 8)
Compkm(V)
#> $cluster
#> [1] 1 1 1 1 1 1 1 1
#> 
#> $centers
#>      [,1] [,2]
#> [1,]    1    1
#> 
#> $clr_centers
#>      [,1]
#> [1,]    0
#> [2,]    0
#> 
#> $withinss
#> [1] 0 0
#> 
#> $tot_withinss
#> [1] 0
#> 
#> $iterations
#> [1] 2
#> 
#> $n
#> [1] 8
#> 
#> $D
#> [1] 1
#> 
#> $k
#> [1] 2
#> 
#> $method
#> [1] "Lloyd k-means in clr coordinates"
#> 
#> attr(,"class")
#> [1] "morie_rich_result" "list"