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

## Purpose

nag_mv_canon_corr (g03ad) performs canonical correlation analysis upon input data matrices.

## Syntax

[e, ncv, cvx, cvy, ifail] = g03ad(z, isz, nx, ny, mcv, tol, 'n', n, 'm', m, 'wt', wt)
[e, ncv, cvx, cvy, ifail] = nag_mv_canon_corr(z, isz, nx, ny, mcv, tol, '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 At Mark 22: n was made optional

## Description

Let there be two sets of variables, $x$ and $y$. For a sample of $n$ observations on ${n}_{x}$ variables in a data matrix $X$ and ${n}_{y}$ variables in a data matrix $Y$, canonical correlation analysis seeks to find a small number of linear combinations of each set of variables in order to explain or summarise the relationships between them. The variables thus formed are known as canonical variates.
Let the variance-covariance matrix of the two datasets be
 $Sxx Sxy Syx Syy$
and let
 $Σ=Syy -1SyxSxx -1Sxy$
then the canonical correlations can be calculated from the eigenvalues of the matrix $\Sigma$. However, nag_mv_canon_corr (g03ad) calculates the canonical correlations by means of a singular value decomposition (SVD) of a matrix $V$. If the rank of the data matrix $X$ is ${k}_{x}$ and the rank of the data matrix $Y$ is ${k}_{y}$, and both $X$ and $Y$ have had variable (column) means subtracted then the ${k}_{x}$ by ${k}_{y}$ matrix $V$ is given by:
 $V=QxTQy,$
where ${Q}_{x}$ is the first ${k}_{x}$ columns of the orthogonal matrix $Q$ either from the $QR$ decomposition of $X$ if $X$ is of full column rank, i.e., ${k}_{x}={n}_{x}$:
 $X=QxRx$
or from the SVD of $X$ if ${k}_{x}<{n}_{x}$:
 $X=QxDxPxT.$
Similarly ${Q}_{y}$ is the first ${k}_{y}$ columns of the orthogonal matrix $Q$ either from the $QR$ decomposition of $Y$ if $Y$ is of full column rank, i.e., ${k}_{y}={n}_{y}$:
 $Y=QyRy$
or from the SVD of $Y$ if ${k}_{y}<{n}_{y}$:
 $Y=QyDyPyT.$
Let the SVD of $V$ be:
 $V=UxΔUyT$
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}_{x},{k}_{y}\right)$.
The eigenvalues, ${\lambda }_{i}^{2}$, of the matrix $\Sigma$ are given by:
 $λi2 = δi2 .$
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 - 12 kx + ky + 3 ∑ j=i+1 l log 1 - δj2$
can be used. This is asymptotically distributed as a ${\chi }^{2}$-distribution with $\left({k}_{x}-i\right)\left({k}_{y}-i\right)$ degrees of freedom. If the test for $i={k}_{\mathrm{min}}$ is not significant, then the remaining tests for $i>{k}_{\mathrm{min}}$ should be ignored.
The loadings for the canonical variates are calculated from the matrices ${U}_{x}$ and ${U}_{y}$ respectively. These matrices are scaled so that the canonical variates have unit variance.

## References

