naginterfaces.library.mv.prin_comp¶
- naginterfaces.library.mv.prin_comp(matrix, std, x, isx, s, nvar, wt=None)[source]¶
prin_comp
performs a principal component analysis on a data matrix; both the principal component loadings and the principal component scores are returned.For full information please refer to the NAG Library document for g03aa
https://support.nag.com/numeric/nl/nagdoc_30.2/flhtml/g03/g03aaf.html
- Parameters
- matrixstr, length 1
Indicates for which type of matrix the principal component analysis is to be carried out.
It is for the correlation matrix.
It is for a standardized matrix, with standardizations given by .
It is for the sums of squares and cross-products matrix.
It is for the variance-covariance matrix.
- stdstr, length 1
Indicates if the principal component scores are to be standardized.
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.
- xfloat, array-like, shape
must contain the th observation for the th variable, for , for .
- isxint, array-like, shape
indicates whether or not the th variable is to be included in the analysis.
If , the variable contained in the th column of is included in the principal component analysis, for .
- sfloat, array-like, shape
The standardizations to be used, if any.
If , the first elements of must contain the standardization coefficients, the diagonal elements of .
- nvarint
, the number of variables in the principal component analysis.
- wtNone or float, array-like, shape , optional
If , the first elements of must contain the weights to be used in the principal component analysis.
If , the th observation is not included in the analysis.
The effective number of observations is the sum of the weights.
If is None the effective number of observations is .
- Returns
- sfloat, ndarray, shape
If , is unchanged on exit.
If , contains the variances of the selected variables. contains the variance of the variable in the th column of if .
If or , is not referenced.
- efloat, ndarray, shape
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 th principal components, for .
The statistics, for .
The degrees of freedom for the statistics, for .
If , contains significance level for the statistic, for .
If , is returned as zero.
- pfloat, ndarray, shape
The first columns of contain the principal component loadings, . The th column of contains the coefficients for the th principal component.
- vfloat, ndarray, shape
The first columns of contain the principal component scores. The th column of contains the scores for the th principal component.
If , any rows for which is zero will be set to zero.
- Raises
- NagValueError
- (errno )
On entry, and .
Constraint: .
- (errno )
On entry, .
Constraint: .
- (errno )
On entry, and .
Constraint: .
- (errno )
On entry, .
Constraint: , , or .
- (errno )
On entry, .
Constraint: , , or .
- (errno )
On entry, .
Constraint: .
- (errno )
On entry, .
Constraint: .
- (errno )
On entry, and .
Constraint: .
- (errno )
Number of selected variables effective number of observations.
- (errno )
On entry, and values of .
Constraint: exactly elements of .
- (errno )
On entry, and .
Constraint: .
- (errno )
The singular value decomposition has failed to converge.
- Warns
- NagAlgorithmicWarning
- (errno )
All eigenvalues/singular values are zero. This will be caused by all the variables being constant.
- Notes
In the NAG Library the traditional C interface for this routine uses a different algorithmic base. Please contact NAG if you have any questions about compatibility.
Let be an data matrix of observations on variables and let the variance-covariance matrix of be . A vector of length is found such that:
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:
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
prin_comp
, withwhere is a diagonal matrix with elements , is the matrix with columns and is an 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 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.
- 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