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_anova_hier2 (g04ag)

## Purpose

nag_anova_hier2 (g04ag) performs an analysis of variance for a two-way hierarchical classification with subgroups of possibly unequal size, and also computes the treatment group and subgroup means. A fixed effects model is assumed.

## Syntax

[ngp, gbar, sgbar, gm, ss, idf, f, fp, ifail] = g04ag(y, lsub, nobs, 'n', n, 'k', k, 'l', l)
[ngp, gbar, sgbar, gm, ss, idf, f, fp, ifail] = nag_anova_hier2(y, lsub, nobs, 'n', n, 'k', k, 'l', l)

## Description

In a two-way hierarchical classification, there are $k$ ($\text{}\ge 2$) treatment groups, the $i$th of which is subdivided into ${l}_{i}$ treatment subgroups. The $j$th subgroup of group $i$ contains ${n}_{ij}$ observations, which may be denoted by
 $y1ij,y2ij,…,ynijij.$
The general observation is denoted by ${y}_{mij}$, being the $m$th observation in subgroup $j$ of group $i$, for $1\le i\le k$, $1\le j\le {l}_{i}$, $1\le m\le {n}_{ij}$.
The following quantities are computed
(i) The subgroup means
 $y-.ij=∑m=1nijymijnij$
(ii) The group means
 $y-.i.=∑j= 1li∑m= 1nijymij ∑j= 1linij$
(iii) The grand mean
 $y-…=∑i=1k∑j=1li∑m=1nijymij ∑i=1k∑j=1linij$
(iv) The number of observations in each group
 $ni.= ∑j= 1 li nij$
(v) Sums of squares
 $Between groups =SSg = ∑ i=1 k ni. y-.i.-y-… 2 Between subgroups within groups =SSsg= ∑ i=1 k ∑ j=1 li nij y.ij-y-.i. 2 Residual (within subgroups) =SSres= ∑ i=1 k ∑ j=1 li ∑ m=1 nij ymij-y-.ij 2 =SStot-SSg-SSsg Corrected total =SStot= ∑ i=1 k ∑ j=1 li ∑ m=1 nij ymij-y-… 2$
(vi) Degrees of freedom of variance components
 Between groups: $k-1$ Subgroups within groups: $l-k$ Residual: $n-l$ Total: $n-1$
where
• $l=\sum _{i=1}^{k}{l}_{i}$,
• $n=\sum _{i=1}^{k}{n}_{i.}$
(vii) $F$ ratios. These are the ratios of the group and subgroup mean squares to the residual mean square.
 Groups ${F}_{1}=\frac{\text{Between groups sum of squares}/\left(k-1\right)}{\text{Residual sum of squares}/\left(n-l\right)}=\frac{{\mathrm{SS}}_{g}/\left(k-1\right)}{{\mathrm{SS}}_{\mathrm{res}}/\left(n-l\right)}$ Subgroups ${F}_{2}=\frac{\text{Between subgroups (within group) sum of squares}/\left(l-k\right)}{\text{Residual sum of squares}/\left(n-l\right)}=\frac{{\mathrm{SS}}_{sg}/\left(l-k\right)}{{\mathrm{SS}}_{\mathrm{res}}/\left(n-l\right)}$
If either $F$ ratio exceeds $9999.0$, the value $9999.0$ is assigned instead.
(viii) f significances. The probability of obtaining a value from the appropriate $F$-distribution which exceeds the computed mean square ratio.
 Groups ${p}_{1}=\mathrm{Prob}\left({F}_{\left(k-1\right),\left(n-l\right)}>{F}_{1}\right)$ Subgroups ${p}_{2}=\mathrm{Prob}\left({F}_{\left(l-k\right),\left(n-l\right)}>{F}_{2}\right)$
where ${F}_{{\nu }_{1},{\nu }_{2}}$ denotes the central $F$-distribution with degrees of freedom ${\nu }_{1}$ and ${\nu }_{2}$.
If any ${F}_{i}=9999.0$, then ${p}_{i}$ is set to zero, $i=1,2$.

## References

Kendall M G and Stuart A (1976) The Advanced Theory of Statistics (Volume 3) (3rd Edition) Griffin
Moore P G, Shirley E A and Edwards D E (1972) Standard Statistical Calculations Pitman

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{y}\left({\mathbf{n}}\right)$ – double array
The elements of y must contain the observations ${y}_{\mathrm{mij}}$ in the following order:
 $y111,y211,…,yn1111,y112,y212,…,yn1212,…,y11l1,…,$
 $yn1l11l1,…, y1ij,…,ynijij,…,y1klk,…, ynklkklk.$
