G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG03EFF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G03EFF performs $K$-means cluster analysis.

## 2  Specification

 SUBROUTINE G03EFF ( WEIGHT, N, M, X, LDX, ISX, NVAR, K, CMEANS, LDC, WT, INC, NIC, CSS, CSW, MAXIT, IWK, WK, IFAIL)
 INTEGER N, M, LDX, ISX(M), NVAR, K, LDC, INC(N), NIC(K), MAXIT, IWK(N+3*K), IFAIL REAL (KIND=nag_wp) X(LDX,M), CMEANS(LDC,NVAR), WT(*), CSS(K), CSW(K), WK(N+2*K) CHARACTER(1) WEIGHT

## 3  Description

Given $n$ objects with $p$ variables measured on each object, ${x}_{\mathit{i}\mathit{j}}$, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,p$, G03EFF allocates each object to one of $K$ groups or clusters to minimize the within-cluster sum of squares:
 $∑k=1K∑i∈Sk∑j=1p xij-x-kj 2,$
where ${S}_{k}$ is the set of objects in the $k$th cluster and ${\stackrel{-}{x}}_{kj}$ is the mean for the variable $j$ over cluster $k$. This is often known as $K$-means clustering.
In addition to the data matrix, a $K$ by $p$ matrix giving the initial cluster centres for the $K$ clusters is required. The objects are then initially allocated to the cluster with the nearest cluster mean. Given the initial allocation, the procedure is to iteratively search for the $K$-partition with locally optimal within-cluster sum of squares by moving points from one cluster to another.
Optionally, weights for each object, ${w}_{i}$, can be used so that the clustering is based on within-cluster weighted sums of squares:
 $∑k=1K∑i∈Sk∑j=1pwi xij-x~kj 2,$
where ${\stackrel{~}{x}}_{kj}$ is the weighted mean for variable $j$ over cluster $k$.
The routine is based on the algorithm of Hartigan and Wong (1979).
Everitt B S (1974) Cluster Analysis Heinemann
Hartigan J A and Wong M A (1979) Algorithm AS 136: A K-means clustering algorithm Appl. Statist. 28 100–108
Kendall M G and Stuart A (1976) The Advanced Theory of Statistics (Volume 3) (3rd Edition) Griffin
Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press

## 5  Parameters

