NAG Library Function Document
nag_tabulate_percentile (g11bbc)
1
Purpose
nag_tabulate_percentile (g11bbc) computes a table from a set of classification factors using a given percentile or quantile, for example the median.
2
Specification
#include <nag.h> |
#include <nagg11.h> |
void |
nag_tabulate_percentile (Nag_TabulateVar type,
Integer n,
Integer nfac,
const Integer sf[],
const Integer lfac[],
const Integer factor[],
Integer tdf,
double percnt,
const double y[],
const double wt[],
double table[],
Integer maxt,
Integer *ncells,
Integer *ndim,
Integer idim[],
Integer count[],
NagError *fail) |
|
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 by 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 percent quantile or percentile, , is the estimate of the value such that percent of observations are less than . This is calculated in two different ways depending on whether the tabulated variable is continuous or discrete. Let there be values in a cell and let , be the values for that cell sorted into ascending order. Also, associated with each value there is a weight, , , which could represent the observed frequency for that value, with and . For the percentile let and then the percentiles for the two cases are as given below.
If the variable is discrete, that is takes only a limited number of (usually integer) values then the percentile is defined as:
If the data is continuous then the quantiles are estimated by linear interpolation.
where
.
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:
– Nag_TabulateVarInput
-
On entry: indicates whether the variable to be tabulated is discrete or continuous.
- The percentiles are computed for a discrete variable.
- The percentiles are computed for a continuous variable using linear interpolation.
Constraint:
or .
- 2:
– IntegerInput
-
On entry: the number of observations.
Constraint:
.
- 3:
– IntegerInput
-
On entry: the number of classifying factors in
factor.
Constraint:
.
- 4:
– const IntegerInput
-
On entry: indicates which factors in
factor are to be used in the tabulation.
If
the
th factor in
factor is included in the tabulation.
Note that if , for then the statistic for the whole sample is calculated and returned in a 1 by 1 table.
- 5:
– const IntegerInput
-
On entry: the number of levels of the classifying factors in
factor.
Constraint:
if , , for .
- 6:
– const IntegerInput
-
On entry: the
nfac coded classification factors for the
n observations.
Constraint:
if , , for and .
- 7:
– IntegerInput
-
On entry: the stride separating matrix column elements in the array
factor.
Constraint:
.
- 8:
– doubleInput
-
On entry: the percentile to be tabulated, .
Constraint:
.
- 9:
– const doubleInput
-
On entry: the variable to be tabulated.
- 10:
– const doubleInput
-
On entry:
wt must contain the
n weights. Otherwise
wt must be set to
NULL.
Constraint:
, for .
- 11:
– doubleOutput
-
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
factor changes faster. For further details see
Section 9.
- 12:
– IntegerInput
-
On entry: the maximum size of the table to be computed.
Constraint:
product of the levels of the factors included in the tabulation.
- 13:
– Integer *Output
-
On exit: the number of cells in the table.
- 14:
– Integer *Output
-
On exit: the number of factors defining the table.
- 15:
– IntegerOutput
-
On exit: the first
ndim elements contain the number of levels for the factors defining the table.
- 16:
– IntegerOutput
-
On exit: a table containing the number of observations contributing to each cell of the table, stored identically to
table.
- 17:
– NagError *Input/Output
-
The NAG error argument (see
Section 3.7 in How to Use the NAG Library and its Documentation).
6
Error Indicators and Warnings
- NE_2_INT_ARG_LT
-
On entry, while . These arguments must satisfy .
- NE_2_INT_ARRAY_CONS
-
On entry, while .
Constraint: if , , for .
- NE_2D_1D_INT_ARRAYS_CONS
-
On entry, while .
Constraint: , for and .
- NE_2D_INT_ARRAY_CONS
-
On entry, .
Constraint: , for and .
- NE_ALLOC_FAIL
-
Dynamic memory allocation failed.
- NE_BAD_PARAM
-
On entry, argument
type had an illegal value.
- NE_CELL_EMPTY
-
At least one cell is empty.
- NE_INT_ARG_LT
-
On entry, .
Constraint: .
On entry, .
Constraint: .
- NE_INTERNAL_ERROR
-
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact
NAG for assistance.
- NE_MAXT
-
The maximum size of the table to be computed,
maxt is too small.
- NE_REAL
-
On entry, .
Constraint: .
- NE_REAL_ARRAY_CONS
-
On entry, .
Constraint: , for .
7
Accuracy
Not applicable.
8
Parallelism and Performance
nag_tabulate_percentile (g11bbc) is not threaded in any implementation.
The tables created by
nag_tabulate_percentile (g11bbc) and stored in
table and
count are stored in the following way. Let there be
factors defining the table with factor
having
levels, then the cell defined by the levels
of the factors is stored in
th cell given by:
where
, for
and
.
10
Example
The data, given by
John and Quenouille (1977), are for a 3 by 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 by 6 table of treatment medians for yield over blocks is computed and printed.
10.1
Program Text
Program Text (g11bbce.c)
10.2
Program Data
Program Data (g11bbce.d)
10.3
Program Results
Program Results (g11bbce.r)