This function provides a demo of the k-Means cluster algorithm for data containing only two variables (columns).
kmeans.ani(
x = cbind(X1 = runif(50), X2 = runif(50)),
centers = 3,
hints = c("Move centers!", "Find cluster?"),
pch = 1:3,
col = 1:3
)
A numercal matrix or an object that can be coerced to such a matrix (such as a numeric vector or a data frame with all numeric columns) containing only 2 columns.
Either the number of clusters or a set of initial (distinct)
cluster centres. If a number, a random set of (distinct) rows in x
is chosen as the initial centres.
Two text strings indicating the steps of k-means clustering: move the center or find the cluster membership?
Symbols and colors for different clusters; the length of these two arguments should be equal to the number of clusters, or they will be recycled.
A list with components
A vector of integers indicating the cluster to which each point is allocated.
A matrix of cluster centers.
The k-Means cluster algorithm may be regarded as a series of iterations of: finding cluster centers, computing distances between sample points, and redefining cluster membership.
The data given by x
is clustered by the \(k\)-means method, which
aims to partition the points into \(k\) groups such that the sum of squares
from points to the assigned cluster centers is minimized. At the minimum, all
cluster centres are at the mean of their Voronoi sets (the set of data points
which are nearest to the cluster centre).
Examples at https://yihui.org/animation/example/kmeans-ani/