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_nonpar_test_cochranq (g08al)


    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example


nag_nonpar_test_cochranq (g08al) performs the Cochran Q-test on cross-classified binary data.


[q, prob, ifail] = g08al(x, 'n', n, 'k', k)
[q, prob, ifail] = nag_nonpar_test_cochranq(x, 'n', n, 'k', k)
Note: the interface to this routine has changed since earlier releases of the toolbox:
At Mark 22: n was made optional


Cochran's Q-test may be used to test for differences between k treatments applied independently to n individuals or blocks (k related samples of equal size n), where the observed response can take only one of two possible values; for example a treatment may result in a ‘success’ or ‘failure’. The data is recorded as either 1 or 0 to represent this dichotomization.
The use of this ‘randomized block design’ allows the effect of differences between the blocks to be separated from the differences between the treatments. The test assumes that the blocks were randomly selected from all possible blocks and that the result may be one of two possible outcomes common to all treatments within blocks.
The null and alternative hypotheses to be tested may be stated as follows.
H0 :the treatments are equally effective, that is the probability of obtaining a 1 within a block is the same for each treatment.
H1 :there is a difference between the treatments, that is the probability of obtaining a 1 is not the same for different treatments within blocks.
The data is often represented in the form of a table with the n rows representing the blocks and the k columns the treatments. Let Ri represent the row totals, for i=1,2,,n, and Cj represent the column totals, for j=1,2,,k. Let xij represent the response or result where xij=0​ or ​1.
Blocks 1 2   k Row Totals
1 x11 x12 x1k R1
2 x21 x22 x2k R2
n xn1 xn2 xnk Rn
Column Totals C1 C2   Ck N=Grand Total
If pij=Prxij=1, for i=1,2,,n and j=1,2,,k, then the hypotheses may be restated as follows
H0 :pi1=pi2==pik, for each i=1,2,,n.
H1:pijpik, for some j and k, and for some i.
The test statistic is defined as
Q=kk-1j=1k Cj-Nk 2 i=1nRik-Ri .  
When the number of blocks, n, is large relative to the number of treatments, k, Q has an approximate χ2-distribution with k-1 degrees of freedom. This is used to find the probability, p, of obtaining a statistic greater than or equal to the computed value of Q. Thus p is the upper tail probability associated with the computed value of Q, where the χ2-distribution is used to approximate the true distribution of Q.


Conover W J (1980) Practical Nonparametric Statistics Wiley
Siegel S (1956) Non-parametric Statistics for the Behavioral Sciences McGraw–Hill


Compulsory Input Parameters

1:     xldxk – double array
ldx, the first dimension of the array, must satisfy the constraint ldxn.
The matrix of observed zero-one data. xij must contain the value xij, for i=1,2,,n and j=1,2,,k.
Constraint: xij=0.0 or 1.0, for i=1,2,,n and j=1,2,,k.

Optional Input Parameters

1:     n int64int32nag_int scalar
Default: the first dimension of the array x.
n, the number of blocks.
Constraint: n2.
2:     k int64int32nag_int scalar
Default: the second dimension of the array x.
k, the number of treatments.
Constraint: k2.

Output Parameters

1:     q – double scalar
The value of the Cochran Q-test statistic.
2:     prob – double scalar
The upper tail probability, p, associated with the Cochran Q-test statistic, that is the probability of obtaining a value greater than or equal to the observed value (the output value of q).
3:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and 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.

On entry,n<2,
On entry,xij0.0 or 1.0 for some i and j, i=1,2,,n and j=1,2,,k.
W  ifail=3
The approximation process used to calculate the tail probability has failed to converge. The result returned in prob may still be a reasonable approximation.
An unexpected error has been triggered by this routine. Please contact NAG.
Your licence key may have expired or may not have been installed correctly.
Dynamic memory allocation failed.


The use of the χ2-distribution as an approximation to the true distribution of the Cochran Q-test statistic improves as k increases and as n increases relative to k. This approximation should be a reasonable one when the total number of observations left, after omitting those rows containing all 0 or 1, is greater than about 25 and the number of rows left is larger than 5.

Further Comments



The following example is taken from page 201 of Conover (1980). The data represents the success of three basketball enthusiasts in predicting the outcome of 12 collegiate basketball games, selected at random, using 1 for successful prediction of the outcome and 0 for unsuccessful prediction. This data is read in and the Cochran Q-test statistic and its corresponding upper tail probability are computed and printed.
function g08al_example

fprintf('g08al example results\n\n');

x = [1, 1, 1;
     1, 1, 1;
     0, 1, 0;
     1, 1, 0;
     0, 0, 0;
     1, 1, 1;
     1, 1, 1;
     1, 1, 0;
     0, 0, 1;
     0, 1, 0;
     1, 1, 1;
     1, 1, 1];

[ifail] = x04ca( ...
                 'General', 'Non-unit', x, 'Data matrix');


[q, prob, ifail] = g08al(x);

fprintf('Cochrans Q test statistic = %10.4f\n', q);
fprintf('Degrees of freedom        = %5d\n', size(x,2)-1);
fprintf('Upper-tail probability    = %10.4f\n', prob);

g08al example results

 Data matrix
           1       2       3
  1   1.0000  1.0000  1.0000
  2   1.0000  1.0000  1.0000
  3   0.0000  1.0000  0.0000
  4   1.0000  1.0000  0.0000
  5   0.0000  0.0000  0.0000
  6   1.0000  1.0000  1.0000
  7   1.0000  1.0000  1.0000
  8   1.0000  1.0000  0.0000
  9   0.0000  0.0000  1.0000
 10   0.0000  1.0000  0.0000
 11   1.0000  1.0000  1.0000
 12   1.0000  1.0000  1.0000

Cochrans Q test statistic =     2.8000
Degrees of freedom        =     2
Upper-tail probability    =     0.2466

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–2015