G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG03DCF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G03DCF allocates observations to groups according to selected rules. It is intended for use after G03DAF.

## 2  Specification

 SUBROUTINE G03DCF ( TYP, EQUAL, PRIORS, NVAR, NG, NIG, GMN, LDGMN, GC, DET, NOBS, M, ISX, X, LDX, PRIOR, P, LDP, IAG, ATIQ, ATI, WK, IFAIL)
 INTEGER NVAR, NG, NIG(NG), LDGMN, NOBS, M, ISX(M), LDX, LDP, IAG(NOBS), IFAIL REAL (KIND=nag_wp) GMN(LDGMN,NVAR), GC((NG+1)*NVAR*(NVAR+1)/2), DET(NG), X(LDX,M), PRIOR(NG), P(LDP,NG), ATI(LDP,*), WK(2*NVAR) LOGICAL ATIQ CHARACTER(1) TYP, EQUAL, PRIORS

## 3  Description

Discriminant analysis is concerned with the allocation of observations to groups using information from other observations whose group membership is known, ${X}_{t}$; these are called the training set. Consider $p$ variables observed on ${n}_{g}$ populations or groups. Let ${\stackrel{-}{x}}_{j}$ be the sample mean and ${S}_{j}$ the within-group variance-covariance matrix for the $j$th group; these are calculated from a training set of $n$ observations with ${n}_{j}$ observations in the $j$th group, and let ${x}_{k}$ be the $k$th observation from the set of observations to be allocated to the ${n}_{g}$ groups. The observation can be allocated to a group according to a selected rule. The allocation rule or discriminant function will be based on the distance of the observation from an estimate of the location of the groups, usually the group means. A measure of the distance of the observation from the $j$th group mean is given by the Mahalanobis distance, ${D}_{kj}$:
 $Dkj2=xk-x-jTSj-1xk-x-j.$ (1)
If the pooled estimate of the variance-covariance matrix $S$ is used rather than the within-group variance-covariance matrices, then the distance is:
 $Dkj2=xk-x-jTS-1xk-x-j.$ (2)
Instead of using the variance-covariance matrices $S$ and ${S}_{j}$, G03DCF uses the upper triangular matrices $R$ and ${R}_{j}$ supplied by G03DAF such that $S={R}^{\mathrm{T}}R$ and ${S}_{j}={R}_{j}^{\mathrm{T}}{R}_{j}$. ${D}_{kj}^{2}$ can then be calculated as ${z}^{\mathrm{T}}z$ where ${{R}^{\mathrm{T}}}_{j}z=\left({x}_{k}-{x}_{j}\right)$ or ${R}^{\mathrm{T}}z=\left({x}_{k}-x\right)$ as appropriate.
In addition to the distances, a set of prior probabilities of group membership, ${\pi }_{j}$, for $j=1,2,\dots ,{n}_{g}$, may be used, with $\sum {\pi }_{j}=1$. The prior probabilities reflect your view as to the likelihood of the observations coming from the different groups. Two common cases for prior probabilities are ${\pi }_{1}={\pi }_{2}=\cdots ={\pi }_{{n}_{g}}$, that is, equal prior probabilities, and ${\pi }_{\mathit{j}}={n}_{\mathit{j}}/n$, for $\mathit{j}=1,2,\dots ,{n}_{g}$, that is, prior probabilities proportional to the number of observations in the groups in the training set.
G03DCF uses one of four allocation rules. In all four rules the $p$ variables are assumed to follow a multivariate Normal distribution with mean ${\mu }_{j}$ and variance-covariance matrix ${\Sigma }_{j}$ if the observation comes from the $j$th group. The different rules depend on whether or not the within-group variance-covariance matrices are assumed equal, i.e., ${\Sigma }_{1}={\Sigma }_{2}=\cdots ={\Sigma }_{{n}_{g}}$, and whether a predictive or estimative approach is used. If $p\left({x}_{k}\mid {\mu }_{j},{\Sigma }_{j}\right)$ is the probability of observing the observation ${x}_{k}$ from group $j$, then the posterior probability of belonging to group $j$ is:
 $p j∣xk,μj,Σj∝ p xk∣ μj ,Σj πj.$ (3)
