G08AEF (PDF version)
G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG08AEF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

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

## 2  Specification

 SUBROUTINE G08AEF ( X, LDX, K, N, W1, W2, FR, P, IFAIL)
 INTEGER LDX, K, N, IFAIL REAL (KIND=nag_wp) X(LDX,N), W1(K), W2(K), FR, P

## 3  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.$
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.
Siegel S (1956) Non-parametric Statistics for the Behavioral Sciences McGraw–Hill

## 5  Parameters

1:     $\mathrm{X}\left({\mathbf{LDX}},{\mathbf{N}}\right)$ – REAL (KIND=nag_wp) arrayInput
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:     $\mathrm{LDX}$ – INTEGERInput
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:     $\mathrm{K}$ – INTEGERInput
On entry: $k$, the number of samples.
Constraint: ${\mathbf{K}}\ge 2$.
4:     $\mathrm{N}$ – INTEGERInput
On entry: $n$, the size of each sample.
Constraint: ${\mathbf{N}}\ge 1$.
5:     $\mathrm{W1}\left({\mathbf{K}}\right)$ – REAL (KIND=nag_wp) arrayWorkspace
6:     $\mathrm{W2}\left({\mathbf{K}}\right)$ – REAL (KIND=nag_wp) arrayWorkspace
7:     $\mathrm{FR}$ – REAL (KIND=nag_wp)Output
On exit: the value of the Friedman test statistic, $F$.
8:     $\mathrm{P}$ – REAL (KIND=nag_wp)Output
On exit: the approximate significance, $p$, of the Friedman test statistic.
9:     $\mathrm{IFAIL}$ – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{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}}<1$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{LDX}}<{\mathbf{K}}$.
${\mathbf{IFAIL}}=3$
 On entry, ${\mathbf{K}}\le 1$.
${\mathbf{IFAIL}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-999$
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

## 7  Accuracy

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$.

Not applicable.

## 9  Further Comments

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.

## 10  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.

### 10.1  Program Text

Program Text (g08aefe.f90)

### 10.2  Program Data

Program Data (g08aefe.d)

### 10.3  Program Results

Program Results (g08aefe.r)

G08AEF (PDF version)
G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual