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_stat_contingency_table (g01af)

## Purpose

nag_stat_contingency_table (g01af) performs the analysis of a two-way $r×c$ contingency table or classification. If $r=c=2$, and the total number of objects classified is $40$ or fewer, then the probabilities for Fisher's exact test are computed. Otherwise, a test statistic is computed (with Yates' correction when $r=c=2$), which under the assumption of no association between the classifications has approximately a chi-square distribution with $\left(r-1\right)×\left(c-1\right)$ degrees of freedom.

## Syntax

[nobs, num, pred, chis, p, npos, ndf, m1, n1, ifail] = g01af(nobs, 'm', m, 'n', n, 'num', num)
[nobs, num, pred, chis, p, npos, ndf, m1, n1, ifail] = nag_stat_contingency_table(nobs, 'm', m, 'n', n, 'num', num)
Note: the interface to this routine has changed since earlier releases of the toolbox:
 At Mark 23: num was made optional (default 0) At Mark 22: m was made optional

## Description

The data consist of the frequencies for the two-way classification, denoted by ${n}_{\mathit{i}\mathit{j}}$, for $\mathit{i}=1,2,\dots ,m$ and $\mathit{j}=1,2,\dots ,n$ with $m,n>1$.
A check is made to see whether any row or column of the matrix of frequencies consists entirely of zeros, and if so, the matrix of frequencies is reduced by omitting that row or column. Suppose the final size of the matrix is ${m}_{1}$ by ${n}_{1}$ (${m}_{1},{n}_{1}>1$), and let
• ${R}_{\mathit{i}}=\sum _{j=1}^{{n}_{1}}{n}_{\mathit{i}j}$, the total frequency for the $\mathit{i}$th row, for $\mathit{i}=1,2,\dots ,{m}_{1}$,
• ${C}_{\mathit{j}}=\sum _{i=1}^{{m}_{1}}{n}_{i\mathit{j}}$, the total frequency for the $\mathit{j}$th column, for $\mathit{j}=1,2,\dots ,{n}_{1}$, and
• $T=\sum _{i=1}^{{m}_{1}}{R}_{i}=\sum _{j=1}^{{n}_{1}}{C}_{j}$, the total frequency.
There are two situations:
(i) If ${m}_{1}>2$ and/or ${n}_{1}>2$, or ${m}_{1}={n}_{1}=2$ and $T>40$, then the matrix of expected frequencies, denoted by ${r}_{ij}$, for $i=1,2,\dots ,{m}_{1}$ and $j=1,2,\dots ,{n}_{1}$, and the test statistic, ${\chi }^{2}$, are computed, where
 $rij=RiCj/T, i=1,2,…,m1;j=1,2,…,n1$
and
 $χ2=∑i= 1m1∑j= 1n1rij-nij-Y2/rij,$
where
 $Y= 12 if ​ m1=n1=2 0 otherwise$
is Yates' correction for continuity.
Under the assumption that there is no association between the two classifications, ${\chi }^{2}$ will have approximately a chi-square distribution with $\left({m}_{1}-1\right)×\left({n}_{1}-1\right)$ degrees of freedom.
An option exists which allows for further ‘shrinkage’ of the matrix of frequencies in the case where ${r}_{ij}<1$ for the ($i,j$)th cell. If this is the case, then row $i$ or column $j$ will be combined with the adjacent row or column with smaller total. Row $i$ is selected for combination if ${R}_{i}×{m}_{1}\le {C}_{j}×{n}_{1}$. This ‘shrinking’ process is continued until ${r}_{ij}\ge 1$ for all cells ($i,j$).
(ii) If ${m}_{1}={n}_{1}=2$ and $T\le 40$, the probabilities to enable Fisher's exact test to be made are computed.
The matrix of frequencies may be rearranged so that ${R}_{1}$ is the smallest marginal (i.e., column and row) total, and ${C}_{2}\ge {C}_{1}$. Under the assumption of no association between the classifications, the probability of obtaining $r$ entries in cell $\left(1,1\right)$ is computed where
 $Pr+1=R1!R2!C1!C2! T!r!R1-r!C1-r!T-C1-R1+r! , r=0,1,…,R1.$
