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$k$ (2$\text{}\ge 2$) treatment groups, the i$i$th of which is subdivided into li${l}_{i}$ treatment subgroups. The j$j$th subgroup of group i$i$ contains nij${n}_{ij}$ observations, which may be denoted by
 y1ij,y2ij, … ,ynijij. $y1ij,y2ij,…,ynijij.$
The general observation is denoted by ymij${y}_{mij}$, being the m$m$th observation in subgroup j$j$ of group i$i$, for 1ik$1\le i\le k$, 1jli$1\le j\le {l}_{i}$, 1mnij$1\le m\le {n}_{ij}$.
The following quantities are computed
(i) The subgroup means
 y . ij = ( ∑ m = 1nijymij)/(nij) $y-.ij=∑m=1nijymijnij$
(ii) The group means
 y . i . = ( ∑ j = 1li ∑ m = 1nijymij)/( ∑ j = 1linij) $y-.i.=∑j= 1li∑m= 1nijymij ∑j= 1linij$
(iii) The grand mean
 y… = ( ∑ i = 1k ∑ j = 1li ∑ m = 1nijymij)/( ∑ i = 1k ∑ j = 1linij) $y-…=∑i=1k∑j=1li∑m=1nijymij ∑i=1k∑j=1linij$
(iv) The number of observations in each group
 li ni . = ∑ nij j = 1
$ni.= ∑j= 1 li nij$
(v) Sums of squares
Between groups
 k = SSg = ∑ ni . (y . i . − y…)2 i = 1
Between subgroups within groups
 k li = SSsg = ∑ ∑ nij(y . ij − y . i . )2 i = 1 j = 1
Residual (within subgroups)
 k li nij = SSres = ∑ ∑ ∑ (ymij − y . ij)2 = SStot − SSg − SSsg i = 1 j = 1 m = 1
Corrected total
 k li nij = SStot = ∑ ∑ ∑ (ymij − y…)2 i = 1 j = 1 m = 1
