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

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_contab_tabulate_stat (g11ba)

## Purpose

nag_contab_tabulate_stat (g11ba) computes a table from a set of classification factors using a selected statistic.

## Syntax

[table, ncells, ndim, idim, icount, auxt, ifail] = g11ba(stat, update, weight, isf, lfac, ifac, y, wt, table, ncells, icount, auxt, 'n', n, 'nfac', nfac, 'maxt', maxt)
[table, ncells, ndim, idim, icount, auxt, ifail] = nag_contab_tabulate_stat(stat, update, weight, isf, lfac, ifac, y, wt, table, ncells, icount, auxt, 'n', n, 'nfac', nfac, 'maxt', maxt)

## 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 average age could be computed:
 Sex Habitat Inner-city Suburban Rural Male 25.5 30.3 35.6 Female 23.2 29.1 30.4
That is the average age for all observations for males living in rural areas is $35.6$. Other statistics can also be computed: the number of observations, the total, the variance, the largest value and the smallest value.
nag_contab_tabulate_stat (g11ba) computes a table for one of the selected statistics. The factors have to be coded with levels $1,2,\dots \text{}$. Weights can be used to eliminate values from the calculations, e.g., if they represent ‘missing values’. There is also the facility to update an existing table with the addition of new observations.

## 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
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{stat}$ – string (length ≥ 1)
Indicates which statistic is to be computed for the table cells.
${\mathbf{stat}}=\text{'N'}$
The number of observations for each cell.
${\mathbf{stat}}=\text{'T'}$
The total for the variable in y for each cell.
${\mathbf{stat}}=\text{'A'}$
The average (mean) for the variable in y for each cell.
${\mathbf{stat}}=\text{'V'}$
The variance for the variable in y for each cell.
${\mathbf{stat}}=\text{'L'}$
The largest value for the variable in y for each cell.
${\mathbf{stat}}=\text{'S'}$
The smallest value for the variable in y for each cell.
Constraint: ${\mathbf{stat}}=\text{'N'}$, $\text{'T'}$, $\text{'A'}$, $\text{'V'}$, $\text{'L'}$ or $\text{'S'}$.
2:     $\mathrm{update}$ – string (length ≥ 1)
Indicates if an existing table is to be updated by further observation.
${\mathbf{update}}=\text{'I'}$
The table cells will be initialized to zero before tabulations take place.
${\mathbf{update}}=\text{'U'}$
The table input in table will be updated. The arguments ncells, table, icount and auxt must remain unchanged from the previous call to nag_contab_tabulate_stat (g11ba).
Constraint: ${\mathbf{update}}=\text{'I'}$ or $\text{'U'}$.
3:     $\mathrm{weight}$ – string (length ≥ 1)
Indicates if weights are to be used.
${\mathbf{weight}}=\text{'U'}$
Weights are not used and unit weights are assumed.
${\mathbf{weight}}=\text{'W'}$ or $\text{'V'}$
Weights are used and must be supplied in wt. The only difference between ${\mathbf{weight}}=\text{'W'}$ and ${\mathbf{weight}}=\text{'V'}$ is if the variance is computed.
${\mathbf{weight}}=\text{'W'}$
The divisor for the variance is the sum of the weights minus one and if ${\mathbf{weight}}=\text{'V'}$, the divisor is the number of observations with nonzero weights minus one. The former is useful if the weights represent the frequency of the observed values.
If ${\mathbf{stat}}=\text{'T'}$ or $\text{'A'}$, the weighted total or mean is computed respectively.
If ${\mathbf{stat}}=\text{'N'}$, $\text{'L'}$ or $\text{'S'}$, the only effect of weights is to eliminate values with zero weights from the computations.
Constraint: ${\mathbf{weight}}=\text{'U'}$, $\text{'V'}$ or $\text{'W'}$.
4:     $\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.
5:     $\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}=\mathrm{Ai},\dots ,\mathrm{Ai}$.
6:     $\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}}$.
7:     $\mathrm{y}\left({\mathbf{n}}\right)$ – double array
The variable to be tabulated. If ${\mathbf{stat}}=\text{'N'}$, y is not referenced.
8:     $\mathrm{wt}\left(:\right)$ – double array
The dimension of the array wt must be at least ${\mathbf{n}}$ if ${\mathbf{weight}}=\text{'W'}$ or $\text{'V'}$, and at least $1$ otherwise
If ${\mathbf{weight}}=\text{'W'}$ or $\text{'V'}$, wt must contain the n weights. Otherwise wt is not referenced.
Constraint: if ${\mathbf{weight}}=\text{'W'}$ or $\text{'V'}$, ${\mathbf{wt}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=\mathrm{Ai},\dots ,\mathrm{Ai}$.
9:     $\mathrm{table}\left({\mathbf{maxt}}\right)$ – double array
If ${\mathbf{update}}=\text{'U'}$, table must be unchanged from the previous call to nag_contab_tabulate_stat (g11ba), otherwise table need not be set.
10:   $\mathrm{ncells}$int64int32nag_int scalar
If ${\mathbf{update}}=\text{'U'}$, ncells must be unchanged from the previous call to nag_contab_tabulate_stat (g11ba), otherwise ncells need not be set.
11:   $\mathrm{icount}\left({\mathbf{maxt}}\right)$int64int32nag_int array
If ${\mathbf{update}}=\text{'U'}$, icount must be unchanged from the previous call to nag_contab_tabulate_stat (g11ba), otherwise icount need not be set.
12:   $\mathrm{auxt}\left(:\right)$ – double array
The dimension of the array auxt must be at least ${\mathbf{ncells}}$ if ${\mathbf{stat}}=\text{'A'}$, $2×{\mathbf{ncells}}$ if ${\mathbf{stat}}=\text{'V'}$, and at least $1$ otherwise
If ${\mathbf{update}}=\text{'U'}$, auxt must be unchanged from the previous call to nag_contab_tabulate_stat (g11ba), otherwise auxt need not be set.

### 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{maxt}$int64int32nag_int scalar
Default: the dimension of the arrays table, icount. (An error is raised if these dimensions are not equal.)
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.

### 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 referred to 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. Note if ${\mathbf{stat}}=\text{'N'}$ this is the same as is returned in table.
6:     $\mathrm{auxt}\left(:\right)$ – double array
The dimension of the array auxt will be ${\mathbf{ncells}}$ if ${\mathbf{stat}}=\text{'A'}$, $2×{\mathbf{ncells}}$ if ${\mathbf{stat}}=\text{'V'}$ and $1$ otherwise
If ${\mathbf{stat}}=\text{'A'}$ or $\text{'V'}$, the first ncells values hold the table containing the sum of the weights for the observations contributing to each cell, stored identically to table.
If ${\mathbf{stat}}=\text{'V'}$, the second set of ncells values hold the table of cell means. Otherwise auxt is not referenced.
7:     $\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{update}}\ne \text{'I'}$ or $\text{'U'}$, or ${\mathbf{weight}}\ne \text{'U'}$, $\text{'W'}$ or $\text{'V'}$, or ${\mathbf{stat}}\ne \text{'N'}$, $\text{'T'}$, $\text{'A'}$, $\text{'V'}$, $\text{'L'}$ or $\text{'S'}$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{isf}}\left(i\right)>0$ and ${\mathbf{lfac}}\left(i\right)<2$, 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 ${\mathbf{weight}}=\text{'W'}$ or $\text{'V'}$ and ${\mathbf{wt}}\left(i\right)<0.0$, for some $i$.
${\mathbf{ifail}}=3$
${\mathbf{stat}}=\text{'V'}$ and the divisor for the variance is $\text{}\le 0.0$.
${\mathbf{ifail}}=4$
${\mathbf{update}}=\text{'U'}$ and at least one of ncells, table, auxt or icount have been changed since previous call to nag_contab_tabulate_stat (g11ba).
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