In the estimative approach, the parameters ${\mu }_{j}$ and ${\Sigma }_{j}$ in (3) are replaced by their estimates calculated from ${X}_{t}$. In the predictive approach, a non-informative prior distribution is used for the parameters and a posterior distribution for the parameters, $p\left({\mu }_{j},{\Sigma }_{j}\mid {X}_{t}\right)$, is found. A predictive distribution is then obtained by integrating $p\left(j\mid {x}_{k},{\mu }_{j},{\Sigma }_{j}\right)p\left({\mu }_{j},{\Sigma }_{j}\mid X\right)$ over the parameter space. This predictive distribution then replaces $p\left({x}_{k}\mid {\mu }_{j},{\Sigma }_{j}\right)$ in (3). See Aitchison and Dunsmore (1975), Aitchison et al. (1977) and Moran and Murphy (1979) for further details.
The observation is allocated to the group with the highest posterior probability. Denoting the posterior probabilities, $p\left(j\mid {x}_{k},{\mu }_{j},{\Sigma }_{j}\right)$, by ${q}_{j}$, the four allocation rules are:
(i) Estimative with equal variance-covariance matrices – Linear Discrimination
 $log⁡qj∝-12Dkj2+log⁡πj$
(ii) Estimative with unequal variance-covariance matrices – Quadratic Discrimination
 $log⁡qj∝-12Dkj2+log⁡πj-12logSj$
(iii) Predictive with equal variance-covariance matrices
 $q j - 1 ∝ n j +1 / n j p / 2 1 + n j / n - n g n j +1 D k j 2 n +1 - n g / 2$
(iv) Predictive with unequal variance-covariance matrices
 $q j - 1 ∝ C n j 2 - 1 / n j S j p / 2 1 + n j / n j 2 - 1 D k j 2 n j / 2 ,$
where
 $C=Γ12nj-p Γ12nj .$
In the above the appropriate value of ${D}_{kj}^{2}$ from (1) or (2) is used. The values of the ${q}_{j}$ are standardized so that,
 $∑j=1ngqj=1.$
Moran and Murphy (1979) show the similarity between the predictive methods and methods based upon likelihood ratio tests.
In addition to allocating the observation to a group, G03DCF computes an atypicality index, ${I}_{j}\left({x}_{k}\right)$. The predictive atypicality index is returned, irrespective of the value of the parameter TYP. This represents the probability of obtaining an observation more typical of group $j$ than the observed ${x}_{k}$ (see Aitchison and Dunsmore (1975) and Aitchison et al. (1977)). The atypicality index is computed for unequal within-group variance-covariance matrices as:
 $Ijxk=PB≤z:12p,12nj-p$
where $P\left(B\le \beta :a,b\right)$ is the lower tail probability from a beta distribution and
 $z=Dkj2/Dkj2+nj2-1/nj,$
and for equal within-group variance-covariance matrices as:
 $Ijxk=PB≤z : 12p,12n-ng-p+ 1,$
with
 $z=Dkj2/Dkj2+n-ngnj+1/nj.$
If ${I}_{j}\left({x}_{k}\right)$ is close to $1$ for all groups it indicates that the observation may come from a grouping not represented in the training set. Moran and Murphy (1979) provide a frequentist interpretation of ${I}_{j}\left({x}_{k}\right)$.

## 4  References

Aitchison J and Dunsmore I R (1975) Statistical Prediction Analysis Cambridge
Aitchison J, Habbema J D F and Kay J W (1977) A critical comparison of two methods of statistical discrimination Appl. Statist. 26 15–25
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
Moran M A and Murphy B J (1979) A closer look at two alternative methods of statistical discrimination Appl. Statist. 28 223–232
Morrison D F (1967) Multivariate Statistical Methods McGraw–Hill