The probability of obtaining the table of given frequencies is returned. A test of the assumption against some alternative may then be made by summing the relevant values of ${P}_{r}$.

None.

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{nobs}\left(\mathit{ldnob},{\mathbf{n}}\right)$int64int32nag_int array
ldnob, the first dimension of the array, must satisfy the constraint $\mathit{ldnob}\ge {\mathbf{m}}$.
The elements ${\mathbf{nobs}}\left(\mathit{i},\mathit{j}\right)$, for $\mathit{i}=1,2,\dots ,m$ and $\mathit{j}=1,2,\dots ,n$, must contain the frequencies for the two-way classification. The $\left(m+1\right)$th row and the $\left(n+1\right)$th column of nobs need not be set.
Constraint: ${\mathbf{nobs}}\left(\mathit{i},\mathit{j}\right)\ge 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{m}}-1$ and $\mathit{j}=1,2,\dots ,{\mathbf{n}}-1$.

### Optional Input Parameters

1:     $\mathrm{m}$int64int32nag_int scalar
Default: the first dimension of the array nobs.
$m+1$, one more than the number of rows of the frequency matrix.
Constraint: ${\mathbf{m}}>2$.
2:     $\mathrm{n}$int64int32nag_int scalar
Default: the second dimension of the array nobs.
$n+1$, one more than the number of columns of the frequency matrix.
Constraint: ${\mathbf{n}}>2$.
3:     $\mathrm{num}$int64int32nag_int scalar
Default: $0$
The value assigned to num must determine whether automatic ‘shrinkage’ is required when any ${r}_{ij}<1$, as outlined in Description(i).
If ${\mathbf{num}}=1$, shrinkage is required, otherwise shrinkage is not required.

### Output Parameters

1:     $\mathrm{nobs}\left(\mathit{ldnob},{\mathbf{n}}\right)$int64int32nag_int array
Contains the following information:
• ${\mathbf{nobs}}\left(\mathit{i},\mathit{j}\right)$, for $\mathit{i}=1,2,\dots ,{m}_{1}$ and $\mathit{j}=1,2,\dots ,{n}_{1}$, contain the frequencies for the two-way classification after ‘shrinkage’ has taken place (see Description).
• ${\mathbf{nobs}}\left(\mathit{i},n+1\right)$, for $\mathit{i}=1,2,\dots ,{m}_{1}$, contain the total frequencies in the remaining rows, ${R}_{i}$.
• ${\mathbf{nobs}}\left(m+1,\mathit{j}\right)$, for $\mathit{j}=1,2,\dots ,{n}_{1}$, contain the total frequencies in the remaining columns, ${C}_{j}$.
• ${\mathbf{nobs}}\left(m+1,n+1\right)$, contains the total frequency, $\mathrm{T}$.
If any ‘shrinkage’ has occurred, then all other cells contain no useful information.
2:     $\mathrm{num}$int64int32nag_int scalar
Default: $0$
When Fisher's exact test for a $2×2$ classification is used then num contains the number of elements used in the array p, otherwise num is set to zero.
3:     $\mathrm{pred}\left(\mathit{ldpred},{\mathbf{n}}\right)$ – double array
The elements ${\mathbf{pred}}\left(i,j\right)$, where $i=1,2,\dots ,{\mathbf{m1}}$ and $j=1,2,\dots ,{\mathbf{n1}}$ contain the expected frequencies, ${r}_{ij}$ corresponding to the observed frequencies ${\mathbf{nobs}}\left(i,j\right)$, except in the case when Fisher's exact test for a $2×2$ classification is to be used, when pred is not used. No other elements are utilized.
4:     $\mathrm{chis}$ – double scalar
The value of the test statistic, ${\chi }^{2}$, except when Fisher's exact test for a $2×2$ classification is used in which case it is unspecified.
5:     $\mathrm{p}\left(21\right)$ – double array
The first num elements contain the probabilities associated with the various possible frequency tables, ${P}_{\mathit{r}}$, for $\mathit{r}=0,1,\dots ,{R}_{1}$, the remainder are unspecified.
6:     $\mathrm{npos}$int64int32nag_int scalar
${\mathbf{p}}\left({\mathbf{npos}}\right)$ holds the probability associated with the given table of frequencies.
7:     $\mathrm{ndf}$int64int32nag_int scalar
The value of ndf gives the number of degrees of freedom for the chi-square distribution, $\left({m}_{1}-1\right)×\left({n}_{1}-1\right)$; when Fisher's exact test is used ${\mathbf{ndf}}=1$.
8:     $\mathrm{m1}$int64int32nag_int scalar
The number of rows of the two-way classification, after any ‘shrinkage’, ${m}_{1}$.
9:     $\mathrm{n1}$int64int32nag_int scalar
The number of columns of the two-way classification, after any ‘shrinkage’, ${n}_{1}$.
10:   $\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$
The number of rows or columns of nobs is less than $2$, possibly after shrinkage.
${\mathbf{ifail}}=2$
At least one frequency is negative, or all frequencies are zero.
${\mathbf{ifail}}=4$
 On entry, $\mathit{ldpred}<{\mathbf{m}}$, or $\mathit{ldnob}<{\mathbf{m}}$.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

