Learn R Programming

animation (version 0.2-0)

kmeans.ani: Demonstration of K-Means cluster analysis

Description

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).

Usage

kmeans.ani(x = matrix(runif(100), ncol = 2), centers = 2, 
    control = ani.control(interval = 2, nmax = 30), ...)

Arguments

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

Value

  • A list with components
  • clusterA vector of integers indicating the cluster to which each point is allocated.
  • centersA matrix of cluster centers.

Details

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.

References

Hartigan, J. A. and Wong, M. A. (1979). A K-means clustering algorithm. Applied Statistics 28, 100-108.

See Also

kmeans

Examples

Run this code
#set larger 'interval' if the speed is too fast 
x = matrix(runif(200), ncol = 2)
kmeans.ani(x = x, centers = 2, interval = 0.5) 

# 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)

Run the code above in your browser using DataLab