g03 Chapter Contents
g03 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_mv_canon_var (g03acc)

## 1  Purpose

nag_mv_canon_var (g03acc) performs a canonical variate (canonical discrimination) analysis.

## 2  Specification

 #include #include
 void nag_mv_canon_var (Nag_Weightstype weight, Integer n, Integer m, const double x[], Integer tdx, const Integer isx[], Integer nx, const Integer ing[], Integer ng, const double wt[], Integer nig[], double cvm[], Integer tdcvm, double e[], Integer tde, Integer *ncv, double cvx[], Integer tdcvx, double tol, Integer *irankx, NagError *fail)

## 3  Description

Let a sample of $n$ observations on ${n}_{x}$ variables in a data matrix come from ${n}_{g}$ groups with ${n}_{1},{n}_{2},\dots ,{n}_{{n}_{g}}$ observations in each group, $\sum {n}_{i}=n$. Canonical variate analysis finds the linear combination of the ${n}_{x}$ variables that maximizes the ratio of between-group to within-group variation. The variables formed, the canonical variates can then be used to discriminate between groups.
The canonical variates can be calculated from the eigenvectors of the within-group sums of squares and cross-products matrix. However, nag_mv_canon_var (g03acc) calculates the canonical variates by means of a singular value decomposition (SVD) of a matrix $V$. Let the data matrix with variable (column) means subtracted be $X$, and let its rank be $k$; then the $k$ by $\left({n}_{g}-1\right)$ matrix $V$ is given by:
$V={Q}_{X}^{\mathrm{T}}{Q}_{g}\text{,}$ where ${Q}_{g}$ is an $n$ by $\left({n}_{g}-1\right)$ orthogonal matrix that defines the groups and ${Q}_{X}$ is the first $k$ rows of the orthogonal matrix $Q$ either from the $QR$ decomposition of $X$:
 $X = QR$
if $X$ is of full column rank, i.e., $k={n}_{x}$, else from the SVD of $X$:
 $X = QDPT .$
Let the SVD of $V$ be:
 $V = U x Δ UgT$
then the nonzero elements of the diagonal matrix $\Delta$, ${\delta }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,l$, are the $l$ canonical correlations associated with the $l$ canonical variates, where $l=\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(k,{n}_{g}\right)$.
The eigenvalues, ${\lambda }_{i}^{2}$, of the within-group sums of squares matrix are given by:
 $λ i 2 = δ i 2 1 - δ i 2 .$
and the value of ${\pi }_{i}={\lambda }_{i}^{2}/\sum {\lambda }_{i}^{2}$ gives the proportion of variation explained by the $i$th canonical variate. The values of the ${\pi }_{i}$'s give an indication as to how many canonical variates are needed to adequately describe the data, i.e., the dimensionality of the problem.
To test for a significant dimensionality greater than $i$ the ${\chi }^{2}$ statistic:
 $n - 1 - n g - 1 2 k-n g ∑ j = i + 1 l log 1 + λ j 2$
can be used. This is asymptotically distributed as a ${\chi }^{2}$ distribution with $\left(k-i\right)\left({n}_{g}-1-i\right)$ degrees of freedom. If the test for $i=h$ is not significant, then the remaining tests for $i>h$ should be ignored.
The loadings for the canonical variates are calculated from the matrix ${U}_{x}$. This matrix is scaled so that the canonical variates have unit within group variance.
In addition to the canonical variates loadings the means for each canonical variate are calculated for each group.
Weights can be used with the analysis, in which case the weighted means are subtracted from each column and then each row is scaled by an amount $\sqrt{{w}_{i}}$, where ${w}_{i}$ is the weight for the $i$th observation (row).

## 4  References

Chatfield C and Collins A J (1980) Introduction to Multivariate Analysis Chapman and Hall
Gnanadesikan R (1977) Methods for Statistical Data Analysis of Multivariate Observations Wiley
Hammarling S (1985) The singular value decomposition in multivariate statistics SIGNUM Newsl. 20(3) 2–25
Kendall M G and Stuart A (1979) The Advanced Theory of Statistics (3 Volumes) (4th Edition) Griffin

## 5  Arguments

