# NAG Library Function Document

## 1Purpose

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

## 2Specification

 #include #include
 void nag_friedman_test (Integer k, Integer n, const double x[], Integer tdx, double *fr, double *p, NagError *fail)

## 3Description

The Friedman test investigates the score differences between $k$ matched samples of size $n$, the scores in the $i$th sample being denoted by:
 $x i1 , x i2 , … , x in .$
(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 $FR$ is computed, where
 $FR = 12 nk k+1 ∑ i=1 k t i - 1 2 n k+1 2 .$
nag_friedman_test (g08aec) returns the value of $FR$, and also an approximation, $p$, to the significance of this value. ($FR$ approximately follows a ${\chi }_{k-1}^{2}$ distribution, so large values of $FR$ 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$ or 4, tables should be consulted (e.g., n of Siegel (1956)); for $k=2$ the Sign test (see nag_sign_test (g08aac)) or Wilcoxon test (see nag_wilcoxon_test (g08agc)) is in any case more appropriate.

## 4References

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

## 5Arguments

1:    $\mathbf{k}$IntegerInput
On entry: $k$, the number of samples.
Constraint: ${\mathbf{k}}\ge 2$.
2:    $\mathbf{n}$IntegerInput
On entry: the size of each sample, $n$.
Constraint: ${\mathbf{n}}\ge 1$.
3:    $\mathbf{x}\left[{\mathbf{k}}×{\mathbf{tdx}}\right]$const doubleInput
On entry: ${\mathbf{x}}\left[\left(\mathit{i}-1\right)×{\mathbf{tdx}}+\mathit{j}-1\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$.
4:    $\mathbf{tdx}$IntegerInput
On entry: the stride separating matrix column elements in the array x.
Constraint: ${\mathbf{tdx}}\ge {\mathbf{n}}$.
5:    $\mathbf{fr}$double *Output
On exit: the value of the Friedman test statistic, $FR$.
6:    $\mathbf{p}$double *Output
On exit: the approximate significance, $p$, of the Friedman test statistic.
7:    $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

## 6Error Indicators and Warnings

NE_2_INT_ARG_LT
On entry, ${\mathbf{tdx}}=〈\mathit{\text{value}}〉$ while ${\mathbf{n}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{tdx}}\ge {\mathbf{n}}$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_INT_ARG_LE
On entry, ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{k}}\ge 2$.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.

## 7Accuracy

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

## 8Parallelism and Performance

nag_friedman_test (g08aec) is not threaded in any implementation.

The time taken by nag_friedman_test (g08aec) is approximately proportional to the product $nk$.
If $k=2$, the Sign test (see nag_sign_test (g08aac)) or Wilcoxon test (see nag_wilcoxon_test (g08agc)) is more appropriate.

## 10Example

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

### 10.1Program Text

Program Text (g08aece.c)

### 10.2Program Data

Program Data (g08aece.d)

### 10.3Program Results

Program Results (g08aece.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017