Cluster quality across a range of k

cluster_sweep(
  data,
  x,
  ks,
  frame = NULL,
  key = NULL,
  value = NULL,
  distance = "euclidean",
  scale_units = FALSE,
  FUN = cluster_scale,
  ...
)

Arguments

data

Long data: unit codes, one or more identifier columns saying which observation each row is, and a value column. A unit that is not observed over the same set as the others is an error rather than a hole.

x

A NestedScale.

ks

Values of k to try.

frame

Frame whose units are clustered. Defaults to the atom frame.

key, value

Columns of data; inferred when unambiguous.

distance

A name from list_scale_distances().

scale_units

Standardise each unit's profile before measuring; see scale_distance().

FUN

The clustering function to sweep: cluster_scale() (default) or cluster_contiguous().

...

Passed to FUN.

Value

A data frame with one row per k: k, within (total within-cluster sum of distances to the cluster medoid), silhouette (average, NA at k = 1), and smallest (the size of the smallest cluster, which is often what rules a k out).

Examples

s <- scale_example()
d <- data.frame(
  unit = rep(scale_units(s), each = 4),
  t = rep(sprintf("t%d", 1:4), 7), v = as.numeric(seq_len(28))
)
cluster_sweep(d, s, ks = 2:4)
#>   k within silhouette smallest
#> 1 2     48  0.5147392        3
#> 2 3     32  0.4197279        2
#> 3 4     24  0.2387755        1