1:     WEIGHT – CHARACTER(1)Input
On entry: indicates if weights are to be used.
${\mathbf{WEIGHT}}=\text{'U'}$
No weights are used.
${\mathbf{WEIGHT}}=\text{'W'}$
Weights are used and must be supplied in WT.
Constraint: ${\mathbf{WEIGHT}}=\text{'U'}$ or $\text{'W'}$.
2:     N – INTEGERInput
On entry: $n$, the number of objects.
Constraint: ${\mathbf{N}}>1$.
3:     M – INTEGERInput
On entry: the total number of variables in array X.
Constraint: ${\mathbf{M}}\ge {\mathbf{NVAR}}$.
4:     X(LDX,M) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{X}}\left(\mathit{i},\mathit{j}\right)$ must contain the value of the $\mathit{j}$th variable for the $\mathit{i}$th object, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
5:     LDX – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G03EFF is called.
Constraint: ${\mathbf{LDX}}\ge {\mathbf{N}}$.
6:     ISX(M) – INTEGER arrayInput
On entry: ${\mathbf{ISX}}\left(\mathit{j}\right)$ indicates whether or not the $\mathit{j}$th variable is to be included in the analysis. If ${\mathbf{ISX}}\left(\mathit{j}\right)>0$, the variable contained in the $\mathit{j}$th column of X is included, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
Constraint: ${\mathbf{ISX}}\left(j\right)>0$ for NVAR values of $j$.
7:     NVAR – INTEGERInput
On entry: $p$, the number of variables included in the sums of squares calculations.
Constraint: $1\le {\mathbf{NVAR}}\le {\mathbf{M}}$.
8:     K – INTEGERInput
On entry: $K$, the number of clusters.
Constraint: ${\mathbf{K}}\ge 2$.
9:     CMEANS(LDC,NVAR) – REAL (KIND=nag_wp) arrayInput/Output
On entry: ${\mathbf{CMEANS}}\left(\mathit{i},\mathit{j}\right)$ must contain the value of the $\mathit{j}$th variable for the $\mathit{i}$th initial cluster centre, for $\mathit{i}=1,2,\dots ,K$ and $\mathit{j}=1,2,\dots ,p$.
On exit: ${\mathbf{CMEANS}}\left(\mathit{i},\mathit{j}\right)$ contains the value of the $\mathit{j}$th variable for the $\mathit{i}$th computed cluster centre, for $\mathit{i}=1,2,\dots ,K$ and $\mathit{j}=1,2,\dots ,p$.
10:   LDC – INTEGERInput
On entry: the first dimension of the array CMEANS as declared in the (sub)program from which G03EFF is called.
Constraint: ${\mathbf{LDC}}\ge {\mathbf{K}}$.
11:   WT($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least ${\mathbf{N}}$ if ${\mathbf{WEIGHT}}=\text{'W'}$, and at least $1$ otherwise.
On entry: if ${\mathbf{WEIGHT}}=\text{'W'}$, the first $n$ elements of WT must contain the weights to be used.
If ${\mathbf{WT}}\left(i\right)=0.0$, the $i$th observation is not included in the analysis. The effective number of observation is the sum of the weights.
If ${\mathbf{WEIGHT}}=\text{'U'}$, WT is not referenced and the effective number of observations is $n$.
Constraint: if ${\mathbf{WEIGHT}}=\text{'W'}$, ${\mathbf{WT}}\left(\mathit{i}\right)\ge 0.0$ and ${\mathbf{WT}}\left(\mathit{i}\right)>0.0$ for at least two values of $\mathit{i}$, for $\mathit{i}=1,2,\dots ,n$.
12:   INC(N) – INTEGER arrayOutput
On exit: ${\mathbf{INC}}\left(\mathit{i}\right)$ contains the cluster to which the $\mathit{i}$th object has been allocated, for $\mathit{i}=1,2,\dots ,n$.
13:   NIC(K) – INTEGER arrayOutput
On exit: ${\mathbf{NIC}}\left(\mathit{i}\right)$ contains the number of objects in the $\mathit{i}$th cluster, for $\mathit{i}=1,2,\dots ,K$.
14:   CSS(K) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{CSS}}\left(\mathit{i}\right)$ contains the within-cluster (weighted) sum of squares of the $\mathit{i}$th cluster, for $\mathit{i}=1,2,\dots ,K$.
15:   CSW(K) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{CSW}}\left(\mathit{i}\right)$ contains the within-cluster sum of weights of the $\mathit{i}$th cluster, for $\mathit{i}=1,2,\dots ,K$. If ${\mathbf{WEIGHT}}=\text{'U'}$, the sum of weights is the number of objects in the cluster.
16:   MAXIT – INTEGERInput
On entry: the maximum number of iterations allowed in the analysis.
Suggested value: ${\mathbf{MAXIT}}=10$.
Constraint: ${\mathbf{MAXIT}}>0$.
17:   IWK(${\mathbf{N}}+3×{\mathbf{K}}$) – INTEGER arrayWorkspace
18:   WK(${\mathbf{N}}+2×{\mathbf{K}}$) – REAL (KIND=nag_wp) arrayWorkspace
19:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{WEIGHT}}\ne \text{'W'}$ or $\text{'U'}$, or ${\mathbf{N}}<2$, or ${\mathbf{NVAR}}<1$, or ${\mathbf{M}}<{\mathbf{NVAR}}$, or ${\mathbf{K}}<2$, or ${\mathbf{LDX}}<{\mathbf{N}}$, or ${\mathbf{LDC}}<{\mathbf{K}}$, or ${\mathbf{MAXIT}}\le 0$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{WEIGHT}}=\text{'W'}$ and a value of ${\mathbf{WT}}\left(i\right)<0.0$ for some $i$, or ${\mathbf{WEIGHT}}=\text{'W'}$ and ${\mathbf{WT}}\left(i\right)=0.0$ for all or all but one values of $i$.
${\mathbf{IFAIL}}=3$
 On entry, the number of positive values in ISX does not equal NVAR.
${\mathbf{IFAIL}}=4$
On entry, at least one cluster is empty after the initial assignment. Try a different set of initial cluster centres in CMEANS and also consider decreasing the value of K. The empty clusters may be found by examining the values in NIC.
${\mathbf{IFAIL}}=5$
Convergence has not been achieved within the maximum number of iterations given by MAXIT. Try increasing MAXIT and, if possible, use the returned values in CMEANS as the initial cluster centres.

## 7  Accuracy

G03EFF produces clusters that are locally optimal; the within-cluster sum of squares may not be decreased by transferring a point from one cluster to another, but different partitions may have the same or smaller within-cluster sum of squares.

The time per iteration is approximately proportional to $\mathit{np}K$.

## 9  Example

The data consists of observations of five variables on twenty soils (see Hartigan and Wong (1979)). The data is read in, the $K$-means clustering performed and the results printed.

### 9.1  Program Text

Program Text (g03effe.f90)

### 9.2  Program Data

Program Data (g03effe.d)

### 9.3  Program Results

Program Results (g03effe.r)