# NAG CL Interfaceg03ecc (cluster_​hier)

Settings help

CL Name Style:

## 1Purpose

g03ecc performs hierarchical cluster analysis.

## 2Specification

 #include
 void g03ecc (Nag_ClusterMethod method, Integer n, double d[], Integer ilc[], Integer iuc[], double cd[], Integer iord[], double dord[], NagError *fail)
The function may be called by the names: g03ecc, nag_mv_cluster_hier or nag_mv_hierar_cluster_analysis.

## 3Description

Given a distance or dissimilarity matrix for $n$ objects (see g03eac), cluster analysis aims to group the $n$ objects into a number of more or less homogeneous groups or clusters. With agglomerative clustering methods, a hierarchical tree is produced by starting with $n$ clusters, each with a single object and then at each of $n-1$ stages, merging two clusters to form a larger cluster, until all objects are in a single cluster. This process may be represented by a dendrogram (see g03ehc).
At each stage, the clusters that are nearest are merged, methods differ as to how the distance between the new cluster and other clusters are computed. For three clusters $i$, $j$ and $k$ let ${n}_{i}$, ${n}_{j}$ and ${n}_{k}$ be the number of objects in each cluster and let ${d}_{ij}$, ${d}_{ik}$ and ${d}_{jk}$ be the distances between the clusters. Let clusters $j$ and $k$ be merged to give cluster $jk$, then the distance from cluster $i$ to cluster $jk$, ${d}_{i.jk}$ can be computed in the following ways:
1. 1.Single link or nearest neighbour: ${d}_{i.jk}=\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({d}_{ij},{d}_{ik}\right)$.
2. 2.Complete link or furthest neighbour: ${d}_{i.jk}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({d}_{ij},{d}_{ik}\right)$.
3. 3.Group average: ${d}_{i.jk}=\frac{{n}_{j}}{{n}_{j}+{n}_{k}}{d}_{ij}+\frac{{n}_{k}}{{n}_{j}+{n}_{k}}{d}_{ik}$.
4. 4.Centroid: ${d}_{i.jk}=\frac{{n}_{j}}{{n}_{j}+{n}_{k}}{d}_{ij}+\frac{{n}_{k}}{{n}_{j}+{n}_{k}}{d}_{ik}-\frac{{n}_{j}{n}_{k}}{{\left({n}_{j}+{n}_{k}\right)}^{2}}{d}_{jk}$.
5. 5.Median: ${d}_{i.jk}=\frac{1}{2}{d}_{ij}+\frac{1}{2}{d}_{ik}-\frac{1}{4}{d}_{jk}$.
6. 6.Minimum variance: ${d}_{i.jk}=\left\{\left({n}_{i}+{n}_{j}\right){d}_{ij}+\left({n}_{i}+{n}_{k}\right){d}_{ik}-{n}_{i}{d}_{jk}\right\}/\left({n}_{i}+{n}_{j}+{n}_{k}\right)$.
For further details see Everitt (1974) or Krzanowski (1990).
If the clusters are numbered $1,2,\dots ,n$ then, for convenience, if clusters $j$ and $k$, $j, merge then the new cluster will be referred to as cluster $j$. Information on the clustering history is given by the values of $j$, $k$ and ${d}_{jk}$ for each of the $n-1$ clustering steps. In order to produce a dendrogram, the ordering of the objects such that the clusters that merge are adjacent is required. This ordering is computed so that the first element is $1$. The associated distances with this ordering are also computed.

## 4References

Everitt B S (1974) Cluster Analysis Heinemann
Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press

## 5Arguments

1: $\mathbf{method}$Nag_ClusterMethod Input
On entry: indicates which clustering.
${\mathbf{method}}=\mathrm{Nag_SingleLink}$
${\mathbf{method}}=\mathrm{Nag_CompleteLink}$
${\mathbf{method}}=\mathrm{Nag_GroupAverage}$
Group average.
${\mathbf{method}}=\mathrm{Nag_Centroid}$
Centroid.
${\mathbf{method}}=\mathrm{Nag_Median}$
Median.
${\mathbf{method}}=\mathrm{Nag_MinVariance}$
Minimum variance.
Constraint: ${\mathbf{method}}=\mathrm{Nag_SingleLink}$, $\mathrm{Nag_CompleteLink}$, $\mathrm{Nag_GroupAverage}$, $\mathrm{Nag_Centroid}$, $\mathrm{Nag_Median}$ or $\mathrm{Nag_MinVariance}$.
2: $\mathbf{n}$Integer Input
On entry: the number of objects, $n$.
Constraint: ${\mathbf{n}}\ge 2$.
3: $\mathbf{d}\left[{\mathbf{n}}×\left({\mathbf{n}}-1\right)/2\right]$double Input/Output
On entry: the strictly lower triangle of the distance matrix. $D$ must be stored packed by rows, i.e., ${\mathbf{d}}\left[\left(i-1\right)\left(i-2\right)/2+j-1\right]$, $i>j$ must contain ${d}_{ij}$.
On exit: is overwritten.
Constraint: ${\mathbf{d}}\left[\mathit{i}-1\right]\ge 0.0$, for $\mathit{i}=1,2,\dots ,n\left(n-1\right)/2$.
4: $\mathbf{ilc}\left[{\mathbf{n}}-1\right]$Integer Output
On exit: ${\mathbf{ilc}}\left[\mathit{l}-1\right]$ contains the number, $j$, of the cluster merged with cluster $k$ (see iuc), $j, at step $\mathit{l}$, for $\mathit{l}=1,2,\dots ,n-1$.
5: $\mathbf{iuc}\left[{\mathbf{n}}-1\right]$Integer Output
On exit: ${\mathbf{iuc}}\left[\mathit{l}-1\right]$ contains the number, $k$, of the cluster merged with cluster $j$, $j, at step $\mathit{l}$, for $\mathit{l}=1,2,\dots ,n-1$.
6: $\mathbf{cd}\left[{\mathbf{n}}-1\right]$double Output
On exit: ${\mathbf{cd}}\left[\mathit{l}-1\right]$ contains the distance ${d}_{jk}$, between clusters $j$ and $k$, $j, merged at step $\mathit{l}$, for $\mathit{l}=1,2,\dots ,n-1$.
7: $\mathbf{iord}\left[{\mathbf{n}}\right]$Integer Output
On exit: the objects in dendrogram order.
8: $\mathbf{dord}\left[{\mathbf{n}}\right]$double Output
On exit: the clustering distances corresponding to the order in iord. ${\mathbf{dord}}\left[\mathit{l}-1\right]$ contains the distance at which cluster ${\mathbf{iord}}\left[\mathit{l}-1\right]$ and ${\mathbf{iord}}\left[\mathit{l}\right]$ merge, for $\mathit{l}=1,2,\dots ,n-1$. ${\mathbf{dord}}\left[n-1\right]$ contains the maximum distance.
9: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument method had an illegal value.
NE_DENDROGRAM
A true dendrogram cannot be formed because the distances at which clusters have merged are not increasing for all steps, i.e., ${\mathbf{cd}}\left[i-1\right]<{\mathbf{cd}}\left[i-2\right]$ for some $i=2,3,\dots ,n-1$. This can occur for the ${\mathbf{method}}=\mathrm{Nag_Centroid}$ and ${\mathbf{method}}=\mathrm{Nag_Median}$ methods.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 2$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_REALARR
On entry, ${\mathbf{d}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{d}}\left[\mathit{i}-1\right]\ge 0.0$, for $\mathit{i}=1,2,\dots ,n×\left(n-1\right)/2$.

## 7Accuracy

For methods other than ${\mathbf{method}}=\mathrm{Nag_SingleLink}$ or $\mathrm{Nag_CompleteLink}$, slight rounding errors may occur in the calculations of the updated distances. These would not normally significantly affect the results, however there may be an effect if distances are (almost) equal.
If at a stage, two distances ${d}_{ij}$ and ${d}_{kl}$, $i or $i=k$ and $j, are equal then clusters $k$ and $l$ will be merged rather than clusters $i$ and $j$. For single link clustering this choice will only affect the order of the objects in the dendrogram. However, for other methods the choice of $kl$ rather than $ij$ may affect the shape of the dendrogram. If either of the distances ${d}_{ij}$ or ${d}_{kl}$ are affected by rounding errors then their equality, and hence the dendrogram, may be affected.

## 8Parallelism and Performance

g03ecc is not threaded in any implementation.

The dendrogram may be formed using g03ehc. Groupings based on the clusters formed at a given distance can be computed using g03ejc.

## 10Example

Data consisting of three variables on five objects are read in. Euclidean squared distances based on two variables are computed using g03eac, the objects are clustered using g03ecc and the dendrogram computed using g03ehc. The dendrogram is then printed.

### 10.1Program Text

Program Text (g03ecce.c)

### 10.2Program Data

Program Data (g03ecce.d)

### 10.3Program Results

Program Results (g03ecce.r)