## 5  Parameters

1:     TYP – CHARACTER(1)Input
On entry: whether the estimative or predictive approach is used.
${\mathbf{TYP}}=\text{'E'}$
The estimative approach is used.
${\mathbf{TYP}}=\text{'P'}$
The predictive approach is used.
Constraint: ${\mathbf{TYP}}=\text{'E'}$ or $\text{'P'}$.
2:     EQUAL – CHARACTER(1)Input
On entry: indicates whether or not the within-group variance-covariance matrices are assumed to be equal and the pooled variance-covariance matrix used.
${\mathbf{EQUAL}}=\text{'E'}$
The within-group variance-covariance matrices are assumed equal and the matrix $R$ stored in the first $p\left(p+1\right)/2$ elements of GC is used.
${\mathbf{EQUAL}}=\text{'U'}$
The within-group variance-covariance matrices are assumed to be unequal and the matrices ${R}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{n}_{g}$, stored in the remainder of GC are used.
Constraint: ${\mathbf{EQUAL}}=\text{'E'}$ or $\text{'U'}$.
3:     PRIORS – CHARACTER(1)Input
On entry: indicates the form of the prior probabilities to be used.
${\mathbf{PRIORS}}=\text{'E'}$
Equal prior probabilities are used.
${\mathbf{PRIORS}}=\text{'P'}$
Prior probabilities proportional to the group sizes in the training set, ${n}_{j}$, are used.
${\mathbf{PRIORS}}=\text{'I'}$
The prior probabilities are input in PRIOR.
Constraint: ${\mathbf{PRIORS}}=\text{'E'}$, $\text{'I'}$ or $\text{'P'}$.
4:     NVAR – INTEGERInput
On entry: $p$, the number of variables in the variance-covariance matrices.
Constraint: ${\mathbf{NVAR}}\ge 1$.
5:     NG – INTEGERInput
On entry: the number of groups, ${n}_{g}$.
Constraint: ${\mathbf{NG}}\ge 2$.
6:     NIG(NG) – INTEGER arrayInput
On entry: the number of observations in each group in the training set, ${n}_{j}$.
Constraints:
• if ${\mathbf{EQUAL}}=\text{'E'}$, ${\mathbf{NIG}}\left(\mathit{j}\right)>0$ and $\sum _{\mathit{j}=1}^{{n}_{g}}{\mathbf{NIG}}\left(\mathit{j}\right)>{\mathbf{NG}}+{\mathbf{NVAR}}$, for $\mathit{j}=1,2,\dots ,{n}_{g}$;
• if ${\mathbf{EQUAL}}=\text{'U'}$, ${\mathbf{NIG}}\left(\mathit{j}\right)>{\mathbf{NVAR}}$, for $\mathit{j}=1,2,\dots ,{n}_{g}$.
7:     GMN(LDGMN,NVAR) – REAL (KIND=nag_wp) arrayInput
On entry: the $\mathit{j}$th row of GMN contains the means of the $p$ variables for the $\mathit{j}$th group, for $\mathit{j}=1,2,\dots ,{n}_{j}$. These are returned by G03DAF.
8:     LDGMN – INTEGERInput
On entry: the first dimension of the array GMN as declared in the (sub)program from which G03DCF is called.
Constraint: ${\mathbf{LDGMN}}\ge {\mathbf{NG}}$.
9:     GC($\left({\mathbf{NG}}+1\right)×{\mathbf{NVAR}}×\left({\mathbf{NVAR}}+1\right)/2$) – REAL (KIND=nag_wp) arrayInput
On entry: the first $p\left(p+1\right)/2$ elements of GC should contain the upper triangular matrix $R$ and the next ${n}_{g}$ blocks of $p\left(p+1\right)/2$ elements should contain the upper triangular matrices ${R}_{j}$.
All matrices must be stored packed by column. These matrices are returned by G03DAF. If ${\mathbf{EQUAL}}=\text{'E'}$ only the first $p\left(p+1\right)/2$ elements are referenced, if ${\mathbf{EQUAL}}=\text{'U'}$ only the elements $p\left(p+1\right)/2+1$ to $\left({n}_{g}+1\right)p\left(p+1\right)/2$ are referenced.
Constraints:
• if ${\mathbf{EQUAL}}=\text{'E'}$, the diagonal elements of $R$ must be $\text{}\ne 0.0$;
• if ${\mathbf{EQUAL}}=\text{'U'}$, the diagonal elements of the ${R}_{\mathit{j}}$ must be $\text{}\ne 0.0$, for $\mathit{j}=1,2,\dots ,{n}_{g}$.
10:   DET(NG) – REAL (KIND=nag_wp) arrayInput
On entry: if ${\mathbf{EQUAL}}=\text{'U'}$. the logarithms of the determinants of the within-group variance-covariance matrices as returned by G03DAF. Otherwise DET is not referenced.
11:   NOBS – INTEGERInput
On entry: the number of observations in X which are to be allocated.
Constraint: ${\mathbf{NOBS}}\ge 1$.
12:   M – INTEGERInput
On entry: the number of variables in the data array X.
Constraint: ${\mathbf{M}}\ge {\mathbf{NVAR}}$.
13:   ISX(M) – INTEGER arrayInput
On entry: ${\mathbf{ISX}}\left(l\right)$ indicates if the $l$th variable in X is to be included in the distance calculations.
If ${\mathbf{ISX}}\left(\mathit{l}\right)>0$, the $\mathit{l}$th variable is included, for $\mathit{l}=1,2,\dots ,{\mathbf{M}}$; otherwise the $\mathit{l}$th variable is not referenced.
Constraint: ${\mathbf{ISX}}\left(l\right)>0$ for NVAR values of $l$.
14:   X(LDX,M) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{X}}\left(\mathit{k},\mathit{l}\right)$ must contain the $\mathit{k}$th observation for the $\mathit{l}$th variable, for $\mathit{k}=1,2,\dots ,{\mathbf{NOBS}}$ and $\mathit{l}=1,2,\dots ,{\mathbf{M}}$.
15:   LDX – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G03DCF is called.
Constraint: ${\mathbf{LDX}}\ge {\mathbf{NOBS}}$.
16:   PRIOR(NG) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{PRIORS}}=\text{'I'}$, the prior probabilities for the ${n}_{g}$ groups.
Constraint: if ${\mathbf{PRIORS}}=\text{'I'}$, ${\mathbf{PRIOR}}\left(\mathit{j}\right)>0.0$ and , for $\mathit{j}=1,2,\dots ,{n}_{g}$.
On exit: if ${\mathbf{PRIORS}}=\text{'P'}$, the computed prior probabilities in proportion to group sizes for the ${n}_{g}$ groups.
If ${\mathbf{PRIORS}}=\text{'I'}$, the input prior probabilities will be unchanged.
If ${\mathbf{PRIORS}}=\text{'E'}$, PRIOR is not set.
17:   P(LDP,NG) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{P}}\left(\mathit{k},\mathit{j}\right)$ contains the posterior probability ${p}_{\mathit{k}\mathit{j}}$ for allocating the $\mathit{k}$th observation to the $\mathit{j}$th group, for $\mathit{k}=1,2,\dots ,{\mathbf{NOBS}}$ and $\mathit{j}=1,2,\dots ,{n}_{g}$.
18:   LDP – INTEGERInput
On entry: the first dimension of the arrays P and ATI as declared in the (sub)program from which G03DCF is called.
Constraint: ${\mathbf{LDP}}\ge {\mathbf{NOBS}}$.
19:   IAG(NOBS) – INTEGER arrayOutput
On exit: the groups to which the observations have been allocated.
20:   ATIQ – LOGICALInput
On entry: ATIQ must be .TRUE. if atypicality indices are required. If ATIQ is .FALSE. the array ATI is not set.
21:   ATI(LDP,$*$) – REAL (KIND=nag_wp) arrayOutput
Note: the second dimension of the array ATI must be at least ${\mathbf{NG}}$ if ${\mathbf{ATIQ}}=\mathrm{.TRUE.}$, and at least $1$ otherwise.
On exit: if ATIQ is .TRUE., ${\mathbf{ATI}}\left(\mathit{k},\mathit{j}\right)$ will contain the predictive atypicality index for the $\mathit{k}$th observation with respect to the $\mathit{j}$th group, for $\mathit{k}=1,2,\dots ,{\mathbf{NOBS}}$ and $\mathit{j}=1,2,\dots ,{n}_{g}$.
If ATIQ is .FALSE., ATI is not set.
22:   WK($2×{\mathbf{NVAR}}$) – REAL (KIND=nag_wp) arrayWorkspace
23:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{NVAR}}<1$, or ${\mathbf{NG}}<2$, or ${\mathbf{NOBS}}<1$, or ${\mathbf{M}}<{\mathbf{NVAR}}$, or ${\mathbf{LDGMN}}<{\mathbf{NG}}$, or ${\mathbf{LDX}}<{\mathbf{NOBS}}$, or ${\mathbf{LDP}}<{\mathbf{NOBS}}$, or ${\mathbf{TYP}}\ne \text{'E'}$ or ‘P’, or ${\mathbf{EQUAL}}\ne \text{'E'}$ or ‘U’, or ${\mathbf{PRIORS}}\ne \text{'E'}$, ‘I’ or ‘P’.
${\mathbf{IFAIL}}=2$
 On entry, the number of variables indicated by ISX is not equal to NVAR, or ${\mathbf{EQUAL}}=\text{'E'}$ and ${\mathbf{NIG}}\left(j\right)\le 0$, for some $j$, or ${\mathbf{EQUAL}}=\text{'E'}$ and $\sum _{j=1}^{{n}_{g}}{\mathbf{NIG}}\left(j\right)\le {\mathbf{NG}}+{\mathbf{NVAR}}$, or ${\mathbf{EQUAL}}=\text{'U'}$ and ${\mathbf{NIG}}\left(j\right)\le {\mathbf{NVAR}}$ for some $j$.
