G03ECF (PDF version)
G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G03ECF

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.

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

G03ECF performs hierarchical cluster analysis.

2  Specification

SUBROUTINE G03ECF ( METHOD, N, D, ILC, IUC, CD, IORD, DORD, IWK, IFAIL)
INTEGER  METHOD, N, ILC(N-1), IUC(N-1), IORD(N), IWK(2*N), IFAIL
REAL (KIND=nag_wp)  D(N*(N-1)/2), CD(N-1), DORD(N)

3  Description

Given a distance or dissimilarity matrix for n objects (see G03EAF), 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 G03EHF).
At each stage, the clusters that are nearest are merged, methods differ as to how the distances between the new cluster and other clusters are computed. For three clusters i, j and k let ni, nj and nk be the number of objects in each cluster and let dij, dik and djk 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, di.jk can be computed in the following ways.
  1. Single link or nearest neighbour : di.jk = mindij,dik .
  2. Complete link or furthest neighbour : di.jk=maxdij,dik.
  3. Group average : di.jk= njnj+nk dij+ nknj+nk dik.
  4. Centroid : di.jk= njnj+nk dij+ nknj+nk dik- njnk nj+nk 2djk.
  5. Median : di.jk=12dij+12dik-14djk.
  6. Minimum variance : di.jk=ni+njdij+ni+nkdik-nidjk/ni+nj+nk.
For further details see Everitt (1974) or Krzanowski (1990).
If the clusters are numbered 1,2,,n then, for convenience, if clusters j and k, j<k, 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 djk 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.

4  References

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

5  Parameters

1:     METHOD – INTEGERInput
On entry: indicates which clustering method is used.
METHOD=1
Single link.
METHOD=2
Complete link.
METHOD=3
Group average.
METHOD=4
Centroid.
METHOD=5
Median.
METHOD=6
Minimum variance.
Constraint: METHOD=1, 2, 3, 4, 5 or 6.
2:     N – INTEGERInput
On entry: n, the number of objects.
Constraint: N2.
3:     D(N×N-1/2) – REAL (KIND=nag_wp) arrayInput/Output
On entry: the strictly lower triangle of the distance matrix. D must be stored packed by rows, i.e., Di-1i-2/2+j, i>j must contain dij.
On exit: is overwritten.
Constraint: Di0.0, for i=1,2,,nn-1/2.
4:     ILC(N-1) – INTEGER arrayOutput
On exit: ILCl contains the number, j, of the cluster merged with cluster k (see IUC), j<k, at step l, for l=1,2,,n-1.
5:     IUC(N-1) – INTEGER arrayOutput
On exit: IUCl contains the number, k, of the cluster merged with cluster j, j<k, at step l, for l=1,2,,n-1.
6:     CD(N-1) – REAL (KIND=nag_wp) arrayOutput
On exit: CDl contains the distance djk, between clusters j and k, j<k, merged at step l, for l=1,2,,n-1.
7:     IORD(N) – INTEGER arrayOutput
On exit: the objects in dendrogram order.
8:     DORD(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the clustering distances corresponding to the order in IORD. DORDl contains the distance at which cluster IORDl and IORDl+1 merge, for l=1,2,,n-1. DORDn contains the maximum distance.
9:     IWK(2×N) – INTEGER arrayWorkspace
10:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ 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​ 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 -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
IFAIL=1
On entry,METHOD1, 2, 3, 4, 5 or 6,
orN<2.
IFAIL=2
On entry,Di<0.0 for some i=1,2,,nn-1/2.
IFAIL=3
A true dendrogram cannot be formed because the distances at which clusters have merged are not increasing for all steps, i.e., CDl<CDl-1 for some l=2,3,,n-1. This can occur for the median and centroid methods.

7  Accuracy

For METHOD3 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 dij and dkl, (i<k) or (i=k), and j<l, 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 dij and dkl is affected by rounding errors then their equality, and hence the dendrogram, may be affected.

8  Further Comments

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

9  Example

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

9.1  Program Text

Program Text (g03ecfe.f90)

9.2  Program Data

Program Data (g03ecfe.d)

9.3  Program Results

Program Results (g03ecfe.r)


G03ECF (PDF version)
G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012