# NAG FL Interfaceg04agf (hier2)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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.

## 2Specification

Fortran Interface
 Subroutine g04agf ( y, n, k, lsub, nobs, l, ngp, gbar, gm, ss, idf, f, fp,
 Integer, Intent (In) :: n, k, lsub(k), nobs(l), l Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: ngp(k), idf(4) Real (Kind=nag_wp), Intent (In) :: y(n) Real (Kind=nag_wp), Intent (Out) :: gbar(k), sgbar(l), gm, ss(4), f(2), fp(2)
#include <nag.h>
 void g04agf_ (const double y[], const Integer *n, const Integer *k, const Integer lsub[], const Integer nobs[], const Integer *l, Integer ngp[], double gbar[], double sgbar[], double *gm, double ss[], Integer idf[], double f[], double fp[], Integer *ifail)
The routine may be called by the names g04agf or nagf_anova_hier2.

## 3Description

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
1. (i)The subgroup means
 $y¯.ij=∑m=1nijymijnij$
2. (ii)The group means
 $y¯.i.=∑j= 1li∑m= 1nijymij ∑j= 1linij$
3. (iii)The grand mean
 $y¯…=∑i=1k∑j=1li∑m=1nijymij ∑i=1k∑j=1linij$
4. (iv)The number of observations in each group
 $ni.= ∑j= 1 li nij$
5. (v)Sums of squares
 Between groups $\text{}={\mathrm{SS}}_{g}=\sum _{i=1}^{k}{n}_{i.}{\left({\overline{y}}_{.i.}-{\overline{y}}_{\dots }\right)}^{2}$ Between subgroups within groups $\text{}={\mathrm{SS}}_{sg}=\sum _{i=1}^{k}\sum _{j=1}^{{l}_{i}}{n}_{ij}{\left({y}_{.ij}-{\overline{y}}_{.i.}\right)}^{2}$ Residual (within subgroups) $={\mathrm{SS}}_{\mathrm{res}}=\sum _{i=1}^{k}\sum _{j=1}^{{l}_{i}}\sum _{m=1}^{{n}_{ij}}{\left({y}_{mij}-{\overline{y}}_{.ij}\right)}^{2}={\mathrm{SS}}_{\mathrm{tot}}-{\mathrm{SS}}_{g}-{\mathrm{SS}}_{sg}$ Corrected total $\text{}={\mathrm{SS}}_{\mathrm{tot}}=\sum _{i=1}^{k}\sum _{j=1}^{{l}_{i}}\sum _{m=1}^{{n}_{ij}}{\left({y}_{mij}-{\overline{y}}_{\dots }\right)}^{2}$
6. (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.}$
7. (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.
8. (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$.

## 4References

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

## 5Arguments

1: $\mathbf{y}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
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: $\mathbf{n}$Integer Input
On entry: $n$, the total number of observations.
3: $\mathbf{k}$Integer Input
On entry: $k$, the number of groups.
Constraint: ${\mathbf{k}}\ge 2$.
4: $\mathbf{lsub}\left({\mathbf{k}}\right)$Integer array Input
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: $\mathbf{nobs}\left({\mathbf{l}}\right)$Integer array Input
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: $\mathbf{l}$Integer Input
On entry: $l$, the total number of subgroups.
Constraint: ${\mathbf{l}}=\sum _{i=1}^{k}{\mathbf{lsub}}\left(i\right)$.
7: $\mathbf{ngp}\left({\mathbf{k}}\right)$Integer array Output
On exit: the total number of observations in group $\mathit{i}$, ${n}_{\mathit{i}.}$, for $\mathit{i}=1,2,\dots ,k$.
8: $\mathbf{gbar}\left({\mathbf{k}}\right)$Real (Kind=nag_wp) array Output
On exit: the mean for group $\mathit{i}$, ${\overline{y}}_{.\mathit{i}.}$, for $\mathit{i}=1,2,\dots ,k$.
9: $\mathbf{sgbar}\left({\mathbf{l}}\right)$Real (Kind=nag_wp) array Output
On exit: the subgroup means, ${\overline{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: $\mathbf{gm}$Real (Kind=nag_wp) Output
On exit: the grand mean, ${\overline{y}}_{\dots }$.
11: $\mathbf{ss}\left(4\right)$Real (Kind=nag_wp) array Output
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: $\mathbf{idf}\left(4\right)$Integer array Output
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: $\mathbf{f}\left(2\right)$Real (Kind=nag_wp) array Output
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: $\mathbf{fp}\left(2\right)$Real (Kind=nag_wp) array Output
On exit: contains the significances of the mean square ratios, ${p}_{1}$ and ${p}_{2}$ respectively.
15: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-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}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{k}}>1$.
${\mathbf{ifail}}=2$
On entry, $i=⟨\mathit{\text{value}}⟩$ and ${\mathbf{lsub}}\left(i\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lsub}}\left(i\right)>0$.
${\mathbf{ifail}}=3$
On entry, ${\sum }_{i}{\mathbf{lsub}}\left(i\right)=⟨\mathit{\text{value}}⟩$ and ${\mathbf{l}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\sum }_{i}{\mathbf{lsub}}\left(i\right)={\mathbf{l}}$.
${\mathbf{ifail}}=4$
On entry, $i=⟨\mathit{\text{value}}⟩$ and ${\mathbf{nobs}}\left(i\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nobs}}\left(i\right)>0$.
${\mathbf{ifail}}=5$
On entry, ${\sum }_{i}{\mathbf{nobs}}\left(i\right)=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\sum }_{i}{\mathbf{nobs}}\left(i\right)={\mathbf{n}}$.
${\mathbf{ifail}}=6$
The total corrected sum of squares is zero. This indicates 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.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

The computations are believed to be stable.

## 8Parallelism and Performance

g04agf is not threaded in any implementation.

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

## 10Example

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

### 10.1Program Text

Program Text (g04agfe.f90)

### 10.2Program Data

Program Data (g04agfe.d)

### 10.3Program Results

Program Results (g04agfe.r)