Skip to contents

k-means clustering with a full model-object contract

Usage

morie_cluster(
  x,
  k = 2L,
  scale = FALSE,
  na_action = c("omit", "fail"),
  case_labels = NULL,
  nofit = FALSE,
  seed = 42L,
  iter_max = 100L
)

Arguments

x

A numeric matrix or data.frame (numeric columns used).

k

Number of clusters.

scale

Standardise columns before clustering. Applying scale removes the dominance of large-variance columns; not applying it lets them dominate the distance metric – the examples contrast both.

na_action

"omit" (drop incomplete rows) or "fail" (error).

case_labels

Optional labels for cases when x has no row names.

nofit

If TRUE, return an unfitted specification.

seed

RNG seed.

iter_max

Maximum number of k-means iterations (passed to stats::kmeans() as iter.max, default 100); raise it when the fit warns that it did not converge.

Value

A morie_cluster object (or morie_cluster_spec if nofit = TRUE) whose cluster labels are ordered by decreasing size.

Examples

# with vs without scaling changes which columns drive the clusters; cases are labelled
# (an input without row names draws the UL1.2 warning and gets positional labels)
x <- iris[1:4]
rownames(x) <- paste0("s", seq_len(nrow(x)))
morie_cluster(x, k = 3)
#> <morie_cluster> k=3  n=150
#>   sizes (largest first): 62, 50, 38 
#>   assignments (first rows):
#>  s1  s2  s3  s4  s5  s6  s7  s8  s9 s10 
#>   2   2   2   2   2   2   2   2   2   2 
#>   ... 140 more
morie_cluster(x, k = 3, scale = TRUE)
#> <morie_cluster> k=3  n=150
#>   sizes (largest first): 53, 50, 47 
#>   assignments (first rows):
#>  s1  s2  s3  s4  s5  s6  s7  s8  s9 s10 
#>   2   2   2   2   2   2   2   2   2   2 
#>   ... 140 more