NAG Library Routine Document
G03AAF
1 Purpose
G03AAF performs a principal component analysis on a data matrix; both the principal component loadings and the principal component scores are returned.
2 Specification
SUBROUTINE G03AAF ( 
MATRIX, STD, WEIGHT, N, M, X, LDX, ISX, S, WT, NVAR, E, LDE, P, LDP, V, LDV, WK, IFAIL) 
INTEGER 
N, M, LDX, ISX(M), NVAR, LDE, LDP, LDV, IFAIL 
REAL (KIND=nag_wp) 
X(LDX,M), S(M), WT(*), E(LDE,6), P(LDP,NVAR), V(LDV,NVAR), WK(1) 
CHARACTER(1) 
MATRIX, STD, WEIGHT 

3 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$ variancecovariance matrix of
${x}_{1},{x}_{2},\dots ,{x}_{p}$ be
$S$. A vector
${a}_{1}$ of length
$p$ is found such that:
The variable
${z}_{1}={\displaystyle \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}={\displaystyle \sum _{i=1}^{p}}{a}_{2i}{x}_{i}$, is found such that:
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(n1\right)}$,
${X}_{s}$. This latter approach is used in G03AAF, with
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
$pk$ eigenvalues is
which has, asymptotically, a
${\chi}^{2}$distribution with
$\frac{1}{2}\left(pk1\right)\left(pk+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 variancecovariance matrix the correlation matrix, the sums of squares and crossproducts matrix or a standardized sums of squares and crossproducts 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.
4 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
5 Parameters
 1: MATRIX – CHARACTER(1)Input
On entry: 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 crossproducts matrix.
 ${\mathbf{MATRIX}}=\text{'V'}$
 It is for the variancecovariance matrix.
Constraint:
${\mathbf{MATRIX}}=\text{'C'}$, $\text{'S'}$, $\text{'U'}$ or $\text{'V'}$.
 2: STD – CHARACTER(1)Input
On entry: 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: WEIGHT – CHARACTER(1)Input
On entry: indicates if weights are to be used.
 ${\mathbf{WEIGHT}}=\text{'U'}$
 No weights are used.
 ${\mathbf{WEIGHT}}=\text{'W'}$
 Weights are used and must be supplied in WT.
Constraint:
${\mathbf{WEIGHT}}=\text{'U'}$ or $\text{'W'}$.
 4: N – INTEGERInput
On entry: $n$, the number of observations.
Constraint:
${\mathbf{N}}\ge 2$.
 5: M – INTEGERInput
On entry: $m$, the number of variables in the data matrix.
Constraint:
${\mathbf{M}}\ge 1$.
 6: X(LDX,M) – REAL (KIND=nag_wp) arrayInput
On entry: ${\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$.
 7: LDX – INTEGERInput
On entry: the first dimension of the array
X as declared in the (sub)program from which G03AAF is called.
Constraint:
${\mathbf{LDX}}\ge {\mathbf{N}}$.
 8: ISX(M) – INTEGER arrayInput
On entry:
${\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$.
 9: S(M) – REAL (KIND=nag_wp) arrayInput/Output
On entry: 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$.
On exit: 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.
 10: WT($*$) – REAL (KIND=nag_wp) arrayInput

Note: the dimension of the array
WT
must be at least
${\mathbf{N}}$ if
${\mathbf{WEIGHT}}=\text{'W'}$, and at least
$1$ otherwise.
On entry: if
${\mathbf{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
${\mathbf{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$.
 11: NVAR – INTEGERInput
On entry: $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)$.
 12: E(LDE,$6$) – REAL (KIND=nag_wp) arrayOutput
On exit: 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.
 13: LDE – INTEGERInput
On entry: the first dimension of the array
E as declared in the (sub)program from which G03AAF is called.
Constraint:
${\mathbf{LDE}}\ge {\mathbf{NVAR}}$.
 14: P(LDP,NVAR) – REAL (KIND=nag_wp) arrayOutput
On exit: 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.
 15: LDP – INTEGERInput
On entry: the first dimension of the array
P as declared in the (sub)program from which G03AAF is called.
Constraint:
${\mathbf{LDP}}\ge {\mathbf{NVAR}}$.
 16: V(LDV,NVAR) – REAL (KIND=nag_wp) arrayOutput
On exit: 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 ${\mathbf{WEIGHT}}=\text{'W'}$, any rows for which ${\mathbf{WT}}\left(i\right)$ is zero will be set to zero.
 17: LDV – INTEGERInput
On entry: the first dimension of the array
V as declared in the (sub)program from which G03AAF is called.
Constraint:
${\mathbf{LDV}}\ge {\mathbf{N}}$.
 18: WK($1$) – REAL (KIND=nag_wp) arrayInput
This parameter is no longer accessed by G03AAF. Workspace is provided internally by dynamic allocation instead.
 19: IFAIL – INTEGERInput/Output

On entry:
IFAIL must be set to
$0$,
$1\text{ 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\text{ 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 $\mathbf{1}\text{ or}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit:
${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see
Section 6).
6 Error Indicators and Warnings
If on entry
${\mathbf{IFAIL}}={\mathbf{0}}$ or
${{\mathbf{1}}}$, explanatory error messages are output on the current error message unit (as defined by
X04AAF).
Errors or warnings detected by the routine:
 ${\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  ${\mathbf{LDX}}<{\mathbf{N}}$, 
or  ${\mathbf{LDV}}<{\mathbf{N}}$, 
or  ${\mathbf{LDP}}<{\mathbf{NVAR}}$, 
or  ${\mathbf{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  ${\mathbf{WEIGHT}}\ne \text{'U'}$ or $\text{'W'}$. 
 ${\mathbf{IFAIL}}=2$
On entry,  ${\mathbf{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  ${\mathbf{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.
 ${\mathbf{IFAIL}}=6$
All eigenvalues/singular values are zero. This will be caused by all the variables being constant.
7 Accuracy
As G03AAF uses a singular value decomposition of the data matrix, it will be less affected by illconditioned problems than traditional methods using the eigenvalue decomposition of the variancecovariance matrix.
None.
9 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 variancecovariance 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.
9.1 Program Text
Program Text (g03aafe.f90)
9.2 Program Data
Program Data (g03aafe.d)
9.3 Program Results
Program Results (g03aafe.r)