NAG Library Routine Document
g01aff
(contingency_table)
1
Purpose
g01aff performs the analysis of a two-way contingency table or classification. If , and the total number of objects classified is or fewer, then the probabilities for Fisher's exact test are computed. Otherwise, a test statistic is computed (with Yates' correction when ), which under the assumption of no association between the classifications has approximately a chi-square distribution with degrees of freedom.
2
Specification
Fortran Interface
Subroutine g01aff ( |
ldnob,
ldpred,
m,
n,
nobs,
num,
pred,
chis,
p,
npos,
ndf,
m1,
n1,
ifail) |
Integer, Intent (In) | :: |
ldnob,
ldpred,
m,
n | Integer, Intent (Inout) | :: |
nobs(ldnob,n),
num,
ifail | Integer, Intent (Out) | :: |
npos,
ndf,
m1,
n1 | Real (Kind=nag_wp), Intent (Inout) | :: |
pred(ldpred,n) | Real (Kind=nag_wp), Intent (Out) | :: |
chis,
p(21) |
|
C Header Interface
#include nagmk26.h
void |
g01aff_ (
const Integer *ldnob,
const Integer *ldpred,
const Integer *m,
const Integer *n,
Integer nobs[],
Integer *num,
double pred[],
double *chis,
double p[],
Integer *npos,
Integer *ndf,
Integer *m1,
Integer *n1,
Integer *ifail) |
|
3
Description
The data consist of the frequencies for the two-way classification, denoted by , for and with .
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
by
(
), and let
- , the total frequency for the th row, for ,
- , the total frequency for the th column, for , and
- , the total frequency.
There are two situations:
(i) |
If and/or , or and , then the matrix of expected frequencies, denoted by , for and , and the test statistic, , are computed, where
and
where
is Yates' correction for continuity.
Under the assumption that there is no association between the two classifications, will have approximately a chi-square distribution with degrees of freedom.
An option exists which allows for further ‘shrinkage’ of the matrix of frequencies in the case where for the ()th cell. If this is the case, then row or column will be combined with the adjacent row or column with smaller total. Row is selected for combination if . This ‘shrinking’ process is continued until for all cells (). |
(ii) |
If and , the probabilities to enable Fisher's exact test to be made are computed.
The matrix of frequencies may be rearranged so that is the smallest marginal (i.e., column and row) total, and . Under the assumption of no association between the classifications, the probability of obtaining entries in cell 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 . |
4
References
None.
5
Arguments
- 1: – IntegerInput
-
On entry: the first dimension of the array
nobs as declared in the (sub)program from which
g01aff is called.
Constraint:
.
- 2: – IntegerInput
-
On entry: the first dimension of the array
pred as declared in the (sub)program from which
g01aff is called.
Constraint:
.
- 3: – IntegerInput
-
On entry: , one more than the number of rows of the frequency matrix.
Constraint:
.
- 4: – IntegerInput
-
On entry: , one more than the number of columns of the frequency matrix.
Constraint:
.
- 5: – Integer arrayInput/Output
-
On entry: the elements
, for
and
, must contain the frequencies for the two-way classification. The
th row and the
th column of
nobs need not be set.
On exit: contains the following information:
- , for and , contain the frequencies for the two-way classification after ‘shrinkage’ has taken place (see Section 3).
- , for , contain the total frequencies in the remaining rows, .
- , for , contain the total frequencies in the remaining columns, .
- , contains the total frequency, .
If any ‘shrinkage’ has occurred, all other cells contain no useful information.
Constraint:
, for and .
- 6: – IntegerInput/Output
-
On entry: the value assigned to
num must determine whether automatic ‘shrinkage’ is required when any
, as outlined in
Section 3(i).
If , shrinkage is required, otherwise shrinkage is not required.
On exit: when Fisher's exact test for a
classification is used then
num contains the number of elements used in the array
p, otherwise
num is set to zero.
- 7: – Real (Kind=nag_wp) arrayOutput
-
On exit: the elements
, where
and
contain the expected frequencies,
corresponding to the observed frequencies
, except in the case when Fisher's exact test for a
classification is to be used, when
pred is not used. No other elements are utilized.
- 8: – Real (Kind=nag_wp)Output
-
On exit: the value of the test statistic, , except when Fisher's exact test for a classification is used in which case it is unspecified.
- 9: – Real (Kind=nag_wp) arrayOutput
-
p is used only when Fisher's exact test for a
classification is to be used.
On exit: the first
num elements contain the probabilities associated with the various possible frequency tables,
, for
, the remainder are unspecified.
- 10: – IntegerOutput
-
npos is used only when Fisher's exact test for a
classification is to be used.
On exit: holds the probability associated with the given table of frequencies.
- 11: – IntegerOutput
-
On exit: the value of
ndf gives the number of degrees of freedom for the chi-square distribution,
; when Fisher's exact test is used
.
- 12: – IntegerOutput
-
On exit: the number of rows of the two-way classification, after any ‘shrinkage’, .
- 13: – IntegerOutput
-
On exit: the number of columns of the two-way classification, after any ‘shrinkage’, .
- 14: – IntegerInput/Output
-
On entry:
ifail must be set to
,
. If you are unfamiliar with this argument you should refer to
Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, if you are not familiar with this argument, the recommended value is
.
When the value is used it is essential to test the value of ifail on exit.
On exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6
Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
x04aaf).
Errors or warnings detected by the routine:
-
On entry, .
Constraint: .
On entry, .
Constraint: .
The number of rows or columns of
nobs is less than
.
-
At least one frequency is negative, or all frequencies are zero.
-
On entry, and .
Constraint: .
On entry, and .
Constraint: .
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See
Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See
Section 3.7 in How to Use the NAG Library and its Documentation for further information.
7
Accuracy
The method used is believed to be stable.
8
Parallelism and Performance
g01aff is not threaded in any implementation.
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,
, or alternatively
ndf is
and
, the probabilities for use in Fisher's exact test for a
classification will be calculated, and not the test statistic with approximately a chi-square distribution.
10
Example
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 classification, with shrinkage not requested.
10.1
Program Text
Program Text (g01affe.f90)
10.2
Program Data
Program Data (g01affe.d)
10.3
Program Results
Program Results (g01affe.r)