Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_mv_multidimscal_metric (g03fa)

## Purpose

nag_mv_multidimscal_metric (g03fa) performs a principal coordinate analysis also known as classical metric scaling.

## Syntax

[x, eval, ifail] = g03fa(roots, n, d, ndim)
[x, eval, ifail] = nag_mv_multidimscal_metric(roots, n, d, ndim)

## Description

For a set of $n$ objects a distance matrix $D$ can be calculated such that ${d}_{ij}$ is a measure of how ‘far apart’ are objects $i$ and $j$ (see nag_mv_distance_mat (g03ea) 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, $k\ll \left(n-1\right)$, 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\left({d}_{ij}^{2}-{d}_{i.}^{2}-{d}_{.j}^{2}+{d}_{..}^{2}\right)$ with ${d}_{i.}^{2}$ denoting the average of ${d}_{ij}^{2}$ 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, ${\lambda }_{i}$, of the matrix $E$ are all positive, $\sum _{i=1}^{k}{\lambda }_{i}/\sum _{i=1}^{n-1}{\lambda }_{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 ${d}_{ij}$ satisfies the inequality: ${d}_{ij}\le {d}_{ik}+{d}_{jk}$ 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. nag_mv_multidimscal_metric (g03fa) provides the option for all eigenvalues to be computed so that the smallest eigenvalues can be checked.

## References

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{roots}$ – string (length ≥ 1)
Indicates if all the eigenvalues are to be computed or just the ndim largest.
${\mathbf{roots}}=\text{'A'}$
All the eigenvalues are computed.
${\mathbf{roots}}=\text{'L'}$
Only the largest ndim eigenvalues are computed.
Constraint: ${\mathbf{roots}}=\text{'A'}$ or $\text{'L'}$.
2:     $\mathrm{n}$int64int32nag_int scalar
$n$, the number of objects in the distance matrix.
Constraint: ${\mathbf{n}}>{\mathbf{ndim}}$.
3:     $\mathrm{d}\left({\mathbf{n}}×\left({\mathbf{n}}-1\right)/2\right)$ – double array
The lower triangle of the distance matrix $D$ stored packed by rows. That is ${\mathbf{d}}\left(\left(i-1\right)×\left(i-2\right)/2+j\right)$ must contain ${d}_{ij}$ for $i=2,3,\dots ,n\text{;}j=1,2,\dots ,i-1$.
Constraint: ${\mathbf{d}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,n\left(n-1\right)/2$.
4:     $\mathrm{ndim}$int64int32nag_int scalar
$k$, the number of dimensions used to represent the data.
Constraint: ${\mathbf{ndim}}\ge 1$.

None.

### Output Parameters

1:     $\mathrm{x}\left(\mathit{ldx},{\mathbf{ndim}}\right)$ – double array
The $i$th row contains $k$ coordinates for the $i$th point, $i=1,2,\dots ,n$.
2:     $\mathrm{eval}\left({\mathbf{n}}\right)$ – double array
If ${\mathbf{roots}}=\text{'A'}$, eval contains the $n$ scaled eigenvalues of the matrix $E$.
If ${\mathbf{roots}}=\text{'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$).
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{ndim}}<1$, or ${\mathbf{n}}<{\mathbf{ndim}}$, or ${\mathbf{roots}}\ne \text{'A'}$ or $\text{'L'}$, or $\mathit{ldx}<{\mathbf{n}}$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{d}}\left(i\right)<0.0$ for some $i$, $i=1,2,\dots ,n\left(n-1\right)/2$, or all elements of ${\mathbf{d}}=0.0$.
${\mathbf{ifail}}=3$
There are less than ndim eigenvalues greater than zero. Try a smaller number of dimensions (ndim) or use non-metric scaling (nag_mv_multidimscal_ordinal (g03fc)).
${\mathbf{ifail}}=4$
The computation of the eigenvalues or eigenvectors has failed. Seek expert help.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

nag_mv_multidimscal_metric (g03fa) uses nag_lapack_dsterf (f08jf) or nag_lapack_dstebz (f08jj) to compute the eigenvalues and nag_lapack_dstein (f08jk) to compute the eigenvectors. These functions should be consulted for a discussion of the accuracy of the computations involved.

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

## Example

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

fprintf('g03fa example results\n\n');

roots = 'l';
n = int64(14);
% Distance matrix (lower part)
d = [0.099 ...
0.033 0.022 ...
0.183 0.114 0.042 ...
0.148 0.224 0.059 0.068 ...
0.198 0.039 0.053 0.085 0.051 ...
0.462 0.266 0.322 0.435 0.268 0.025 ...
0.628 0.442 0.444 0.406 0.240 0.129 0.014 ...
0.113 0.070 0.046 0.047 0.034 0.002 0.106 0.129 ...
0.173 0.119 0.162 0.331 0.177 0.039 0.089 0.237 0.071 ...
0.434 0.419 0.339 0.505 0.469 0.390 0.315 0.349 0.151 0.430 ...
0.762 0.633 0.781 0.700 0.758 0.625 0.469 0.618 0.440 0.538 0.607 ...
0.530 0.389 0.482 0.579 0.597 0.498 0.374 0.562 0.247 0.383 0.387 ...
0.084 ...
0.586 0.435 0.550 0.530 0.552 0.509 0.369 0.471 0.234 0.346 0.456 ...
0.090 0.038];
ndim = int64(2);

[x, eval, ifail] = g03fa( ...
roots, n, d, ndim);

% Display results
disp(' Scaled Eigenvalues');
disp(eval(1:ndim)');

mtitle = 'Co-ordinates';
matrix = 'General';
diag = ' ';
[ifail] = x04ca( ...
matrix,diag,x,mtitle);

fig1 = figure;
hold on;
xlabel('PC 1');
ylabel('PC 2');
title({'Observation numbers', 'for PC 1 and PC 2'});
axis([-0.6 0.3 -0.4 0.3]);
for j = 1:size(x,1)
ch = sprintf('%d',j);
text(x(j,1),x(j,2),ch);
end
plot([-0.6 0.3], [0 0], ':');
plot([0 0], [-0.4 0.3], ':');
hold off;

```
```g03fa example results

Scaled Eigenvalues
0.7871    0.2808

Co-ordinates
1       2
1   0.2408  0.2337
2   0.1137  0.1168
3   0.2394  0.0760
4   0.2129  0.0605
5   0.2495 -0.0693
6   0.1487 -0.0778
7  -0.0514 -0.1623
8   0.0115 -0.3446
9  -0.0039  0.0059
10   0.0386 -0.0089
11  -0.0421 -0.0566
12  -0.5158  0.0291
13  -0.3180  0.1501
14  -0.3238  0.0475
``` 