hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_mv_discrim (g03da)


    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example


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


[nig, gmn, det, gc, stat, df, sig, ifail] = g03da(x, isx, nvar, ing, ng, 'n', n, 'm', m, 'wt', wt)
[nig, gmn, det, gc, stat, df, sig, ifail] = nag_mv_discrim(x, isx, nvar, ing, ng, 'n', n, 'm', m, 'wt', wt)
Note: the interface to this routine has changed since earlier releases of the toolbox:
At Mark 24: weight was removed from the interface; wt was made optional


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-nglogS-j=1ngnj-1logSj ,  
C= 1-2p2+3p- 1 6p+ 1ng- 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=1ngnj-1Sj n-ng .  
For large n, G is approximately distributed as a χ2 variable with 12pp+1ng-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, nag_mv_discrim (g03da) 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 nag_mv_discrim_mahal (g03db). The distances are used in discriminant analysis and nag_mv_discrim_group (g03dc) uses the results of nag_mv_discrim (g03da) 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.


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


Compulsory Input Parameters

1:     xldxm – double array
ldx, the first dimension of the array, must satisfy the constraint ldxn.
xkl must contain the kth observation for the lth variable, for k=1,2,,n and l=1,2,,m.
2:     isxm int64int32nag_int array
isxl indicates whether or not the lth variable in x is to be included in the variance-covariance matrices.
If isxl>0 the lth variable is included, for l=1,2,,m; otherwise it is not referenced.
Constraint: isxl>0 for nvar values of l.
3:     nvar int64int32nag_int scalar
p, the number of variables in the variance-covariance matrices.
Constraint: nvar1.
4:     ingn int64int32nag_int array
ingk indicates to which group the kth observation belongs, for k=1,2,,n.
Constraint: 1ingkng, for k=1,2,,n
The values of ing must be such that each group has at least nvar members.
5:     ng int64int32nag_int scalar
The number of groups, ng.
Constraint: ng2.

Optional Input Parameters

1:     n int64int32nag_int scalar
Default: the dimension of the array ing and the first dimension of the array x. (An error is raised if these dimensions are not equal.)
n, the number of observations.
Constraint: n1.
2:     m int64int32nag_int scalar
Default: the dimension of the array isx and the second dimension of the array x. (An error is raised if these dimensions are not equal.)
The number of variables in the data array x.
Constraint: mnvar.
3:     wt: – double array
The dimension of the array wt must be at least n if weight='W', and at least 1 otherwise
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 wtk=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', wtk0.0, for k=1,2,,n.

Output Parameters

1:     nigng int64int32nag_int array
nigj contains the number of observations in the jth group, for j=1,2,,ng.
2:     gmnldgmnnvar – double array
The jth row of gmn contains the means of the p selected variables for the jth group, for j=1,2,,ng.
3:     detng – double array
The logarithm of the determinants of the within-group variance-covariance matrices.
4:     gcng+1×nvar×nvar+1/2 – double array
The first pp+1/2 elements of gc contain R and the remaining ng blocks of pp+1/2 elements contain the Rj matrices. All are stored in packed form by columns.
5:     stat – double scalar
The likelihood-ratio test statistic, G.
6:     df – double scalar
The degrees of freedom for the distribution of G.
7:     sig – double scalar
The significance level for G.
8:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
On entry,nvar<1,
orweight'U' or 'W'.
On entry,weight='W' and a value of wt<0.0.
On entry,there are not exactly nvar elements of isx>0,
ora value of ing is not in the range 1 to ng,
orthe effective number of observations for a group is less than 1,
ora group has less than nvar members.
R or one of the Rj is not of full rank.
An unexpected error has been triggered by this routine. Please contact NAG.
Your licence key may have expired or may not have been installed correctly.
Dynamic memory allocation failed.


The accuracy is dependent on the accuracy of the computation of the QR decomposition. See nag_lapack_dgeqrf (f08ae) for further details.

Further Comments

The time taken will be approximately proportional to np2.


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 nag_mv_discrim (g03da). The printed results show that there is evidence that the within-group variance-covariance matrices are not equal.
function g03da_example

fprintf('g03da example results\n\n');

x = [1.1314,  2.4596;
     1.0986,  0.2624;
     0.6419, -2.3026;
     1.3350, -3.2189;
     1.4110,  0.0953;
     0.6419, -0.9163;
     2.1163,  0.0000;
     1.3350, -1.6094;
     1.3610, -0.5108;
     2.0541,  0.1823;
     2.2083, -0.5108;
     2.7344,  1.2809;
     2.0412,  0.4700;
     1.8718, -0.9163;
     1.7405, -0.9163;
     2.6101,  0.4700;
     2.3224,  1.8563;
     2.2192,  2.0669;
     2.2618,  1.1314;
     3.9853,  0.9163;
     2.7600,  2.0281];
[n,m] = size(x);
isx  = ones(m,1,'int64');
nvar = int64(m);
ing  = ones(n,1,'int64');
ing(7:16) = int64(2);
ing(17:n) = int64(3);
ng        = int64(3);

[nig, gmean, det, gc, stat, df, sig, ifail] = ...
  g03da( ...
	 x, isx, nvar, ing, ng);

mtitle = 'Group means';
matrix = 'General';
diag   = ' ';
[ifail] = x04ca( ...
                 matrix, diag, gmean, mtitle);
fprintf('\nLog of determinants\n\n');
fprintf('%10.4f%10.4f%10.4f\n\n', det);
fprintf(' Stat = %7.4f\n', stat);
fprintf('   DF = %7.4f\n', df);
fprintf('  SIG = %7.4f\n', sig);

g03da example results

 Group means
             1          2
 1      1.0433    -0.6034
 2      2.0073    -0.2060
 3      2.7097     1.5998

Log of determinants

   -0.8273   -3.0460   -2.2877

 Stat = 19.2410
   DF =  6.0000
  SIG =  0.0038

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015