G11 Chapter Contents
G11 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG11BBF

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

G11BBF computes a table from a set of classification factors using a given percentile or quantile, for example the median.

## 2  Specification

 SUBROUTINE G11BBF ( TYP, WEIGHT, N, NFAC, ISF, LFAC, IFAC, LDF, PERCNT, Y, WT, TABLE, MAXT, NCELLS, NDIM, IDIM, ICOUNT, IWK, WK, IFAIL)
 INTEGER N, NFAC, ISF(NFAC), LFAC(NFAC), IFAC(LDF,NFAC), LDF, MAXT, NCELLS, NDIM, IDIM(NFAC), ICOUNT(MAXT), IWK(2*NFAC+N), IFAIL REAL (KIND=nag_wp) PERCNT, Y(N), WT(*), TABLE(MAXT), WK(2*N) CHARACTER(1) TYP, 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 median age could be computed:
 Sex Habitat Inner-city Suburban Rural Male 24 31 37 Female 21.5 28.5 33
That is, the median age for all observations for males living in rural areas is $37$, the median being the 50% quantile. Other quantiles can also be computed: the $p$ percent quantile or percentile, ${q}_{p}$, is the estimate of the value such that $p$ percent of observations are less than ${q}_{p}$. This is calculated in two different ways depending on whether the tabulated variable is continuous or discrete. Let there be $m$ values in a cell and let ${y}_{\left(1\right)}$, ${y}_{\left(2\right)},\dots ,{y}_{\left(m\right)}$ be the values for that cell sorted into ascending order. Also, associated with each value there is a weight, ${w}_{\left(1\right)}$, ${w}_{\left(2\right)},\dots ,{w}_{\left(m\right)}$, which could represent the observed frequency for that value, with ${W}_{j}=\sum _{i=1}^{j}{w}_{\left(i\right)}$ and ${W}_{j}^{\prime }=\sum _{i=1}^{j}{w}_{\left(i\right)}-\frac{1}{2}{w}_{\left(j\right)}$. For the $p$ percentile let ${p}_{w}=\left(p/100\right){W}_{m}$ and ${p}_{w}^{\prime }=\left(p/100\right){W}_{m}^{\prime }$, then the percentiles for the two cases are as given below.
If the variable is discrete, that is, it takes only a limited number of (usually integer) values, then the percentile is defined as
 $yj if ​Wj-1
If the data is continuous then the quantiles are estimated by linear interpolation.
 $y1 if ​ pw′≤W1′ 1-fyj- 1+fyj if ​ Wj- 1′Wm′,$
where $f=\left({p}_{w}^{\prime }-{W}_{j-1}^{\prime }\right)/\left({W}_{j}^{\prime }-{W}_{j-1}^{\prime }\right)$.

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

## 5  Parameters

1:     TYP – CHARACTER(1)Input
On entry: indicates if the variable to be tabulated is discrete or continuous.
${\mathbf{TYP}}=\text{'D'}$
The percentiles are computed for a discrete variable.
${\mathbf{TYP}}=\text{'C'}$
The percentiles are computed for a continuous variable using linear interpolation.
Constraint: ${\mathbf{TYP}}=\text{'D'}$ or $\text{'C'}$.
2:     WEIGHT – CHARACTER(1)Input
On entry: indicates if there are weights associated with the variable to be tabulated.
${\mathbf{WEIGHT}}=\text{'U'}$
Weights are not input and unit weights are assumed.
${\mathbf{WEIGHT}}=\text{'W'}$
Weights must be supplied in WT.
Constraint: ${\mathbf{WEIGHT}}=\text{'U'}$ or $\text{'W'}$.
3:     N – INTEGERInput
On entry: the number of observations.
Constraint: ${\mathbf{N}}\ge 2$.
4:     NFAC – INTEGERInput
On entry: the number of classifying factors in IFAC.
Constraint: ${\mathbf{NFAC}}\ge 1$.
5:     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.
6:     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}=1,2,\dots ,{\mathbf{NFAC}}$.
7:     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}}$.
8:     LDF – INTEGERInput
On entry: the first dimension of the array IFAC as declared in the (sub)program from which G11BBF is called.
Constraint: ${\mathbf{LDF}}\ge {\mathbf{N}}$.
9:     PERCNT – REAL (KIND=nag_wp)Input
On entry: $p$, the percentile to be tabulated.
Constraint: $0.0.
10:   Y(N) – REAL (KIND=nag_wp) arrayInput
On entry: the variable to be tabulated.
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'}$, and at least $1$ otherwise.
On entry: if ${\mathbf{WEIGHT}}=\text{'W'}$, WT must contain the N weights. Otherwise WT is not referenced.
Constraint: if ${\mathbf{WEIGHT}}=\text{'W'}$, ${\mathbf{WT}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{N}}$.
12:   TABLE(MAXT) – REAL (KIND=nag_wp) arrayOutput
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 occurring 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 – INTEGEROutput
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 arrayOutput
On exit: a table containing the number of observations contributing to each cell of the table, stored identically to TABLE.
18:   IWK($2×{\mathbf{NFAC}}+{\mathbf{N}}$) – INTEGER arrayWorkspace
19:   WK($2×{\mathbf{N}}$) – REAL (KIND=nag_wp) 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{TYP}}\ne \text{'D'}$ or $\text{'C'}$, or ${\mathbf{WEIGHT}}\ne \text{'U'}$ or $\text{'W'}$, or ${\mathbf{PERCNT}}\le 0.0$, or ${\mathbf{PERCNT}}\ge 100.0$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{ISF}}\left(i\right)>0$ and ${\mathbf{LFAC}}\left(i\right)\le 1$, 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'}$ and ${\mathbf{WT}}\left(i\right)<0.0$, for some $i$.
${\mathbf{IFAIL}}=3$
At least one cell is empty.

## 7  Accuracy

Not applicable.

The tables created by G11BBF and stored in TABLE and ICOUNT 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}_{\mathit{j}}=\prod _{k=\mathit{j}+1}^{n}{l}_{k}$, for $\mathit{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, and the $3×6$ table of treatment medians for yield over blocks is computed and printed.

### 9.1  Program Text

Program Text (g11bbfe.f90)

### 9.2  Program Data

Program Data (g11bbfe.d)

### 9.3  Program Results

Program Results (g11bbfe.r)