Only applicable when ${\mathbf{stat}}=\text{'V'}$. In this case a one pass algorithm is used as described by West (1979).

The tables created by nag_contab_tabulate_stat (g11ba) and stored in table, icount and, depending on stat, also in auxt 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}_{j}=\prod _{k=j+1}^{n}{l}_{k}$, for $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. The $3×6$ table of treatment means for yield over blocks is computed and printed.
```function g11ba_example

fprintf('g11ba 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];

weight = 'U';
wt     = [];

stat   = 'A';
update = 'I';
lfac   = [int64(3); 3; 6];
isf    = [int64(0); 1; 1];
maxt   = prod(lfac(isf~=0));

table  = zeros(maxt,1);
icount = zeros(maxt,1,'int64');
auxt   = zeros(2*maxt,1);
ncells = int64(0);

% Compute table
[table, ncells, ndim, idim, icount, auxt, ifail] = ...
g11ba( ...
stat, update, weight, isf, lfac, ifac, y, wt, ...
table, ncells, icount, auxt);

% Display results
fprintf(' Table\n\n');
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

```
```g11ba example results

Table

235.33( 3)  342.67( 3)  309.33( 3)  395.00( 3)  373.33( 3)  350.00( 3)
332.67( 3)  341.67( 3)  370.33( 3)  370.33( 3)  326.67( 3)  381.00( 3)
196.33( 3)  332.67( 3)  320.33( 3)  338.00( 3)  292.33( 3)  351.00( 3)
```