g01af performs the analysis of a two-way r×c contingency table or classification. If r=c=2, and the total number of objects classified is 40 or fewer, then the probabilities for Fisher's exact test are computed. Otherwise, a test statistic is computed (with Yates' correction when r=c=2), which under the assumption of no association between the classifications has approximately a chi-square distribution with r-1×c-1 degrees of freedom.

Syntax

C#
public static void g01af(
	int m,
	int n,
	int[,] nobs,
	ref int num,
	double[,] pred,
	out double chis,
	double[] p,
	out int npos,
	out int ndf,
	out int m1,
	out int n1,
	out int ifail
)
Visual Basic
Public Shared Sub g01af ( _
	m As Integer, _
	n As Integer, _
	nobs As Integer(,), _
	ByRef num As Integer, _
	pred As Double(,), _
	<OutAttribute> ByRef chis As Double, _
	p As Double(), _
	<OutAttribute> ByRef npos As Integer, _
	<OutAttribute> ByRef ndf As Integer, _
	<OutAttribute> ByRef m1 As Integer, _
	<OutAttribute> ByRef n1 As Integer, _
	<OutAttribute> ByRef ifail As Integer _
)
Visual C++
public:
static void g01af(
	int m, 
	int n, 
	array<int,2>^ nobs, 
	int% num, 
	array<double,2>^ pred, 
	[OutAttribute] double% chis, 
	array<double>^ p, 
	[OutAttribute] int% npos, 
	[OutAttribute] int% ndf, 
	[OutAttribute] int% m1, 
	[OutAttribute] int% n1, 
	[OutAttribute] int% ifail
)
F#
static member g01af : 
        m : int * 
        n : int * 
        nobs : int[,] * 
        num : int byref * 
        pred : float[,] * 
        chis : float byref * 
        p : float[] * 
        npos : int byref * 
        ndf : int byref * 
        m1 : int byref * 
        n1 : int byref * 
        ifail : int byref -> unit 

Parameters

m
Type: System..::..Int32
On entry: m+1, one more than the number of rows of the frequency matrix.
Constraint: m>2.
n
Type: System..::..Int32
On entry: n+1, one more than the number of columns of the frequency matrix.
Constraint: n>2.
nobs
Type: array<System..::..Int32,2>[,](,)[,][,]
An array of size [dim1, n]
Note: dim1 must satisfy the constraint: dim1m
On entry: the elements nobs[i-1,j-1], for i=1,2,,m and j=1,2,,n, must contain the frequencies for the two-way classification. The m+1th row and the n+1th column of nobs need not be set.
On exit: contains the following information:
  • nobs[i-1,j-1], for i=1,2,,m1 and j=1,2,,n1, contain the frequencies for the two-way classification after ‘shrinkage’ has taken place (see [Description]).
  • nobs[i-1,n], for i=1,2,,m1, contain the total frequencies in the remaining rows, Ri.
  • nobs[m,j-1], for j=1,2,,n1, contain the total frequencies in the remaining columns, Cj.
  • nobs[m,n], contains the total frequency, T.