${\mathbf{IFAIL}}=3$
 On entry, ${\mathbf{PRIORS}}=\text{'I'}$ and ${\mathbf{PRIOR}}\left(j\right)\le 0.0$ for some $j$, or ${\mathbf{PRIORS}}=\text{'I'}$ and $\sum _{j=1}^{{n}_{g}}{\mathbf{PRIOR}}\left(j\right)$ is not within  of $1$.
${\mathbf{IFAIL}}=4$
 On entry, ${\mathbf{EQUAL}}=\text{'E'}$ and a diagonal element of $R$ is zero, or ${\mathbf{EQUAL}}=\text{'U'}$ and a diagonal element of ${R}_{j}$ for some $j$ is zero.

## 7  Accuracy

The accuracy of the returned posterior probabilities will depend on the accuracy of the input $R$ or ${R}_{j}$ matrices. The atypicality index should be accurate to four significant places.

The distances ${D}_{kj}^{2}$ can be computed using G03DBF if other forms of discrimination are required.

## 9  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 group means and $R$ matrices are computed by G03DAF. A further six observations of unknown type are input and allocations made using the predictive approach and under the assumption that the within-group covariance matrices are not equal. The posterior probabilities of group membership, ${q}_{j}$, and the atypicality index are printed along with the allocated group. The atypicality index shows that observations $5$ and $6$ do not seem to be typical of the three types present in the initial $21$ observations.

### 9.1  Program Text

Program Text (g03dcfe.f90)

### 9.2  Program Data

Program Data (g03dcfe.d)

### 9.3  Program Results

Program Results (g03dcfe.r)