In words, the ordering is by group, and within each group is by subgroup, the members of each subgroup being in consecutive locations in y.
2:     $\mathrm{lsub}\left({\mathbf{k}}\right)$int64int32nag_int array
The number of subgroups within group $\mathit{i}$, ${l}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,k$.
Constraint: ${\mathbf{lsub}}\left(\mathit{i}\right)>0$, for $\mathit{i}=1,2,\dots ,k$.
3:     $\mathrm{nobs}\left({\mathbf{l}}\right)$int64int32nag_int array
The numbers of observations in each subgroup, ${n}_{ij}$, in the following order:
 $n11,n12,…,n1l1,n21,…,n2l2,…,nk1,…,nklk$
Constraint: $n=\sum _{\mathit{i}=1}^{k}\sum _{j=1}^{{l}_{i}}{n}_{\mathit{i}j}$, that is ${\mathbf{n}}=\sum _{\mathit{i}=1}^{l}{\mathbf{nobs}}\left(\mathit{i}\right)$ and ${\mathbf{nobs}}\left(\mathit{i}\right)>0$, for $\mathit{i}=1,2,\dots ,l$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array y.
$n$, the total number of observations.
2:     $\mathrm{k}$int64int32nag_int scalar
Default: the dimension of the array lsub.
$k$, the number of groups.
Constraint: ${\mathbf{k}}\ge 2$.
3:     $\mathrm{l}$int64int32nag_int scalar
Default: the dimension of the array nobs.
$l$, the total number of subgroups.
Constraint: ${\mathbf{l}}=\sum _{i=1}^{k}{\mathbf{lsub}}\left(i\right)$.

### Output Parameters

1:     $\mathrm{ngp}\left({\mathbf{k}}\right)$int64int32nag_int array
The total number of observations in group $\mathit{i}$, ${n}_{\mathit{i}.}$, for $\mathit{i}=1,2,\dots ,k$.
2:     $\mathrm{gbar}\left({\mathbf{k}}\right)$ – double array
The mean for group $\mathit{i}$, ${\stackrel{-}{y}}_{.\mathit{i}.}$, for $\mathit{i}=1,2,\dots ,k$.
3:     $\mathrm{sgbar}\left({\mathbf{l}}\right)$ – double array
The subgroup means, ${\stackrel{-}{y}}_{.ij}$, in the following order:
 $y-.11,y-.12,…,y-.1l1,y-.21,y-.22,…,y-.2l2,…,y-.k1,y-.k2,…,y-.klk.$
4:     $\mathrm{gm}$ – double scalar
The grand mean, ${\stackrel{-}{y}}_{\dots }$.
5:     $\mathrm{ss}\left(4\right)$ – double array
Contains the sums of squares for the analysis of variance, as follows;
• ${\mathbf{ss}}\left(1\right)=\text{}$ Between group sum of squares, ${{\mathbf{ss}}}_{g}$,
• ${\mathbf{ss}}\left(2\right)=\text{}$ Between subgroup within groups sum of squares, ${{\mathbf{ss}}}_{sg}$,
• ${\mathbf{ss}}\left(3\right)=\text{}$ Residual sum of squares, ${{\mathbf{ss}}}_{\mathrm{res}}$,
• ${\mathbf{ss}}\left(4\right)=\text{}$ Corrected total sum of squares, ${{\mathbf{ss}}}_{\mathrm{tot}}$.
6:     $\mathrm{idf}\left(4\right)$int64int32nag_int array
Contains the degrees of freedom attributable to each sum of squares in the analysis of variance, as follows:
• ${\mathbf{idf}}\left(1\right)=\text{}$ Degrees of freedom for between group sum of squares,
• ${\mathbf{idf}}\left(2\right)=\text{}$ Degrees of freedom for between subgroup within groups sum of squares,
• ${\mathbf{idf}}\left(3\right)=\text{}$ Degrees of freedom for residual sum of squares,
• ${\mathbf{idf}}\left(4\right)=\text{}$ Degrees of freedom for corrected total sum of squares.
7:     $\mathrm{f}\left(2\right)$ – double array
Contains the mean square ratios, ${F}_{1}$ and ${F}_{2}$, for the between groups variation, and the between subgroups within groups variation, with respect to the residual, respectively.
8:     $\mathrm{fp}\left(2\right)$ – double array
Contains the significances of the mean square ratios, ${p}_{1}$ and ${p}_{2}$ respectively.
9:     $\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{k}}\le 1$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{lsub}}\left(i\right)\le 0$, for some $i=1,2,\dots ,k$.
${\mathbf{ifail}}=3$
 On entry, ${\mathbf{l}}\ne \sum _{i=1}^{k}{\mathbf{lsub}}\left(i\right)$
