NAG Library Function Document
nag_chi_sq_2_way_table (g11aac) computes statistics for a two-way contingency table. For a table with a small number of observations exact probabilities are computed.
||nag_chi_sq_2_way_table (Integer nrow,
const Integer nobst,
For a set of
observations classified by two variables, with
levels respectively, a two-way table of frequencies with
columns can be computed.
To measure the association between the two classification variables two statistics that can be used are:
The Pearson statistic ,
The likelihood ratio test statistic .
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,
Under the hypothesis of no association between the two classification variables, both these statistics have, approximately, a
degrees of freedom. This distribution is arrived at under the assumption that the expected cell frequencies,
, 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
in the smallest cell’'.
In the case of the
approximation can be improved by using Yates' continuity correction factor. This decreases the absolute value of
tables with a small value of
the exact probabilities from Fisher's test are computed. These are based on the hypergeometric distribution and are computed using nag_hypergeom_dist (g01blc)
. A two-tail probability is computed as
are the upper and lower one-tail probabilities from the hypergeometric distribution.
Everitt B S (1977) The Analysis of Contingency Tables Chapman and Hall
Kendall M G and Stuart A (1979) The Advanced Theory of Statistics (3 Volumes) (4th Edition) Griffin
nrow – IntegerInput
On entry: the number of rows in the contingency table, .
ncol – IntegerInput
On entry: the number of columns in the contingency table, .
nobst – const IntegerInput
On entry: the contingency table, must contain , for and .
for and .
tdt – IntegerInput
: the stride separating matrix column elements in the arrays nobst
expt – doubleOutput
On exit: the table of expected values, contains , for and .
chist – doubleOutput
On exit: the table of contributions, contains , for and .
prob – double *Output
contains the two-tail significance level for Fisher's exact test, otherwise prob
contains the significance level from the Pearson
chi – double *Output
On exit: the Pearson statistic.
g – double *Output
On exit: the likelihood ratio test statistic.
df – double *Output
On exit: the degrees of freedom for the statistics.
fail – NagError *Input/Output
The NAG error argument (see Section 3.6
in the Essential Introduction).
6 Error Indicators and Warnings
On entry, while . These arguments must satisfy .
On entry, . All elements of this array must be .
On entry, all elements of the array nobst
are 0. At least one element of this array must be
On entry, .
On entry, .
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
At least one cell has expected frequency . The chi-square approximation may be poor.
On entry, a 2 by 2 table has a row or column with both elements zero, i.e., the table is degenerate.
For the accuracy of the probabilities for Fisher's exact test see nag_hypergeom_dist (g01blc)
8 Parallelism and Performance
Multidimensional contingency tables can be analysed using log-linear models fitted by nag_glm_binomial (g02gbc)
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.
The data is read in and the statistics computed and printed.
10.1 Program Text
Program Text (g11aace.c)
10.2 Program Data
Program Data (g11aace.d)
10.3 Program Results
Program Results (g11aace.r)