hide long namesshow long names
hide short namesshow short names
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:
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 11 and 22 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 × 32×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 3737, the median being the 50% quantile. Other quantiles can also be computed: the pp percent quantile or percentile, qpqp, is the estimate of the value such that pp percent of observations are less than qpqp. This is calculated in two different ways depending on whether the tabulated variable is continuous or discrete. Let there be mm values in a cell and let y(1)y(1), y(2),,y(m)y(2),,y(m) be the values for that cell sorted into ascending order. Also, associated with each value there is a weight, w(1)w(1), w(2),, w(m)w(2),, w(m), which could represent the observed frequency for that value, with Wj = i = 1jw(i)Wj=i=1jw(i) and Wj = i = 1jw(i)(1/2)w(j)Wj=i=1jw(i)-12w(j). For the pp percentile let pw = (p / 100)Wmpw=(p/100)Wm and pw = (p / 100)Wmpw=(p/100)Wm, 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 ​Wj1 < pW < Wj
(y(j + 1) + y(j))/2 if ​pw = Wj.
y(j) if ​Wj-1<pW<Wj y(j+1)+y(j)2 if ​pw=Wj.
If the data is continuous then the quantiles are estimated by linear interpolation.
y(1) if ​ pwW1
(1f)y(j1) + fy(j) if ​ Wj 1 < pwWj
y(m) if ​ pw > Wm,
y(1) if ​ pwW1 (1-f)y(j- 1)+fy(j) if ​ Wj- 1<pwWj y(m) if ​ pw>Wm,
where f = (pwWj1) / (WjWj1)f=(pw-Wj-1)/(Wj-Wj-1).

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'typ='D'
The percentiles are computed for a discrete variable.
typ = 'C'typ='C'
The percentiles are computed for a continuous variable using linear interpolation.
Constraint: typ = 'D'typ='D' or 'C''C'.
2:     isf(nfac) – int64int32nag_int array
nfac, the dimension of the array, must satisfy the constraint nfac1nfac1.
Indicates which factors in ifac are to be used in the tabulation.
If isf(i) > 0isfi>0 the iith factor in ifac is included in the tabulation.
Note that if isf(i)0isfi0, for i = 1,2,,nfaci=1,2,,nfac then the statistic for the whole sample is calculated and returned in a 1 × 11×1 table.
3:     lfac(nfac) – int64int32nag_int array
nfac, the dimension of the array, must satisfy the constraint nfac1nfac1.
The number of levels of the classifying factors in ifac.
Constraint: if isf(i) > 0isfi>0, lfac(i)2lfaci2, for i = 1,2,,nfaci=1,2,,nfac.
4:     ifac(ldf,nfac) – int64int32nag_int array
ldf, the first dimension of the array, must satisfy the constraint ldfnldfn.
The nfac coded classification factors for the n observations.
Constraint: 1ifac(i,j)lfac(j)1ifacijlfacj, for i = 1,2,,ni=1,2,,n and j = 1,2,,nfacj=1,2,,nfac.
5:     percnt – double scalar
pp, the percentile to be tabulated.
Constraint: 0.0 < p < 100.00.0<p<100.0.
6:     y(n) – double array
n, the dimension of the array, must satisfy the constraint n2n2.
The variable to be tabulated.
7:     maxt – int64int32nag_int scalar
The maximum size of the table to be computed.
Constraint: maxtmaxt 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: n2n2.
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: nfac1nfac1.
3:     wt( : :) – double array
Note: the dimension of the array wt must be at least nn if weight = 'W'weight='W', and at least 11 otherwise.
If weight = 'W'weight='W', wt must contain the n weights. Otherwise wt is not referenced.
Constraint: if weight = 'W'weight='W', wt(i)0.0wti0.0, for i = 1,2,,ni=1,2,,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
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:
  ifail = 1ifail=1
On entry,n < 2n<2,
ornfac < 1nfac<1,
orldf < nldf<n,
ortyp'D'typ'D' or 'C''C',
orweight'U'weight'U' or 'W''W',
orpercnt0.0percnt0.0,
orpercnt100.0percnt100.0.
  ifail = 2ifail=2
On entry,isf(i) > 0isfi>0 and lfac(i)1lfaci1, for some ii,
orifac(i,j) < 1ifacij<1, for some i,ji,j,
orifac(i,j) > lfac(j)ifacij>lfacj, for some i,ji,j,
ormaxt is too small,
orweight = 'W'weight='W' and wt(i) < 0.0wti<0.0, for some ii.
  ifail = 3ifail=3
At least one cell is empty.

Accuracy

Not applicable.

Further Comments

The tables created by nag_contab_tabulate_percentile (g11bb) and stored in table and icount are stored in the following way. Let there be nn factors defining the table with factor kk having lklk levels, then the cell defined by the levels i1i1, i2,,ini2,,in of the factors is stored in the mmth cell given by:
n
m = 1 + [(ik1)ck],
k = 1
m=1+k=1n[(ik-1)ck],
where cj = k = j + 1nlkcj=k=j+1nlk, for j = 1,2,,n1j=1,2,,n-1 and cn = 1cn=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



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–2013