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_margin (g11bc)

## Purpose

nag_contab_tabulate_margin (g11bc) computes a marginal table from a table computed by nag_contab_tabulate_stat (g11ba) or nag_contab_tabulate_percentile (g11bb) using a selected statistic.

## Syntax

[stable, mcells, mdim, mlevel, auxt, ifail] = g11bc(stat, table, idim, isdim, maxst, 'ncells', ncells, 'ndim', ndim)
[stable, mcells, mdim, mlevel, auxt, ifail] = nag_contab_tabulate_margin(stat, table, idim, isdim, maxst, 'ncells', ncells, 'ndim', ndim)

## Description

For a dataset containing classification variables (known as factors) the functions nag_contab_tabulate_stat (g11ba) and nag_contab_tabulate_percentile (g11bb) compute a table using selected statistics, for example the mean or the median. The table is indexed by the levels of the selected factors, for example if there were three factors A, B and C with $3$, $2$ and $4$ levels respectively and the mean was to be tabulated the resulting table would be $3×2×4$ with each cell being the mean of all observations with the appropriate combination of levels of the three factors. In further analysis the table of means averaged over C for A and B may be required; this can be computed from the full table by taking the mean over the third dimension of the table, C.
In general, given a table computed by nag_contab_tabulate_stat (g11ba) or nag_contab_tabulate_percentile (g11bb), nag_contab_tabulate_margin (g11bc) computes a sub-table defined by a subset of the factors used to define the table such that each cell of the sub-table is the selected statistic computed over the remaining factors. The statistics that can be used are the total, the mean, the median, the variance, the smallest and the largest value.

## 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 used to compute the marginal table.
${\mathbf{stat}}=\text{'T'}$
The total.
${\mathbf{stat}}=\text{'A'}$
The average or mean.
${\mathbf{stat}}=\text{'M'}$
The median.
${\mathbf{stat}}=\text{'V'}$
The variance.
${\mathbf{stat}}=\text{'L'}$
The largest value.
${\mathbf{stat}}=\text{'S'}$
The smallest value.
Constraint: ${\mathbf{stat}}=\text{'T'}$, $\text{'A'}$, $\text{'M'}$, $\text{'V'}$, $\text{'L'}$ or $\text{'S'}$.
2:     $\mathrm{table}\left({\mathbf{ncells}}\right)$ – double array
3:     $\mathrm{idim}\left({\mathbf{ndim}}\right)$int64int32nag_int array
The number of levels for each dimension of table as returned by nag_contab_tabulate_stat (g11ba) or nag_contab_tabulate_percentile (g11bb).
Constraint: ${\mathbf{idim}}\left(\mathit{i}\right)\ge 2$, for $\mathit{i}=1,2,\dots ,{\mathbf{ndim}}$.
4:     $\mathrm{isdim}\left({\mathbf{ndim}}\right)$int64int32nag_int array
Indicates which dimensions of table are to be included in the sub-table. If ${\mathbf{isdim}}\left(i\right)>0$ the dimension or factor indicated by ${\mathbf{idim}}\left(i\right)$ is to be included in the sub-table, otherwise it is excluded.
5:     $\mathrm{maxst}$int64int32nag_int scalar
The maximum size of sub-table to be computed.
Constraint: ${\mathbf{maxst}}\ge \text{}$ the product of the levels of the dimensions of table included in the sub-table, stable.

### Optional Input Parameters

1:     $\mathrm{ncells}$int64int32nag_int scalar
Default: the dimension of the array table.
2:     $\mathrm{ndim}$int64int32nag_int scalar
Default: the dimension of the arrays idim, isdim. (An error is raised if these dimensions are not equal.)
The number of dimensions for table as returned by nag_contab_tabulate_stat (g11ba) or nag_contab_tabulate_percentile (g11bb).
Constraint: ${\mathbf{ndim}}\ge 2$.

### Output Parameters

1:     $\mathrm{stable}\left({\mathbf{maxst}}\right)$ – double array
The first mcells elements contain the sub-table computed using the statistic indicated by stat. The table is stored in a similar way to table with the mcells cells stored so that for any two dimensions the index relating to the dimension given later in idim changes faster. For further details see Further Comments.
2:     $\mathrm{mcells}$int64int32nag_int scalar
The number of cells in the sub-table in stable.
3:     $\mathrm{mdim}$int64int32nag_int scalar
The number of dimensions to the sub-table in stable.
4:     $\mathrm{mlevel}\left({\mathbf{ndim}}\right)$int64int32nag_int array
The first mdim elements contain the number of levels for the dimensions of the sub-table in stable. The remaining elements are not referenced.
5:     $\mathrm{auxt}\left(:\right)$ – double array
The dimension of the array auxt will be ${\mathbf{maxst}}$ if ${\mathbf{stat}}=\text{'V'}$ and $1$ otherwise
If ${\mathbf{stat}}=\text{'V'}$ auxt contains the sub-table of means corresponding to the sub-table of variances in stable. Otherwise auxt is not referenced.
6:     $\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{ndim}}<2$, or ${\mathbf{stat}}\ne \text{'T'}$, $\text{'A'}$, $\text{'M'}$, $\text{'V'}$, $\text{'L'}$ or $\text{'S'}$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{idim}}\left(i\right)\le 1$, for some $i=1,2,\dots ,{\mathbf{ndim}}$, or ncells is incompatible with idim, or the requested sub-table is of dimension $0$, or the requested sub-table is the full table, or maxst is too small, the minimum value is returned in mdim.
${\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 describe in West (1979).

The sub-tables created by nag_contab_tabulate_margin (g11bc) and stored in stable and, depending on stat, also in auxt are stored in the following way. Let there be $m$ dimensions defining the table with dimension $k$ having ${l}_{k}$ levels, then the cell defined by the levels ${i}_{1},{i}_{2},\dots ,{i}_{m}$ of the factors is stored in $s$th cell given by
 $s=1+∑k=1mik-1ck,$
where
 $cj=∏k=j+ 1m lk for ​ j= 1, 2,…, n- 1 and cm=1.$

## Example

The data, given by John and Quenouille (1977), is for 3 blocks of a $3×6$ factorial experiment. The data can be considered as a $3×6×3$ table (i.e., blocks $×$ treatment with $6$ levels $×$ treatment with $3$ levels). This table is input and the $6×3$ table of treatment means for over blocks is computed and printed.
```function g11bc_example

fprintf('g11bc example results\n\n');

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];

idim   = [int64(3); 6; 3];
isdim  = [int64(0); 1; 1];
maxt   = prod(idim(isdim~=0));
maxt   = int64(maxt);

% Compute marginal table
stat = 'A';
[table, mcells, mdim, mlevel, auxt, ifail] = ...
g11bc( ...
stat, y, idim, isdim, maxt);

% Display results
fprintf(' Marginal Table\n\n');
ncol = mlevel(mdim);
nrow = mcells/ncol;
table  = transpose(reshape(table,[ncol,nrow]));
for i = 1:nrow
fprintf('%8.2f', table(i,:));
fprintf('\n');
end

```
```g11bc example results

Marginal Table

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

Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015