KMEANS: K-means method

KMEANSR Documentation

K-means method

Description

Run K-means for clustering.

Usage

KMEANS(
  d,
  k = 9,
  criterion = c("none", "pseudo-F", "silhouette", "gap", "elbow"),
  nstart = 10,
  B = 100,
  graph = FALSE,
  seed = NULL,
  ...
)

Arguments

d

The dataset (matrix or data.frame).

k

The number of cluster.

criterion

How the number of clusters is chosen: "none" (the default, use k as it is), "pseudo-F", "silhouette", "gap" or "elbow". With any but the first, k is read as the largest number of clusters to consider. See the Details section.

nstart

Define how many random sets should be chosen.

B

The number of bootstrap samples used by criterion = "gap".

graph

A logical indicating whether or not a graphic should be plotted (cluster number selection).

seed

A specified seed for random number generation. K-means starts from a random initialisation, so without a seed two calls on the same data give different clusterings; every other clustering function of the package already had this parameter.

...

Other parameters.

Details

The four criteria criterion offers, all computed between 2 clusters and k:

"pseudo-F"

the Calinski-Harabasz index, between-cluster over within-cluster variance corrected for the number of clusters. Maximised.

"silhouette"

the mean silhouette width – how much closer each observation is to its own cluster than to the nearest other one. Maximised.

"gap"

the gap statistic: the distance between the observed within-cluster dispersion and the one expected with no cluster structure at all. The retained k is the smallest whose gap is within one standard error of the next. It is the only criterion that can answer k = 1, and much the slowest, needing B bootstrap samples.

"elbow"

the bend of the total within-cluster sum of squares. That quantity decreases with k whatever the data, so there is no optimum to take: the retained k is the point furthest from the chord joining the two ends of the curve, drawn on the graphic.

The last three need the cluster package.

Value

The clustering (kmeans object).

See Also

kmeans, predict.kmeans

Examples

require (datasets)
data (iris)
KMEANS (iris [, -5], k = 3)
KMEANS (iris [, -5], criterion = "pseudo-F") # With automatic detection of the nmber of clusters

fdm2id documentation built on Aug. 28, 2026, 9:07 a.m.