# NAG CL Interfaceg11bac (tabulate_​stat)

Settings help

CL Name Style:

## 1Purpose

g11bac computes a table from a set of classification factors using a selected statistic.

## 2Specification

 #include
 void g11bac (Nag_TableStats stat, Nag_TableUpdate update, Nag_Weightstype weight, Integer n, Integer nfac, const Integer sf[], const Integer lfac[], const Integer factor[], Integer tdf, const double y[], const double wt[], double table[], Integer maxt, Integer *ncells, Integer *ndim, Integer idim[], Integer count[], double comm_ar[], NagError *fail)
The function may be called by the names: g11bac, nag_contab_tabulate_stat or nag_tabulate_stats.

## 3Description

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:
Habitat
Sex 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:
Habitat
Sex 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.
g11bac computes a table for one of the selected statistics. The factors have to be coded with levels $1,2,\dots$. 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.
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

## 5Arguments

1: $\mathbf{stat}$Nag_TableStats Input
On entry: indicates which statistic is to be computed for the table cells.
${\mathbf{stat}}=\mathrm{Nag_TableStatsNObs}$
The number of observations for each cell.
${\mathbf{stat}}=\mathrm{Nag_TableStatsTotal}$
The total for the variable in y for each cell.
${\mathbf{stat}}=\mathrm{Nag_TableStatsAv}$
The average (mean) for the variable in y for each cell.
${\mathbf{stat}}=\mathrm{Nag_TableStatsVar}$
The variance for the variable in y for each cell.
${\mathbf{stat}}=\mathrm{Nag_TableStatsLarge}$
The largest value for the variable in y for each cell.
${\mathbf{stat}}=\mathrm{Nag_TableStatsSmall}$
The smallest value for the variable in y for each cell.
Constraint: ${\mathbf{stat}}=\mathrm{Nag_TableStatsNObs}$, $\mathrm{Nag_TableStatsTotal}$, $\mathrm{Nag_TableStatsAv}$, $\mathrm{Nag_TableStatsVar}$, $\mathrm{Nag_TableStatsLarge}$ or $\mathrm{Nag_TableStatsSmall}$.
2: $\mathbf{update}$Nag_TableUpdate Input
On entry: indicates if an existing table is to be updated by further observation.
${\mathbf{update}}=\mathrm{Nag_TableUpdateI}$
The table cells will be initialized to zero before tabulations take place.
${\mathbf{update}}=\mathrm{Nag_TableUpdateU}$
The table input in table will be updated. The arguments ncells, table, count and comm_ar must remain unchanged from the previous call to g11bac.
Constraint: ${\mathbf{update}}=\mathrm{Nag_TableUpdateI}$ or $\mathrm{Nag_TableUpdateU}$.
3: $\mathbf{weight}$Nag_Weightstype Input
On entry: indicates if weights are to be used.
${\mathbf{weight}}=\mathrm{Nag_NoWeights}$
Weights are not used and unit weights are assumed.
${\mathbf{weight}}=\mathrm{Nag_Weights}$ or $\mathrm{Nag_Weightsvar}$
Weights are used and must be supplied in wt. The only difference between ${\mathbf{weight}}=\mathrm{Nag_Weights}$ and ${\mathbf{weight}}=\mathrm{Nag_Weightsvar}$ is if the variance is computed.
${\mathbf{weight}}=\mathrm{Nag_Weights}$
The divisor for the variance is the sum of the weights minus one and if ${\mathbf{weight}}=\mathrm{Nag_Weightsvar}$, 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}}=\mathrm{Nag_TableStatsTotal}$ or $\mathrm{Nag_TableStatsAv}$, the weighted total or mean is computed respectively.
If ${\mathbf{stat}}=\mathrm{Nag_TableStatsNObs}$, $\mathrm{Nag_TableStatsLarge}$ or $\mathrm{Nag_TableStatsSmall}$ the only effect of weights is to eliminate values with zero weights from the computations.
Constraint: ${\mathbf{weight}}=\mathrm{Nag_NoWeights}$, $\mathrm{Nag_Weightsvar}$ or $\mathrm{Nag_Weights}$.
4: $\mathbf{n}$Integer Input
On entry: the number of observations.
Constraint: ${\mathbf{n}}\ge 2$.
5: $\mathbf{nfac}$Integer Input
On entry: the number of classifying factors in factor.
Constraint: ${\mathbf{nfac}}\ge 1$.
6: $\mathbf{sf}\left[{\mathbf{nfac}}\right]$const Integer Input
On entry: indicates which factors in factor are to be used in the tabulation.
If ${\mathbf{sf}}\left[i-1\right]>0$ the $i$th factor in factor is included in the tabulation.
Note that if ${\mathbf{sf}}\left[i-1\right]\le 0$ for $i=1,2,\dots ,{\mathbf{nfac}}$ then the statistic for the whole sample is calculated and returned in a 1 by 1 table.
7: $\mathbf{lfac}\left[{\mathbf{nfac}}\right]$const Integer Input
On entry: the number of levels of the classifying factors in factor.
Constraint: if ${\mathbf{sf}}\left[i-1\right]>0$, ${\mathbf{lfac}}\left[\mathit{i}-1\right]\ge 2$, for $\mathit{i}=1,2,\dots ,{\mathbf{nfac}}$.
8: $\mathbf{factor}\left[{\mathbf{n}}×{\mathbf{tdf}}\right]$const Integer Input
On entry: the nfac coded classification factors for the n observations.
Constraint: $1\le {\mathbf{factor}}\left[\left(\mathit{i}-1\right)×{\mathbf{tdf}}+\mathit{j}-1\right]\le {\mathbf{lfac}}\left[\mathit{j}-1\right]$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{nfac}}$.
9: $\mathbf{tdf}$Integer Input
On entry: the stride separating matrix column elements in the array factor.
Constraint: ${\mathbf{tdf}}\ge {\mathbf{nfac}}$.
10: $\mathbf{y}\left[{\mathbf{n}}\right]$const double Input
On entry: the variable to be tabulated.
If ${\mathbf{stat}}=\mathrm{Nag_TableStatsNObs}$, y is not referenced.
11: $\mathbf{wt}\left[{\mathbf{n}}\right]$const double Input
On entry: if ${\mathbf{weight}}=\mathrm{Nag_Weights}$ or $\mathrm{Nag_Weightsvar}$, wt must contain the n weights. Otherwise wt is not referenced and can be set to null, (double *)0.
Constraint: if ${\mathbf{weight}}=\mathrm{Nag_Weights}$ or $\mathrm{Nag_Weightsvar}$, ${\mathbf{wt}}\left[\mathit{i}-1\right]\ge 0.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
12: $\mathbf{table}\left[{\mathbf{maxt}}\right]$double Input/Output
On entry: if ${\mathbf{update}}=\mathrm{Nag_TableUpdateU}$, table must be unchanged from the previous call to g11bac, otherwise table need not be set.
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 referred to later in lfac and factor changes faster. For further details see Section 9.
13: $\mathbf{maxt}$Integer Input
On entry: the maximum size of the table to be computed.
Constraint: ${\mathbf{maxt}}\ge$ product of the levels of the factors included in the tabulation.
14: $\mathbf{ncells}$Integer * Input/Output
On entry: if ${\mathbf{update}}=\mathrm{Nag_TableUpdateU}$, ncells must be unchanged from the previous call to g11bac, otherwise ncells need not be set.
On exit: the number of cells in the table.
15: $\mathbf{ndim}$Integer * Output
On exit: the number of factors defining the table.
16: $\mathbf{idim}\left[{\mathbf{nfac}}\right]$Integer Output
On exit: the first ndim elements contain the number of levels for the factors defining the table.
17: $\mathbf{count}\left[{\mathbf{maxt}}\right]$Integer Input/Output
On entry: if ${\mathbf{update}}=\mathrm{Nag_TableUpdateU}$, count must be unchanged from the previous call to g11bac, otherwise count need not be set.
On exit: a table containing the number of observations contributing to each cell of the table, stored identically to table. Note if ${\mathbf{stat}}=\mathrm{Nag_TableStatsNObs}$ this is the same as is returned in table.
18: $\mathbf{comm_ar}\left[*\right]$double Input/Output
On entry: if ${\mathbf{update}}=\mathrm{Nag_TableUpdateU}$, comm_ar must be unchanged from the previous call to g11bac, otherwise comm_ar need not be set.
On exit: if ${\mathbf{stat}}=\mathrm{Nag_TableStatsAv}$ or $\mathrm{Nag_TableStatsVar}$, 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}}=\mathrm{Nag_TableStatsVar}$, then the second set of ncells values hold the table of cell means. Otherwise comm_ar is not referenced.
19: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_2_INT_ARG_LT
On entry, ${\mathbf{tdf}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{nfac}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{tdf}}\ge {\mathbf{nfac}}$.
NE_2_INT_ARRAY_CONS
On entry, ${\mathbf{sf}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$ while ${\mathbf{lfac}}\left[0\right]=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{sf}}\left[i\right]>0$, ${\mathbf{lfac}}\left[i\right]\ge 2$ for $i=0,1,\dots ,{\mathbf{nfac}}$.
NE_2D_1D_INT_ARRAYS_CONS
On entry, ${\mathbf{factor}}\left[\left(⟨\mathit{\text{value}}⟩\right)×{\mathbf{tdf}}+⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$ while ${\mathbf{lfac}}\left[0\right]=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{factor}}\left[\left(\mathit{i}\right)×{\mathbf{tdf}}+\mathit{j}\right]\le {\mathbf{lfac}}\left[\mathit{j}\right]$, for $\mathit{i}=0,1,\dots ,{\mathbf{n}}-1$ and $\mathit{j}=0,1,\dots ,{\mathbf{nfac}}-1$.
NE_2D_INT_ARRAY_CONS
On entry, ${\mathbf{factor}}\left[\left(⟨\mathit{\text{value}}⟩\right)×{\mathbf{tdf}}+⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{factor}}\left[\left(\mathit{i}\right)×{\mathbf{tdf}}+\mathit{j}\right]\ge 1$, for $\mathit{i}=0,1,\dots ,{\mathbf{n}}-1$ and $\mathit{j}=0,1,\dots ,{\mathbf{nfac}}-1$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument stat had an illegal value.
On entry, argument update had an illegal value.
On entry, argument weight had an illegal value.
NE_G11BA_CHANGED
${\mathbf{update}}=\mathrm{Nag_TableUpdateU}$ and at least one of ncells, table, comm_ar or count have been changed since previous call to g11bac.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 2$.
On entry, ${\mathbf{nfac}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nfac}}\ge 1$.
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_ARRAY_CONS
On entry, ${\mathbf{wt}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{weight}}=\mathrm{Nag_Weights}$ or $\mathrm{Nag_Weightsvar}$, ${\mathbf{wt}}\left[i\right]\ge 0.0$.
NE_VAR_DIV
${\mathbf{stat}}=\mathrm{Nag_TableStatsVar}$ and the divisor for the variance $\le 0.0$.
NE_WT_ARGS
The wt array argument must not be NULL when the weight argument indicates weights.

## 7Accuracy

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

## 8Parallelism and Performance

g11bac is not threaded in any implementation.

The tables created by g11bac and stored in table, count and, depending on stat, also in comm_ar 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 $m$th cell given by:
 $m = 1 + ∑ k=1 n {( i k -1) c k } ,$
where ${c}_{\mathit{j}}={\prod }_{k=\mathit{j}+1}^{n}{l}_{k}$, for $\mathit{j}=1,2,\dots ,n-1$ and ${c}_{n}=1$.

## 10Example

The data, given by John and Quenouille (1977), is 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. The 3 by 6 table of treatment means for yield over blocks is computed and printed.

### 10.1Program Text

Program Text (g11bace.c)

### 10.2Program Data

Program Data (g11bace.d)

### 10.3Program Results

Program Results (g11bace.r)