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

Syntax

C#
public static void g03aa(
	string matrix,
	string std,
	string weight,
	int n,
	int m,
	double[,] x,
	int[] isx,
	double[] s,
	double[] wt,
	int nvar,
	double[,] e,
	double[,] p,
	double[,] v,
	out int ifail
)
Visual Basic
Public Shared Sub g03aa ( _
	matrix As String, _
	std As String, _
	weight As String, _
	n As Integer, _
	m As Integer, _
	x As Double(,), _
	isx As Integer(), _
	s As Double(), _
	wt As Double(), _
	nvar As Integer, _
	e As Double(,), _
	p As Double(,), _
	v As Double(,), _
	<OutAttribute> ByRef ifail As Integer _
)
Visual C++
public:
static void g03aa(
	String^ matrix, 
	String^ std, 
	String^ weight, 
	int n, 
	int m, 
	array<double,2>^ x, 
	array<int>^ isx, 
	array<double>^ s, 
	array<double>^ wt, 
	int nvar, 
	array<double,2>^ e, 
	array<double,2>^ p, 
	array<double,2>^ v, 
	[OutAttribute] int% ifail
)
F#
static member g03aa : 
        matrix : string * 
        std : string * 
        weight : string * 
        n : int * 
        m : int * 
        x : float[,] * 
        isx : int[] * 
        s : float[] * 
        wt : float[] * 
        nvar : int * 
        e : float[,] * 
        p : float[,] * 
        v : float[,] * 
        ifail : int byref -> unit 

Parameters

matrix
Type: System..::..String
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".
std
Type: System..::..String
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".
weight
Type: System..::..String
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".
n
Type: System..::..Int32
On entry: n, the number of observations.
Constraint: n2.
m
Type: System..::..Int32
On entry: m, the number of variables in the data matrix.
Constraint: m1.
x
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, m]
Note: dim1 must satisfy the constraint: dim1n
On entry: x[i-1,j-1] must contain the ith observation for the jth variable, for i=1,2,,n and j=1,2,,m.
isx
Type: array<System..::..Int32>[]()[][]
An array of size [m]
On entry: isx[j-1] indicates whether or not the jth variable is to be included in the analysis.
If isx[j-1]>0, the variable contained in the jth column of x is included in the principal component analysis, for j=1,2,,m.
Constraint: isx[j-1]>0 for nvar values of j.
s
Type: array<System..::..Double>[]()[][]
An array of size [m]
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 isx[j-1]>0, s[j]>0.0, for j=0,1,,m-1.
On exit: if matrix="S", s is unchanged on exit.
If matrix="C", s contains the variances of the selected variables. s[j-1] contains the variance of the variable in the jth column of x if isx[j-1]>0.
If matrix="U" or "V", s is not referenced.
wt
Type: array<System..::..Double>[]()[][]
An array of size [dim1]
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 wt[i-1]=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:
  • wt[i-1]0.0, for i=1,2,,n;
  • the sum of weights nvar+1.
nvar
Type: System..::..Int32
On entry: p, the number of variables in the principal component analysis.
Constraint: 1nvarminn-1,m.
e
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, 6]
Note: dim1 must satisfy the constraint: dim1nvar
On exit: the statistics of the principal component analysis.
e[i-1,0]
The eigenvalues associated with the ith principal component, λi2, for i=1,2,,p.
e[i-1,1]
The proportion of variation explained by the ith principal component, for i=1,2,,p.
e[i-1,2]
The cumulative proportion of variation explained by the first ith principal components, for i=1,2,,p.
e[i-1,3]
The χ2 statistics, for i=1,2,,p.
e[i-1,4]
The degrees of freedom for the χ2 statistics, for i=1,2,,p.
If matrix"C", e[i-1,5] contains significance level for the χ2 statistic, for i=1,2,,p.
If matrix="C", e[i-1,5] is returned as zero.
p
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, nvar]
Note: dim1 must satisfy the constraint: dim1nvar
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.
v
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, nvar]
Note: dim1 must satisfy the constraint: dim1n
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 wt[i-1] is zero will be set to zero.
ifail
Type: System..::..Int32%
On exit: ifail=0 unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

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 g03aa, 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.

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

Error Indicators and Warnings

Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDX, LDE, LDP, LDV) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
ifail=1
On entry,m<1,
orn<2,
ornvar<1,
ornvar>m,
ornvarn,
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,s[j-1]0.0 for some j=1,2,,m, when matrix="S" and isx[j-1]>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=-9000
An error occured, see message report.
ifail=-6000
Invalid Parameters value
ifail=-4000
Invalid dimension for array value
ifail=-8000
Negative dimension for array value
ifail=-6000
Invalid Parameters value

Accuracy

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

Parallelism and Performance

None.

Further Comments

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.

Example program (C#): g03aae.cs

Example program data: g03aae.d

Example program results: g03aae.r

See Also