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$ related samples of size $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:
 At Mark 22: k was made optional

## Description

The Friedman test investigates the score differences between $k$ matched samples of size $n$, the scores in the $i$th sample being denoted by
 $xi1,xi2,…,xin.$
(Thus the sample scores may be regarded as a two-way table with $k$ rows and $n$ columns.) The hypothesis under test, ${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 ${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, ${r}_{ij}$ denoting the rank within column $j$ of the observation in row $i$. Average ranks are assigned to tied scores.
(b) The ranks are summed over each row to give rank sums ${t}_{\mathit{i}}=\sum _{j=1}^{n}{r}_{\mathit{i}j}$, for $\mathit{i}=1,2,\dots ,k$.
(c) The Friedman test statistic $F$ is computed, where
 $F=12nkk+1 ∑i=1kti-12nk+12.$
nag_nonpar_test_friedman (g08ae) returns the value of $F$, and also an approximation, $p$, to the significance of this value. ($F$ approximately follows a ${\chi }_{k-1}^{2}$ distribution, so large values of $F$ imply rejection of ${H}_{0}$). ${H}_{0}$ is rejected by a test of chosen size $\alpha$ if $p<\alpha$. The approximation $p$ is acceptable unless $k=4$ and $n<5$, or $k=3$ and $n<10$, or $k=2$ and $n<20$; for $k=3\text{​ or ​}4$, tables should be consulted (e.g., Siegel (1956)); for $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:     $\mathrm{x}\left(\mathit{ldx},{\mathbf{n}}\right)$ – double array
ldx, the first dimension of the array, must satisfy the constraint $\mathit{ldx}\ge {\mathbf{k}}$.
${\mathbf{x}}\left(\mathit{i},\mathit{j}\right)$ must be set to the value, ${x}_{\mathit{i}\mathit{j}}$, of observation $\mathit{j}$ in sample $\mathit{i}$, for $\mathit{i}=1,2,\dots ,k$ and $\mathit{j}=1,2,\dots ,n$.

### Optional Input Parameters

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

### Output Parameters

1:     $\mathrm{fr}$ – double scalar
The value of the Friedman test statistic, $F$.
2:     $\mathrm{p}$ – double scalar
The approximate significance, $p$, of the Friedman test statistic.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<1$.
${\mathbf{ifail}}=2$
 On entry, $\mathit{ldx}<{\mathbf{k}}$.
${\mathbf{ifail}}=3$
 On entry, ${\mathbf{k}}\le 1$.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

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

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

## Example

This example is taken from page 169 of Siegel (1956). The data relates to training scores of three matched samples of $18$ rats, trained under three different patterns of reinforcement.
```function g08ae_example

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

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];

fprintf('Friedman test\n\n');
mtitle = 'Data values';
matrix = 'General';
diag   = ' ';
[ifail] = x04ca( ...
matrix, diag, x, mtitle);

[fr, p, ifail] = g08ae(x);

fprintf('\nTest statistic       %6.3f\n', fr);
fprintf('Degrees of freedom   %6d\n', size(x,1)-1);
fprintf('Significance         %6.3f\n', p);

```
```g08ae example results

Friedman test

Data values
1          2          3          4          5          6          7
1      1.0000     2.0000     1.0000     1.0000     3.0000     2.0000     3.0000
2      3.0000     3.0000     3.0000     2.0000     1.0000     3.0000     2.0000
3      2.0000     1.0000     2.0000     3.0000     2.0000     1.0000     1.0000

8          9         10         11         12         13         14
1      1.0000     3.0000     3.0000     2.0000     2.0000     3.0000     2.0000
2      3.0000     1.0000     1.0000     3.0000     3.0000     2.0000     3.0000
3      2.0000     2.0000     2.0000     1.0000     1.0000     1.0000     1.0000

15         16         17         18
1      2.5000     3.0000     3.0000     2.0000
2      2.5000     2.0000     2.0000     3.0000
3      1.0000     1.0000     1.0000     1.0000

Test statistic        8.583
Degrees of freedom        2
Significance          0.014
```