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_prin_comp (g03aa)

## Purpose

nag_mv_prin_comp (g03aa) performs a principal component analysis on a data matrix; both the principal component loadings and the principal component scores are returned.

## Syntax

[s, e, p, v, ifail] = g03aa(matrix, std, x, isx, s, nvar, 'n', n, 'm', m, 'wt', wt)
[s, e, p, v, ifail] = nag_mv_prin_comp(matrix, std, x, isx, s, nvar, 'n', n, 'm', m, 'wt', wt)
Note: the interface to this routine has changed since earlier releases of the toolbox:
 At Mark 24: weight was removed from the interface; wt was made optional At Mark 22: n was made optional

## Description

Let $X$ be an $n$ by $p$ data matrix of $n$ observations on $p$ variables ${x}_{1},{x}_{2},\dots ,{x}_{p}$ and let the $p$ by $p$ variance-covariance matrix of ${x}_{1},{x}_{2},\dots ,{x}_{p}$ be $S$. A vector ${a}_{1}$ of length $p$ is found such that:
 $a1TSa1 is maximized subject to a1Ta1=1.$
The variable ${z}_{1}=\sum _{i=1}^{p}{a}_{1i}{x}_{i}$ is known as the first principal component and gives the linear combination of the variables that gives the maximum variation. A second principal component, ${z}_{2}=\sum _{i=1}^{p}{a}_{2i}{x}_{i}$, is found such that:
 $a2TSa2 is maximized subject to ​a2Ta2=1and ​a2Ta1=0.$
This gives the linear combination of variables that is orthogonal to the first principal component that gives the maximum variation. Further principal components are derived in a similar way.
The vectors ${a}_{1},{a}_{2},\dots ,{a}_{p}$, are the eigenvectors of the matrix $S$ and associated with each eigenvector is the eigenvalue, ${\lambda }_{i}^{2}$. The value of ${\lambda }_{i}^{2}/\sum {\lambda }_{i}^{2}$ gives the proportion of variation explained by the $i$th principal component. Alternatively, the ${a}_{i}$'s can be considered as the right singular vectors in a singular value decomposition with singular values ${\lambda }_{i}$ of the data matrix centred about its mean and scaled by $1/\sqrt{\left(n-1\right)}$, ${X}_{s}$. This latter approach is used in nag_mv_prin_comp (g03aa), with
 $Xs=VΛP′$
where $\Lambda$ is a diagonal matrix with elements ${\lambda }_{i}$, $P$ is the $p$ by $p$ matrix with columns ${a}_{i}$ and $V$ is an $n$ by $p$ matrix with ${V}^{\prime }V=I$, which gives the principal component scores.
Principal component analysis is often used to reduce the dimension of a dataset, replacing a large number of correlated variables with a smaller number of orthogonal variables that still contain most of the information in the original dataset.
The choice of the number of dimensions required is usually based on the amount of variation accounted for by the leading principal components. If $k$ principal components are selected, then a test of the equality of the remaining $p-k$ eigenvalues is
 $n-2p+5/6 -∑i=k+1plogλi2+p-klog∑i=k+1pλi2/p-k$
which has, asymptotically, a ${\chi }^{2}$-distribution with $\frac{1}{2}\left(p-k-1\right)\left(p-k+2\right)$ degrees of freedom.
Equality of the remaining eigenvalues indicates that if any more principal components are to be considered then they all should be considered.
Instead of the variance-covariance matrix the correlation matrix, the sums of squares and cross-products matrix or a standardized sums of squares and cross-products matrix may be used. In the last case $S$ is replaced by ${\sigma }^{-\frac{1}{2}}S{\sigma }^{-\frac{1}{2}}$ for a diagonal matrix $\sigma$ with positive elements. If the correlation matrix is used, the ${\chi }^{2}$ approximation for the statistic given above is not valid.
The principal component scores, $F$, are the values of the principal component variables for the observations. These can be standardized so that the variance of these scores for each principal component is $1.0$ or equal to the corresponding eigenvalue.
Weights can be used with the analysis, in which case the matrix $X$ is first centred about the weighted means then each row is scaled by an amount $\sqrt{{w}_{i}}$, where ${w}_{i}$ is the weight for the $i$th observation.

