# NAG FL Interfaceg11bbf (tabulate_​percentile)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g11bbf ( typ, n, nfac, isf, lfac, ifac, ldf, y, wt, maxt, ndim, idim, iwk, wk,
 Integer, Intent (In) :: n, nfac, isf(nfac), lfac(nfac), ifac(ldf,nfac), ldf, maxt Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: ncells, ndim, idim(nfac), icount(maxt), iwk(2*nfac+n) Real (Kind=nag_wp), Intent (In) :: percnt, y(n), wt(*) Real (Kind=nag_wp), Intent (Out) :: table(maxt), wk(2*n) Character (1), Intent (In) :: typ, weight
#include <nag.h>
 void g11bbf_ (const char *typ, const char *weight, const Integer *n, const Integer *nfac, const Integer isf[], const Integer lfac[], const Integer ifac[], const Integer *ldf, const double *percnt, const double y[], const double wt[], double table[], const Integer *maxt, Integer *ncells, Integer *ndim, Integer idim[], Integer icount[], Integer iwk[], double wk[], Integer *ifail, const Charlen length_typ, const Charlen length_weight)
The routine may be called by the names g11bbf or nagf_contab_tabulate_percentile.

## 3Description

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
Habitat
Sex 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:
Habitat
Sex 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
 $y(j) if ​Wj-1
If the data is continuous then the quantiles are estimated by linear interpolation.
 $y(1) if ​ pw′≤W1′ (1-f)y(j-1)+fy(j) if ​ Wj- 1′Wm′,$
where $f=\left({p}_{w}^{\prime }-{W}_{j-1}^{\prime }\right)/\left({W}_{j}^{\prime }-{W}_{j-1}^{\prime }\right)$.

## 4References

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

## 5Arguments

1: $\mathbf{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: $\mathbf{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: $\mathbf{n}$Integer Input
On entry: the number of observations.
Constraint: ${\mathbf{n}}\ge 2$.
4: $\mathbf{nfac}$Integer Input
On entry: the number of classifying factors in ifac.
Constraint: ${\mathbf{nfac}}\ge 1$.
5: $\mathbf{isf}\left({\mathbf{nfac}}\right)$Integer array Input
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: $\mathbf{lfac}\left({\mathbf{nfac}}\right)$Integer array Input
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: $\mathbf{ifac}\left({\mathbf{ldf}},{\mathbf{nfac}}\right)$Integer array Input
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: $\mathbf{ldf}$Integer Input
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: $\mathbf{percnt}$Real (Kind=nag_wp) Input
On entry: $p$, the percentile to be tabulated.
Constraint: $0.0.
10: $\mathbf{y}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the variable to be tabulated.
11: $\mathbf{wt}\left(*\right)$Real (Kind=nag_wp) array Input
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: $\mathbf{table}\left({\mathbf{maxt}}\right)$Real (Kind=nag_wp) array Output
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 9.
13: $\mathbf{maxt}$Integer Input
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: $\mathbf{ncells}$Integer Output
On exit: the number of cells in the table.
15: $\mathbf{ndim}$Integer Output
On exit: the number of factors defining the table.
16: $\mathbf{idim}\left({\mathbf{nfac}}\right)$Integer array Output
On exit: the first ndim elements contain the number of levels for the factors defining the table.
17: $\mathbf{icount}\left({\mathbf{maxt}}\right)$Integer array Output
On exit: a table containing the number of observations contributing to each cell of the table, stored identically to table.
18: $\mathbf{iwk}\left(2×{\mathbf{nfac}}+{\mathbf{n}}\right)$Integer array Workspace
19: $\mathbf{wk}\left(2×{\mathbf{n}}\right)$Real (Kind=nag_wp) array Workspace
20: $\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{ldf}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldf}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 2$.
On entry, ${\mathbf{nfac}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nfac}}\ge 1$.
On entry, ${\mathbf{percnt}}=⟨\mathit{\text{value}}⟩$.
Constraint: $0.0.
On entry, ${\mathbf{typ}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{typ}}=\text{'D'}$ or $\text{'C'}$.
On entry, ${\mathbf{weight}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{weight}}=\text{'U'}$ or $\text{'W'}$.
${\mathbf{ifail}}=2$
On entry, $i=⟨\mathit{\text{value}}⟩$, $j=⟨\mathit{\text{value}}⟩$, ${\mathbf{lfac}}\left(j\right)=⟨\mathit{\text{value}}⟩$ and ${\mathbf{ifac}}\left(i,j\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ifac}}\left(i,j\right)\le {\mathbf{lfac}}\left(j\right)$.
On entry, $i=⟨\mathit{\text{value}}⟩$, $j=⟨\mathit{\text{value}}⟩$ and ${\mathbf{ifac}}\left(i,j\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ifac}}\left(i,j\right)>0$.
On entry, $i=⟨\mathit{\text{value}}⟩$ and ${\mathbf{lfac}}\left(i\right)=⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{lfac}}\left(i\right)>2$.
On entry, $i=⟨\mathit{\text{value}}⟩$ and ${\mathbf{wt}}\left(i\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{wt}}\left(i\right)\ge 0.0$.
On entry, ${\mathbf{maxt}}=⟨\mathit{\text{value}}⟩$ and minimum value for ${\mathbf{maxt}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{maxt}}\ge \text{product}$ of the levels of the factors included in the tabulation.
${\mathbf{ifail}}=3$
Some cells of the table are empty.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
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.

Not applicable.

## 8Parallelism and Performance

g11bbf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

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=1n[(ik-1)ck],$
where ${c}_{\mathit{j}}=\prod _{k=\mathit{j}+1}^{n}{l}_{k}$, for $\mathit{j}=1,2,\dots ,n-1$ and ${c}_{n}=1$.

## 10Example

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.

### 10.1Program Text

Program Text (g11bbfe.f90)

### 10.2Program Data

Program Data (g11bbfe.d)

### 10.3Program Results

Program Results (g11bbfe.r)