nagdmc pca

NAG DMC

Principal Component Analysis: nagdmc pca
Purposenagdmc pca computes a principal component analysis.

Declaration#include <nagdmc.h>void nagdmc pca(long rec1, long nvar, long nrec, long dblk, double data[],void (*dfun)(long, long, double [], char *, int *), char *comm,long chunksize, long iwts, int pcatype, double xbar[],double s[], double loadings[], double results[], int *info);

Parameters1:

rec1 – longInput

On entry: the index in the data of the first data record used in the analysis.
Constraint: rec1 ≥ 0.
2:

nvar – long

Input

On entry: the number of variables in the data.
Constraint: nvar > 1.
3:

nrec – long

Input

On entry: the number of consecutive records, beginning at rec1, used in the analysis.
Constraint: nrec > 1.
4:

dblk – long

Input

On entry: the total number of records in the data block.
Constraint: dblk ≥ rec1 + nrec.
5:

data[dblk ∗ nvar] – double

Input

On entry: the data values for the jth variable (for j = 0, 1, . . . , nvar−1) are stored in data[i∗nvar+j],
for i = 0, 1, . . . , dblk − 1. When the data function is used, data is not referenced.
6:

dfun – function supplied by user

External Procedure

On entry: the pointer to a data function supplied by the user.
Constraint: if dfun is a valid pointer, data must be 0.
The specification of dfun is:void dfun(long irec, long chunksize, double x[], char *comm, int *ierr)1:

irec – long
On entry: the index in the data of the first record returned.

Input

2:chunksize – long
On entry: the number of consecutive records returned.

Input

3:

x[chunksize∗nvar] – double
OutputOn exit: data values for the jth variable (for j = 0, 1, . . . , nvar − 1) must be returned
in x[i ∗ nvar + j], for i = 0, 1, . . . , chunksize − 1.

4:comm – char *Input
On entry: a communication parameter allowing additional information to be passed
to dfun. This parameter is passed ‘as is’ through the calling function.

nagdmc pca.1

nagdmc pca

NAG DMC

5:ierr – int *Output
On exit: if the value pointed to by ierr on return is greater than 100, the NAG DMC
function will terminate immediately and info will point to this value.

7:

comm – char *Input
On entry: a communication parameter allowing additional information to be passed to dfun. Thisparameter is passed ‘as is’ through the calling function.

8:

chunksize – long
Input
On entry: if the data function is used, the function inputs no more than chunksize data records ata time; otherwise chunksize is not referenced.
Constraint: if dfun 6= 0, chunksize ≥ 1.

9:

iwts – long
Input
On entry: if iwts = −1, no weights are used; otherwise iwts is the index in data in which theweights are stored.
Constraints: −1 ≤ iwts < nvar; iwts 6= yvar; and if nxvar > 0, iwts 6= xvar[i], fori = 0, 1, . . . , nxvar − 1.

10:pcatype – int
Input
On entry: indicates the equivalent matrix for which the principal components are derived:
pcatype = 0 sum of squares and cross-products;
pcatype = 1 variance-covariance matrix;
pcatype = 2 correlation matrix;
pcatype = 3 user-supplied standardisation.
Constraint: pcatype ∈ {0, 1, 2, 3}.

11:xbar[nvar] – double
Input
On entry: if variable means are available, they should be supplied in xbar; otherwise xbar should
be set to 0 and nagdmc pca will compute the means internally using an additional pass throughthe data. Note that values corresponding to the column of weights, if any, will be ignored.

12:s[nvar] – double
Input
On entry: the vector of standard deviations or scaling factors. If pcatype = 2 and standard
deviations are available, they should be supplied in s; otherwise nagdmc pca will compute the means
and standard deviations internally using an additional pass through the data. If pcatype = 3, smust contain the user-supplied standardisations. If pcatype is zero or one, s is not referenced andcan be set to 0.
Constraints: if s is not 0 and is referenced, s[i] > 0.0, for i = 0, 1, . . . , nvar − 1.

