# NAG FL Interfaceg08aef (test_​friedman)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g08aef performs the Friedman two-way analysis of variance by ranks on $k$ related samples of size $n$.

## 2Specification

Fortran Interface
 Subroutine g08aef ( x, ldx, k, n, w1, w2, fr, p,
 Integer, Intent (In) :: ldx, k, n Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(ldx,n) Real (Kind=nag_wp), Intent (Out) :: w1(k), w2(k), fr, p
#include <nag.h>
 void g08aef_ (const double x[], const Integer *ldx, const Integer *k, const Integer *n, double w1[], double w2[], double *fr, double *p, Integer *ifail)
The routine may be called by the names g08aef or nagf_nonpar_test_friedman.

## 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
 $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
1. (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.
2. (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$.
3. (c)The Friedman test statistic $F$ is computed, where
 $F=12nk(k+1) ∑i=1k{ti-12n(k+1)}2.$
g08aef 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 g08aaf) or Wilcoxon test (see g08agf) is in any case more appropriate.

## 4References

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

## 5Arguments

1: $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\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$.
2: $\mathbf{ldx}$Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g08aef is called.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{k}}$.
3: $\mathbf{k}$Integer Input
On entry: $k$, the number of samples.
Constraint: ${\mathbf{k}}\ge 2$.
4: $\mathbf{n}$Integer Input
On entry: $n$, the size of each sample.
Constraint: ${\mathbf{n}}\ge 1$.
5: $\mathbf{w1}\left({\mathbf{k}}\right)$Real (Kind=nag_wp) array Workspace
6: $\mathbf{w2}\left({\mathbf{k}}\right)$Real (Kind=nag_wp) array Workspace
7: $\mathbf{fr}$Real (Kind=nag_wp) Output
On exit: the value of the Friedman test statistic, $F$.
8: $\mathbf{p}$Real (Kind=nag_wp) Output
On exit: the approximate significance, $p$, of the Friedman test statistic.
9: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{ldx}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{k}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{k}}$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{k}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{k}}\ge 2$.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

For estimates of the accuracy of the significance $p$, see g01ecf. 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

g08aef is not threaded in any implementation.

The time taken by g08aef is approximately proportional to the product $nk$.
If $k=2$, the Sign test (see g08aaf) or Wilcoxon test (see g08agf) is more appropriate.

## 10Example

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.

### 10.1Program Text

Program Text (g08aefe.f90)

### 10.2Program Data

Program Data (g08aefe.d)

### 10.3Program Results

Program Results (g08aefe.r)