${\mathbf{ifail}}=4$
 On entry, ${\mathbf{nobs}}\left(i\right)\le 0$, for some $i=1,2,\dots ,l$.
${\mathbf{ifail}}=5$
 On entry, ${\mathbf{n}}\ne \sum _{i=1}^{l}{\mathbf{nobs}}\left(i\right)$.
W  ${\mathbf{ifail}}=6$
The total corrected sum of squares is zero, indicating that all the data values are equal. The means returned are therefore all equal, and the sums of squares are zero. No assignments are made to idf, f, and fp.
W  ${\mathbf{ifail}}=7$
The residual sum of squares is zero. This arises when either each subgroup contains exactly one observation, or the observations within each subgroup are equal. The means, sums of squares, and degrees of freedom are computed, but no assignments are made to f and fp.
${\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

The computations are believed to be stable.

The time taken by nag_anova_hier2 (g04ag) increases approximately linearly with the total number of observations, $n$.

## Example

This example has two groups, the first of which consists of five subgroups, and the second of three subgroups. The numbers of observations in each subgroup are not equal. The data represent the percentage stretch in the length of samples of sack kraft drawn from consignments (subgroups) received over two years (groups). For details see Moore et al. (1972).
```function g04ag_example

fprintf('g04ag example results\n\n');

% Observations in two groups with 5 and 3 sub-groups respectively
y = [2.1     2.4     2       2       2   ...
2.4     2.1     2.2     ...
2.4     2.2     2.6     ...
2.4     2.4     2.5     ...
1.9     1.7     ...
...
2.1     1.5     2       ...
1.9     1.7     1.9     1.9     1.9  ...
2       2.1     2.3];
k    = 2;
lsub = [int64(5);                 3];
nobs = [int64(5); 3; 3; 3; 2;     3;5;3];
n = sum(nobs);

fprintf('Data values\n\n Group  Subgroup  Observations\n');
nsub = 0;
nlo  = 1;
for i = 1:k
for j = 1:lsub(i)
nsub = nsub + 1;
nhi = nlo + nobs(nsub) - 1;
fprintf('%5d%9d   ', i, j);
fprintf('%4.1f',y(nlo:nhi));
fprintf('\n');
nlo = nhi + 1;
end
end

% Perform ANOVA
[ngp, gbar, sgbar, gm, ss, idf, f, fp, ifail] = ...
g04ag(y, lsub, nobs);

% Display results
fprintf('\nSubgroup means\n\n');
fprintf('   Group  Subgroup  Mean\n');
ii = 0;
for i = 1:k
for j = 1:lsub(i)
ii = ii + 1;
fprintf('%6d%8d%10.2f\n', i, j, sgbar(ii));
end
end
fprintf('\n');
fprintf('%s%5.2f%s%2d%s\n', '    Group 1 mean =', gbar(1), ...
'   (', ngp(1), ' observations)');
fprintf('%s%5.2f%s%2d%s\n', '    Group 2 mean =', gbar(2), ...
'   (', ngp(2), ' observations)');
fprintf('%s%5.2f%s%2d%s\n', '    Grand mean   =', gm, ...
'   (', n, ' observations)');
fprintf('\nAnalysis of variance table\n\n');
fprintf('   Source                SS    DF  F ratio  Sig\n\n');
fprintf('%s%6.3f%5d%7.2f%8.3f\n', 'Between groups        ', ...
ss(1), idf(1), f(1), fp(1));
fprintf('%s%6.3f%5d%7.2f%8.3f\n', 'Between subgroups     ', ...
ss(2), idf(2), f(2), fp(2));
fprintf('%s%6.3f%5d\n', 'Residual              ', ss(3), idf(3));
fprintf('\n%s%6.3f%5d\n', 'Total                 ', ss(4), idf(4));

```
```g04ag example results

Data values

Group  Subgroup  Observations
1        1    2.1 2.4 2.0 2.0 2.0
1        2    2.4 2.1 2.2
1        3    2.4 2.2 2.6
1        4    2.4 2.4 2.5
1        5    1.9 1.7
2        1    2.1 1.5 2.0
2        2    1.9 1.7 1.9 1.9 1.9
2        3    2.0 2.1 2.3

Subgroup means

Group  Subgroup  Mean
1       1      2.10
1       2      2.23
1       3      2.40
1       4      2.43
1       5      1.80
2       1      1.87
2       2      1.86
2       3      2.13

Group 1 mean = 2.21   (16 observations)
Group 2 mean = 1.94   (11 observations)
Grand mean   = 2.10   (27 observations)

Analysis of variance table

Source                SS    DF  F ratio  Sig

Between groups         0.475    1  16.15   0.001
Between subgroups      0.816    6   4.63   0.005
Residual               0.559   19

Total                  1.850   26
```