Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_mv_discrim (g03da)

## Purpose

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.

## Syntax

[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:
Mark 24: drop weight, wt optional
.

## Description

Let a sample of n$n$ observations on p$p$ variables come from ng${n}_{g}$ groups with nj${n}_{j}$ observations in the j$j$th group and nj = n$\sum {n}_{j}=n$. If the data is assumed to follow a multivariate Normal distribution with the variance-covariance matrix of the j$j$th group Σj${\Sigma }_{j}$, then to test for equality of the variance-covariance matrices between groups, that is, Σ1 = Σ2 = = Σng = Σ${\Sigma }_{1}={\Sigma }_{2}=\cdots ={\Sigma }_{{n}_{g}}=\Sigma$, the following likelihood-ratio test statistic, G$G$, can be used;
G = C
 ( ng ) (n − ng)log|S| − ∑ (nj − 1)log|Sj| j = 1
,
$G=C {(n-ng)log|S|-∑j=1ng(nj-1)log|Sj|} ,$
where
C = 1(2p2 + 3p 1)/(6(p + 1)(ng1))
 ( ng ) ∑ 1/((nj − 1)) − 1/((n − ng)) j = 1
,
$C= 1-2p2+3p- 1 6(p+ 1)(ng- 1) (∑j= 1ng1 (nj- 1) -1 (n-ng) ) ,$
and Sj${S}_{j}$ are the within-group variance-covariance matrices and S$S$ is the pooled variance-covariance matrix given by
 S = ( ∑ j = 1ng(nj − 1)Sj)/((n − ng)). $S=∑j=1ng(nj-1)Sj (n-ng) .$
For large n$n$, G$G$ is approximately distributed as a χ2${\chi }^{2}$ variable with (1/2)p(p + 1)(ng1)$\frac{1}{2}p\left(p+1\right)\left({n}_{g}-1\right)$ degrees of freedom, see Morrison (1967) for further comments. If weights are used, then S$S$ and Sj${S}_{j}$ are the weighted pooled and within-group variance-covariance matrices and n$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$QR$ decomposition. The group means are subtracted from the data and then for each group, a QR$QR$ decomposition is computed to give an upper triangular matrix Rj * ${R}_{j}^{*}$. This matrix can be scaled to give a matrix Rj${R}_{j}$ such that Sj = RjTRj${S}_{j}={R}_{j}^{\mathrm{T}}{R}_{j}$. The pooled R$R$ matrix is then computed from the Rj${R}_{j}$ matrices. The values of |S|$|S|$ and the |Sj|$|{S}_{j}|$ can then be calculated from the diagonal elements of R$R$ and the Rj${R}_{j}$.
This approach means that the Mahalanobis squared distances for a vector observation x$x$ can be computed as zTz${z}^{\mathrm{T}}z$, where Rjz = (xxj)${R}_{j}z=\left(x-{\stackrel{-}{x}}_{j}\right)$, xj${\stackrel{-}{x}}_{j}$ being the vector of means of the j$j$th 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.

## 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

## Parameters

### Compulsory Input Parameters

1:     x(ldx,m) – double array
ldx, the first dimension of the array, must satisfy the constraint ldxn$\mathit{ldx}\ge {\mathbf{n}}$.
x(k,l)${\mathbf{x}}\left(\mathit{k},\mathit{l}\right)$ must contain the k$\mathit{k}$th observation for the l$\mathit{l}$th variable, for k = 1,2,,n$\mathit{k}=1,2,\dots ,n$ and l = 1,2,,m$\mathit{l}=1,2,\dots ,{\mathbf{m}}$.
2:     isx(m) – int64int32nag_int array
m, the dimension of the array, must satisfy the constraint ${\mathbf{m}}\ge {\mathbf{nvar}}$.
isx(l)${\mathbf{isx}}\left(l\right)$ indicates whether or not the l$l$th variable in x is to be included in the variance-covariance matrices.
If isx(l) > 0${\mathbf{isx}}\left(\mathit{l}\right)>0$ the l$\mathit{l}$th variable is included, for l = 1,2,,m$\mathit{l}=1,2,\dots ,{\mathbf{m}}$; otherwise it is not referenced.
Constraint: isx(l) > 0${\mathbf{isx}}\left(l\right)>0$ for nvar values of l$l$.
3:     nvar – int64int32nag_int scalar
p$p$, the number of variables in the variance-covariance matrices.
Constraint: nvar1${\mathbf{nvar}}\ge 1$.
4:     ing(n) – int64int32nag_int array
n, the dimension of the array, must satisfy the constraint n1${\mathbf{n}}\ge 1$.
ing(k)${\mathbf{ing}}\left(\mathit{k}\right)$ indicates to which group the k$\mathit{k}$th observation belongs, for k = 1,2,,n$\mathit{k}=1,2,\dots ,n$.
Constraint: 1ing(k)ng$1\le {\mathbf{ing}}\left(\mathit{k}\right)\le {\mathbf{ng}}$, for k = 1,2,,n$\mathit{k}=1,2,\dots ,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${n}_{g}$.
Constraint: ng2${\mathbf{ng}}\ge 2$.

### 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$n$, the number of observations.
Constraint: n1${\mathbf{n}}\ge 1$.
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: ${\mathbf{m}}\ge {\mathbf{nvar}}$.
3:     wt( : $:$) – double array
Note: the dimension of the array wt must be at least n${\mathbf{n}}$ if weight = 'W'$\mathit{weight}=\text{'W'}$, and at least 1$1$ otherwise.
If weight = 'W'$\mathit{weight}=\text{'W'}$ the first n$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${\mathbf{wt}}\left(k\right)=0.0$ the k$k$th observation is excluded from the calculations.
If weight = 'U'$\mathit{weight}=\text{'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'$\mathit{weight}=\text{'W'}$, wt(k)0.0${\mathbf{wt}}\left(\mathit{k}\right)\ge 0.0$, for k = 1,2,,n$\mathit{k}=1,2,\dots ,n$.

### Input Parameters Omitted from the MATLAB Interface

weight ldx ldgmn wk iwk

### Output Parameters

1:     nig(ng) – int64int32nag_int array
nig(j)${\mathbf{nig}}\left(\mathit{j}\right)$ contains the number of observations in the j$\mathit{j}$th group, for j = 1,2,,ng$\mathit{j}=1,2,\dots ,{n}_{g}$.
2:     gmn(ldgmn,nvar) – double array
ldgmnng$\mathit{ldgmn}\ge {\mathbf{ng}}$.
The j$\mathit{j}$th row of gmn contains the means of the p$p$ selected variables for the j$\mathit{j}$th group, for j = 1,2,,ng$\mathit{j}=1,2,\dots ,{n}_{g}$.
3:     det(ng) – double array
The logarithm of the determinants of the within-group variance-covariance matrices.
4:     gc((ng + 1) × nvar × (nvar + 1) / 2$\left({\mathbf{ng}}+1\right)×{\mathbf{nvar}}×\left({\mathbf{nvar}}+1\right)/2$) – double array
The first p(p + 1) / 2$p\left(p+1\right)/2$ elements of gc contain R$R$ and the remaining ng${n}_{g}$ blocks of p(p + 1) / 2$p\left(p+1\right)/2$ elements contain the Rj${R}_{j}$ matrices. All are stored in packed form by columns.
5:     stat – double scalar
The likelihood-ratio test statistic, G$G$.
6:     df – double scalar
The degrees of freedom for the distribution of G$G$.
7:     sig – double scalar
The significance level for G$G$.
8:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
 On entry, nvar < 1${\mathbf{nvar}}<1$, or n < 1${\mathbf{n}}<1$, or ng < 2${\mathbf{ng}}<2$, or ${\mathbf{m}}<{\mathbf{nvar}}$, or ldx < n$\mathit{ldx}<{\mathbf{n}}$, or ldgmn < ng$\mathit{ldgmn}<{\mathbf{ng}}$, or weight ≠ 'U'$\mathit{weight}\ne \text{'U'}$ or 'W'$\text{'W'}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, weight = 'W'$\mathit{weight}=\text{'W'}$ and a value of wt < 0.0${\mathbf{wt}}<0.0$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, there are not exactly nvar elements of isx > 0${\mathbf{isx}}>0$, or a value of ing is not in the range 1$1$ to ng, or the effective number of observations for a group is less than 1$1$, or a group has less than nvar members.
ifail = 4${\mathbf{ifail}}=4$
R$R$ or one of the Rj${R}_{j}$ is not of full rank.

## Accuracy

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

The time taken will be approximately proportional to np2$n{p}^{2}$.

## Example

```function nag_mv_discrim_example
x = [1.1314, 2.4596;
1.0986, 0.2624;
0.6419, -2.3026;
1.335, -3.2189;
1.411, 0.0953;
0.6419, -0.9163;
2.1163, 0;
1.335, -1.6094;
1.361, -0.5108;
2.0541, 0.1823;
2.2083, -0.5108;
2.7344, 1.2809;
2.0412, 0.47;
1.8718, -0.9163;
1.7405, -0.9163;
2.6101, 0.47;
2.3224, 1.8563;
2.2192, 2.0669;
2.2618, 1.1314;
3.9853, 0.9163;
2.76, 2.0281];
isx = [int64(1);1];
nvar = int64(2);
ing = [int64(1);1;1;1;1;1;2;2;2;2;2;2;2;2;2;2;3;3;3;3;3];
ng = int64(3);
[nig, gmean, det, gc, stat, df, sig, ifail] = ...
nag_mv_discrim(x, isx, nvar, ing, ng)
```
```

nig =

6
10
5

gmean =

1.0433   -0.6034
2.0073   -0.2060
2.7097    1.5998

det =

-0.8273
-3.0460
-2.2877

gc =

-0.5100
-0.2797
-1.2173
-0.3327
-0.3724
-1.9876
-0.4603
-0.7042
0.4737
0.7451
-0.3251
-0.4276

stat =

19.2410

df =

6

sig =

0.0038

ifail =

0

```
```function g03da_example
x = [1.1314, 2.4596;
1.0986, 0.2624;
0.6419, -2.3026;
1.335, -3.2189;
1.411, 0.0953;
0.6419, -0.9163;
2.1163, 0;
1.335, -1.6094;
1.361, -0.5108;
2.0541, 0.1823;
2.2083, -0.5108;
2.7344, 1.2809;
2.0412, 0.47;
1.8718, -0.9163;
1.7405, -0.9163;
2.6101, 0.47;
2.3224, 1.8563;
2.2192, 2.0669;
2.2618, 1.1314;
3.9853, 0.9163;
2.76, 2.0281];
isx = [int64(1);1];
nvar = int64(2);
ing = [int64(1);1;1;1;1;1;2;2;2;2;2;2;2;2;2;2;3;3;3;3;3];
ng = int64(3);
[nig, gmean, det, gc, stat, df, sig, ifail] = ...
g03da(x, isx, nvar, ing, ng)
```
```

nig =

6
10
5

gmean =

1.0433   -0.6034
2.0073   -0.2060
2.7097    1.5998

det =

-0.8273
-3.0460
-2.2877

gc =

-0.5100
-0.2797
-1.2173
-0.3327
-0.3724
-1.9876
-0.4603
-0.7042
0.4737
0.7451
-0.3251
-0.4276

stat =

19.2410

df =

6

sig =

0.0038

ifail =

0

```