NAG Library Routine Document

g03faf  (multidimscal_metric)


    1  Purpose
    7  Accuracy


g03faf performs a principal coordinate analysis also known as classical metric scaling.


Fortran Interface
Subroutine g03faf ( roots, n, d, ndim, x, ldx, eval, wk, iwk, ifail)
Integer, Intent (In):: n, ndim, ldx
Integer, Intent (Inout):: ifail
Integer, Intent (Out):: iwk(5*n)
Real (Kind=nag_wp), Intent (In):: d(n*(n-1)/2)
Real (Kind=nag_wp), Intent (Inout):: x(ldx,ndim)
Real (Kind=nag_wp), Intent (Out):: eval(n), wk(n*(n+17)/2-1)
Character (1), Intent (In):: roots
C Header Interface
#include nagmk26.h
void  g03faf_ ( const char *roots, const Integer *n, const double d[], const Integer *ndim, double x[], const Integer *ldx, double eval[], double wk[], Integer iwk[], Integer *ifail, const Charlen length_roots)


For a set of n objects a distance matrix D can be calculated such that dij is a measure of how ‘far apart’ are objects i and j (see g03eaf for example). Principal coordinate analysis or metric scaling starts with a distance matrix and finds points X in Euclidean space such that those points have the same distance matrix. The aim is to find a small number of dimensions, kn-1, that provide an adequate representation of the distances.
The principal coordinates of the points are computed from the eigenvectors of the matrix E where e ij = -1 / 2 d ij 2 - d i. 2 - d .j 2 + d .. 2  with di.2 denoting the average of dij2 over the suffix j, etc.. The eigenvectors are then scaled by multiplying by the square root of the value of the corresponding eigenvalue.
Provided that the ordered eigenvalues, λi, of the matrix E are all positive, i=1 k λi / i=1 n-1 λi  shows how well the data is represented in k dimensions. The eigenvalues will be non-negative if E is positive semidefinite. This will be true provided dij satisfies the inequality: dij dik + djk  for all i,j,k. If this is not the case the size of the negative eigenvalue reflects the amount of deviation from this condition and the results should be treated cautiously in the presence of large negative eigenvalues. See Krzanowski (1990) for further discussion. g03faf provides the option for all eigenvalues to be computed so that the smallest eigenvalues can be checked.


Chatfield C and Collins A J (1980) Introduction to Multivariate Analysis Chapman and Hall
Gower J C (1966) Some distance properties of latent root and vector methods used in multivariate analysis Biometrika 53 325–338
Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press


1:     roots – Character(1)Input
On entry: indicates if all the eigenvalues are to be computed or just the ndim largest.
All the eigenvalues are computed.
Only the largest ndim eigenvalues are computed.
Constraint: roots='A' or 'L'.
2:     n – IntegerInput
On entry: n, the number of objects in the distance matrix.
Constraint: n>ndim.
3:     dn×n-1/2 – Real (Kind=nag_wp) arrayInput
On entry: the lower triangle of the distance matrix D stored packed by rows. That is d i-1 × i-2 /2+j  must contain dij for i=2,3,,n;j=1,2,,i-1.
Constraint: di0.0, for i=1,2,,nn-1/2.
4:     ndim – IntegerInput
On entry: k, the number of dimensions used to represent the data.
Constraint: ndim1.
5:     xldxndim – Real (Kind=nag_wp) arrayOutput
On exit: the ith row contains k coordinates for the ith point, i=1,2,,n.
6:     ldx – IntegerInput
On entry: the first dimension of the array x as declared in the (sub)program from which g03faf is called.
Constraint: ldxn.
7:     evaln – Real (Kind=nag_wp) arrayOutput
On exit: if roots='A', eval contains the n scaled eigenvalues of the matrix E.
If roots='L', eval contains the largest k scaled eigenvalues of the matrix E.
In both cases the eigenvalues are divided by the sum of the eigenvalues (that is, the trace of E).
8:     wkn×n+17/2-1 – Real (Kind=nag_wp) arrayWorkspace
9:     iwk5×n – Integer arrayWorkspace
10:   ifail – IntegerInput/Output
On entry: ifail must be set to 0, -1​ or ​1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation 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 argument, 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).

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:
On entry,ndim<1,
orroots'A' or 'L',
On entry,di<0.0 for some i, i=1,2,,nn-1/2,
orall elements of d=0.0.
There are less than ndim eigenvalues greater than zero. Try a smaller number of dimensions (ndim) or use non-metric scaling (g03fcf).
The computation of the eigenvalues or eigenvectors has failed. Seek expert help.
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.


g03faf uses f08jff (dsterf) or f08jjf (dstebz) to compute the eigenvalues and f08jkf (dstein) to compute the eigenvectors. These routines should be consulted for a discussion of the accuracy of the computations involved.

Parallelism and Performance

g03faf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g03faf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
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.

Further Comments

Alternative, non-metric, methods of scaling are provided by g03fcf.
The relationship between principal coordinates and principal components, see g03fcf, is discussed in Krzanowski (1990) and Gower (1966).


The data, given by Krzanowski (1990), are dissimilarities between water vole populations in Europe. The first two principal coordinates are computed.

Program Text

Program Text (g03fafe.f90)

Program Data

Program Data (g03fafe.d)

Program Results

Program Results (g03fafe.r)

GnuplotProduced by GNUPLOT 4.6 patchlevel 3 −0.4 −0.3 −0.2 −0.1 0 0.1 0.2 0.3 −0.6 −0.5 −0.4 −0.3 −0.2 −0.1 0 0.1 0.2 0.3 Second Principal Coordinate First Principal Coordinate Example Program Plot of Observation Number by the First Two Principal Coordinates gnuplot_plot_1 1 2 3 4 5 6 7 8 9 10 11 12 13 14
© The Numerical Algorithms Group Ltd, Oxford, UK. 2017