# NAG FL Interfaceg11aaf (chisq)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g11aaf 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.

## 2Specification

Fortran Interface
 Subroutine g11aaf ( nrow, ncol, nobs, expt, prob, chi, g, df,
 Integer, Intent (In) :: nrow, ncol, nobs(ldnobs,ncol), ldnobs Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (Inout) :: expt(ldnobs,ncol), chist(ldnobs,ncol) Real (Kind=nag_wp), Intent (Out) :: prob, chi, g, df
#include <nag.h>
 void g11aaf_ (const Integer *nrow, const Integer *ncol, const Integer nobs[], const Integer *ldnobs, double expt[], double chist[], double *prob, double *chi, double *g, double *df, Integer *ifail)
The routine may be called by the names g11aaf or nagf_contab_chisq.

## 3Description

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 g01blf. 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.

## 4References

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

## 5Arguments

1: $\mathbf{nrow}$Integer Input
On entry: $r$, the number of rows in the contingency table.
Constraint: ${\mathbf{nrow}}\ge 2$.
2: $\mathbf{ncol}$Integer Input
On entry: $c$, the number of columns in the contingency table.
Constraint: ${\mathbf{ncol}}\ge 2$.
3: $\mathbf{nobs}\left({\mathbf{ldnobs}},{\mathbf{ncol}}\right)$Integer array Input
On entry: 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$.
4: $\mathbf{ldnobs}$Integer Input
On entry: the first dimension of the arrays nobs, expt and chist as declared in the (sub)program from which g11aaf is called.
Constraint: ${\mathbf{ldnobs}}\ge {\mathbf{nrow}}$.
5: $\mathbf{expt}\left({\mathbf{ldnobs}},{\mathbf{ncol}}\right)$Real (Kind=nag_wp) array Output
On exit: 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$.
6: $\mathbf{chist}\left({\mathbf{ldnobs}},{\mathbf{ncol}}\right)$Real (Kind=nag_wp) array Output
On exit: 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$.
7: $\mathbf{prob}$Real (Kind=nag_wp) Output
On exit: 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.
8: $\mathbf{chi}$Real (Kind=nag_wp) Output
On exit: the Pearson ${\chi }^{2}$ statistic.
9: $\mathbf{g}$Real (Kind=nag_wp) Output
On exit: the likelihood ratio test statistic.
10: $\mathbf{df}$Real (Kind=nag_wp) Output
On exit: the degrees of freedom for the statistics.
11: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $-1$ is recommended since useful values can be provided in some output arguments even when ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
Note: in some cases g11aaf may return useful information.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{ldnobs}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{nrow}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldnobs}}\ge {\mathbf{nrow}}$.
On entry, ${\mathbf{ncol}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ncol}}\ge 2$.
On entry, ${\mathbf{nrow}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nrow}}\ge 2$.
${\mathbf{ifail}}=2$
On entry, all elements of ${\mathbf{nobs}}=0$.
On entry, $i=⟨\mathit{\text{value}}⟩$, $j=⟨\mathit{\text{value}}⟩$ and ${\mathbf{nobs}}\left(i,j\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nobs}}\left(i,j\right)\ge 0$.
${\mathbf{ifail}}=3$
On entry, a $2×2$ table has a row or column with both values zero.
${\mathbf{ifail}}=4$
At least one cell has an expected frequency, ${f}_{ij}\le 0.5$. The ${\chi }^{2}$ approximation may be poor.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

For the accuracy of the probabilities for Fisher's exact test see g01blf.

## 8Parallelism and Performance

g11aaf is not threaded in any implementation.

The routine g01aff 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 g02gbf.

## 10Example

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.

### 10.1Program Text

Program Text (g11aafe.f90)

### 10.2Program Data

Program Data (g11aafe.d)

### 10.3Program Results

Program Results (g11aafe.r)