1:     weightNag_WeightstypeInput
On entry: indicates the type of weights to be used in the analysis.
${\mathbf{weight}}=\mathrm{Nag_NoWeights}$
No weights are used.
${\mathbf{weight}}=\mathrm{Nag_Weightsfreq}$
The weights are treated as frequencies and the effective number of observations is the sum of the weights.
${\mathbf{weight}}=\mathrm{Nag_Weightsvar}$
The weights are treated as being inversely proportional to the variance of the observations and the effective number of observations is the number of observations with nonzero weights.
Constraint: ${\mathbf{weight}}=\mathrm{Nag_NoWeights}$, $\mathrm{Nag_Weightsfreq}$ or $\mathrm{Nag_Weightsvar}$.
2:     nIntegerInput
On entry: the number of observations, $n$.
Constraint: ${\mathbf{n}}\ge {\mathbf{nx}}+{\mathbf{ng}}$.
3:     mIntegerInput
On entry: the total number of variables, $m$.
Constraint: ${\mathbf{m}}\ge {\mathbf{nx}}$.
4:     x[${\mathbf{n}}×{\mathbf{tdx}}$]const doubleInput
On entry: ${\mathbf{x}}\left[\left(\mathit{i}-1\right)×{\mathbf{tdx}}+\mathit{j}-1\right]$ must contain the $\mathit{i}$th observation for the $\mathit{j}$th variable, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,m$.
5:     tdxIntegerInput
On entry: the stride separating matrix column elements in the array x.
Constraint: ${\mathbf{tdx}}\ge {\mathbf{m}}$.
6:     isx[m]const IntegerInput
On entry: ${\mathbf{isx}}\left[j-1\right]$ indicates whether or not the $j$th variable is to be included in the analysis.
If ${\mathbf{isx}}\left[\mathit{j}-1\right]>0$, then the variable contained in the $\mathit{j}$th column of x is included in the canonical variate analysis, for $\mathit{j}=1,2,\dots ,m$.
Constraint: ${\mathbf{isx}}\left[j-1\right]>0$ for nx values of $j$.
7:     nxIntegerInput
On entry: the number of variables in the analysis, ${n}_{x}$.
Constraint: ${\mathbf{nx}}\ge 1$.
8:     ing[n]const IntegerInput
On entry: ${\mathbf{ing}}\left[\mathit{i}-1\right]$ indicates which group the $\mathit{i}$th observation is in, for $\mathit{i}=1,2,\dots ,n$. The effective number of groups is the number of groups with nonzero membership.
Constraint: $1\le {\mathbf{ing}}\left[\mathit{i}-1\right]\le {\mathbf{ng}}$, for $\mathit{i}=1,2,\dots ,n$.
9:     ngIntegerInput
On entry: the number of groups, ${n}_{g}$.
Constraint: ${\mathbf{ng}}\ge 2$.
10:   wt[n]const doubleInput
On entry: if ${\mathbf{weight}}=\mathrm{Nag_Weightsfreq}$ or $\mathrm{Nag_Weightsvar}$ then the elements of wt must contain the weights to be used in the analysis.
If ${\mathbf{wt}}\left[i-1\right]=0.0$ then the $i$th observation is not included in the analysis.
Constraints:
• ${\mathbf{wt}}\left[\mathit{i}-1\right]\ge 0.0$, for $\mathit{i}=1,2,\dots ,n$;
• ${\sum }_{i=1}^{n}{\mathbf{wt}}\left[i-1\right]\ge {\mathbf{nx}}+$ effective number of groups.
Note: if ${\mathbf{weight}}=\mathrm{Nag_NoWeights}$ then wt is not referenced and may be set to the null pointer NULL, i.e., (double *)0.
11:   nig[ng]IntegerOutput
On exit: ${\mathbf{nig}}\left[\mathit{j}-1\right]$ gives the number of observations in group $\mathit{j}$, for $\mathit{j}=1,2,\dots ,{n}_{g}$.
12:   cvm[${\mathbf{ng}}×{\mathbf{tdcvm}}$]doubleOutput
On exit: ${\mathbf{cvm}}\left[\left(\mathit{i}-1\right)×{\mathbf{tdcvm}}+\mathit{j}-1\right]$ contains the mean of the $\mathit{j}$th canonical variate for the $\mathit{i}$th group, for $\mathit{i}=1,2,\dots ,{n}_{g}$ and $\mathit{j}=1,2,\dots ,l$; the remaining columns, if any, are used as workspace.
13:   tdcvmIntegerInput
On entry: the stride separating matrix column elements in the array cvm.
Constraint: ${\mathbf{tdcvm}}\ge {\mathbf{nx}}$.
14:   e[$\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{nx}},{\mathbf{ng}}-1\right)×{\mathbf{tde}}$]doubleOutput
On exit: the statistics of the canonical variate analysis. ${\mathbf{e}}\left[\left(\mathit{i}-1\right)×{\mathbf{tde}}+0\right]$, the canonical correlations, ${\delta }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{e}}\left[\left(i-1\right)×{\mathbf{tde}}+1\right]$, the eigenvalues of the within-group sum of squares matrix, ${\lambda }_{\mathit{i}}^{2}$, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{e}}\left[\left(\mathit{i}-1\right)×{\mathbf{tde}}+2\right]$, the proportion of variation explained by the $\mathit{i}$th canonical variate, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{e}}\left[\left(i-1\right)×{\mathbf{tde}}+3\right]$, the ${\chi }^{2}$ statistic for the $\mathit{i}$th canonical variate, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{e}}\left[\left(i-1\right)×{\mathbf{tde}}+4\right]$, the degrees of freedom for ${\chi }^{2}$ statistic for the $\mathit{i}$th canonical variate, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{e}}\left[\left(i-1\right)×{\mathbf{tde}}+5\right]$, the significance level for the ${\chi }^{2}$ statistic for the $\mathit{i}$th canonical variate, for $\mathit{i}=1,2,\dots ,l$.
15:   tdeIntegerInput
On entry: the stride separating matrix column elements in the array e.
Constraint: ${\mathbf{tde}}\ge 6$.
16:   ncvInteger *Output
On exit: the number of canonical variates, $l$. This will be the minimum of ${n}_{g}-1$ and the rank of x.
17:   cvx[${\mathbf{nx}}×{\mathbf{tdcvx}}$]doubleOutput
On exit: the canonical variate loadings. ${\mathbf{cvx}}\left[\left(\mathit{i}-1\right)×{\mathbf{tdcvx}}+\mathit{j}-1\right]$ contains the loading coefficient for the $\mathit{i}$th variable on the $\mathit{j}$th canonical variate, for $\mathit{i}=1,2,\dots ,{n}_{x}$ and $\mathit{j}=1,2,\dots ,l$; the remaining columns, if any, are used as workspace.
18:   tdcvxIntegerInput
On entry: the stride separating matrix column elements in the array cvx.
Constraint: ${\mathbf{tdcvx}}\ge {\mathbf{ng}}-1$.
19:   toldoubleInput
On entry: the value of tol is used to decide if the variables are of full rank and, if not, what is the rank of the variables. The smaller the value of tol the stricter the criterion for selecting the singular value decomposition. If a non-negative value of tol less than machine precision is entered, then the square root of machine precision is used instead.
Constraint: ${\mathbf{tol}}\ge 0.0$.
20:   irankxInteger *Output
On exit: the rank of the dependent variables.
If the variables are of full rank then ${\mathbf{irankx}}={\mathbf{nx}}$.
If the variables are not of full rank then irankx is an estimate of the rank of the dependent variables. irankx is calculated as the number of singular values greater than ${\mathbf{tol}}×$(largest singular value).
21:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_2_INT_ARG_LT
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$ while ${\mathbf{nx}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{m}}\ge {\mathbf{nx}}$.
On entry, ${\mathbf{tdcvm}}=〈\mathit{\text{value}}〉$ while ${\mathbf{nx}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{tdcvm}}\ge {\mathbf{nx}}$.
On entry, ${\mathbf{tdcvx}}=〈\mathit{\text{value}}〉$ while ${\mathbf{ng}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{tdcvx}}\ge {\mathbf{ng}}-1$.
On entry, ${\mathbf{tdx}}=〈\mathit{\text{value}}〉$ while ${\mathbf{m}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{tdx}}\ge {\mathbf{m}}$.
NE_3_INT_ARG_CONS
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$, ${\mathbf{nx}}=〈\mathit{\text{value}}〉$ and ${\mathbf{ng}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{n}}\ge {\mathbf{nx}}+{\mathbf{ng}}$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument weight had an illegal value.
NE_CANON_CORR_1
A canonical correlation is equal to one. This will happen if the variables provide an exact indication as to which group every observation is allocated.
NE_GROUPS
Either the effective number of groups is less than two or the effective number of groups plus the number of variables, nx is greater than the effective number of observations.
NE_INT_ARG_LT
On entry, ${\mathbf{ng}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ng}}\ge 2$.
On entry, ${\mathbf{nx}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nx}}\ge 1$.
On entry, ${\mathbf{tde}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{tde}}\ge 6$.
NE_INTARR_INT
On entry, ${\mathbf{ing}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$, ${\mathbf{ng}}=〈\mathit{\text{value}}〉$. Constraint: $1\le {\mathbf{ing}}\left[\mathit{i}-1\right]\le {\mathbf{ng}}$, for $\mathit{i}=1,2,\dots ,n$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_NEG_WEIGHT_ELEMENT
On entry, ${\mathbf{wt}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.
Constraint: When referenced, all elements of wt must be non-negative.
NE_RANK_ZERO
The rank of the variables is zero. This will happen if all the variables are constants.
NE_REAL_ARG_LT
On entry, tol must not be less than $0.0$: ${\mathbf{tol}}=〈\mathit{\text{value}}〉$.
NE_SVD_NOT_CONV
The singular value decomposition has failed to converge. This is an unlikely error exit.
NE_VAR_INCL_INDICATED
The number of variables, nx in the analysis $\text{}=〈\mathit{\text{value}}〉$, while number of variables included in the analysis via array ${\mathbf{isx}}=〈\mathit{\text{value}}〉$.
Constraint: these two numbers must be the same.
NE_WT_ARGS
The wt array argument must not be NULL when the weight argument indicates weights.

## 7  Accuracy

As the computation involves the use of orthogonal matrices and a singular value decomposition rather than the traditional computing of a sum of squares matrix and the use of an eigenvalue decomposition, nag_mv_canon_var (g03acc) should be less affected by ill conditioned problems.

None.

## 9  Example

A sample of nine observations, each consisting of three variables plus group indicator, is read in. There are three groups. An unweighted canonical variate analysis is performed and the results printed.

### 9.1  Program Text

Program Text (g03acce.c)

### 9.2  Program Data

Program Data (g03acce.d)

### 9.3  Program Results

Program Results (g03acce.r)