The method used is believed to be stable.

The time taken by nag_stat_contingency_table (g01af) will increase with m and n, except when Fisher's exact test is to be used, in which case it increases with size of the marginal and total frequencies.
If, on exit, ${\mathbf{num}}>0$, or alternatively ndf is $1$ and ${\mathbf{nobs}}\left({\mathbf{m}},{\mathbf{n}}\right)\le 40$, the probabilities for use in Fisher's exact test for a $2×2$ classification will be calculated, and not the test statistic with approximately a chi-square distribution.

## Example

In the example program, NPROB determines the number of two-way classifications to be analysed. For each classification the frequencies are read, nag_stat_contingency_table (g01af) called, and information given on how much ‘shrinkage’ has taken place. If Fisher's exact test is to be used, the given frequencies and the array of probabilities associated with the possible frequency tables are printed. Otherwise, if the chi-square test is to be used, the given and expected frequencies, and the test statistic with its degrees of freedom are printed. In the example, there is one $2×3$ classification, with shrinkage not requested.
```function g01af_example

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

nr = 2;
nc = 3;
nobs = zeros(nr+1,nc+1,'int64');
nobs(1:nr,1:nc) = [ 86,  51, 13;
130, 115, 41];

[nobs, num, pred, chis, p, npos, ndf, m1, n1, ifail] = ...
g01af(nobs);

if (m1~=nr)
fprintf('Number of rows reduced from %2d to %2d\n', nr, m1);
end
if (n1~=nc)
fprintf('Number of rows reduced from %2d to %2d\n', nc, n1);
end
fprintf('\nTable of observed frequencies\n\n');
fprintf('                            total\n');
for j = 1:m1
fprintf('%8s',' ');
fprintf('%5d',nobs(j,1:n1));
fprintf('%8d\n',nobs(j,n1+1));
end
fprintf('\n%8s','total');
fprintf('%5d',nobs(m1+1,1:n1));
fprintf('%8d\n',nobs(m1+1,n1+1));

fprintf('\n\nTable of expected frequencies\n\n');
for j = 1:m1
fprintf('%8s',' ');
fprintf('%5d',int64(pred(j,1:n1)));
fprintf('\n');
end

fprintf('\nChi-squared        = %7.3f\n', chis);
fprintf('Degrees of freedom = %4d\n', ndf);

function g01af_table(m1,n1,obs)
fprintf('                            total\n');
for j = 1:m1
fprintf('%8s',' ');
fprintf('%5d',obs(j,1:n1));
fprintf('%8d\n',obs(j,n1+1));
end
fprintf('\n%8s','total');
fprintf('%5d',obs(m1+1,1:n1));
fprintf('%8d\n',obs(m1+1,n1+1));
```
```g01af example results

Table of observed frequencies

total
86   51   13     150
130  115   41     286

total  216  166   54     436

Table of expected frequencies

74   57   19
142  109   35

Chi-squared        =   6.352
Degrees of freedom =    2
```

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