NAG FL Interface
g03ecf (cluster_hier)
1
Purpose
g03ecf performs hierarchical cluster analysis.
2
Specification
Fortran Interface
Integer, Intent (In) |
:: |
method, n |
Integer, Intent (Inout) |
:: |
ifail |
Integer, Intent (Out) |
:: |
ilc(n-1), iuc(n-1), iord(n), iwk(2*n) |
Real (Kind=nag_wp), Intent (Inout) |
:: |
d(n*(n-1)/2) |
Real (Kind=nag_wp), Intent (Out) |
:: |
cd(n-1), dord(n) |
|
C Header Interface
#include <nag.h>
void |
g03ecf_ (const Integer *method, const Integer *n, double d[], Integer ilc[], Integer iuc[], double cd[], Integer iord[], double dord[], Integer iwk[], Integer *ifail) |
|
C++ Header Interface
#include <nag.h> extern "C" {
void |
g03ecf_ (const Integer &method, const Integer &n, double d[], Integer ilc[], Integer iuc[], double cd[], Integer iord[], double dord[], Integer iwk[], Integer &ifail) |
}
|
The routine may be called by the names g03ecf or nagf_mv_cluster_hier.
3
Description
Given a distance or dissimilarity matrix for
objects (see
g03eaf), cluster analysis aims to group the
objects into a number of more or less homogeneous groups or clusters. With agglomerative clustering methods, a hierarchical tree is produced by starting with
clusters, each with a single object and then at each of
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
,
and
let
,
and
be the number of objects in each cluster and let
,
and
be the distances between the clusters. Let clusters
and
be merged to give cluster
, then the distance from cluster
to cluster
,
can be computed in the following ways.
-
1.Single link or nearest neighbour : .
-
2.Complete link or furthest neighbour : .
-
3.Group average : .
-
4.Centroid : .
-
5.Median : .
-
6.Minimum variance : .
If the clusters are numbered then, for convenience, if clusters and , , merge then the new cluster will be referred to as cluster . Information on the clustering history is given by the values of , and for each of the 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 . 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
Arguments
-
1:
– Integer
Input
-
On entry: indicates which clustering method is used.
- Single link.
- Complete link.
- Group average.
- Centroid.
- Median.
- Minimum variance.
Constraint:
, , , , or .
-
2:
– Integer
Input
-
On entry: , the number of objects.
Constraint:
.
-
3:
– Real (Kind=nag_wp) array
Input/Output
-
On entry: the strictly lower triangle of the distance matrix. must be stored packed by rows, i.e., , must contain .
On exit: is overwritten.
Constraint:
, for .
-
4:
– Integer array
Output
-
On exit:
contains the number,
, of the cluster merged with cluster
(see
iuc),
, at step
, for
.
-
5:
– Integer array
Output
-
On exit: contains the number, , of the cluster merged with cluster , , at step , for .
-
6:
– Real (Kind=nag_wp) array
Output
-
On exit: contains the distance , between clusters and , , merged at step , for .
-
7:
– Integer array
Output
-
On exit: the objects in dendrogram order.
-
8:
– Real (Kind=nag_wp) array
Output
-
On exit: the clustering distances corresponding to the order in
iord.
contains the distance at which cluster
and
merge, for
.
contains the maximum distance.
-
9:
– Integer array
Workspace
-
-
10:
– Integer
Input/Output
-
On entry:
ifail must be set to
,
or
to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of means that an error message is printed while a value of means that it is not.
If halting is not appropriate, the value
or
is recommended. If message printing is undesirable, then the value
is recommended. Otherwise, the value
is recommended.
When the value or is used it is essential to test the value of ifail on exit.
On exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6
Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
x04aaf).
Errors or warnings detected by the routine:
-
On entry, .
Constraint: , , , , or .
On entry, .
Constraint: .
-
On entry, at least one element of
d is negative.
-
A true dendrogram cannot be formed because the distances at which clusters have merged are not increasing for all steps, i.e., for some . This can occur for the median and centroid methods.
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 7 in the Introduction to the NAG Library FL Interface for further information.
Your licence key may have expired or may not have been installed correctly.
See
Section 8 in the Introduction to the NAG Library FL Interface for further information.
Dynamic memory allocation failed.
See
Section 9 in the Introduction to the NAG Library FL Interface for further information.
7
Accuracy
For 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 and , () or (), and , are equal then clusters and will be merged rather than clusters and . For single link clustering this choice will only affect the order of the objects in the dendrogram. However, for other methods the choice of rather than may affect the shape of the dendrogram. If either of the distances and is affected by rounding errors then their equality, and hence the dendrogram, may be affected.
8
Parallelism and Performance
g03ecf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the
X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the
Users' Note for your implementation for any additional implementation-specific information.
The dendrogram may be formed using
g03ehf. Groupings based on the clusters formed at a given distance can be computed using
g03ejf.
10
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.
10.1
Program Text
10.2
Program Data
10.3
Program Results