Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_nonpar_test_cochranq (g08al)

## Purpose

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

## Syntax

[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:
Mark 22: n has been made optional
.

## Description

Cochran's Q$Q$-test may be used to test for differences between k$k$ treatments applied independently to n$n$ individuals or blocks (k$k$ related samples of equal size n$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$1$ or 0$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${H}_{0}$ : the treatments are equally effective, that is the probability of obtaining a 1$1$ within a block is the same for each treatment. H1${H}_{1}$ : there is a difference between the treatments, that is the probability of obtaining a 1$1$ is not the same for different treatments within blocks.
The data is often represented in the form of a table with the n$n$ rows representing the blocks and the k$k$ columns the treatments. Let Ri${R}_{\mathit{i}}$ represent the row totals, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$, and Cj${C}_{\mathit{j}}$ represent the column totals, for j = 1,2,,k$\mathit{j}=1,2,\dots ,k$. Let xij${x}_{ij}$ represent the response or result where xij = 0​ or ​1${x}_{ij}=0\text{​ or ​}1$.
 Treatments Blocks 1 2 k$k$ Row Totals 1 x11${x}_{11}$ x12${x}_{12}$ ⋯ $\cdots$ x1k${x}_{1k}$ R1${R}_{1}$ 2 x21${x}_{21}$ x22${x}_{22}$ ⋯ $\cdots$ x2k${x}_{2k}$ R2${R}_{2}$ ⋮ $⋮$ ⋮ $⋮$ ⋮ $⋮$ n$n$ xn1${x}_{n1}$ xn2${x}_{n2}$ ⋯ $\cdots$ xnk${x}_{nk}$ Rn${R}_{n}$ Column Totals C1${C}_{1}$ C2${C}_{2}$ Ck${C}_{k}$ N = Grand Total$N=\text{Grand Total}$
If pij = Pr(xij = 1)${p}_{ij}=\mathrm{Pr}\left({x}_{ij}=1\right)$, for i = 1,2,,n$i=1,2,\dots ,n$ and j = 1,2,,k$j=1,2,\dots ,k$, then the hypotheses may be restated as follows
 H0${H}_{0}$ : pi1 = pi2 = … = pik${p}_{i1}={p}_{i2}=\dots ={p}_{ik}$, for each i = 1,2, … ,n$i=1,2,\dots ,n$. H1${H}_{1}$: pij ≠ pik${p}_{ij}\ne {p}_{ik}$, for some j$j$ and k$k$, and for some i$i$.
The test statistic is defined as
 Q = (k(k − 1) ∑ j = 1k (Cj − N/k)2)/( ∑ i = 1nRi(k − Ri)). $Q=k(k-1)∑j=1k (Cj-Nk) 2 ∑i=1nRi(k-Ri) .$
When the number of blocks, n$n$, is large relative to the number of treatments, k$k$, Q$Q$ has an approximate χ2${\chi }^{2}$-distribution with k1$k-1$ degrees of freedom. This is used to find the probability, p$p$, of obtaining a statistic greater than or equal to the computed value of Q$Q$. Thus p$p$ is the upper tail probability associated with the computed value of Q$Q$, where the χ2${\chi }^{2}$-distribution is used to approximate the true distribution of Q$Q$.

## References

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

## Parameters

### Compulsory Input Parameters

1:     x(ldx,k) – double array
ldx, the first dimension of the array, must satisfy the constraint ldxn$\mathit{ldx}\ge {\mathbf{n}}$.
The matrix of observed zero-one data. x(i,j)${\mathbf{x}}\left(\mathit{i},\mathit{j}\right)$ must contain the value xij${x}_{\mathit{i}\mathit{j}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$ and j = 1,2,,k$\mathit{j}=1,2,\dots ,k$.
Constraint: x(i,j) = 0.0${\mathbf{x}}\left(\mathit{i},\mathit{j}\right)=0.0$ or 1.0$1.0$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$ and j = 1,2,,k$\mathit{j}=1,2,\dots ,k$.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The first dimension of the array x.
n$n$, the number of blocks.
Constraint: n2${\mathbf{n}}\ge 2$.
2:     k – int64int32nag_int scalar
Default: The second dimension of the array x.
k$k$, the number of treatments.
Constraint: k2${\mathbf{k}}\ge 2$.

ldx

### Output Parameters

1:     q – double scalar
The value of the Cochran Q$Q$-test statistic.
2:     prob – double scalar
The upper tail probability, p$p$, associated with the Cochran Q$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
${\mathrm{ifail}}={\mathbf{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.

ifail = 1${\mathbf{ifail}}=1$
 On entry, n < 2${\mathbf{n}}<2$, or k < 2${\mathbf{k}}<2$, or ldx < n$\mathit{ldx}<{\mathbf{n}}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, x(i,j) ≠ 0.0${\mathbf{x}}\left(i,j\right)\ne 0.0$ or 1.0$1.0$ for some i$i$ and j$j$, i = 1,2, … ,n$i=1,2,\dots ,n$ and j = 1,2, … ,k$j=1,2,\dots ,k$.
W ifail = 3${\mathbf{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.

## Accuracy

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

None.

## Example

```function nag_nonpar_test_cochranq_example
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];
[q, prob, ifail] = nag_nonpar_test_cochranq(x)
```
```

q =

2.8000

prob =

0.2466

ifail =

0

```
```function g08al_example
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];
[q, prob, ifail] = g08al(x)
```
```

q =

2.8000

prob =

0.2466

ifail =

0

```