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_friedman (g08ae)

## Purpose

nag_nonpar_test_friedman (g08ae) performs the Friedman two-way analysis of variance by ranks on k$k$ related samples of size n$n$.

## Syntax

[fr, p, ifail] = g08ae(x, 'k', k, 'n', n)
[fr, p, ifail] = nag_nonpar_test_friedman(x, 'k', k, 'n', n)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 22: k has been made optional
.

## Description

The Friedman test investigates the score differences between k$k$ matched samples of size n$n$, the scores in the i$i$th sample being denoted by
 xi1,xi2, … ,xin. $xi1,xi2,…,xin.$
(Thus the sample scores may be regarded as a two-way table with k$k$ rows and n$n$ columns.) The hypothesis under test, H0${H}_{0}$, often called the null hypothesis, is that the samples come from the same population, and this is to be tested against the alternative hypothesis H1${H}_{1}$ that they come from different populations.
The test is based on the observed distribution of score rankings between the matched observations in different samples.
The test proceeds as follows
(a) The scores in each column are ranked, rij${r}_{ij}$ denoting the rank within column j$j$ of the observation in row i$i$. Average ranks are assigned to tied scores.
(b) The ranks are summed over each row to give rank sums ti = j = 1nrij${t}_{\mathit{i}}=\sum _{j=1}^{n}{r}_{\mathit{i}j}$, for i = 1,2,,k$\mathit{i}=1,2,\dots ,k$.
(c) The Friedman test statistic F$F$ is computed, where
 k F = 12/(nk(k + 1)) ∑ {ti − (1/2)n(k + 1)}2. i = 1
$F=12nk(k+1) ∑i=1k{ti-12n(k+1)}2.$
nag_nonpar_test_friedman (g08ae) returns the value of F$F$, and also an approximation, p$p$, to the significance of this value. (F$F$ approximately follows a χk12${\chi }_{k-1}^{2}$ distribution, so large values of F$F$ imply rejection of H0${H}_{0}$). H0${H}_{0}$ is rejected by a test of chosen size α$\alpha$ if p < α$p<\alpha$. The approximation p$p$ is acceptable unless k = 4$k=4$ and n < 5$n<5$, or k = 3$k=3$ and n < 10$n<10$, or k = 2$k=2$ and n < 20$n<20$; for k = 3​ or ​4$k=3\text{​ or ​}4$, tables should be consulted (e.g., Siegel (1956)); for k = 2$k=2$ the Sign test (see nag_nonpar_test_sign (g08aa)) or Wilcoxon test (see nag_nonpar_test_wilcoxon (g08ag)) is in any case more appropriate.

## References

Siegel S (1956) Non-parametric Statistics for the Behavioral Sciences McGraw–Hill

## Parameters

### Compulsory Input Parameters

1:     x(ldx,n) – double array
ldx, the first dimension of the array, must satisfy the constraint ldxk$\mathit{ldx}\ge {\mathbf{k}}$.
x(i,j)${\mathbf{x}}\left(\mathit{i},\mathit{j}\right)$ must be set to the value, xij${x}_{\mathit{i}\mathit{j}}$, of observation j$\mathit{j}$ in sample i$\mathit{i}$, for i = 1,2,,k$\mathit{i}=1,2,\dots ,k$ and j = 1,2,,n$\mathit{j}=1,2,\dots ,n$.

### Optional Input Parameters

1:     k – int64int32nag_int scalar
Default: The first dimension of the array x.
k$k$, the number of samples.
Constraint: k2${\mathbf{k}}\ge 2$.
2:     n – int64int32nag_int scalar
Default: The second dimension of the array x.
n$n$, the size of each sample.
Constraint: n1${\mathbf{n}}\ge 1$.

ldx w1 w2

### Output Parameters

1:     fr – double scalar
The value of the Friedman test statistic, F$F$.
2:     p – double scalar
The approximate significance, p$p$, of the Friedman test statistic.
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:
ifail = 1${\mathbf{ifail}}=1$
 On entry, n < 1${\mathbf{n}}<1$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, ldx < k$\mathit{ldx}<{\mathbf{k}}$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, k ≤ 1${\mathbf{k}}\le 1$.

## Accuracy

For estimates of the accuracy of the significance p$p$, see nag_stat_prob_chisq (g01ec). The χ2${\chi }^{2}$ approximation is acceptable unless k = 4$k=4$ and n < 5$n<5$, or k = 3$k=3$ and n < 10$n<10$, or k = 2$k=2$ and n < 20$n<20$.

The time taken by nag_nonpar_test_friedman (g08ae) is approximately proportional to the product nk$nk$.
If k = 2$k=2$, the Sign test (see nag_nonpar_test_sign (g08aa)) or Wilcoxon test (see nag_nonpar_test_wilcoxon (g08ag)) is more appropriate.

## Example

```function nag_nonpar_test_friedman_example
x = [1, 2, 1, 1, 3, 2, 3, 1, 3, 3, 2, 2, 3, 2, 2.5, 3, 3, 2;
3, 3, 3, 2, 1, 3, 2, 3, 1, 1, 3, 3, 2, 3, 2.5, 2, 2, 3;
2, 1, 2, 3, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1];
[fr, p, ifail] = nag_nonpar_test_friedman(x)
```
```

fr =

8.5833

p =

0.0137

ifail =

0

```
```function g08ae_example
x = [1, 2, 1, 1, 3, 2, 3, 1, 3, 3, 2, 2, 3, 2, 2.5, 3, 3, 2;
3, 3, 3, 2, 1, 3, 2, 3, 1, 1, 3, 3, 2, 3, 2.5, 2, 2, 3;
2, 1, 2, 3, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1];
[fr, p, ifail] = g08ae(x)
```
```

fr =

8.5833

p =

0.0137

ifail =

0

```