hide long namesshow long names
hide short namesshow short names

Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox Chapter Introduction

G11 — Contingency Table Analysis

Scope of the Chapter


Background to the Problems

Discrete Data



The basic summary for multivariate discrete data is the multidimensional table in which each dimension is specified by a discrete variable. If the cells of the table are the number of observations with the corresponding values of the discrete variables then it is a contingency table. The discrete variables that can be used to classify a table are known as factors. For example, the factor sex would have the levels male and female. These can be coded as 11 and 22 respectively. Given several factors a multi-way table can be constructed such that each cell of the table represents one level from each factor. For example, a sample of 120120 observations with the two factors sex and habitat, habitat having three levels (inner-city, suburban and rural), would give the 2 × 32×3 contingency table
Sex Habitat
  Inner-city Suburban Rural
Male 32 27 15
Female 21 19 6
Sex Habitat
  Inner-city Suburban Rural
Male 25.5 30.3 35.6
Female 23.2 29.1 30.4
or other summary statistics.
Sex Habitat  
  Inner-city Suburban Rural Total
Male 32 27 15 74
Female 21 19 6 46
Total 53 46 21 120
Note that the marginal totals for columns is itself a 2 × 12×1 table. Also, other summary statistics could be used to produce the marginal tables such as means or medians. Having computed the marginal tables, the cells of the original table may be expressed in terms of the margins, for example in the above table the cells could be expressed as percentages of the column totals.

Discrete Response Variables and Logistic Regression

A second important categorization in addition to that given in Section [Discrete Data] is whether one of the discrete variables can be considered as a response variable or whether it is just the association between the discrete variables that is being considered.
If the response variable is binary, for example, success or failure, then a logistic or probit regression model can be used. The logistic regression model relates the logarithm of the odds-ratio to a linear model. So if pipi is the probability of success, the model relates log(pi / (1pi))log(pi/(1-pi)) to the explanatory variables. If the responses are independent then these models are special cases of the generalized linear model with binomial errors. However, there are cases when the binomial model is not suitable. For example, in a case-control study a number of cases (successes) and number of controls (failures) is chosen for a number of sets of case-controls. In this situation a conditional logistic analysis is required.

Contingency Tables

If there is no response variable then to investigate the association between discrete variables a contingency table can be computed and a suitable test performed on the table. The simplest case is the two-way table formed when considering two discrete variables. For a dataset of nn observations classified by the two variables with rr and cc levels respectively, a two-way table of frequencies or counts 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
If pijpij is the probability of an observation in cell ijij then the model which assumes no association between the two variables is the model
pij = pi . p . j
where pi . pi. is the marginal probability for the row variable and p . jp.j is the marginal probability for the column variable, the marginal probability being the probability of observing a particular value of the variable ignoring all other variables. The appropriateness of this model can be assessed by two commonly used statistics:
the Pearson χ2χ2 statistic
i = 1j = 1
i=1rj=1c (nij-fij) 2fij,
and the likelihood ratio test statistic
2nij × log(nij / fij).
i = 1j = 1
2i= 1rj= 1cnij×log(nij/fij).
The fijfij are the fitted values from the model; these values are the expected cell frequencies and are given by
fij = nij = ni . . j = n(ni . / n)(n . j / n) = 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.
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's continuity correction factor. This decreases the absolute value of (nijfijnij-fij) by 1 / 21/2. For 2 × 22×2 tables with a small values of nn the exact probabilities can be computed; this is known as Fisher's exact test.
log(pij) = log(pi . ) + log(p . j).
A model like this can be fitted as a generalized linear model with Poisson error with the cell counts, nijnij, as the response variable.

Latent Variable Models

Factor analysis, as described in Chapter G03, is based on a linear model of this kind when the observed variables are continuous. Here we consider the case where the observed variables are binary (e.g., coded 0 / 10/1 or true/false) and where there is one latent variable. In educational testing this is known as latent trait analysis, but, more generally, as factor analysis of binary data.

Recommendations on Choice and Use of Available Functions



Analysis of Contingency Tables

nag_contab_chisq (g11aa) computes the Pearson and likelihood ratio χ2χ2 statistics for a two-way contingency table. For 2 × 22×2 tables Yates's correction factor is used and for small samples, n40n40, Fisher's exact test is used.
In addition, nag_correg_glm_poisson (g02gc) can be used to fit a log-linear model to a contingency table.

Binary data

The following functions can be used to analyse binary data:
In addition, nag_correg_glm_binomial (g02gb) fits generalized linear models to binary data.

Functionality Index

Conditional logistic model for stratified data nag_contab_condl_logistic (g11ca)
Frequency count for nag_contab_binary (g11sa) nag_contab_binary_service (g11sb)
Latent variable model for dichotomous data nag_contab_binary (g11sa)
Multiway tables from set of classification factors, 
    marginal table from nag_contab_tabulate_stat (g11ba) or nag_contab_tabulate_percentile (g11bb) nag_contab_tabulate_margin (g11bc)
    using given percentile/quantile nag_contab_tabulate_percentile (g11bb)
    using selected statistic nag_contab_tabulate_stat (g11ba)
χ2 statistics for two-way contingency table nag_contab_chisq (g11aa)



Chapter Contents
Chapter Introduction
NAG Toolbox