$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$k-1$ Subgroups within groups: l − k$l-k$ Residual: n − l$n-l$ Total: n − 1$n-1$
where
• l = i = 1kli$l=\sum _{i=1}^{k}{l}_{i}$,
• n = i = 1kni . $n=\sum _{i=1}^{k}{n}_{i.}$
(vii) F$F$ ratios. These are the ratios of the group and subgroup mean squares to the residual mean square.
 Groups F1 = (Between groups sum of squares / (k − 1))/(Residual sum of squares / (n − l)) = (SSg / (k − 1))/(SSres / (n − l)) ${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 F2 = (Between subgroups (within group) sum of squares / (l − k))/(Residual sum of squares / (n − l)) = (SSsg / (l − k))/(SSres / (n − l)) ${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$F$ ratio exceeds 9999.0$9999.0$, the value 9999.0$9999.0$ is assigned instead.
(viii) f significances. The probability of obtaining a value from the appropriate F$F$-distribution which exceeds the computed mean square ratio.
 Groups p1 = Prob (F (k − 1) , (n − l) > F1) ${p}_{1}=\mathrm{Prob}\left({F}_{\left(k-1\right),\left(n-l\right)}>{F}_{1}\right)$ Subgroups p2 = Prob (F (l − k) , (n − l) > F2) ${p}_{2}=\mathrm{Prob}\left({F}_{\left(l-k\right),\left(n-l\right)}>{F}_{2}\right)$
where Fν1,ν2${F}_{{\nu }_{1},{\nu }_{2}}$ denotes the central F$F$-distribution with degrees of freedom ν1${\nu }_{1}$ and ν2${\nu }_{2}$.
If any Fi = 9999.0${F}_{i}=9999.0$, then pi${p}_{i}$ is set to zero, i = 1,2$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:     y(n) – double array
The elements of y must contain the observations ymij${y}_{\mathrm{mij}}$ in the following order:
 y111,y211, … ,yn1111,y112,y212, … ,yn1212, … ,y11l1, … , $y111,y211,…,yn1111,y112,y212,…,yn1212,…,y11l1,…,$
 yn1l11l1, … , y1ij, … ,ynijij, … ,y1klk, … , ynklkklk. $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:     lsub(k) – int64int32nag_int array
k, the dimension of the array, must satisfy the constraint k2${\mathbf{k}}\ge 2$.
The number of subgroups within group i$\mathit{i}$, li${l}_{\mathit{i}}$, for i = 1,2,,k$\mathit{i}=1,2,\dots ,k$.
Constraint: lsub(i) > 0${\mathbf{lsub}}\left(\mathit{i}\right)>0$, for i = 1,2,,k$\mathit{i}=1,2,\dots ,k$.
3:     nobs(l) – int64int32nag_int array
l, the dimension of the array, must satisfy the constraint l = i = 1klsub(i)${\mathbf{l}}=\sum _{i=1}^{k}{\mathbf{lsub}}\left(i\right)$.
The numbers of observations in each subgroup, nij${n}_{ij}$, in the following order:
 n11,n12, … ,n1l1,n21, … ,n2l2, … ,nk1, … ,nklk $n11,n12,…,n1l1,n21,…,n2l2,…,nk1,…,nklk$
Constraint: n = i = 1kj = 1linij$n=\sum _{\mathit{i}=1}^{k}\sum _{j=1}^{{l}_{i}}{n}_{\mathit{i}j}$, that is n = i = 1lnobs(i)${\mathbf{n}}=\sum _{\mathit{i}=1}^{l}{\mathbf{nobs}}\left(\mathit{i}\right)$ and nobs(i) > 0${\mathbf{nobs}}\left(\mathit{i}\right)>0$, for i = 1,2,,l$\mathit{i}=1,2,\dots ,l$.

### Optional Input Parameters

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

None.

### Output Parameters

1:     ngp(k) – int64int32nag_int array
The total number of observations in group i$\mathit{i}$, ni . ${n}_{\mathit{i}.}$, for i = 1,2,,k$\mathit{i}=1,2,\dots ,k$.
2:     gbar(k) – double array
The mean for group i$\mathit{i}$, y . i . ${\stackrel{-}{y}}_{.\mathit{i}.}$, for i = 1,2,,k$\mathit{i}=1,2,\dots ,k$.
3:     sgbar(l) – double array
The subgroup means, y . ij${\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. $y-.11,y-.12,…,y-.1l1,y-.21,y-.22,…,y-.2l2,…,y-.k1,y-.k2,…,y-.klk.$
4:     gm – double scalar
The grand mean, y${\stackrel{-}{y}}_{\dots }$.
5:     ss(4$4$) – double array
Contains the sums of squares for the analysis of variance, as follows;
• ss(1) = ${\mathbf{ss}}\left(1\right)=\text{}$ Between group sum of squares, ssg${{\mathbf{ss}}}_{g}$,
• ss(2) = ${\mathbf{ss}}\left(2\right)=\text{}$ Between subgroup within groups sum of squares, sssg${{\mathbf{ss}}}_{sg}$,
• ss(3) = ${\mathbf{ss}}\left(3\right)=\text{}$ Residual sum of squares, ssres${{\mathbf{ss}}}_{\mathrm{res}}$,
• ss(4) = ${\mathbf{ss}}\left(4\right)=\text{}$ Corrected total sum of squares, sstot${{\mathbf{ss}}}_{\mathrm{tot}}$.
6:     idf(4$4$) – int64int32nag_int array
Contains the degrees of freedom attributable to each sum of squares in the analysis of variance, as follows:
• idf(1) = ${\mathbf{idf}}\left(1\right)=\text{}$ Degrees of freedom for between group sum of squares,
• idf(2) = ${\mathbf{idf}}\left(2\right)=\text{}$ Degrees of freedom for between subgroup within groups sum of squares,
• idf(3) = ${\mathbf{idf}}\left(3\right)=\text{}$ Degrees of freedom for residual sum of squares,
• idf(4) = ${\mathbf{idf}}\left(4\right)=\text{}$ Degrees of freedom for corrected total sum of squares.
7:     f(2$2$) – double array
Contains the mean square ratios, F1${F}_{1}$ and F2${F}_{2}$, for the between groups variation, and the between subgroups within groups variation, with respect to the residual, respectively.
8:     fp(2$2$) – double array
Contains the significances of the mean square ratios, p1${p}_{1}$ and p2${p}_{2}$ respectively.
9:     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, k ≤ 1${\mathbf{k}}\le 1$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, lsub(i) ≤ 0${\mathbf{lsub}}\left(i\right)\le 0$, for some i = 1,2, … ,k$i=1,2,\dots ,k$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, l ≠ ∑ i = 1klsub(i)${\mathbf{l}}\ne \sum _{i=1}^{k}{\mathbf{lsub}}\left(i\right)$
ifail = 4${\mathbf{ifail}}=4$
 On entry, nobs(i) ≤ 0${\mathbf{nobs}}\left(i\right)\le 0$, for some i = 1,2, … ,l$i=1,2,\dots ,l$.
ifail = 5${\mathbf{ifail}}=5$
 On entry, n ≠ ∑ i = 1lnobs(i)${\mathbf{n}}\ne \sum _{i=1}^{l}{\mathbf{nobs}}\left(i\right)$.
W ifail = 6${\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 ifail = 7${\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.

## 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$n$.

## Example

```function nag_anova_hier2_example
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];
lsub = [int64(5);3];
nobs = [int64(5);3;3;3;2;3;5;3];
[ngp, gbar, sgbar, gm, ss, idf, f, fp, ifail] = nag_anova_hier2(y, lsub, nobs)
```
```

ngp =

16
11

gbar =

2.2062
1.9364

sgbar =

2.1000
2.2333
2.4000
2.4333
1.8000
1.8667
1.8600
2.1333

gm =

2.0963

ss =

0.4748
0.8162
0.5587
1.8496

idf =

1
6
19
26

f =

16.1477
4.6262

fp =

0.0007
0.0047

ifail =

0

```
```function g04ag_example
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];
lsub = [int64(5);3];
nobs = [int64(5);3;3;3;2;3;5;3];
[ngp, gbar, sgbar, gm, ss, idf, f, fp, ifail] = g04ag(y, lsub, nobs)
```
```

ngp =

16
11

gbar =

2.2062
1.9364

sgbar =

2.1000
2.2333
2.4000
2.4333
1.8000
1.8667
1.8600
2.1333

gm =

2.0963

ss =

0.4748
0.8162
0.5587
1.8496

idf =

1
6
19
26

f =

16.1477
4.6262

fp =

0.0007
0.0047

ifail =

0

```