If any ‘shrinkage’ has occurred, then all other cells contain no useful information.
Constraint: nobs[i-1,j-1]0, for i=1,2,,m-1 and j=1,2,,n-1.
num
Type: System..::..Int32%
On entry: the value assigned to num must determine whether automatic ‘shrinkage’ is required when any rij<1, as outlined in [Description](i).
If num=1, shrinkage is required, otherwise shrinkage is not required.
On exit: when Fisher's exact test for a 2×2 classification is used then num contains the number of elements used in the array p, otherwise num is set to zero.
pred
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, n]
Note: dim1 must satisfy the constraint: dim1m
On exit: the elements pred[i-1,j-1], where i=1,2,,m1 and j=1,2,,n1 contain the expected frequencies, rij corresponding to the observed frequencies nobs[i-1,j-1], except in the case when Fisher's exact test for a 2×2 classification is to be used, when pred is not used. No other elements are utilized.
chis
Type: System..::..Double%
On exit: the value of the test statistic, χ2, except when Fisher's exact test for a 2×2 classification is used in which case it is unspecified.
p
Type: array<System..::..Double>[]()[][]
An array of size [21]
p is used only when Fisher's exact test for a 2×2 classification is to be used.
On exit: the first num elements contain the probabilities associated with the various possible frequency tables, Pr, for r=0,1,,R1, the remainder are unspecified.
npos
Type: System..::..Int32%
npos is used only when Fisher's exact test for a 2×2 classification is to be used.
On exit: p[npos-1] holds the probability associated with the given table of frequencies.
ndf
Type: System..::..Int32%
On exit: the value of ndf gives the number of degrees of freedom for the chi-square distribution, m1-1×n1-1; when Fisher's exact test is used ndf=1.
m1
Type: System..::..Int32%
On exit: the number of rows of the two-way classification, after any ‘shrinkage’, m1.
n1
Type: System..::..Int32%
On exit: the number of columns of the two-way classification, after any ‘shrinkage’, n1.
ifail
Type: System..::..Int32%
On exit: ifail=0 unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

Description

The data consist of the frequencies for the two-way classification, denoted by nij, for i=1,2,,m and j=1,2,,n with m,n>1.
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 m1 by n1 (m1,n1>1), and let
  • Ri=j=1n1nij, the total frequency for the ith row, for i=1,2,,m1,
  • Cj=i=1m1nij, the total frequency for the jth column, for j=1,2,,n1, and
  • T=i=1m1Ri=j=1n1Cj, the total frequency.
There are two situations:
(i) If m1>2 and/or n1>2, or m1=n1=2 and T>40, then the matrix of expected frequencies, denoted by rij, for i=1,2,,m1 and j=1,2,,n1, and the test statistic, χ2, are computed, where
rij=RiCj/T,  i=1,2,,m1;j=1,2,,n1
and
χ2=i=1m1j=1n1rij-nij-Y2/rij,
where
Y=12  if ​m1=n1=20  otherwise
is Yates' correction for continuity.
Under the assumption that there is no association between the two classifications, χ2 will have approximately a chi-square distribution with m1-1×n1-1 degrees of freedom.
An option exists which allows for further ‘shrinkage’ of the matrix of frequencies in the case where rij<1 for the (i,j)th cell. If this is the case, then row i or column j will be combined with the adjacent row or column with smaller total. Row i is selected for combination if Ri×m1Cj×n1. This ‘shrinking’ process is continued until rij1 for all cells (i,j).
(ii) If m1=n1=2 and T40, the probabilities to enable Fisher's exact test to be made are computed.
The matrix of frequencies may be rearranged so that R1 is the smallest marginal (i.e., column and row) total, and C2C1. Under the assumption of no association between the classifications, the probability of obtaining r entries in cell 1,1 is computed where
Pr+1=R1!R2!C1!C2!T!r!R1-r!C1-r!T-C1-R1+r!,  r=0,1,,R1.
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 Pr.

References

None.

Error Indicators and Warnings

Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDNOB, LDPRED) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
ifail=1
The number of rows or columns of nobs is less than 2, possibly after shrinkage.
ifail=2
At least one frequency is negative, or all frequencies are zero.
ifail=-9000
An error occured, see message report.
ifail=-6000
Invalid Parameters value
ifail=-4000
Invalid dimension for array value
ifail=-8000
Negative dimension for array value
ifail=-6000
Invalid Parameters value

Accuracy

The method used is believed to be stable.

Parallelism and Performance

None.

Further Comments

The time taken by g01af 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, num>0, or alternatively ndf is 1 and nobs[m-1,n-1]40, the probabilities for use in Fisher's exact test for a 2×2 classification will be calculated, and not the test statistic with approximately a chi-square distribution.

Example

In the example program, NPROB determines the number of two-way classifications to be analysed. For each classification the frequencies are read, g01af 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 2×3 classification, with shrinkage not requested.

Example program (C#): g01afe.cs

Example program data: g01afe.d

Example program results: g01afe.r

See Also