## References

Chatfield C and Collins A J (1980) Introduction to Multivariate Analysis Chapman and Hall
Cooley W C and Lohnes P R (1971) Multivariate Data Analysis Wiley
Hammarling S (1985) The singular value decomposition in multivariate statistics SIGNUM Newsl. 20(3) 2–25
Kendall M G and Stuart A (1969) The Advanced Theory of Statistics (Volume 1) (3rd Edition) Griffin
Morrison D F (1967) Multivariate Statistical Methods McGraw–Hill

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{matrix}$ – string (length ≥ 1)
Indicates for which type of matrix the principal component analysis is to be carried out.
${\mathbf{matrix}}=\text{'C'}$
It is for the correlation matrix.
${\mathbf{matrix}}=\text{'S'}$
It is for a standardized matrix, with standardizations given by s.
${\mathbf{matrix}}=\text{'U'}$
It is for the sums of squares and cross-products matrix.
${\mathbf{matrix}}=\text{'V'}$
It is for the variance-covariance matrix.
Constraint: ${\mathbf{matrix}}=\text{'C'}$, $\text{'S'}$, $\text{'U'}$ or $\text{'V'}$.
2:     $\mathrm{std}$ – string (length ≥ 1)
Indicates if the principal component scores are to be standardized.
${\mathbf{std}}=\text{'S'}$
The principal component scores are standardized so that ${F}^{\prime }F=I$, i.e., $F={X}_{s}P{\Lambda }^{-1}=V$.
${\mathbf{std}}=\text{'U'}$
The principal component scores are unstandardized, i.e., $F={X}_{s}P=V\Lambda$.
${\mathbf{std}}=\text{'Z'}$
The principal component scores are standardized so that they have unit variance.
${\mathbf{std}}=\text{'E'}$
The principal component scores are standardized so that they have variance equal to the corresponding eigenvalue.
Constraint: ${\mathbf{std}}=\text{'E'}$, $\text{'S'}$, $\text{'U'}$ or $\text{'Z'}$.
3:     $\mathrm{x}\left(\mathit{ldx},{\mathbf{m}}\right)$ – double array
ldx, the first dimension of the array, must satisfy the constraint $\mathit{ldx}\ge {\mathbf{n}}$.
${\mathbf{x}}\left(\mathit{i},\mathit{j}\right)$ must contain the $\mathit{i}$th observation for the $\mathit{j}$th variable, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,m$.
4:     $\mathrm{isx}\left({\mathbf{m}}\right)$int64int32nag_int array
${\mathbf{isx}}\left(j\right)$ indicates whether or not the $j$th variable is to be included in the analysis.
If ${\mathbf{isx}}\left(\mathit{j}\right)>0$, the variable contained in the $\mathit{j}$th column of x is included in the principal component analysis, for $\mathit{j}=1,2,\dots ,m$.
Constraint: ${\mathbf{isx}}\left(j\right)>0$ for nvar values of $j$.
5:     $\mathrm{s}\left({\mathbf{m}}\right)$ – double array
The standardizations to be used, if any.
If ${\mathbf{matrix}}=\text{'S'}$, the first $m$ elements of s must contain the standardization coefficients, the diagonal elements of $\sigma$.
Constraint: if ${\mathbf{isx}}\left(j\right)>0$, ${\mathbf{s}}\left(\mathit{j}\right)>0.0$, for $\mathit{j}=1,2,\dots ,m$.
6:     $\mathrm{nvar}$int64int32nag_int scalar
$p$, the number of variables in the principal component analysis.
Constraint: $1\le {\mathbf{nvar}}\le \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{n}}-1,{\mathbf{m}}\right)$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the first dimension of the array x.
$n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 2$.
2:     $\mathrm{m}$int64int32nag_int scalar
Default: the dimension of the arrays isx, s and the second dimension of the array x. (An error is raised if these dimensions are not equal.)
$m$, the number of variables in the data matrix.
Constraint: ${\mathbf{m}}\ge 1$.
3:     $\mathrm{wt}\left(:\right)$ – double array
The dimension of the array wt must be at least ${\mathbf{n}}$ if $\mathit{weight}=\text{'W'}$, and at least $1$ otherwise
If $\mathit{weight}=\text{'W'}$, the first $n$ elements of wt must contain the weights to be used in the principal component analysis.
If ${\mathbf{wt}}\left(i\right)=0.0$, the $i$th observation is not included in the analysis. The effective number of observations is the sum of the weights.
If $\mathit{weight}=\text{'U'}$, wt is not referenced and the effective number of observations is $n$.
Constraints:
• ${\mathbf{wt}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,n$;
• the sum of weights $\text{}\ge {\mathbf{nvar}}+1$.

### Output Parameters

1:     $\mathrm{s}\left({\mathbf{m}}\right)$ – double array
If ${\mathbf{matrix}}=\text{'S'}$, s is unchanged on exit.
If ${\mathbf{matrix}}=\text{'C'}$, s contains the variances of the selected variables. ${\mathbf{s}}\left(j\right)$ contains the variance of the variable in the $j$th column of x if ${\mathbf{isx}}\left(j\right)>0$.
If ${\mathbf{matrix}}=\text{'U'}$ or $\text{'V'}$, s is not referenced.
2:     $\mathrm{e}\left(\mathit{lde},6\right)$ – double array
The statistics of the principal component analysis.
${\mathbf{e}}\left(i,1\right)$
The eigenvalues associated with the $\mathit{i}$th principal component, ${\lambda }_{\mathit{i}}^{2}$, for $\mathit{i}=1,2,\dots ,p$.
${\mathbf{e}}\left(i,2\right)$
The proportion of variation explained by the $\mathit{i}$th principal component, for $\mathit{i}=1,2,\dots ,p$.
${\mathbf{e}}\left(\mathit{i},3\right)$
The cumulative proportion of variation explained by the first $\mathit{i}$th principal components, for $\mathit{i}=1,2,\dots ,p$.
${\mathbf{e}}\left(\mathit{i},4\right)$
The ${\chi }^{2}$ statistics, for $i=1,2,\dots ,p$.
${\mathbf{e}}\left(i,5\right)$
The degrees of freedom for the ${\chi }^{2}$ statistics, for $i=1,2,\dots ,p$.
If ${\mathbf{matrix}}\ne \text{'C'}$, ${\mathbf{e}}\left(\mathit{i},6\right)$ contains significance level for the ${\chi }^{2}$ statistic, for $\mathit{i}=1,2,\dots ,p$.
If ${\mathbf{matrix}}=\text{'C'}$, ${\mathbf{e}}\left(i,6\right)$ is returned as zero.
3:     $\mathrm{p}\left(\mathit{ldp},{\mathbf{nvar}}\right)$ – double array
The first nvar columns of p contain the principal component loadings, ${a}_{i}$. The $j$th column of p contains the nvar coefficients for the $j$th principal component.
4:     $\mathrm{v}\left(\mathit{ldv},{\mathbf{nvar}}\right)$ – double array
The first nvar columns of v contain the principal component scores. The $j$th column of v contains the n scores for the $j$th principal component.
If $\mathit{weight}=\text{'W'}$, any rows for which ${\mathbf{wt}}\left(i\right)$ is zero will be set to zero.
5:     $\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:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

${\mathbf{ifail}}=1$
 On entry, ${\mathbf{m}}<1$, or ${\mathbf{n}}<2$, or ${\mathbf{nvar}}<1$, or ${\mathbf{nvar}}>{\mathbf{m}}$, or ${\mathbf{nvar}}\ge {\mathbf{n}}$, or $\mathit{ldx}<{\mathbf{n}}$, or $\mathit{ldv}<{\mathbf{n}}$, or $\mathit{ldp}<{\mathbf{nvar}}$, or $\mathit{lde}<{\mathbf{nvar}}$, or ${\mathbf{matrix}}\ne \text{'C'}$, $\text{'S'}$, $\text{'U'}$ or $\text{'V'}$, or ${\mathbf{std}}\ne \text{'S'}$, $\text{'U'}$, $\text{'Z'}$ or $\text{'E'}$, or $\mathit{weight}\ne \text{'U'}$ or $\text{'W'}$.
${\mathbf{ifail}}=2$
 On entry, $\mathit{weight}=\text{'W'}$ and a value of ${\mathbf{wt}}<0.0$.
${\mathbf{ifail}}=3$
 On entry, there are not nvar values of ${\mathbf{isx}}>0$, or $\mathit{weight}=\text{'W'}$ and the effective number of observations is less than ${\mathbf{nvar}}+1$.
${\mathbf{ifail}}=4$
 On entry, ${\mathbf{s}}\left(j\right)\le 0.0$ for some $j=1,2,\dots ,m$, when ${\mathbf{matrix}}=\text{'S'}$ and ${\mathbf{isx}}\left(j\right)>0$.
${\mathbf{ifail}}=5$
The singular value decomposition has failed to converge. This is an unlikely error exit.
W  ${\mathbf{ifail}}=6$
All eigenvalues/singular values are zero. This will be caused by all the variables being constant.
${\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

As nag_mv_prin_comp (g03aa) uses a singular value decomposition of the data matrix, it will be less affected by ill-conditioned problems than traditional methods using the eigenvalue decomposition of the variance-covariance matrix.

None.

## Example

A dataset is taken from Cooley and Lohnes (1971), it consists of ten observations on three variables. The unweighted principal components based on the variance-covariance matrix are computed and the principal component scores requested. The principal component scores are standardized so that they have variance equal to the corresponding eigenvalue.
```function g03aa_example

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

x    = [7, 4, 3;
4, 1, 8;
6, 3, 5;
8, 6, 1;
8, 5, 7;
7, 2, 9;
5, 3, 3;
9, 5, 8;
7, 4, 5;
8, 2, 2];
n    = size(x,2);

matrix = 'V';
std  = 'E';
isx  = ones(n,1,'int64');
s    = zeros(n,1);
nvar = int64(n);

[s, e, p, v, ifail] = g03aa( ...
matrix, std, x, isx, s, nvar);

fprintf('Eigenvalues  Percentage  Cumulative     Chisq      DF     Sig\n');
fprintf('              variation   variation\n\n');
fprintf('%11.4f%12.4f%12.4f%10.4f%8.1f%8.4f\n',e');
fprintf('\n');

matrix = 'General';
diag   = ' ';

[ifail] = x04ca( ...
matrix, diag, p, mtitle);

fprintf('\n');
mtitle = 'Principal component scores';
[ifail] = x04ca( ...
matrix, diag, v, mtitle);

fig1 = figure;
subplot(1,2,1);
xlabel('PC 1');
ylabel('PC 2');
title({'Observation numbers', 'for PC 1 and PC 2'});
axis([-5 5 -3 4]);
for j = 1:size(x,1)
ch = sprintf('%d',j);
text(v(j,1),v(j,2),ch);
end
subplot(1,2,2);
bar(e(:,2));
ax = gca;
ax.XTickLabel = {'PC 1','PC 2','PC 3'};
xlabel('PC 1');
ylabel('Percentage variation');
title('Scree plot');

```
```g03aa example results

Eigenvalues  Percentage  Cumulative     Chisq      DF     Sig
variation   variation

8.2739      0.6515      0.6515    8.6127     5.0  0.1255
3.6761      0.2895      0.9410    4.1183     2.0  0.1276
0.7499      0.0590      1.0000    0.0000     0.0  0.0000

1       2       3
1  -0.1376  0.6990 -0.7017
2  -0.2505  0.6609  0.7075
3   0.9583  0.2731  0.0842

Principal component scores
1          2          3
1     -2.1514    -0.1731     0.1068
2      3.8042    -2.8875     0.5104
3      0.1532    -0.9869     0.2694
4     -4.7065     1.3015     0.6517
5      1.2938     2.2791     0.4492
6      4.0993     0.1436    -0.8031
7     -1.6258    -2.2321     0.8028
8      2.1145     3.2512    -0.1684
9     -0.2348     0.3730     0.2751
10     -2.7464    -1.0689    -2.0940
```