NAG FL Interface
g03daf (discrim)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

g03daf computes a test statistic for the equality of within-group covariance matrices and also computes matrices for use in discriminant analysis.

2 Specification

Fortran Interface
Subroutine g03daf ( weight, n, m, x, ldx, isx, nvar, ing, ng, wt, nig, gmn, ldgmn, det, gc, stat, df, sig, wk, iwk, ifail)
Integer, Intent (In) :: n, m, ldx, isx(m), nvar, ing(n), ng, ldgmn
Integer, Intent (Inout) :: ifail
Integer, Intent (Out) :: nig(ng), iwk(ng)
Real (Kind=nag_wp), Intent (In) :: x(ldx,m), wt(*)
Real (Kind=nag_wp), Intent (Inout) :: gmn(ldgmn,nvar)
Real (Kind=nag_wp), Intent (Out) :: det(ng), gc((ng+1)*nvar*(nvar+1)/2), stat, df, sig, wk(n*(nvar+1))
Character (1), Intent (In) :: weight
C Header Interface
#include <nag.h>
void  g03daf_ (const char *weight, const Integer *n, const Integer *m, const double x[], const Integer *ldx, const Integer isx[], const Integer *nvar, const Integer ing[], const Integer *ng, const double wt[], Integer nig[], double gmn[], const Integer *ldgmn, double det[], double gc[], double *stat, double *df, double *sig, double wk[], Integer iwk[], Integer *ifail, const Charlen length_weight)
The routine may be called by the names g03daf or nagf_mv_discrim.

3 Description

Let a sample of n observations on p variables come from ng groups with nj observations in the jth group and nj=n. If the data is assumed to follow a multivariate Normal distribution with the variance-covariance matrix of the jth group Σj, then to test for equality of the variance-covariance matrices between groups, that is, Σ1=Σ2==Σng=Σ, the following likelihood-ratio test statistic, G, can be used;
G=C {(n-ng)log|S|-j=1ng(nj-1)log|Sj|} ,  
where
C= 1-2p2+3p- 1 6(p+1)(ng-1) (j= 1ng1 (nj-1) -1 (n-ng) ) ,  
and Sj are the within-group variance-covariance matrices and S is the pooled variance-covariance matrix given by
S=j=1ng(nj-1)Sj (n-ng) .  
For large n, G is approximately distributed as a χ2 variable with 12p(p+1)(ng-1) degrees of freedom, see Morrison (1967) for further comments. If weights are used, then S and Sj are the weighted pooled and within-group variance-covariance matrices and n is the effective number of observations, that is, the sum of the weights.
Instead of calculating the within-group variance-covariance matrices and then computing their determinants in order to calculate the test statistic, g03daf uses a QR decomposition. The group means are subtracted from the data and then for each group, a QR decomposition is computed to give an upper triangular matrix Rj*. This matrix can be scaled to give a matrix Rj such that Sj=RjTRj. The pooled R matrix is then computed from the Rj matrices. The values of |S| and the |Sj| can then be calculated from the diagonal elements of R and the Rj.
This approach means that the Mahalanobis squared distances for a vector observation x can be computed as zTz, where Rjz=(x-x¯j), x¯j being the vector of means of the jth group. These distances can be calculated by g03dbf. The distances are used in discriminant analysis and g03dcf uses the results of g03daf to perform several different types of discriminant analysis. The differences between the discriminant methods are, in part, due to whether or not the within-group variance-covariance matrices are equal.

4 References

Aitchison J and Dunsmore I R (1975) Statistical Prediction Analysis Cambridge
Kendall M G and Stuart A (1976) The Advanced Theory of Statistics (Volume 3) (3rd Edition) Griffin
Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press
Morrison D F (1967) Multivariate Statistical Methods McGraw–Hill

5 Arguments

