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

PDF version (NAG web site, 64-bit version, 64-bit version)
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:
 At Mark 24: weight was removed from the interface; wt was made 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$ 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)$.

## 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:     $\mathrm{typ}$ – string (length ≥ 1)
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:     $\mathrm{isf}\left({\mathbf{nfac}}\right)$int64int32nag_int array
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.
3:     $\mathrm{lfac}\left({\mathbf{nfac}}\right)$int64int32nag_int array
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}}$.
4:     $\mathrm{ifac}\left(\mathit{ldf},{\mathbf{nfac}}\right)$int64int32nag_int array
ldf, the first dimension of the array, must satisfy the constraint $\mathit{ldf}\ge {\mathbf{n}}$.
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}}$.
5:     $\mathrm{percnt}$ – double scalar
$p$, the percentile to be tabulated.
Constraint: $0.0.
6:     $\mathrm{y}\left({\mathbf{n}}\right)$ – double array
The variable to be tabulated.
7:     $\mathrm{maxt}$int64int32nag_int scalar
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.

### Optional Input Parameters

1:     $\mathrm{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: ${\mathbf{n}}\ge 2$.
2:     $\mathrm{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: ${\mathbf{nfac}}\ge 1$.
3:     $\mathrm{wt}\left(:\right)$ – double array
The dimension of the array wt must be at least ${\mathbf{n}}$ if $\mathit{weight}=\text{'W'}$, and at least $1$ otherwise
If $\mathit{weight}=\text{'W'}$, wt must contain the n weights. Otherwise wt is not referenced.
Constraint: if $\mathit{weight}=\text{'W'}$, ${\mathbf{wt}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.

### Output Parameters

1:     $\mathrm{table}\left({\mathbf{maxt}}\right)$ – 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 Further Comments.
2:     $\mathrm{ncells}$int64int32nag_int scalar
The number of cells in the table.
3:     $\mathrm{ndim}$int64int32nag_int scalar
The number of factors defining the table.
4:     $\mathrm{idim}\left({\mathbf{nfac}}\right)$int64int32nag_int array
The first ndim elements contain the number of levels for the factors defining the table.
5:     $\mathrm{icount}\left({\mathbf{maxt}}\right)$int64int32nag_int array
A table containing the number of observations contributing to each cell of the table, stored identically to table.
6:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<2$, or ${\mathbf{nfac}}<1$, or $\mathit{ldf}<{\mathbf{n}}$, or ${\mathbf{typ}}\ne \text{'D'}$ or $\text{'C'}$, or $\mathit{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 $\mathit{weight}=\text{'W'}$ and ${\mathbf{wt}}\left(i\right)<0.0$, for some $i$.
${\mathbf{ifail}}=3$
At least one cell is empty.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

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

## 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.
```function g11bb_example

fprintf('g11bb example results\n\n');

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];
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];

lfac   = [int64(3); 3; 6];
isf    = [int64(0); 1; 1];
maxt   = prod(lfac(isf~=0));
maxt   = int64(maxt);

typ    = 'C';
percnt = 50;

% Compute classification table
[table, ncells, ndim, idim, icount, ifail] = ...
g11bb( ...
typ, isf, lfac, ifac, percnt, y, maxt);

% Display results
fprintf(' Table for %4dth percentile\n\n', percnt);
ncol = idim(ndim);
nrow = ncells/ncol;
table  = transpose(reshape(table,[ncol,nrow]));
icount = transpose(reshape(icount,[ncol,nrow]));
for i = 1:nrow
row = [table(i,:); double(icount(i,:))];
fprintf('%8.2f(%2d)', row);
fprintf('\n');
end

```
```g11bb example results

Table for   50th percentile

226.00( 3)  320.25( 3)  299.50( 3)  385.75( 3)  348.00( 3)  334.75( 3)
329.25( 3)  343.25( 3)  365.25( 3)  370.50( 3)  327.25( 3)  378.00( 3)
185.50( 3)  328.75( 3)  319.50( 3)  339.25( 3)  286.25( 3)  350.25( 3)
```

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015