NAG Library Function Document
nag_mv_prin_comp (g03aac)
1 Purpose
nag_mv_prin_comp (g03aac) performs a principal component analysis on a data matrix; both the principal component loadings and the principal component scores are returned.
2 Specification
#include <nag.h> |
#include <nagg03.h> |
void |
nag_mv_prin_comp (Nag_PrinCompMat pcmatrix,
Nag_PrinCompScores scores,
Integer n,
Integer m,
const double x[],
Integer tdx,
const Integer isx[],
double s[],
const double wt[],
Integer nvar,
double e[],
Integer tde,
double p[],
Integer tdp,
double v[],
Integer tdv,
NagError *fail) |
|
3 Description
Let
be an
by
data matrix of
observations on
variables
and let the
by
variance-covariance matrix of
be
. A vector
of length
is found such that:
is maximized subject to
The variable
is known as the first principal component and gives the linear combination of the variables that gives the maximum variation. A second principal component,
, is found such that:
is maximized subject to
and
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
, are the eigenvectors of the matrix
and associated with each eigenvector is the eigenvalue,
. The value of
gives the proportion of variation explained by the
th principal component. Alternatively, the
's can be considered as the right singular vectors in a singular value decomposition with singular values
of the data matrix centred about its mean and scaled by
,
. This latter approach is used in nag_mv_prin_comp (g03aac), with
where
is a diagonal matrix with elements
,
is the
by
matrix with columns
and
is an
by
matrix with
, 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
principal components are selected, then a test of the equality of the remaining
eigenvalues is
which has, asymptotically, a
distribution with
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 is replaced by for a diagonal matrix with positive elements. If the correlation matrix is used, the approximation for the statistic given above is not valid.
The principal component scores, , 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 is first centred about the weighted means then each row is scaled by an amount , where is the weight for the 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 (1979) The Advanced Theory of Statistics (3 Volumes) (4th Edition) Griffin
Morrison D F (1967) Multivariate Statistical Methods McGraw–Hill
5 Arguments
- 1:
– Nag_PrinCompMatInput
-
On entry: indicates for which type of matrix the principal component analysis is to be carried out.
- It is for the correlation matrix.
- It is for the standardized matrix, with standardizations given by s.
- It is for the sums of squares and cross-products matrix.
- It is for the variance-covariance matrix.
Constraint:
, , or .
- 2:
– Nag_PrinCompScoresInput
-
On entry: specifies the type of principal component scores to be used.
- The principal component scores are standardized so that , i.e., .
- The principal component scores are unstandardized, i.e., .
- The principal component scores are standardized so that they have unit variance.
- The principal component scores are standardized so that they have variance equal to the corresponding eigenvalue.
Constraint:
, , or .
- 3:
– IntegerInput
-
On entry: the number of observations, .
Constraint:
.
- 4:
– IntegerInput
-
On entry: the number of variables in the data matrix, .
Constraint:
.
- 5:
– const doubleInput
-
On entry: must contain the th observation for the th variable, for and .
- 6:
– IntegerInput
-
On entry: the stride separating matrix column elements in the array
x.
Constraint:
.
- 7:
– const IntegerInput
-
On entry:
indicates whether or not the
th variable is to be included in the analysis. If
, then the variable contained in the
th column of
x is included in the principal component analysis, for
.
Constraint:
for
nvar values of
.
- 8:
– doubleInput/Output
-
On entry: the standardizations to be used, if any.
If
, then the first
elements of
s must contain the standardization coefficients, the diagonal elements of
.
Constraint:
if , , for .
On exit: if
, then
s is unchanged on exit.
If
, then
s contains the variances of the selected variables.
contains the variance of the variable in the
th column of
x if
.
If
or
, then
s is not referenced.
- 9:
– const doubleInput
-
On entry: optionally, the weights to be used in the principal component analysis.
If , then the th observation is not included in the analysis. The effective number of observations is the sum of the weights.
If weights are not provided then
wt must be set to
NULL and the effective number of observations is
n.
Constraints:
- if wt is not NULL, , for ;
- if wt is not NULL, the sum of weights .
- 10:
– IntegerInput
-
On entry: the number of variables in the principal component analysis, .
Constraint:
.
- 11:
– doubleOutput
-
On exit: the statistics of the principal component analysis.
, the eigenvalues associated with the
th principal component,
, for
.
, the proportion of variation explained by the th principal component, for .
, the cumulative proportion of variation explained by the first principal components, for .
, the statistics, for .
, the degrees of freedom for the statistics, for .
If , then contains the significance level for the statistic, for .
If , then is returned as zero.
- 12:
– IntegerInput
-
On entry: the stride separating matrix column elements in the array
e.
Constraint:
.
- 13:
– doubleOutput
-
Note: the th element of the matrix is stored in .
On exit: the first
nvar columns of
p contain the principal component loadings,
. The
th column of
p contains the
nvar coefficients for the
th principal component.
- 14:
– IntegerInput
-
On entry: the stride separating matrix column elements in the array
p.
Constraint:
.
- 15:
– doubleOutput
-
Note: the th element of the matrix is stored in .
On exit: the first
nvar columns of
v contain the principal component scores. The
th column of
v contains the
n scores for the
th principal component.
If weights are supplied in the array
wt, then any rows for which
is zero will be set to zero.
- 16:
– IntegerInput
-
On entry: the stride separating matrix column elements in the array
v.
Constraint:
.
- 17:
– NagError *Input/Output
-
The NAG error argument (see
Section 2.7 in How to Use the NAG Library and its Documentation).
6 Error Indicators and Warnings
- NE_2_INT_ARG_GE
-
On entry, while . These arguments must satisfy .
- NE_2_INT_ARG_GT
-
On entry, while . These arguments must satisfy .
- NE_2_INT_ARG_LT
-
On entry, while . These arguments must satisfy .
On entry, while . These arguments must satisfy .
On entry, while . These arguments must satisfy .
- NE_ALLOC_FAIL
-
Dynamic memory allocation failed.
- NE_BAD_PARAM
-
On entry, argument
pcmatrix had an illegal value.
On entry, argument
scores had an illegal value.
- NE_INT_ARG_LT
-
On entry, .
Constraint: .
On entry, .
Constraint: .
On entry, .
Constraint: .
On entry, .
Constraint: .
- NE_INTERNAL_ERROR
-
An internal error has occurred in this function. Check the function call
and any array sizes. If the call is correct then please contact
NAG for
assistance.
- NE_NEG_WEIGHT_ELEMENT
-
On entry,
.
Constraint: when referenced, all elements of
wt must be non-negative.
- NE_OBSERV_LT_VAR
-
With weighted data, the effective number of observations given by the sum of weights , while the number of variables included in the analysis, .
Constraint: effective number of observations .
- NE_SVD_NOT_CONV
-
The singular value decomposition has failed to converge. This is an unlikely error exit.
- NE_VAR_INCL_INDICATED
-
The number of variables,
nvar in the analysis
, while the number of variables included in the analysis via array
.
Constraint: these two numbers must be the same.
- NE_VAR_INCL_STANDARD
-
On entry, the standardization element , while the variable to be included .
Constraint: when a variable is to be included, the standardization element must be positive.
- NE_ZERO_EIGVALS
-
All eigenvalues/singular values are zero. This will be caused by all the variables being constant.
7 Accuracy
As nag_mv_prin_comp (g03aac) 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
nag_mv_prin_comp (g03aac) is not threaded in any implementation.
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 unstandardized principal component scores requested.
10.1 Program Text
Program Text (g03aace.c)
10.2 Program Data
Program Data (g03aace.d)
10.3 Program Results
Program Results (g03aace.r)