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)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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

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'
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:     isfnfac int64int32nag_int array
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.
3:     lfacnfac int64int32nag_int array
The number of levels of the classifying factors in ifac.
Constraint: if isfi>0, lfaci2, for i=1,2,,nfac.
4:     ifacldfnfac int64int32nag_int array
ldf, the first dimension of the array, must satisfy the constraint ldfn.
The nfac coded classification factors for the n observations.
Constraint: 1ifacijlfacj, for i=1,2,,n and j=1,2,,nfac.
5:     percnt – double scalar
p, the percentile to be tabulated.
Constraint: 0.0<p<100.0.
6:     yn – double array
The variable to be tabulated.
7:     maxt int64int32nag_int scalar
The maximum size of the table to be computed.
Constraint: maxt 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.
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.
3:     wt: – double array
The dimension of the array wt must be at least n if weight='W', and at least 1 otherwise
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.

Output Parameters

1:     tablemaxt – 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:     ncells int64int32nag_int scalar
The number of cells in the table.
3:     ndim int64int32nag_int scalar
The number of factors defining the table.
4:     idimnfac int64int32nag_int array
The first ndim elements contain the number of levels for the factors defining the table.
5:     icountmaxt 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=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
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.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

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

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