Hastings N A J and Peacock J B (1975) Statistical Distributions Butterworth
Kendall M G and Stuart A (1976) The Advanced Theory of Statistics (Volume 3) (3rd Edition) Griffin
Morrison D F (1967) Multivariate Statistical Methods McGraw–Hill

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{z}\left(\mathit{ldz},{\mathbf{m}}\right)$ – double array
ldz, the first dimension of the array, must satisfy the constraint $\mathit{ldz}\ge {\mathbf{n}}$.
${\mathbf{z}}\left(\mathit{i},\mathit{j}\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$.
Both $x$ and $y$ variables are to be included in z, the indicator array, isz, being used to assign the variables in z to the $x$ or $y$ sets as appropriate.
2:     $\mathrm{isz}\left({\mathbf{m}}\right)$int64int32nag_int array
${\mathbf{isz}}\left(j\right)$ indicates whether or not the $j$th variable is included in the analysis and to which set of variables it belongs.
${\mathbf{isz}}\left(j\right)>0$
The variable contained in the $j$th column of z is included as an $x$ variable in the analysis.
${\mathbf{isz}}\left(j\right)<0$
The variable contained in the $j$th column of z is included as a $y$ variable in the analysis.
${\mathbf{isz}}\left(j\right)=0$
The variable contained in the $j$th column of z is not included in the analysis.
Constraint: only nx elements of isz can be $\text{}>0$ and only ny elements of isz can be $\text{}<0$.
3:     $\mathrm{nx}$int64int32nag_int scalar
The number of $x$ variables in the analysis, ${n}_{x}$.
Constraint: ${\mathbf{nx}}\ge 1$.
4:     $\mathrm{ny}$int64int32nag_int scalar
The number of $y$ variables in the analysis, ${n}_{y}$.
Constraint: ${\mathbf{ny}}\ge 1$.
5:     $\mathrm{mcv}$int64int32nag_int scalar
An upper limit to the number of canonical variates.
Constraint: ${\mathbf{mcv}}\ge \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{nx}},{\mathbf{ny}}\right)$.
6:     $\mathrm{tol}$ – double scalar
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, the square root of machine precision is used instead.
Constraint: ${\mathbf{tol}}\ge 0.0$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array wt and the first dimension of the array z. (An error is raised if these dimensions are not equal.)
$n$, the number of observations.
Constraint: ${\mathbf{n}}>{\mathbf{nx}}+{\mathbf{ny}}$.
2:     $\mathrm{m}$int64int32nag_int scalar
Default: the dimension of the array isz and the second dimension of the array z. (An error is raised if these dimensions are not equal.)
$m$, the total number of variables.
Constraint: ${\mathbf{m}}\ge {\mathbf{nx}}+{\mathbf{ny}}$.
3:     $\mathrm{wt}\left(:\right)$ – double array
The dimension of the array wt must be at least ${\mathbf{n}}$ if $\mathit{weight}=\text{'W'}$, and at least $1$ otherwise
If $\mathit{weight}=\text{'W'}$, the first $n$ elements of wt must contain the weights to be used in the analysis.
If ${\mathbf{wt}}\left(i\right)=0.0$, the $i$th observation is not included in the analysis. The effective number of observations is the sum of weights.
If $\mathit{weight}=\text{'U'}$, wt is not referenced and the effective number of observations is $n$.
Constraints:
• ${\mathbf{wt}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,n$;
• the $\text{sum of weights}\ge {\mathbf{nx}}+{\mathbf{ny}}+1$.

### Output Parameters

1:     $\mathrm{e}\left(\mathit{lde},6\right)$ – double array
The statistics of the canonical variate analysis.
${\mathbf{e}}\left(i,1\right)$
The canonical correlations, ${\delta }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{e}}\left(i,2\right)$
The eigenvalues of $\Sigma$, ${\lambda }_{\mathit{i}}^{2}$, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{e}}\left(i,3\right)$
The proportion of variation explained by the $\mathit{i}$th canonical variate, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{e}}\left(i,4\right)$
The ${\chi }^{2}$ statistic for the $\mathit{i}$th canonical variate, for $\mathit{i}=1,2,\dots ,l$.
${\mathbf{e}}\left(i,5\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(i,6\right)$
The significance level for the ${\chi }^{2}$ statistic for the $\mathit{i}$th canonical variate, for $\mathit{i}=1,2,\dots ,l$.
2:     $\mathrm{ncv}$int64int32nag_int scalar
The number of canonical correlations, $l$. This will be the minimum of the rank of $\mathrm{X}$ and the rank of $\mathrm{Y}$.
3:     $\mathrm{cvx}\left(\mathit{ldcvx},{\mathbf{mcv}}\right)$ – double array
The canonical variate loadings for the $x$ variables. ${\mathbf{cvx}}\left(i,j\right)$ contains the loading coefficient for the $i$th $x$ variable on the $j$th canonical variate.
4:     $\mathrm{cvy}\left(\mathit{ldcvy},{\mathbf{mcv}}\right)$ – double array
The canonical variate loadings for the $y$ variables. ${\mathbf{cvy}}\left(i,j\right)$ contains the loading coefficient for the $i$th $y$ variable on the $j$th canonical variate.
5:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

${\mathbf{ifail}}=1$
 On entry, ${\mathbf{nx}}<1$, or ${\mathbf{ny}}<1$, or ${\mathbf{m}}<{\mathbf{nx}}+{\mathbf{ny}}$, or ${\mathbf{n}}\le {\mathbf{nx}}+{\mathbf{ny}}$, or ${\mathbf{mcv}}<\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{nx}},{\mathbf{ny}}\right)$, or $\mathit{ldz}<{\mathbf{n}}$, or $\mathit{ldcvx}<{\mathbf{nx}}$, or $\mathit{ldcvy}<{\mathbf{ny}}$, or $\mathit{lde}<\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{nx}},{\mathbf{ny}}\right)$, or ${\mathbf{nx}}\ge {\mathbf{ny}}$ and $\mathit{iwk}<{\mathbf{n}}×{\mathbf{nx}}+{\mathbf{nx}}+{\mathbf{ny}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(\left(5×\left({\mathbf{nx}}-1\right)+{\mathbf{nx}}×{\mathbf{nx}}\right),{\mathbf{n}}×{\mathbf{ny}}\right)$, or ${\mathbf{nx}}<{\mathbf{ny}}$ and $\mathit{iwk}<{\mathbf{n}}×{\mathbf{ny}}+{\mathbf{nx}}+{\mathbf{ny}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(\left(5×\left({\mathbf{ny}}-1\right)+{\mathbf{ny}}×{\mathbf{ny}}\right),{\mathbf{n}}×{\mathbf{nx}}\right)$, or $\mathit{weight}\ne \text{'U'}$ or $\text{'W'}$, or ${\mathbf{tol}}<0.0$.
${\mathbf{ifail}}=2$
 On entry, a $\mathit{weight}=\text{'W'}$ and value of ${\mathbf{wt}}<0.0$.
${\mathbf{ifail}}=3$
 On entry, the number of $x$ variables to be included in the analysis as indicated by isz is not equal to nx. or the number of $y$ variables to be included in the analysis as indicated by isz is not equal to ny.
${\mathbf{ifail}}=4$
 On entry, the effective number of observations is less than ${\mathbf{nx}}+{\mathbf{ny}}+1$.
${\mathbf{ifail}}=5$
A singular value decomposition has failed to converge. See nag_eigen_real_triang_svd (f02wu). This is an unlikely error exit.
W  ${\mathbf{ifail}}=6$
A canonical correlation is equal to $1$. This will happen if the $x$ and $y$ variables are perfectly correlated.
W  ${\mathbf{ifail}}=7$
On entry, the rank of the $X$ matrix or the rank of the $Y$ matrix is $0$. This will happen if all the $x$ or $y$ variables are constants.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## 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_corr (g03ad) should be less affected by ill-conditioned problems.

None.

## Example

This example has nine observations and two variables in each set of the four variables read in, the second and third are $x$ variables while the first and last are $y$ variables. Canonical variate analysis is performed and the results printed.
```function g03ad_example

z   = [80, 58.4, 14.0, 21;
75, 59.2, 15.0, 27;
78, 60.3, 15.0, 27;
75, 57.4, 13.0, 22;
79, 59.5, 14.0, 26;
78, 58.1, 14.5, 26;
75, 58.0, 12.5, 23;
64, 55.5, 11.0, 22;
80, 59.2, 12.5, 22];
isz = [int64(-1);1;1;-1];
nx  = int64(2);
ny  = nx;
mcv = nx;
tol = 1e-06;

[e, ncv, cvx, cvy, ifail] = ...
z, isz, nx, ny, mcv, tol);

fprintf('Rank of x = %d, Rank of y = %d\n\n', nx, ny);
fprintf('Canonical    Eigenvalues Percentage     Chisq      DF     Sig\n');
fprintf('correlations              variation\n');
fprintf('%11.4f%12.4f%12.4f%10.4f%8.1f%8.4f\n',e');
fprintf('\n');

mtitle = 'Canonical Coefficients for x';
matrix = 'General';
diag   = ' ';
[ifail] = x04ca( ...
matrix, diag, cvx, mtitle);

fprintf('\n');
mtitle = 'Canonical Coefficients for y';
[ifail] = x04ca( ...
matrix, diag, cvy, mtitle);

```
```g03ad example results

Rank of x = 2, Rank of y = 2

Canonical    Eigenvalues Percentage     Chisq      DF     Sig
correlations              variation
0.9570      0.9159      0.8746   14.3914     4.0  0.0061
0.3624      0.1313      0.1254    0.7744     1.0  0.3789

Canonical Coefficients for x
1          2
1     -0.4261     1.0337
2     -0.3444    -1.1136

Canonical Coefficients for y
1       2
1  -0.1415  0.1504
2  -0.2384 -0.3424
```