g01aff performs the analysis of a two-way $r\times 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 $(r-1)\times (c-1)$ degrees of freedom.
The routine may be called by the names g01aff or nagf_stat_contingency_table.
3Description
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}\times {n}_{1}$ (${m}_{1},{n}_{1}>1$), and let
${R}_{\mathit{i}}={\displaystyle \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}}={\displaystyle \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={\displaystyle \sum _{i=1}^{{m}_{1}}}{R}_{i}={\displaystyle \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
Under the assumption that there is no association between the two classifications, ${\chi}^{2}$ will have approximately a chi-square distribution with $({m}_{1}-1)\times ({n}_{1}-1)$ 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}\times {m}_{1}\le {C}_{j}\times {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 $(1,1)$ is computed where
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}$.
4References
None.
5Arguments
1: $\mathbf{ldnob}$ – IntegerInput
On entry: the first dimension of the array nobs as declared in the (sub)program from which g01aff is called.
Constraint:
${\mathbf{ldnob}}\ge {\mathbf{m}}$.
2: $\mathbf{ldpred}$ – IntegerInput
On entry: the first dimension of the array pred as declared in the (sub)program from which g01aff is called.
Constraint:
${\mathbf{ldpred}}\ge {\mathbf{m}}$.
3: $\mathbf{m}$ – IntegerInput
On entry: $m+1$, one more than the number of rows of the frequency matrix.
Constraint:
${\mathbf{m}}>2$.
4: $\mathbf{n}$ – IntegerInput
On entry: $n+1$, one more than the number of columns of the frequency matrix.
On entry: the elements
${\mathbf{nobs}}(\mathit{i},\mathit{j})$, for $\mathit{i}=1,2,\dots ,m$ and $\mathit{j}=1,2,\dots ,n$, must contain the frequencies for the two-way classification. The $(m+1)$th row and the $(n+1)$th column of nobs need not be set.
On exit: contains the following information:
${\mathbf{nobs}}(\mathit{i},\mathit{j})$, 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 Section 3).
${\mathbf{nobs}}(\mathit{i},n+1)$, for $\mathit{i}=1,2,\dots ,{m}_{1}$, contain the total frequencies in the remaining rows, ${R}_{i}$.
${\mathbf{nobs}}(m+1,\mathit{j})$, for $\mathit{j}=1,2,\dots ,{n}_{1}$, contain the total frequencies in the remaining columns, ${C}_{j}$.
${\mathbf{nobs}}(m+1,n+1)$, contains the total frequency, $\mathrm{T}$.
If any ‘shrinkage’ has occurred, all other cells contain no useful information.
Constraint:
${\mathbf{nobs}}(\mathit{i},\mathit{j})\ge 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{m}}-1$ and $\mathit{j}=1,2,\dots ,{\mathbf{n}}-1$.
6: $\mathbf{num}$ – IntegerInput/Output
On entry: the value assigned to num must determine whether automatic ‘shrinkage’ is required when any ${r}_{ij}<1$, as outlined in Section 3(i).
If ${\mathbf{num}}=1$, shrinkage is required, otherwise shrinkage is not required.
On exit: when Fisher's exact test for a $2\times 2$ classification is used then num contains the number of elements used in the array p, otherwise num is set to zero.
7: $\mathbf{pred}({\mathbf{ldpred}},{\mathbf{n}})$ – Real (Kind=nag_wp) arrayOutput
On exit: the elements ${\mathbf{pred}}(i,j)$, 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}}(i,j)$, except in the case when Fisher's exact test for a $2\times 2$ classification is to be used, when pred is not used. No other elements are utilized.
8: $\mathbf{chis}$ – Real (Kind=nag_wp)Output
On exit: the value of the test statistic, ${\chi}^{2}$, except when Fisher's exact test for a $2\times 2$ classification is used in which case it is unspecified.
9: $\mathbf{p}\left(21\right)$ – Real (Kind=nag_wp) arrayOutput
p is used only when Fisher's exact test for a $2\times 2$ classification is to be used.
On exit: 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.
10: $\mathbf{npos}$ – IntegerOutput
npos is used only when Fisher's exact test for a $2\times 2$ classification is to be used.
On exit: ${\mathbf{p}}\left({\mathbf{npos}}\right)$ holds the probability associated with the given table of frequencies.
11: $\mathbf{ndf}$ – IntegerOutput
On exit: the value of ndf gives the number of degrees of freedom for the chi-square distribution, $({m}_{1}-1)\times ({n}_{1}-1)$; when Fisher's exact test is used ${\mathbf{ndf}}=1$.
12: $\mathbf{m1}$ – IntegerOutput
On exit: the number of rows of the two-way classification, after any ‘shrinkage’, ${m}_{1}$.
13: $\mathbf{n1}$ – IntegerOutput
On exit: the number of columns of the two-way classification, after any ‘shrinkage’, ${n}_{1}$.
14: $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $\mathrm{-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 $\mathrm{-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 $\mathrm{-1}$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. 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 $\mathrm{-1}$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{m}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{m}}>2$.
On entry, ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{n}}>2$.
The number of rows or columns of nobs is less than $2$.
${\mathbf{ifail}}=2$
At least one frequency is negative, or all frequencies are zero.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{ldnob}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{m}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{ldnob}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{ldpred}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{m}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{ldpred}}\ge {\mathbf{m}}$.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please
contact NAG.
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
The method used is believed to be stable.
8Parallelism and Performance
Background information to multithreading can be found in the Multithreading documentation.
g01aff is not threaded in any implementation.
9Further Comments
The time taken by g01aff 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}}({\mathbf{m}},{\mathbf{n}})\le 40$, the probabilities for use in Fisher's exact test for a $2\times 2$ classification will be calculated, and not the test statistic with approximately a chi-square distribution.
10Example
In the example program, NPROB determines the number of two-way classifications to be analysed. For each classification the frequencies are read, g01aff 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\times 3$ classification, with shrinkage not requested.