G04 Chapter Contents
G04 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG04AGF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G04AGF 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.

## 2  Specification

 SUBROUTINE G04AGF ( Y, N, K, LSUB, NOBS, L, NGP, GBAR, SGBAR, GM, SS, IDF, F, FP, IFAIL)
 INTEGER N, K, LSUB(K), NOBS(L), L, NGP(K), IDF(4), IFAIL REAL (KIND=nag_wp) Y(N), GBAR(K), SGBAR(L), GM, SS(4), F(2), FP(2)

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

## 4  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

## 5  Parameters

1:     Y(N) – REAL (KIND=nag_wp) arrayInput
On entry: 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:     N – INTEGERInput
On entry: $n$, the total number of observations.
3:     K – INTEGERInput
On entry: $k$, the number of groups.
Constraint: ${\mathbf{K}}\ge 2$.
4:     LSUB(K) – INTEGER arrayInput
On entry: 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$.
5:     NOBS(L) – INTEGER arrayInput
On entry: 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$.
6:     L – INTEGERInput
On entry: $l$, the total number of subgroups.
Constraint: ${\mathbf{L}}=\sum _{i=1}^{k}{\mathbf{LSUB}}\left(i\right)$.
7:     NGP(K) – INTEGER arrayOutput
On exit: the total number of observations in group $\mathit{i}$, ${n}_{\mathit{i}.}$, for $\mathit{i}=1,2,\dots ,k$.
8:     GBAR(K) – REAL (KIND=nag_wp) arrayOutput
On exit: the mean for group $\mathit{i}$, ${\stackrel{-}{y}}_{.\mathit{i}.}$, for $\mathit{i}=1,2,\dots ,k$.
9:     SGBAR(L) – REAL (KIND=nag_wp) arrayOutput
On exit: 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.$
10:   GM – REAL (KIND=nag_wp)Output
On exit: the grand mean, ${\stackrel{-}{y}}_{\dots }$.
11:   SS($4$) – REAL (KIND=nag_wp) arrayOutput
On exit: 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}}$.
12:   IDF($4$) – INTEGER arrayOutput
On exit: 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.
13:   F($2$) – REAL (KIND=nag_wp) arrayOutput
On exit: 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.
14:   FP($2$) – REAL (KIND=nag_wp) arrayOutput
On exit: contains the significances of the mean square ratios, ${p}_{1}$ and ${p}_{2}$ respectively.
15:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\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)$.
${\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.
${\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.

## 7  Accuracy

The computations are believed to be stable.

The time taken by G04AGF increases approximately linearly with the total number of observations, $n$.

## 9  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).

### 9.1  Program Text

Program Text (g04agfe.f90)

### 9.2  Program Data

Program Data (g04agfe.d)

### 9.3  Program Results

Program Results (g04agfe.r)