G03AAF (PDF version)
G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G03AAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

 Contents

    1  Purpose
    7  Accuracy

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 x1,x2,,xp and let the p by p variance-covariance matrix of x1,x2,,xp be S. A vector a1 of length p is found such that:
a1TSa1  is maximized subject to  a1Ta1=1.  
The variable z1=i=1pa1ixi is known as the first principal component and gives the linear combination of the variables that gives the maximum variation. A second principal component, z2=i=1pa2ixi, 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 a1,a2,,ap, are the eigenvectors of the matrix S and associated with each eigenvector is the eigenvalue, λi2. The value of λi2/λi2 gives the proportion of variation explained by the ith principal component. Alternatively, the ai's can be considered as the right singular vectors in a singular value decomposition with singular values λi of the data matrix centred about its mean and scaled by 1/n-1, Xs. This latter approach is used in G03AAF, with
Xs=VΛP  
where Λ is a diagonal matrix with elements λi, P is the p by p matrix with columns ai and V is an n by p matrix with VV=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-klogi=k+1pλi2/p-k  
which has, asymptotically, a χ2-distribution with 12p-k-1p-k+2 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 σ-12Sσ-12 for a diagonal matrix σ with positive elements. If the correlation matrix is used, the χ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 wi, where wi is the weight for the ith 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.
MATRIX='C'
It is for the correlation matrix.
MATRIX='S'
It is for a standardized matrix, with standardizations given by S.
MATRIX='U'
It is for the sums of squares and cross-products matrix.
MATRIX='V'
It is for the variance-covariance matrix.
Constraint: MATRIX='C', 'S', 'U' or 'V'.
2:     STD – CHARACTER(1)Input
On entry: indicates if the principal component scores are to be standardized.
STD='S'
The principal component scores are standardized so that FF=I, i.e., F=XsPΛ-1=V.
STD='U'
The principal component scores are unstandardized, i.e., F=XsP=VΛ.
STD='Z'
The principal component scores are standardized so that they have unit variance.
STD='E'
The principal component scores are standardized so that they have variance equal to the corresponding eigenvalue.
Constraint: STD='E', 'S', 'U' or 'Z'.
3:     WEIGHT – CHARACTER(1)Input
On entry: indicates if weights are to be used.
WEIGHT='U'
No weights are used.
WEIGHT='W'
Weights are used and must be supplied in WT.
Constraint: WEIGHT='U' or 'W'.
4:     N – INTEGERInput
On entry: n, the number of observations.
Constraint: N2.
5:     M – INTEGERInput
On entry: m, the number of variables in the data matrix.
Constraint: M1.
6:     XLDXM – REAL (KIND=nag_wp) arrayInput
On entry: Xij must contain the ith observation for the jth variable, for i=1,2,,n and j=1,2,,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: LDXN.
8:     ISXM – INTEGER arrayInput
On entry: ISXj indicates whether or not the jth variable is to be included in the analysis.
If ISXj>0, the variable contained in the jth column of X is included in the principal component analysis, for j=1,2,,m.
Constraint: ISXj>0 for NVAR values of j.
9:     SM – REAL (KIND=nag_wp) arrayInput/Output
On entry: the standardizations to be used, if any.
If MATRIX='S', the first m elements of S must contain the standardization coefficients, the diagonal elements of σ.
Constraint: if ISXj>0, Sj>0.0, for j=1,2,,m.
On exit: if MATRIX='S', S is unchanged on exit.
If MATRIX='C', S contains the variances of the selected variables. Sj contains the variance of the variable in the jth column of X if ISXj>0.
If MATRIX='U' or 'V', S is not referenced.
10:   WT* – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least N if WEIGHT='W', and at least 1 otherwise.
On entry: if WEIGHT='W', the first n elements of WT must contain the weights to be used in the principal component analysis.
If WTi=0.0, the ith observation is not included in the analysis. The effective number of observations is the sum of the weights.
If WEIGHT='U', WT is not referenced and the effective number of observations is n.
Constraints:
  • WTi0.0, for i=1,2,,n;
  • the sum of weights NVAR+1.
11:   NVAR – INTEGERInput
On entry: p, the number of variables in the principal component analysis.
Constraint: 1NVARminN-1,M.
12:   ELDE6 – REAL (KIND=nag_wp) arrayOutput
On exit: the statistics of the principal component analysis.
Ei1
The eigenvalues associated with the ith principal component, λi2, for i=1,2,,p.
Ei2
The proportion of variation explained by the ith principal component, for i=1,2,,p.
Ei3
The cumulative proportion of variation explained by the first ith principal components, for i=1,2,,p.
Ei4
The χ2 statistics, for i=1,2,,p.
Ei5
The degrees of freedom for the χ2 statistics, for i=1,2,,p.
If MATRIX'C', Ei6 contains significance level for the χ2 statistic, for i=1,2,,p.
If MATRIX='C', Ei6 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: LDENVAR.
14:   PLDPNVAR – REAL (KIND=nag_wp) arrayOutput
On exit: the first NVAR columns of P contain the principal component loadings, ai. The jth column of P contains the NVAR coefficients for the jth 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: LDPNVAR.
16:   VLDVNVAR – REAL (KIND=nag_wp) arrayOutput
On exit: the first NVAR columns of V contain the principal component scores. The jth column of V contains the N scores for the jth principal component.
If WEIGHT='W', any rows for which WTi 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: LDVN.
18:   WK1 – 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​ 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​ 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 -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).

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:
IFAIL=1
On entry,M<1,
orN<2,
orNVAR<1,
orNVAR>M,
orNVARN,
orLDX<N,
orLDV<N,
orLDP<NVAR,
orLDE<NVAR,
orMATRIX'C', 'S', 'U' or 'V',
orSTD'S', 'U', 'Z' or 'E',
orWEIGHT'U' or 'W'.
IFAIL=2
On entry,WEIGHT='W' and a value of WT<0.0.
IFAIL=3
On entry,there are not NVAR values of ISX>0,
orWEIGHT='W' and the effective number of observations is less than NVAR+1.
IFAIL=4
On entry,Sj0.0 for some j=1,2,,m, when MATRIX='S' and ISXj>0.
IFAIL=5
The singular value decomposition has failed to converge. This is an unlikely error exit.
IFAIL=6
All eigenvalues/singular values are zero. This will be caused by all the variables being constant.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

7  Accuracy

As G03AAF 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.

8  Parallelism and Performance

G03AAF is not threaded by NAG in any implementation.
G03AAF 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.

9  Further Comments

None.

10  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.

10.1  Program Text

Program Text (g03aafe.f90)

10.2  Program Data

Program Data (g03aafe.d)

10.3  Program Results

Program Results (g03aafe.r)

GnuplotProduced by GNUPLOT 4.6 patchlevel 3 Example Program Principal Component Analysis −3 −2 −1 0 1 2 3 4 −5 −4 −3 −2 −1 0 1 2 3 4 5 Second Principal Component (PC 2) First Principal Component (PC 1) Observation ID for the First and Second Principal Components gnuplot_plot_1a 1 2 3 4 5 6 7 8 9 10 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 PC 1 PC 2 PC 3 Percentage Variation Scree Plot gnuplot_plot_1b

G03AAF (PDF version)
G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015