Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_contab_tabulate_percentile (g11bb)

## Purpose

nag_contab_tabulate_percentile (g11bb) computes a table from a set of classification factors using a given percentile or quantile, for example the median.

## Syntax

[table, ncells, ndim, idim, icount, ifail] = g11bb(typ, isf, lfac, ifac, percnt, y, maxt, 'n', n, 'nfac', nfac, 'wt', wt)
[table, ncells, ndim, idim, icount, ifail] = nag_contab_tabulate_percentile(typ, isf, lfac, ifac, percnt, y, maxt, 'n', n, 'nfac', nfac, 'wt', wt)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 24: drop weight, wt optional
.

## 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$1$ and 2$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$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$37$, the median being the 50% quantile. Other quantiles can also be computed: the p$p$ percent quantile or percentile, qp${q}_{p}$, is the estimate of the value such that p$p$ percent of observations are less than qp${q}_{p}$. This is calculated in two different ways depending on whether the tabulated variable is continuous or discrete. Let there be m$m$ values in a cell and let y(1)${y}_{\left(1\right)}$, y(2),,y(m)${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(1)${w}_{\left(1\right)}$, w(2),, w(m)${w}_{\left(2\right)},\dots ,{w}_{\left(m\right)}$, which could represent the observed frequency for that value, with Wj = i = 1jw(i)${W}_{j}=\sum _{i=1}^{j}{w}_{\left(i\right)}$ and Wj = i = 1jw(i)(1/2)w(j)${W}_{j}^{\prime }=\sum _{i=1}^{j}{w}_{\left(i\right)}-\frac{1}{2}{w}_{\left(j\right)}$. For the p$p$ percentile let pw = (p / 100)Wm${p}_{w}=\left(p/100\right){W}_{m}$ and pw = (p / 100)Wm${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 < pW < Wj (y(j + 1) + y(j))/2 if ​pw = Wj.
$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 ′ < pw ′ ≤ Wj ′ y(m) if ​ pw ′ > Wm ′ ,
$y(1) if ​ pw′≤W1′ (1-f)y(j- 1)+fy(j) if ​ Wj- 1′Wm′,$
where f = (pwWj1) / (WjWj1)$f=\left({p}_{w}^{\prime }-{W}_{j-1}^{\prime }\right)/\left({W}_{j}^{\prime }-{W}_{j-1}^{\prime }\right)$.

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

## Parameters

### Compulsory Input Parameters

