NAG CL Interface
g11aac (chisq)

Settings help

CL Name Style:

1 Purpose

g11aac computes χ 2 statistics for a two-way contingency table. For a 2×2 table with a small number of observations exact probabilities are computed.

2 Specification

#include <nag.h>
void  g11aac (Integer nrow, Integer ncol, const Integer nobst[], Integer tdt, double expt[], double chist[], double *prob, double *chi, double *g, double *df, NagError *fail)
The function may be called by the names: g11aac, nag_contab_chisq or nag_chi_sq_2_way_table.

3 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.
n 11 n 12 n 1c n 1. n 21 n 22 n 2c n 2. n r1 n r2 n rc n r. n .1 n .2 n .c n r.  
To measure the association between the two classification variables two statistics that can be used are:
The Pearson χ 2 statistic = i=1 r j=1 c ( n ij - f ij ) 2 f ij ,
The likelihood ratio test statistic = 2 i=1 r j=1 c n ij × log( n ij / f ij ) .
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,
f ij = n i. n .j / n .  
Under the hypothesis of no association between the two classification variables, both these statistics have, approximately, a χ 2 distribution with (c-1) (r-1) 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 χ 2 approximation can be improved by using Yates' continuity correction factor. This decreases the absolute value of ( n ij - f ij ) × 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 g01blc. A two-tail probability is computed as min(1, 2 p u , 2 p l ) , where p u and p l are the upper and lower one-tail probabilities from the hypergeometric distribution.

4 References

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

5 Arguments

1: nrow Integer Input
On entry: the number of rows in the contingency table, r .
Constraint: nrow2 .
2: ncol Integer Input
On entry: the number of columns in the contingency table, c .
Constraint: ncol2 .
3: nobst[nrow×tdt] const Integer Input
On entry: the contingency table, nobst[(i-1)×tdt+j-1] must contain n ij , for i = 1 , 2 , , r and j = 1 , 2 , , c .
Constraint: nobst[(i-1)×tdt+j-1] 0 for i = 1 , 2 , , r and j = 1 , 2 , , c .
4: tdt Integer Input
On entry: the stride separating matrix column elements in the arrays nobst, expt, chist.
Constraint: tdtncol .
5: expt[nrow×tdt] double Output
On exit: the table of expected values, expt[(i-1)×tdt+j-1] contains f ij , for i = 1 , 2 , , r and j = 1 , 2 , , c .
6: chist[nrow×tdt] double Output
On exit: the table of χ 2 contributions, chist[(i-1)×tdt+j-1] contains ( n ij - f ij ) 2 f ij , for i = 1 , 2 , , r and j = 1 , 2 , , c .
7: prob double * Output
On exit: if c=2 , r=2 and n40 then prob contains the two-tail significance level for Fisher's exact test, otherwise prob contains the significance level from the Pearson χ 2 statistic.
8: chi double * Output
On exit: the Pearson χ 2 statistic.
9: g double * Output
On exit: the likelihood ratio test statistic.
10: df double * Output
On exit: the degrees of freedom for the statistics.
11: fail NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

6 Error Indicators and Warnings

On entry, tdt=value while ncol=value . These arguments must satisfy tdtncol .
On entry, nobst[(value)×tdt+value] = value. All elements of this array must be 0 .
On entry, all elements of the array nobst are 0. At least one element of this array must be > 0 .
On entry, ncol=value.
Constraint: ncol2.
On entry, nrow=value.
Constraint: nrow2
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 for assistance.
At least one cell has expected frequency 0.5 . 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.

7 Accuracy

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

8 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
g11aac is not threaded in any implementation.

9 Further Comments

Multidimensional contingency tables can be analysed using log-linear models fitted by g02gbc.

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

10.1 Program Text

Program Text (g11aace.c)

10.2 Program Data

Program Data (g11aace.d)

10.3 Program Results

Program Results (g11aace.r)