kmeans.ani {animation} | R Documentation |
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. This function provides a demo of K-Means cluster algorithm for data containing only two variables (columns).
kmeans.ani(x = matrix(runif(100), ncol = 2), centers = 2, control = ani.control(interval = 2, nmax = 30), ...)
x |
A numercal matrix containing only 2 columns. |
centers |
The number of clusters; a random set of (distinct) rows in x is chosen as the initial centers. |
control |
control parameters for the animation; see ani.control |
... |
other arguments passed to ani.control |
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).
For practical applications please refer to kmeans
.
Note that nmax
is defined as the maximum number of iterations in such a sense: an iteration includes the process of computing distances, redefining membership and finding centers. Thus there might be 2*nmax
animation frames in the output if saveANI = TRUE
.
A list with components
cluster |
A vector of integers indicating the cluster to which each point is allocated. |
centers |
A matrix of cluster centers. |
Yihui Xie
Hartigan, J. A. and Wong, M. A. (1979). A K-means clustering algorithm. Applied Statistics 28, 100-108.
#set larger 'interval' if the speed is too fast x = matrix(runif(200), ncol = 2) kmeans.ani(x = x, centers = 2, interval = 0.5) ## Not run: # create HTML animation page op = par(mar = c(2, 2, 3, 1), cex.axis = 0.75, cex.main = 1) ani.start() kmeans.ani(saveANI = TRUE, x = x, centers = 3, interval = 1, width = 600, height = 600) ani.stop() par(op) ## End(Not run)