G11 Chapter Contents
G11 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG11BAF

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

G11BAF computes a table from a set of classification factors using a selected statistic.

## 2  Specification

 SUBROUTINE G11BAF ( STAT, UPDATE, WEIGHT, N, NFAC, ISF, LFAC, IFAC, LDF, Y, WT, TABLE, MAXT, NCELLS, NDIM, IDIM, ICOUNT, AUXT, IWK, IFAIL)
 INTEGER N, NFAC, ISF(NFAC), LFAC(NFAC), IFAC(LDF,NFAC), LDF, MAXT, NCELLS, NDIM, IDIM(NFAC), ICOUNT(MAXT), IWK(2*NFAC), IFAIL REAL (KIND=nag_wp) Y(N), WT(*), TABLE(MAXT), AUXT(*) CHARACTER(1) STAT, UPDATE, WEIGHT

## 3  Description

A dataset may include both classification variables and general variables. The classification variables, known as factors, take a small number of values known as levels. For example, the factor sex would have the levels male and female. These can be coded as $1$ and $2$ respectively. Given several factors, a multi-way table can be constructed such that each cell of the table represents one level from each factor. For example, the two factors sex and habitat, habitat having three levels (inner-city, suburban and rural) define the $2×3$ contingency table
 Sex Habitat Inner-city Suburban Rural Male Female
For each cell statistics can be computed. If a third variable in the dataset was age, then for each cell the average age could be computed:
 Sex Habitat Inner-city Suburban Rural Male 25.5 30.3 35.6 Female 23.2 29.1 30.4
That is the average age for all observations for males living in rural areas is $35.6$. Other statistics can also be computed: the number of observations, the total, the variance, the largest value and the smallest value.
G11BAF computes a table for one of the selected statistics. The factors have to be coded with levels $1,2,\dots \text{}$. Weights can be used to eliminate values from the calculations, e.g., if they represent ‘missing values’. There is also the facility to update an existing table with the addition of new observations.

## 4  References

John J A and Quenouille M H (1977) Experiments: Design and Analysis Griffin
Kendall M G and Stuart A (1969) The Advanced Theory of Statistics (Volume 1) (3rd Edition) Griffin
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555

## 5  Parameters

