NAG Library Routine Document

g11bbf  (tabulate_percentile)

 Contents

    1  Purpose
    7  Accuracy

1
Purpose

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

2
Specification

Fortran Interface
Subroutine g11bbf ( typ, weight, n, nfac, isf, lfac, ifac, ldf, percnt, y, wt, table, maxt, ncells, ndim, idim, icount, iwk, wk, ifail)
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
C Header Interface
#include nagmk26.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)

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, qp, is the estimate of the value such that p percent of observations are less than qp. 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 y1, y2,,ym be the values for that cell sorted into ascending order. Also, associated with each value there is a weight, w1, w2,, wm, which could represent the observed frequency for that value, with Wj=i=1jwi and Wj=i=1jwi-12wj. For the p percentile let pw=p/100Wm and pw=p/100Wm, 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<pW<Wj yj+1+yj2 if ​pw=Wj.  
If the data is continuous then the quantiles are estimated by linear interpolation.
y1 if ​ pwW1 1-fyj- 1+fyj if ​ Wj- 1<pwWj ym if ​ pw>Wm,  
where f=pw-Wj-1/Wj-Wj-1.

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
Arguments

1:     typ – Character(1)Input
On entry: indicates if the variable to be tabulated is discrete or continuous.
typ='D'
The percentiles are computed for a discrete variable.
typ='C'
The percentiles are computed for a continuous variable using linear interpolation.
Constraint: typ='D' or 'C'.
2:     weight – Character(1)Input
On entry: indicates if there are weights associated with the variable to be tabulated.
weight='U'
Weights are not input and unit weights are assumed.
weight='W'
Weights must be supplied in wt.
Constraint: weight='U' or 'W'.
3:     n – IntegerInput
On entry: the number of observations.
Constraint: n2.
4:     nfac – IntegerInput
On entry: the number of classifying factors in ifac.
Constraint: nfac1.
5:     isfnfac – Integer arrayInput
On entry: indicates which factors in ifac are to be used in the tabulation.
If isfi>0 the ith factor in ifac is included in the tabulation.
Note that if isfi0, for i=1,2,,nfac then the statistic for the whole sample is calculated and returned in a 1×1 table.
6:     lfacnfac – Integer arrayInput
On entry: the number of levels of the classifying factors in ifac.
Constraint: if isfi>0, lfaci2, for i=1,2,,nfac.
7:     ifacldfnfac – Integer arrayInput
On entry: the nfac coded classification factors for the n observations.
Constraint: 1ifacijlfacj, for i=1,2,,n and j=1,2,,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: ldfn.
9:     percnt – Real (Kind=nag_wp)Input
On entry: p, the percentile to be tabulated.
Constraint: 0.0<p<100.0.
10:   yn – 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 n if weight='W', and at least 1 otherwise.
On entry: if weight='W', wt must contain the n weights. Otherwise wt is not referenced.
Constraint: if weight='W', wti0.0, for i=1,2,,n.
12:   tablemaxt – 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 9.
13:   maxt – IntegerInput
On entry: the maximum size of the table to be computed.
Constraint: maxt 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:   idimnfac – Integer arrayOutput
On exit: the first ndim elements contain the number of levels for the factors defining the table.
17:   icountmaxt – Integer arrayOutput
On exit: a table containing the number of observations contributing to each cell of the table, stored identically to table.
18:   iwk2×nfac+n – Integer arrayWorkspace
19:   wk2×n – Real (Kind=nag_wp) arrayWorkspace
20:   ifail – IntegerInput/Output
On entry: ifail must be set to 0, -1​ or ​1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ 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 argument, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6
Error Indicators and Warnings

If on entry 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:
ifail=1
On entry,n<2,
ornfac<1,
orldf<n,
ortyp'D' or 'C',
orweight'U' or 'W',
orpercnt0.0,
orpercnt100.0.
ifail=2
On entry,isfi>0 and lfaci1, for some i,
orifacij<1, for some i,j,
orifacij>lfacj, for some i,j,
ormaxt is too small,
orweight='W' and wti<0.0, for some i.
ifail=3
At least one cell is empty.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

7
Accuracy

Not applicable.

8
Parallelism 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.

9
Further Comments

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 lk levels, then the cell defined by the levels i1, i2,,in of the factors is stored in the mth cell given by:
m=1+k=1nik-1ck,  
where cj=k=j+1nlk, for j=1,2,,n-1 and cn=1.

10
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.

10.1
Program Text

Program Text (g11bbfe.f90)

10.2
Program Data

Program Data (g11bbfe.d)

10.3
Program Results

Program Results (g11bbfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017