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_canon_var (g03ac)

Purpose

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

Syntax

[nig, cvm, e, ncv, cvx, irankx, ifail] = g03ac(weight, x, isx, nx, ing, ng, wt, tol, 'n', n, 'm', m)
[nig, cvm, e, ncv, cvx, irankx, ifail] = nag_mv_canon_var(weight, x, isx, nx, ing, ng, wt, tol, 'n', n, 'm', m)

Description

Let a sample of n$n$ observations on nx${n}_{x}$ variables in a data matrix come from ng${n}_{g}$ groups with n1,n2,,nng${n}_{1},{n}_{2},\dots ,{n}_{{n}_{g}}$ observations in each group, ni = n$\sum {n}_{i}=n$. Canonical variate analysis finds the linear combination of the nx${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 (g03ac) calculates the canonical variates by means of a singular value decomposition (SVD) of a matrix V$V$. Let the data matrix with variable (column) means subtracted be X$X$, and let its rank be k$k$; then the k$k$ by (ng1${n}_{g}-1$) matrix V$V$ is given by:
 V = QXT Qg , $V = QXT Qg ,$
where Qg${Q}_{g}$ is an n$n$ by (ng1)$\left({n}_{g}-1\right)$ orthogonal matrix that defines the groups and QX${Q}_{X}$ is the first k$k$ rows of the orthogonal matrix Q$Q$ either from the QR$QR$ decomposition of X$X$:
 X = QR $X=QR$
if X$X$ is of full column rank, i.e., k = nx$k={n}_{x}$, else from the SVD of X$X$:
 X = QDPT . $X=QDPT .$
Let the SVD of V$V$ be:
 V = Ux Δ UgT $V = Ux Δ UgT$
then the nonzero elements of the diagonal matrix Δ$\Delta$, δi${\delta }_{\mathit{i}}$, for i = 1,2,,l$\mathit{i}=1,2,\dots ,l$, are the l$l$ canonical correlations associated with the l = min (k,ng1) $l=\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(k,{n}_{g}-1\right)$ canonical variates, where l = min (k,ng) $l=\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(k,{n}_{g}\right)$.
The eigenvalues, λi2${\lambda }_{i}^{2}$, of the within-group sums of squares matrix are given by:
 λi2 = (δi2)/(1 − δi2) $λi2=δi2 1-δi2$
and the value of πi = λi2 / λi2${\pi }_{i}={\lambda }_{i}^{2}/\sum {\lambda }_{i}^{2}$ gives the proportion of variation explained by the i$i$th canonical variate. The values of the πi${\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$i$ the χ2${\chi }^{2}$ statistic:
 l (n − 1 − ng − (1/2)(k − ng)) ∑ log(1 + λj2) j = i + 1
$( n-1-ng-12(k-ng) ) ∑ j=i+1 l log( 1 + λj2 )$
can be used. This is asymptotically distributed as a χ2${\chi }^{2}$-distribution with (ki)(ng1i)$\left(k-i\right)\left({n}_{g}-1-i\right)$ degrees of freedom. If the test for i = h$i=h$ is not significant, then the remaining tests for i > h$i>h$ should be ignored.
The loadings for the canonical variates are calculated from the matrix Ux${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(wi)$\sqrt{{w}_{i}}$, where wi${w}_{i}$ is the weight for the i$i$th observation (row).

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 (1969) The Advanced Theory of Statistics (Volume 1) (3rd Edition) Griffin

Parameters

Compulsory Input Parameters

1:     weight – string (length ≥ 1)
Indicates if weights are to be used.
weight = 'U'${\mathbf{weight}}=\text{'U'}$
No weights are used.
weight = 'W'${\mathbf{weight}}=\text{'W'}$ or 'V'$\text{'V'}$
Weights are used and must be supplied in wt.
If weight = 'W'${\mathbf{weight}}=\text{'W'}$, the weights are treated as frequencies and the effective number of observations is the sum of the weights.
If weight = 'V'${\mathbf{weight}}=\text{'V'}$, 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: weight = 'U'${\mathbf{weight}}=\text{'U'}$, 'W'$\text{'W'}$ or 'V'$\text{'V'}$.
2:     x(ldx,m) – double array
ldx, the first dimension of the array, must satisfy the constraint ldxn$\mathit{ldx}\ge {\mathbf{n}}$.
x(i,j)${\mathbf{x}}\left(\mathit{i},\mathit{j}\right)$ must contain the i$\mathit{i}$th observation for the j$\mathit{j}$th variable, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$ and j = 1,2,,m$\mathit{j}=1,2,\dots ,m$.
3:     isx(m) – int64int32nag_int array
m, the dimension of the array, must satisfy the constraint mnx${\mathbf{m}}\ge {\mathbf{nx}}$.
isx(j)${\mathbf{isx}}\left(j\right)$ indicates whether or not the j$j$th variable is to be included in the analysis.
If isx(j) > 0${\mathbf{isx}}\left(\mathit{j}\right)>0$, the variables contained in the j$\mathit{j}$th column of x is included in the canonical variate analysis, for j = 1,2,,m$\mathit{j}=1,2,\dots ,m$.
Constraint: isx(j) > 0${\mathbf{isx}}\left(j\right)>0$ for nx values of j$j$.
4:     nx – int64int32nag_int scalar
The number of variables in the analysis, nx${n}_{x}$.
Constraint: nx1${\mathbf{nx}}\ge 1$.
5:     ing(n) – int64int32nag_int array
n, the dimension of the array, must satisfy the constraint nnx + ng${\mathbf{n}}\ge {\mathbf{nx}}+{\mathbf{ng}}$.
ing(i)${\mathbf{ing}}\left(\mathit{i}\right)$ indicates which group the i$\mathit{i}$th observation is in, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$. The effective number of groups is the number of groups with nonzero membership.
Constraint: 1ing(i)ng$1\le {\mathbf{ing}}\left(\mathit{i}\right)\le {\mathbf{ng}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
6:     ng – int64int32nag_int scalar
The number of groups, ng${n}_{g}$.
Constraint: ng2${\mathbf{ng}}\ge 2$.
7:     wt( : $:$) – double array
Note: the dimension of the array wt must be at least n${\mathbf{n}}$ if weight = 'W'${\mathbf{weight}}=\text{'W'}$ or 'V'$\text{'V'}$, and at least 1$1$ otherwise.
If weight = 'W'${\mathbf{weight}}=\text{'W'}$ or 'V'$\text{'V'}$, the first n$n$ elements of wt must contain the weights to be used in the analysis.
If wt(i) = 0.0${\mathbf{wt}}\left(i\right)=0.0$, the i$i$th observation is not included in the analysis.
If weight = 'U'${\mathbf{weight}}=\text{'U'}$, wt is not referenced.
Constraints:
• wt(i)0.0${\mathbf{wt}}\left(\mathit{i}\right)\ge 0.0$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$;
• 1nwt(i)nx + effective number of groups$\sum _{1}^{n}{\mathbf{wt}}\left(i\right)\ge {\mathbf{nx}}+\text{effective number of groups}$.
8:     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: tol0.0${\mathbf{tol}}\ge 0.0$.

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: nnx + ng${\mathbf{n}}\ge {\mathbf{nx}}+{\mathbf{ng}}$.
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.)
m$m$, the total number of variables.
Constraint: mnx${\mathbf{m}}\ge {\mathbf{nx}}$.

Input Parameters Omitted from the MATLAB Interface

ldx ldcvm lde ldcvx wk iwk

Output Parameters

1:     nig(ng) – int64int32nag_int array
nig(j)${\mathbf{nig}}\left(\mathit{j}\right)$ gives the number of observations in group j$\mathit{j}$, for j = 1,2,,ng$\mathit{j}=1,2,\dots ,{n}_{g}$.
2:     cvm(ldcvm,nx) – double array
ldcvmng$\mathit{ldcvm}\ge {\mathbf{ng}}$.
cvm(i,j)${\mathbf{cvm}}\left(\mathit{i},\mathit{j}\right)$ contains the mean of the j$\mathit{j}$th canonical variate for the i$\mathit{i}$th group, for i = 1,2,,ng$\mathit{i}=1,2,\dots ,{n}_{g}$ and j = 1,2,,l$\mathit{j}=1,2,\dots ,l$; the remaining columns, if any, are used as workspace.
3:     e(lde,6$6$) – double array
ldemin (nx,ng1)$\mathit{lde}\ge \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{nx}},{\mathbf{ng}}-1\right)$.
The statistics of the canonical variate analysis.
e(i,1)${\mathbf{e}}\left(i,1\right)$
The canonical correlations, δi${\delta }_{\mathit{i}}$, for i = 1,2,,l$\mathit{i}=1,2,\dots ,l$.
e(i,2)${\mathbf{e}}\left(i,2\right)$
The eigenvalues of the within-group sum of squares matrix, λi2${\lambda }_{\mathit{i}}^{2}$, for i = 1,2,,l$\mathit{i}=1,2,\dots ,l$.
e(i,3)${\mathbf{e}}\left(i,3\right)$
The proportion of variation explained by the i$\mathit{i}$th canonical variate, for i = 1,2,,l$\mathit{i}=1,2,\dots ,l$.
e(i,4)${\mathbf{e}}\left(i,4\right)$
The χ2${\chi }^{2}$ statistic for the i$\mathit{i}$th canonical variate, for i = 1,2,,l$\mathit{i}=1,2,\dots ,l$.
e(i,5)${\mathbf{e}}\left(i,5\right)$
The degrees of freedom for χ2${\chi }^{2}$ statistic for the i$\mathit{i}$th canonical variate, for i = 1,2,,l$\mathit{i}=1,2,\dots ,l$.
e(i,6)${\mathbf{e}}\left(i,6\right)$
The significance level for the χ2${\chi }^{2}$ statistic for the i$\mathit{i}$th canonical variate, for i = 1,2,,l$\mathit{i}=1,2,\dots ,l$.
4:     ncv – int64int32nag_int scalar
The number of canonical variates, l$l$. This will be the minimum of ng1${n}_{g}-1$ and the rank of x.
5:     cvx(ldcvx,ng1${\mathbf{ng}}-1$) – double array
ldcvxnx$\mathit{ldcvx}\ge {\mathbf{nx}}$.
The canonical variate loadings. cvx(i,j)${\mathbf{cvx}}\left(\mathit{i},\mathit{j}\right)$ contains the loading coefficient for the i$\mathit{i}$th variable on the j$\mathit{j}$th canonical variate, for i = 1,2,,nx$\mathit{i}=1,2,\dots ,{n}_{x}$ and j = 1,2,,l$\mathit{j}=1,2,\dots ,l$; the remaining columns, if any, are used as workspace.
6:     irankx – int64int32nag_int scalar
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 tol × (largest singular value)${\mathbf{tol}}×\text{(largest singular value)}$.
7:     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:

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

ifail = 1${\mathbf{ifail}}=1$
 On entry, nx < 1${\mathbf{nx}}<1$, or ng < 2${\mathbf{ng}}<2$, or m < nx${\mathbf{m}}<{\mathbf{nx}}$, or n < nx + ng${\mathbf{n}}<{\mathbf{nx}}+{\mathbf{ng}}$, or ldx < n$\mathit{ldx}<{\mathbf{n}}$, or ldcvx < nx$\mathit{ldcvx}<{\mathbf{nx}}$, or ldcvm < ng$\mathit{ldcvm}<{\mathbf{ng}}$, or lde < min (nx,ng − 1) $\mathit{lde}<\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{nx}},{\mathbf{ng}}-1\right)$, or nx ≥ ng − 1${\mathbf{nx}}\ge {\mathbf{ng}}-1$ and iwk < n × nx + max (5 × (nx − 1) + (nx + 1) × nx,n) $\mathit{iwk}<{\mathbf{n}}×{\mathbf{nx}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(5×\left({\mathbf{nx}}-1\right)+\left({\mathbf{nx}}+1\right)×{\mathbf{nx}},{\mathbf{n}}\right)$, or nx < ng − 1${\mathbf{nx}}<{\mathbf{ng}}-1$ and iwk < n × nx + max (5 × (nx − 1) + (ng − 1) × nx,n) $\mathit{iwk}<{\mathbf{n}}×{\mathbf{nx}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(5×\left({\mathbf{nx}}-1\right)+\left({\mathbf{ng}}-1\right)×{\mathbf{nx}},{\mathbf{n}}\right)$, or weight ≠ 'U'${\mathbf{weight}}\ne \text{'U'}$, 'W'$\text{'W'}$ or 'V'$\text{'V'}$, or tol < 0.0${\mathbf{tol}}<0.0$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, weight = 'W'${\mathbf{weight}}=\text{'W'}$ or 'V'$\text{'V'}$ and a value of wt < 0.0${\mathbf{wt}}<0.0$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, a value of ing < 1${\mathbf{ing}}<1$, or a value of ${\mathbf{ing}}>{\mathbf{ng}}$.
ifail = 4${\mathbf{ifail}}=4$
On entry, the number of variables to be included in the analysis as indicated by isx is not equal to nx.
ifail = 5${\mathbf{ifail}}=5$
A singular value decomposition has failed to converge. This is an unlikely error exit.
W ifail = 6${\mathbf{ifail}}=6$
A canonical correlation is equal to 1$1$. This will happen if the variables provide an exact indication as to which group every observation is allocated.
ifail = 7${\mathbf{ifail}}=7$
 On entry, less than two groups have nonzero membership, i.e., the effective number of groups is less than 2$2$, or the effective number of groups plus the number of variables, nx, is greater than the effective number of observations.
W ifail = 8${\mathbf{ifail}}=8$
The rank of the variables is 0$0$. This will happen if all the variables are constants.

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

None.

Example

```function nag_mv_canon_var_example
weight = 'U';
x = [13.3, 10.6, 21.2;
13.6, 10.2, 21;
14.2, 10.7, 21.1;
13.4, 9.4, 21;
13.2, 9.6, 20.1;
13.9, 10.4, 19.8;
12.9, 10, 20.5;
12.2, 9.9, 20.7;
13.9, 11, 19.1];
isx = [int64(1);1;1];
nx = int64(3);
ing = [int64(1);2;3;1;2;3;1;2;3];
ng = int64(3);
wt = [];
tol = 1e-06;
[nig, cvm, e, ncv, cvx, irankx, ifail] = nag_mv_canon_var(weight, x, isx, nx, ing, ng, wt, tol)
```
```

nig =

3
3
3

cvm =

0.9841    0.2797   -0.1653
1.1805   -0.2632    0.0177
-2.1646   -0.0164    0.1476

e =

0.8826    3.5238    0.9795    7.9032    6.0000    0.2453
0.2623    0.0739    0.0205    0.3564    2.0000    0.8368

ncv =

2

cvx =

-1.7070    0.7277
-1.3481    0.3138
0.9327    1.2199

irankx =

3

ifail =

0

```
```function g03ac_example
weight = 'U';
x = [13.3, 10.6, 21.2;
13.6, 10.2, 21;
14.2, 10.7, 21.1;
13.4, 9.4, 21;
13.2, 9.6, 20.1;
13.9, 10.4, 19.8;
12.9, 10, 20.5;
12.2, 9.9, 20.7;
13.9, 11, 19.1];
isx = [int64(1);1;1];
nx = int64(3);
ing = [int64(1);2;3;1;2;3;1;2;3];
ng = int64(3);
wt = [];
tol = 1e-06;
[nig, cvm, e, ncv, cvx, irankx, ifail] = g03ac(weight, x, isx, nx, ing, ng, wt, tol)
```
```

nig =

3
3
3

cvm =

0.9841    0.2797   -0.1653
1.1805   -0.2632    0.0177
-2.1646   -0.0164    0.1476

e =

0.8826    3.5238    0.9795    7.9032    6.0000    0.2453
0.2623    0.0739    0.0205    0.3564    2.0000    0.8368

ncv =

2

cvx =

-1.7070    0.7277
-1.3481    0.3138
0.9327    1.2199

irankx =

3

ifail =

0

```