hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_contab_chisq (g11aa)

Purpose

nag_contab_chisq (g11aa) computes χ2χ2 statistics for a two-way contingency table. For a 2 × 22×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 nn observations classified by two variables, with rr and cc levels respectively, a two-way table of frequencies with rr rows and cc columns can be computed.
n11 n12 n1c n1 .
n21 n22 n2c n2 .
nr1 nr2 nrc nr .
n . 1 n . 2 n . c n
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 χ2χ2 statistic, i = 1rj = 1c ((nijfij)2)/(fij) i=1rj=1c (nij-fij) 2fij , and the likelihood ratio test statistic, 2i = 1rj = 1c nij × log(nij / fij)2i=1rj=1c nij×log(nij/fij), where fijfij 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.
fij=ni.n.j/n.
Under the hypothesis of no association between the two classification variables, both these statistics have, approximately, a χ2χ2-distribution with (c1)(r1)(c-1)(r-1) degrees of freedom. This distribution is arrived at under the assumption that the expected cell frequencies, fijfij, 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.50.5 in the smallest cell’.
In the case of the 2 × 22×2 table, i.e., c = 2c=2 and r = 2r=2, the χ2χ2 approximation can be improved by using Yates' continuity correction factor. This decreases the absolute value of (nijfij)(nij-fij) by (1/2) 12 . For 2 × 22×2 tables with a small value of nn 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 min (1,2pu,2pl) min(1,2pu,2pl) , where pupu and plpl 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:     nrow – int64int32nag_int scalar
rr, the number of rows in the contingency table.
Constraint: nrow2nrow2.
2:     nobs(ldnobs,ncol) – int64int32nag_int array
ldnobs, the first dimension of the array, must satisfy the constraint ldnobsnrowldnobsnrow.
The contingency table nobs(i,j)nobsij must contain nijnij, for i = 1,2,,ri=1,2,,r and j = 1,2,,cj=1,2,,c.
Constraint: nobs(i,j)0nobsij0, for i = 1,2,,ri=1,2,,r and j = 1,2,,cj=1,2,,c.

Optional Input Parameters

1:     ncol – int64int32nag_int scalar
Default: The second dimension of the array nobs.
cc, the number of columns in the contingency table.
Constraint: ncol2ncol2.

Input Parameters Omitted from the MATLAB Interface

ldnobs

Output Parameters

1:     expt(ldnobs,ncol) – double array
ldnobsnrowldnobsnrow.
The table of expected values. expt(i,j)exptij contains fijfij, for i = 1,2,,ri=1,2,,r and j = 1,2,,cj=1,2,,c.
2:     chist(ldnobs,ncol) – double array
ldnobsnrowldnobsnrow.
The table of χ2χ2 contributions. chist(i,j)chistij contains ((nijfij)2)/(fij) (nij-fij) 2fij , for i = 1,2,,ri=1,2,,r and j = 1,2,,cj=1,2,,c.
3:     prob – double scalar
If c = 2c=2, r = 2r=2 and n40n40 then prob contains the two tail significance level for Fisher's exact test, otherwise prob contains the significance level from the Pearson χ2χ2 statistic.
4:     chi – double scalar
The Pearson χ2χ2 statistic.
5:     g – double scalar
The likelihood ratio test statistic.
6:     df – double scalar
The degrees of freedom for the statistics.
7:     ifail – int64int32nag_int scalar
ifail = 0ifail=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.

  ifail = 1ifail=1
On entry,nrow < 2nrow<2,
orncol < 2ncol<2,
orldnobs < nrowldnobs<nrow.
  ifail = 2ifail=2
On entry,a value in nobs < 0nobs<0, or all values in nobs are zero.
  ifail = 3ifail=3
On entry,a 2 × 22×2 table has a row or column with both values 00.
W ifail = 4ifail=4
At least one cell has expected frequency, fijfij, 0.50.5. The χ2χ2 approximation may be poor.

Accuracy

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

Further Comments

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

function nag_contab_chisq_example
nrow = int64(3);
nobst = [int64(23),9,6; ...
             21,4,3; ...
             34,24,17];
[expt, chist, prob, chi, g, df, ifail] = nag_contab_chisq(nrow, nobst)
 

expt =

   21.0213    9.9716    7.0071
   15.4894    7.3475    5.1631
   41.4894   19.6809   13.8298


chist =

    0.1863    0.0947    0.1447
    1.9605    1.5251    0.9063
    1.3519    0.9479    0.7267


prob =

    0.0975


chi =

    7.8441


g =

    8.0958


df =

     4


ifail =

                    0


function g11aa_example
nrow = int64(3);
nobst = [int64(23),9,6; ...
             21,4,3; ...
             34,24,17];
[expt, chist, prob, chi, g, df, ifail] = g11aa(nrow, nobst)
 

expt =

   21.0213    9.9716    7.0071
   15.4894    7.3475    5.1631
   41.4894   19.6809   13.8298


chist =

    0.1863    0.0947    0.1447
    1.9605    1.5251    0.9063
    1.3519    0.9479    0.7267


prob =

    0.0975


chi =

    7.8441


g =

    8.0958


df =

     4


ifail =

                    0



PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013