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_chisq (g11aa)

## Purpose

nag_contab_chisq (g11aa) computes ${\chi }^{2}$ statistics for a two-way contingency table. For a $2×2$ table with a small number of observations exact probabilities are computed.

## Syntax

[expt, chist, prob, chi, g, df, ifail] = g11aa(nrow, nobs, 'ncol', ncol)
[expt, chist, prob, chi, g, df, ifail] = nag_contab_chisq(nrow, nobs, 'ncol', ncol)

## Description

For a set of $n$ observations classified by two variables, with $r$ and $c$ levels respectively, a two-way table of frequencies with $r$ rows and $c$ columns can be computed.
 $n11 n12 … n1c n1. n21 n22 … n2c n2. ⋮ ⋮ ⋮ ⋮ ⋮ nr1 nr2 … nrc nr. n.1 n.2 … n.c n$
To measure the association between the two classification variables two statistics that can be used are, the Pearson ${\chi }^{2}$ statistic, $\sum _{i=1}^{r}\sum _{j=1}^{c}\frac{{\left({n}_{ij}-{f}_{ij}\right)}^{2}}{{f}_{ij}}$, and the likelihood ratio test statistic, $2\sum _{i=1}^{r}\sum _{j=1}^{c}{n}_{ij}×\mathrm{log}\left({n}_{ij}/{f}_{ij}\right)$, where ${f}_{ij}$ are the fitted values from the model that assumes the effects due to the classification variables are additive, i.e., there is no association. These values are the expected cell frequencies and are given by
 $fij=ni.n.j/n.$
Under the hypothesis of no association between the two classification variables, both these statistics have, approximately, a ${\chi }^{2}$-distribution with $\left(c-1\right)\left(r-1\right)$ degrees of freedom. This distribution is arrived at under the assumption that the expected cell frequencies, ${f}_{ij}$, are not too small. For a discussion of this point see Everitt (1977). He concludes by saying, ‘... in the majority of cases the chi-square criterion may be used for tables with expectations in excess of $0.5$ in the smallest cell’.
In the case of the $2×2$ table, i.e., $c=2$ and $r=2$, the ${\chi }^{2}$ approximation can be improved by using Yates' continuity correction factor. This decreases the absolute value of $\left({n}_{ij}-{f}_{ij}\right)$ by $\frac{1}{2}$. For $2×2$ tables with a small value of $n$ the exact probabilities from Fisher's test are computed. These are based on the hypergeometric distribution and are computed using nag_stat_prob_hypergeom (g01bl). A two tail probability is computed as $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(1,2{p}_{u},2{p}_{l}\right)$, where ${p}_{u}$ and ${p}_{l}$ are the upper and lower one-tail probabilities from the hypergeometric distribution.

## References

Everitt B S (1977) The Analysis of Contingency Tables Chapman and Hall
Kendall M G and Stuart A (1973) The Advanced Theory of Statistics (Volume 2) (3rd Edition) Griffin

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{nrow}$int64int32nag_int scalar
$r$, the number of rows in the contingency table.
Constraint: ${\mathbf{nrow}}\ge 2$.
2:     $\mathrm{nobs}\left(\mathit{ldnobs},{\mathbf{ncol}}\right)$int64int32nag_int array
ldnobs, the first dimension of the array, must satisfy the constraint $\mathit{ldnobs}\ge {\mathbf{nrow}}$.
The contingency table ${\mathbf{nobs}}\left(\mathit{i},\mathit{j}\right)$ must contain ${n}_{\mathit{i}\mathit{j}}$, for $\mathit{i}=1,2,\dots ,r$ and $\mathit{j}=1,2,\dots ,c$.
Constraint: ${\mathbf{nobs}}\left(\mathit{i},\mathit{j}\right)\ge 0$, for $\mathit{i}=1,2,\dots ,r$ and $\mathit{j}=1,2,\dots ,c$.

### Optional Input Parameters