1:     typ – string (length ≥ 1)
Indicates if the variable to be tabulated is discrete or continuous.
typ = 'D'${\mathbf{typ}}=\text{'D'}$
The percentiles are computed for a discrete variable.
typ = 'C'${\mathbf{typ}}=\text{'C'}$
The percentiles are computed for a continuous variable using linear interpolation.
Constraint: typ = 'D'${\mathbf{typ}}=\text{'D'}$ or 'C'$\text{'C'}$.
2:     isf(nfac) – int64int32nag_int array
nfac, the dimension of the array, must satisfy the constraint nfac1${\mathbf{nfac}}\ge 1$.
Indicates which factors in ifac are to be used in the tabulation.
If isf(i) > 0${\mathbf{isf}}\left(i\right)>0$ the i$i$th factor in ifac is included in the tabulation.
Note that if isf(i)0${\mathbf{isf}}\left(\mathit{i}\right)\le 0$, for i = 1,2,,nfac$\mathit{i}=1,2,\dots ,{\mathbf{nfac}}$ then the statistic for the whole sample is calculated and returned in a 1 × 1$1×1$ table.
3:     lfac(nfac) – int64int32nag_int array
nfac, the dimension of the array, must satisfy the constraint nfac1${\mathbf{nfac}}\ge 1$.
The number of levels of the classifying factors in ifac.
Constraint: if isf(i) > 0${\mathbf{isf}}\left(\mathit{i}\right)>0$, lfac(i)2${\mathbf{lfac}}\left(\mathit{i}\right)\ge 2$, for i = 1,2,,nfac$\mathit{i}=1,2,\dots ,{\mathbf{nfac}}$.
4:     ifac(ldf,nfac) – int64int32nag_int array
ldf, the first dimension of the array, must satisfy the constraint ldfn$\mathit{ldf}\ge {\mathbf{n}}$.
The nfac coded classification factors for the n observations.
Constraint: 1ifac(i,j)lfac(j)$1\le {\mathbf{ifac}}\left(\mathit{i},\mathit{j}\right)\le {\mathbf{lfac}}\left(\mathit{j}\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,{\mathbf{n}}$ and j = 1,2,,nfac$\mathit{j}=1,2,\dots ,{\mathbf{nfac}}$.
5:     percnt – double scalar
p$p$, the percentile to be tabulated.
Constraint: 0.0 < p < 100.0$0.0.
6:     y(n) – double array
n, the dimension of the array, must satisfy the constraint n2${\mathbf{n}}\ge 2$.
The variable to be tabulated.
7:     maxt – int64int32nag_int scalar
The maximum size of the table to be computed.
Constraint: maxt${\mathbf{maxt}}\ge \text{}$ product of the levels of the factors included in the tabulation.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array y and the first dimension of the array ifac. (An error is raised if these dimensions are not equal.)
The number of observations.
Constraint: n2${\mathbf{n}}\ge 2$.
2:     nfac – int64int32nag_int scalar
Default: The dimension of the arrays isf, lfac and the second dimension of the array ifac. (An error is raised if these dimensions are not equal.)
The number of classifying factors in ifac.
Constraint: nfac1${\mathbf{nfac}}\ge 1$.
3:     wt( : $:$) – double array
Note: the dimension of the array wt must be at least n${\mathbf{n}}$ if weight = 'W'$\mathit{weight}=\text{'W'}$, and at least 1$1$ otherwise.
If weight = 'W'$\mathit{weight}=\text{'W'}$, wt must contain the n weights. Otherwise wt is not referenced.
Constraint: if weight = 'W'$\mathit{weight}=\text{'W'}$, wt(i)0.0${\mathbf{wt}}\left(\mathit{i}\right)\ge 0.0$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,{\mathbf{n}}$.

### Input Parameters Omitted from the MATLAB Interface

weight ldf iwk wk

### Output Parameters

1:     table(maxt) – double array
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 [Further Comments].
2:     ncells – int64int32nag_int scalar
The number of cells in the table.
3:     ndim – int64int32nag_int scalar
The number of factors defining the table.
4:     idim(nfac) – int64int32nag_int array
The first ndim elements contain the number of levels for the factors defining the table.
5:     icount(maxt) – int64int32nag_int array
A table containing the number of observations contributing to each cell of the table, stored identically to table.
6:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
 On entry, n < 2${\mathbf{n}}<2$, or nfac < 1${\mathbf{nfac}}<1$, or ldf < n$\mathit{ldf}<{\mathbf{n}}$, or typ ≠ 'D'${\mathbf{typ}}\ne \text{'D'}$ or 'C'$\text{'C'}$, or weight ≠ 'U'$\mathit{weight}\ne \text{'U'}$ or 'W'$\text{'W'}$, or percnt ≤ 0.0${\mathbf{percnt}}\le 0.0$, or percnt ≥ 100.0${\mathbf{percnt}}\ge 100.0$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, isf(i) > 0${\mathbf{isf}}\left(i\right)>0$ and lfac(i) ≤ 1${\mathbf{lfac}}\left(i\right)\le 1$, for some i$i$, or ifac(i,j) < 1${\mathbf{ifac}}\left(i,j\right)<1$, for some i,j$i,j$, or ifac(i,j) > lfac(j)${\mathbf{ifac}}\left(i,j\right)>{\mathbf{lfac}}\left(j\right)$, for some i,j$i,j$, or maxt is too small, or weight = 'W'$\mathit{weight}=\text{'W'}$ and wt(i) < 0.0${\mathbf{wt}}\left(i\right)<0.0$, for some i$i$.
ifail = 3${\mathbf{ifail}}=3$
At least one cell is empty.

## Accuracy

Not applicable.

The tables created by nag_contab_tabulate_percentile (g11bb) and stored in table and icount are stored in the following way. Let there be n$n$ factors defining the table with factor k$k$ having lk${l}_{k}$ levels, then the cell defined by the levels i1${i}_{1}$, i2,,in${i}_{2},\dots ,{i}_{n}$ of the factors is stored in the m$m$th cell given by:
 n m = 1 + ∑ [(ik − 1)ck], k = 1
$m=1+∑k=1n[(ik-1)ck],$
where cj = k = j + 1nlk${c}_{\mathit{j}}=\prod _{k=\mathit{j}+1}^{n}{l}_{k}$, for j = 1,2,,n1$\mathit{j}=1,2,\dots ,n-1$ and cn = 1${c}_{n}=1$.

## Example

```function nag_contab_tabulate_percentile_example
typ = 'C';
isf = [int64(0);1;1];
lfac = [int64(3);3;6];
ifac = [int64(1),1,1; ...
1,2,1; ...
1,3,1; ...
1,1,2; ...
1,2,2; ...
1,3,2; ...
1,1,3; ...
1,2,3; ...
1,3,3; ...
1,1,4; ...
1,2,4; ...
1,3,4; ...
1,1,5; ...
1,2,5; ...
1,3,5; ...
1,1,6; ...
1,2,6; ...
1,3,6; ...
2,1,1; ...
2,2,1; ...
2,3,1; ...
2,1,2; ...
2,2,2; ...
2,3,2; ...
2,1,3; ...
2,2,3; ...
2,3,3; ...
2,1,4; ...
2,2,4; ...
2,3,4; ...
2,1,5; ...
2,2,5; ...
2,3,5; ...
2,1,6; ...
2,2,6; ...
2,3,6; ...
3,1,1; ...
3,2,1; ...
3,3,1; ...
3,1,2; ...
3,2,2; ...
3,3,2; ...
3,1,3; ...
3,2,3; ...
3,3,3; ...
3,1,4; ...
3,2,4; ...
3,3,4; ...
3,1,5; ...
3,2,5; ...
3,3,5; ...
3,1,6; ...
3,2,6; ...
3,3,6];
percnt = 50;
y = [274;
361;
253;
325;
317;
339;
326;
402;
336;
379;
345;
361;
352;
334;
318;
339;
393;
358;
350;
340;
203;
397;
356;
298;
382;
376;
355;
418;
387;
379;
432;
339;
293;
322;
417;
342;
82;
297;
133;
306;
352;
361;
220;
333;
270;
388;
379;
274;
336;
307;
266;
389;
333;
353];
maxt = int64(18);
[table, ncells, ndim, idim, icount, ifail] = ...
nag_contab_tabulate_percentile(typ, isf, lfac, ifac, percnt, y, maxt)
```
```

table =

226.0000
320.2500
299.5000
385.7500
348.0000
334.7500
329.2500
343.2500
365.2500
370.5000
327.2500
378.0000
185.5000
328.7500
319.5000
339.2500
286.2500
350.2500

ncells =

18

ndim =

2

idim =

3
6
0

icount =

3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3

ifail =

0

```
```function g11bb_example
typ = 'C';
isf = [int64(0);1;1];
lfac = [int64(3);3;6];
ifac = [int64(1),1,1; ...
1,2,1; ...
1,3,1; ...
1,1,2; ...
1,2,2; ...
1,3,2; ...
1,1,3; ...
1,2,3; ...
1,3,3; ...
1,1,4; ...
1,2,4; ...
1,3,4; ...
1,1,5; ...
1,2,5; ...
1,3,5; ...
1,1,6; ...
1,2,6; ...
1,3,6; ...
2,1,1; ...
2,2,1; ...
2,3,1; ...
2,1,2; ...
2,2,2; ...
2,3,2; ...
2,1,3; ...
2,2,3; ...
2,3,3; ...
2,1,4; ...
2,2,4; ...
2,3,4; ...
2,1,5; ...
2,2,5; ...
2,3,5; ...
2,1,6; ...
2,2,6; ...
2,3,6; ...
3,1,1; ...
3,2,1; ...
3,3,1; ...
3,1,2; ...
3,2,2; ...
3,3,2; ...
3,1,3; ...
3,2,3; ...
3,3,3; ...
3,1,4; ...
3,2,4; ...
3,3,4; ...
3,1,5; ...
3,2,5; ...
3,3,5; ...
3,1,6; ...
3,2,6; ...
3,3,6];
percnt = 50;
y = [274;
361;
253;
325;
317;
339;
326;
402;
336;
379;
345;
361;
352;
334;
318;
339;
393;
358;
350;
340;
203;
397;
356;
298;
382;
376;
355;
418;
387;
379;
432;
339;
293;
322;
417;
342;
82;
297;
133;
306;
352;
361;
220;
333;
270;
388;
379;
274;
336;
307;
266;
389;
333;
353];
maxt = int64(18);
[table, ncells, ndim, idim, icount, ifail] = ...
g11bb(typ, isf, lfac, ifac, percnt, y, maxt)
```
```

table =

226.0000
320.2500
299.5000
385.7500
348.0000
334.7500
329.2500
343.2500
365.2500
370.5000
327.2500
378.0000
185.5000
328.7500
319.5000
339.2500
286.2500
350.2500

ncells =

18

ndim =

2

idim =

3
6
0

icount =

3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3

ifail =

0

```