13:loadings[nvar∗nvar] – double
OutputOn exit: loadings[i ∗ nvar + j] is the jth loading from the principal component analysis for the ithvariable, for i = 0, 1, . . . , nvar − 1; for j = 0, 1, . . . , nvar − 1.

14:

results[6∗nvar] – double
OutputOn exit: results[i ∗ 6 + j] is element j of the variance decomposition results for the ith variable,
where elements have the meaning:
Element 0: the eigenvalue.
Element 1: the proportion of variation explained by the component.
Element 2: the cumulative proportion of variation explained by the components.
Element 3: the χ2 statistic.
Element 4: the degrees of freedom.
Element 5: the significance.

15:

info – int *On exit: info gives information on the success of the function call:

Output0: the function successfully completed its task.
i; i = i = 1, 2, 3, 4, 6, 8, 9, 10, 12: the specification of the ith formal parameter was incorrect.

nagdmc pca.2

nagdmc pca

NAG DMC

51: all eigenvalues are zero; this indicates that a null matrix has been entered.
52: the sum of weights is less than 1.
98: the singular value decomposition used in the calculation failed to converge.
99: the function failed to allocate enough memory.
> 100: an error occurred in a function specified by the user.

Notation
nrecnvardata

the number of records, n.
the number of variables in the analysis, p.
the data matrix, X.

Description
Let X be an n by p data matrix of n data records on p variables x1 , x2 , . . . , xp and let the p by pvariance-covariance matrix of x1 , x2 , . . . , xp be S. A vector a1 of length p is found such that
aT1 Sa1 is maximised subject to aT1 a1 = 1.
Pp
The variable z1 =i=1 a1i xi is known as the first principal component and gives the linear
combinationofthevariables
that gives the maximum variation. A second principal component,Pp
z2 = i=1 a2i xi , is found such that
aT2 Sa2 is maximised subject to aT2 a2 = 1 and aT2 a1 = 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 eigenvectorsof the matrix S, and associated with each eigenvector
Pis the eigenvalue, λ2i . The value of λ2i / λ2i gives the proportion of variation explained by the ith
principal component. Alternatively, the ai can be considered as the right singular vectors in asingular value
pdecomposition with singular values λi of the data matrix centred about its mean andscaled by 1/ (n − 1), Xs . This latter approach is used in NAG DMC.
Xs = V ΛP 0 ,where Λ is a diagonal matrix with elements λi , P 0 is the p by p matrix with columns ai , and V isan n by p matrix with V 0 V = I, which gives the principal component scores.
Principal component analysis is often used to reduce the dimension of a data set, replacing a large
number of correlated variables with a smaller number of orthogonal variables that still contain mostof the information in the original data set.
The choice of the number of dimensions required is usually based on the amount of variationaccounted for by the leading principal components. If k principal components are selected, then atest of the equality of the remaining p − k eigenvalues is"!#ppXX
22(n − 1 − (2p + 5)/6) −log(λi ) + (p − k) log
λi /(p − k)
,
i=k+1

i=k+1

which has, asymptotically, a χ2 distribution with 12 (p−k −1)(p−k +2) degrees of freedom. Equalityof the remaining eigenvalues indicates that if any more principal components are to be consideredthen they all should be considered.
Instead of the variance-covariance matrix the correlation matrix, the sums of squares and crossproducts matrix or a standardised sums of squares and cross-products matrix may be used. Inthe last case S is replaced by σ −1/2 Sσ −1/2 for a diagonal matrix σ with positive elements. If thecorrelation matrix is used, the χ2 approximation for the statistic given above is not valid.
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 datarecord.

nagdmc pca.3

nagdmc pca

NAG DMC

References and Further ReadingChatfield C and Collins A J (1980) Introduction to Multivariate Analysis Chapman and Hall.
Hammarling S (1985) The singular value decomposition in multivariate statistics SIGNUM Newsl.
20 (3) 2-25.
Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press.

See Also
nagdmc pca score
pca ex.c

computes PCA scores for a given number of component directions.
the example calling program.

nagdmc pca.4