1:     $\mathrm{ncol}$int64int32nag_int scalar
Default: the second dimension of the array nobs.
$c$, the number of columns in the contingency table.
Constraint: ${\mathbf{ncol}}\ge 2$.

### Output Parameters

1:     $\mathrm{expt}\left(\mathit{ldnobs},{\mathbf{ncol}}\right)$ – double array
The table of expected values. ${\mathbf{expt}}\left(\mathit{i},\mathit{j}\right)$ contains ${f}_{\mathit{i}\mathit{j}}$, for $\mathit{i}=1,2,\dots ,r$ and $\mathit{j}=1,2,\dots ,c$.
2:     $\mathrm{chist}\left(\mathit{ldnobs},{\mathbf{ncol}}\right)$ – double array
The table of ${\chi }^{2}$ contributions. ${\mathbf{chist}}\left(\mathit{i},\mathit{j}\right)$ contains $\frac{{\left({n}_{\mathit{i}\mathit{j}}-{f}_{\mathit{i}\mathit{j}}\right)}^{2}}{{f}_{\mathit{i}\mathit{j}}}$, for $\mathit{i}=1,2,\dots ,r$ and $\mathit{j}=1,2,\dots ,c$.
3:     $\mathrm{prob}$ – double scalar
If $c=2$, $r=2$ and $n\le 40$ then prob contains the two tail significance level for Fisher's exact test, otherwise prob contains the significance level from the Pearson ${\chi }^{2}$ statistic.
4:     $\mathrm{chi}$ – double scalar
The Pearson ${\chi }^{2}$ statistic.
5:     $\mathrm{g}$ – double scalar
The likelihood ratio test statistic.
6:     $\mathrm{df}$ – double scalar
The degrees of freedom for the statistics.
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

Note: nag_contab_chisq (g11aa) may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

${\mathbf{ifail}}=1$
 On entry, ${\mathbf{nrow}}<2$, or ${\mathbf{ncol}}<2$, or $\mathit{ldnobs}<{\mathbf{nrow}}$.
${\mathbf{ifail}}=2$
 On entry, a value in ${\mathbf{nobs}}<0$, or all values in nobs are zero.
${\mathbf{ifail}}=3$
 On entry, a $2×2$ table has a row or column with both values $0$.
W  ${\mathbf{ifail}}=4$
At least one cell has expected frequency, ${f}_{ij}$, $\text{}\le 0.5$. The ${\chi }^{2}$ approximation may be poor.
${\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

For the accuracy of the probabilities for Fisher's exact test see nag_stat_prob_hypergeom (g01bl).

The function nag_stat_contingency_table (g01af) allows for the automatic amalgamation of rows and columns. In most circumstances this is not recommended; see Everitt (1977).
Multidimensional contingency tables can be analysed using log-linear models fitted by nag_correg_glm_binomial (g02gb).

## Example

The data below, taken from Everitt (1977), is from $141$ patients with brain tumours. The row classification variable is the site of the tumour: frontal lobes, temporal lobes and other cerebral areas. The column classification variable is the type of tumour: benign, malignant and other cerebral tumours.
 $23 9 6 38 21 4 3 28 34 24 17 75 78 37 26 141$
The data is read in and the statistics computed and printed.
```function g11aa_example

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

nrow  = int64(3);
nobst = [int64(23),  9,  6;
21,   4,  3;
34,  24, 17];

[expt, chist, prob, chi, g, df, ifail] = ...
g11aa(nrow, nobst);

% Display results
fprintf('Probability                     = %9.4f\n', prob);
fprintf('Pearson Chi-square statistic    = %8.3f\n', chi);
fprintf('Likelihood ratio test statistic = %8.3f\n', g);
fprintf('Degrees of freedom              = %4.0f\n', df);

```
```g11aa example results

Probability                     =    0.0975
Pearson Chi-square statistic    =    7.844
Likelihood ratio test statistic =    8.096
Degrees of freedom              =    4
```