1: weight Character(1) Input
On entry: indicates if weights are to be used.
weight='U'
No weights are used.
weight='W'
Weights are to be used and must be supplied in wt.
Constraint: weight='U' or 'W'.
2: n Integer Input
On entry: n, the number of observations.
Constraint: n1.
3: m Integer Input
On entry: the number of variables in the data array x.
Constraint: mnvar.
4: x(ldx,m) Real (Kind=nag_wp) array Input
On entry: x(k,l) must contain the kth observation for the lth variable, for k=1,2,,n and l=1,2,,m.
5: ldx Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g03daf is called.
Constraint: ldxn.
6: isx(m) Integer array Input
On entry: isx(l) indicates whether or not the lth variable in x is to be included in the variance-covariance matrices.
If isx(l)>0 the lth variable is included, for l=1,2,,m; otherwise it is not referenced.
Constraint: isx(l)>0 for nvar values of l.
7: nvar Integer Input
On entry: p, the number of variables in the variance-covariance matrices.
Constraint: nvar1.
8: ing(n) Integer array Input
On entry: ing(k) indicates to which group the kth observation belongs, for k=1,2,,n.
Constraint: 1ing(k)ng, for k=1,2,,n
The values of ing must be such that each group has at least nvar members.
9: ng Integer Input
On entry: the number of groups, ng.
Constraint: ng2.
10: wt(*) Real (Kind=nag_wp) array Input
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 analysis and the effective number of observations for a group is the sum of the weights of the observations in that group. If wt(k)=0.0 the kth observation is excluded from the calculations.
If weight='U', wt is not referenced and the effective number of observations for a group is the number of observations in that group.
Constraint: if weight='W', wt(k)0.0, for k=1,2,,n.
11: nig(ng) Integer array Output
On exit: nig(j) contains the number of observations in the jth group, for j=1,2,,ng.
12: gmn(ldgmn,nvar) Real (Kind=nag_wp) array Output
On exit: the jth row of gmn contains the means of the p selected variables for the jth group, for j=1,2,,ng.
13: ldgmn Integer Input
On entry: the first dimension of the array gmn as declared in the (sub)program from which g03daf is called.
Constraint: ldgmnng.
14: det(ng) Real (Kind=nag_wp) array Output
On exit: the logarithm of the determinants of the within-group variance-covariance matrices.
15: gc((ng+1)×nvar×(nvar+1)/2) Real (Kind=nag_wp) array Output
On exit: the first p(p+1)/2 elements of gc contain R and the remaining ng blocks of p(p+1)/2 elements contain the Rj matrices. All are stored in packed form by columns.
16: stat Real (Kind=nag_wp) Output
On exit: the likelihood-ratio test statistic, G.
17: df Real (Kind=nag_wp) Output
On exit: the degrees of freedom for the distribution of G.
18: sig Real (Kind=nag_wp) Output
On exit: the significance level for G.
19: wk(n×(nvar+1)) Real (Kind=nag_wp) array Workspace
20: iwk(ng) Integer array Workspace
21: ifail Integer Input/Output
On entry: ifail must be set to 0, −1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of −1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value −1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. 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, ldgmn=value and ng=value.
Constraint: ldgmnng.
On entry, ldx=value and n=value.
Constraint: ldxn.
On entry, m=value and nvar=value.
Constraint: mnvar.
On entry, n=value.
Constraint: n1.
On entry, ng=value.
Constraint: ng2.
On entry, nvar=value.
Constraint: nvar1.
On entry, weight=value.
Constraint: weight='U' or 'W'.
ifail=2
On entry, i=value and wt(i)<0.0.
Constraint: wt(i)0.0.
ifail=3
On entry, i=value, ing(i)=value and ng=value.
Constraint: 1ing(i)ng.
On entry, nvar=value and value values of isx>0
Constraint: exactly nvar elements of isx>0.
The effective number of observations for group value is less than 1.
The number of observations for group value is less than nvar.
ifail=4
R is not of full rank.
Rj is not of full rank for j=value.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

The accuracy is dependent on the accuracy of the computation of the QR decomposition. See f08aef for further details.

8 Parallelism and Performance

g03daf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g03daf 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

The time taken will be approximately proportional to np2.

10 Example

The data, taken from Aitchison and Dunsmore (1975), is concerned with the diagnosis of three ‘types’ of Cushing's syndrome. The variables are the logarithms of the urinary excretion rates (mg/24hr) of two steroid metabolites. Observations for a total of 21 patients are input and the statistics computed by g03daf. The printed results show that there is evidence that the within-group variance-covariance matrices are not equal.

10.1 Program Text

Program Text (g03dafe.f90)

10.2 Program Data

Program Data (g03dafe.d)

10.3 Program Results

Program Results (g03dafe.r)