Groups the units at frame by the similarity of their profiles and returns
the scale with those groups added as a new, coarser frame.
cluster_scale(
data,
x,
k,
frame = NULL,
key = NULL,
value = NULL,
method = c("pam", "hclust", "kmeans"),
distance = "euclidean",
scale_units = FALSE,
new_frame = "cluster",
labels = "c",
hclust_method = "ward.D2",
seed = NULL,
...
)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.
A NestedScale.
Number of clusters.
Frame whose units are clustered. Defaults to the atom frame.
Columns of data; inferred when unambiguous.
"pam" (partitioning around medoids, via the cluster
package), "hclust" (stats::hclust) or "kmeans" (stats::kmeans).
"pam" and "hclust" work from the distance matrix, so they honour
distance=; "kmeans" works on the raw profiles and ignores it.
A name from list_scale_distances().
Standardise each unit's profile before measuring;
see scale_distance().
Name for the frame the clusters form.
How to name the clusters: a prefix (the default "c" gives
c01, c02, ...), or "medoid" to name each cluster after its
representative unit, which keeps the codes meaningful.
Linkage for method = "hclust".
Seed for "kmeans", which starts from a random assignment.
NULL leaves the session's stream alone.
Passed to the underlying algorithm.
The scale, with new_frame inserted directly above frame. The
assignment is also returned as the "clustering" attribute: a data frame
of unit, cluster and (for medoid-based methods) whether the unit is its
cluster's medoid.
The new frame goes directly above the clustered one, which is the only
position that is always true: the clusters are coarser than frame by
construction, but they need not nest inside any existing coarser frame, and
nestedscales does not require them to. Use
scale_nests() to ask whether a given pair happens to nest.
s <- scale_example()
set.seed(1)
d <- data.frame(
unit = rep(scale_units(s), each = 4),
t = rep(sprintf("t%d", 1:4), 7),
v = as.numeric(seq_len(28))
)
cl <- cluster_scale(d, s, k = 3)
scale_frames(cl)
#> [1] "sector" "class" "group" "cluster" "unit"
head(attr(cl, "clustering"))
#> unit cluster medoid
#> 1 U1 c01 FALSE
#> 2 U2 c01 TRUE
#> 3 U3 c01 FALSE
#> 4 U4 c02 TRUE
#> 5 U5 c02 FALSE
#> 6 U6 c03 FALSE