1:     STAT – CHARACTER(1)Input
On entry: indicates which statistic is to be computed for the table cells.
${\mathbf{STAT}}=\text{'N'}$
The number of observations for each cell.
${\mathbf{STAT}}=\text{'T'}$
The total for the variable in Y for each cell.
${\mathbf{STAT}}=\text{'A'}$
The average (mean) for the variable in Y for each cell.
${\mathbf{STAT}}=\text{'V'}$
The variance for the variable in Y for each cell.
${\mathbf{STAT}}=\text{'L'}$
The largest value for the variable in Y for each cell.
${\mathbf{STAT}}=\text{'S'}$
The smallest value for the variable in Y for each cell.
Constraint: ${\mathbf{STAT}}=\text{'N'}$, $\text{'T'}$, $\text{'A'}$, $\text{'V'}$, $\text{'L'}$ or $\text{'S'}$.
2:     UPDATE – CHARACTER(1)Input
On entry: indicates if an existing table is to be updated by further observation.
${\mathbf{UPDATE}}=\text{'I'}$
The table cells will be initialized to zero before tabulations take place.
${\mathbf{UPDATE}}=\text{'U'}$
The table input in TABLE will be updated. The parameters NCELLS, TABLE, ICOUNT and AUXT must remain unchanged from the previous call to G11BAF.
Constraint: ${\mathbf{UPDATE}}=\text{'I'}$ or $\text{'U'}$.
3:     WEIGHT – CHARACTER(1)Input
On entry: indicates if weights are to be used.
${\mathbf{WEIGHT}}=\text{'U'}$
Weights are not used and unit weights are assumed.
${\mathbf{WEIGHT}}=\text{'W'}$ or $\text{'V'}$
Weights are used and must be supplied in WT. The only difference between ${\mathbf{WEIGHT}}=\text{'W'}$ and ${\mathbf{WEIGHT}}=\text{'V'}$ is if the variance is computed.
${\mathbf{WEIGHT}}=\text{'W'}$
The divisor for the variance is the sum of the weights minus one and if ${\mathbf{WEIGHT}}=\text{'V'}$, the divisor is the number of observations with nonzero weights minus one. The former is useful if the weights represent the frequency of the observed values.
If ${\mathbf{STAT}}=\text{'T'}$ or $\text{'A'}$, the weighted total or mean is computed respectively.
If ${\mathbf{STAT}}=\text{'N'}$, $\text{'L'}$ or $\text{'S'}$, the only effect of weights is to eliminate values with zero weights from the computations.
Constraint: ${\mathbf{WEIGHT}}=\text{'U'}$, $\text{'V'}$ or $\text{'W'}$.
4:     N – INTEGERInput
On entry: the number of observations.
Constraint: ${\mathbf{N}}\ge 2$.
5:     NFAC – INTEGERInput
On entry: the number of classifying factors in IFAC.
Constraint: ${\mathbf{NFAC}}\ge 1$.
6:     ISF(NFAC) – INTEGER arrayInput
On entry: indicates which factors in IFAC are to be used in the tabulation.
If ${\mathbf{ISF}}\left(i\right)>0$ the $i$th factor in IFAC is included in the tabulation.
Note that if ${\mathbf{ISF}}\left(\mathit{i}\right)\le 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{NFAC}}$ then the statistic for the whole sample is calculated and returned in a $1×1$ table.
7:     LFAC(NFAC) – INTEGER arrayInput
On entry: the number of levels of the classifying factors in IFAC.
Constraint: if ${\mathbf{ISF}}\left(\mathit{i}\right)>0$, ${\mathbf{LFAC}}\left(\mathit{i}\right)\ge 2$, for $\mathit{i}=\mathrm{Ai},\dots ,\mathrm{Ai}$.
8:     IFAC(LDF,NFAC) – INTEGER arrayInput
On entry: the NFAC coded classification factors for the N observations.
Constraint: $1\le {\mathbf{IFAC}}\left(\mathit{i},\mathit{j}\right)\le {\mathbf{LFAC}}\left(\mathit{j}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{N}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{NFAC}}$.
9:     LDF – INTEGERInput
On entry: the first dimension of the array IFAC as declared in the (sub)program from which G11BAF is called.
Constraint: ${\mathbf{LDF}}\ge {\mathbf{N}}$.
10:   Y(N) – REAL (KIND=nag_wp) arrayInput
On entry: the variable to be tabulated. If ${\mathbf{STAT}}=\text{'N'}$, Y is not referenced.
11:   WT($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least ${\mathbf{N}}$ if ${\mathbf{WEIGHT}}=\text{'W'}$ or $\text{'V'}$, and at least $1$ otherwise.
On entry: if ${\mathbf{WEIGHT}}=\text{'W'}$ or $\text{'V'}$, WT must contain the N weights. Otherwise WT is not referenced.
Constraint: if ${\mathbf{WEIGHT}}=\text{'W'}$ or $\text{'V'}$, ${\mathbf{WT}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=\mathrm{Ai},\dots ,\mathrm{Ai}$.
12:   TABLE(MAXT) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{UPDATE}}=\text{'U'}$, TABLE must be unchanged from the previous call to G11BAF, otherwise TABLE need not be set.
On exit: the computed table. The NCELLS cells of the table are stored so that for any two factors the index relating to the factor referred to later in LFAC and IFAC changes faster. For further details see Section 8.
13:   MAXT – INTEGERInput
On entry: the maximum size of the table to be computed.
Constraint: ${\mathbf{MAXT}}\ge \text{}$ product of the levels of the factors included in the tabulation.
14:   NCELLS – INTEGERInput/Output
On entry: if ${\mathbf{UPDATE}}=\text{'U'}$, NCELLS must be unchanged from the previous call to G11BAF, otherwise NCELLS need not be set.
On exit: the number of cells in the table.
15:   NDIM – INTEGEROutput
On exit: the number of factors defining the table.
16:   IDIM(NFAC) – INTEGER arrayOutput
On exit: the first NDIM elements contain the number of levels for the factors defining the table.
17:   ICOUNT(MAXT) – INTEGER arrayInput/Output
On entry: if ${\mathbf{UPDATE}}=\text{'U'}$, ICOUNT must be unchanged from the previous call to G11BAF, otherwise ICOUNT need not be set.
On exit: a table containing the number of observations contributing to each cell of the table, stored identically to TABLE. Note if ${\mathbf{STAT}}=\text{'N'}$ this is the same as is returned in TABLE.
18:   AUXT($*$) – REAL (KIND=nag_wp) arrayInput/Output
Note: the dimension of the array AUXT must be at least ${\mathbf{NCELLS}}$ if ${\mathbf{STAT}}=\text{'A'}$, $2×{\mathbf{NCELLS}}$ if ${\mathbf{STAT}}=\text{'V'}$, and at least $1$ otherwise.
On entry: if ${\mathbf{UPDATE}}=\text{'U'}$, AUXT must be unchanged from the previous call to G11BAF, otherwise AUXT need not be set.
On exit: if ${\mathbf{STAT}}=\text{'A'}$ or $\text{'V'}$, the first NCELLS values hold the table containing the sum of the weights for the observations contributing to each cell, stored identically to TABLE.
If ${\mathbf{STAT}}=\text{'V'}$, the second set of NCELLS values hold the table of cell means. Otherwise AUXT is not referenced.
19:   IWK($2×{\mathbf{NFAC}}$) – INTEGER arrayWorkspace
20:   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{N}}<2$, or ${\mathbf{NFAC}}<1$, or ${\mathbf{LDF}}<{\mathbf{N}}$, or ${\mathbf{UPDATE}}\ne \text{'I'}$ or $\text{'U'}$, or ${\mathbf{WEIGHT}}\ne \text{'U'}$, $\text{'W'}$ or $\text{'V'}$, or ${\mathbf{STAT}}\ne \text{'N'}$, $\text{'T'}$, $\text{'A'}$, $\text{'V'}$, $\text{'L'}$ or $\text{'S'}$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{ISF}}\left(i\right)>0$ and ${\mathbf{LFAC}}\left(i\right)<2$, for some $i$, or ${\mathbf{IFAC}}\left(i,j\right)<1$, for some $i,j$, or ${\mathbf{IFAC}}\left(i,j\right)>{\mathbf{LFAC}}\left(j\right)$ for some $i,j$, or MAXT is too small, or ${\mathbf{WEIGHT}}=\text{'W'}$ or $\text{'V'}$ and ${\mathbf{WT}}\left(i\right)<0.0$, for some $i$.
${\mathbf{IFAIL}}=3$
${\mathbf{STAT}}=\text{'V'}$ and the divisor for the variance is $\text{}\le 0.0$.
${\mathbf{IFAIL}}=4$
${\mathbf{UPDATE}}=\text{'U'}$ and at least one of NCELLS, TABLE, AUXT or ICOUNT have been changed since previous call to G11BAF.

## 7  Accuracy

Only applicable when ${\mathbf{STAT}}=\text{'V'}$. In this case a one pass algorithm is used as described by West (1979).

The tables created by G11BAF and stored in TABLE, ICOUNT and, depending on STAT, also in AUXT are stored in the following way. Let there be $n$ factors defining the table with factor $k$ having ${l}_{k}$ levels, then the cell defined by the levels ${i}_{1}$, ${i}_{2},\dots ,{i}_{n}$ of the factors is stored in the $m$th cell given by
 $m=1+∑k=1nik-1ck,$
where ${c}_{j}=\prod _{k=j+1}^{n}{l}_{k}$, for $j=1,2,\dots ,n-1$ and ${c}_{n}=1$.

## 9  Example

The data, given by John and Quenouille (1977), is for a $3×6$ factorial experiment in $3$ blocks of $18$ units. The data is input in the order, blocks, factor with $3$ levels, factor with $6$ levels, yield. The $3×6$ table of treatment means for yield over blocks is computed and printed.

### 9.1  Program Text

Program Text (g11bafe.f90)

### 9.2  Program Data

Program Data (g11bafe.d)

### 9.3  Program Results

Program Results (g11bafe.r)