G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG08AGF

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

G08AGF performs the Wilcoxon signed rank test on a single sample of size $n$.

## 2  Specification

 SUBROUTINE G08AGF ( N, X, XME, TAIL, ZER, W, WNOR, P, N1, WRK, IFAIL)
 INTEGER N, N1, IFAIL REAL (KIND=nag_wp) X(N), XME, W, WNOR, P, WRK(3*N) CHARACTER(1) TAIL, ZER

## 3  Description

The Wilcoxon one-sample signed rank test may be used to test whether a particular sample came from a population with a specified median. It is assumed that the population distribution is symmetric. The data consists of a single sample of $n$ observations denoted by ${x}_{1},{x}_{2},\dots ,{x}_{n}$. This sample may arise from the difference between pairs of observations from two matched samples of equal size taken from two populations, in which case the test may be used to test whether the median of the first population is the same as that of the second population.
The hypothesis under test, ${\mathrm{H}}_{0}$, often called the null hypothesis, is that the median is equal to some given value $\left({X}_{\mathrm{med}}\right)$, and this is to be tested against an alternative hypothesis ${H}_{1}$ which is
• ${H}_{1}$: population median $\text{}\ne {X}_{\mathrm{med}}$; or
• ${H}_{1}$: population median $\text{}>{X}_{\mathrm{med}}$; or
• ${H}_{1}$: population median $\text{}<{X}_{\mathrm{med}}$,
using a two tailed, upper tailed or lower tailed probability respectively. You select the alternative hypothesis by choosing the appropriate tail probability to be computed (see the description of parameter TAIL in Section 5).
The Wilcoxon test differs from the Sign test (see G08AAF) in that the magnitude of the scores is taken into account, rather than simply the direction of such scores.
The test procedure is as follows
 (a) For each ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$, the signed difference ${d}_{i}={x}_{i}-{X}_{\mathrm{med}}$ is found, where ${X}_{\mathrm{med}}$ is a given test value for the median of the sample. (b) The absolute differences $\left|{d}_{i}\right|$ are ranked with rank ${r}_{i}$ and any tied values of $\left|{d}_{i}\right|$ are assigned the average of the tied ranks. You may choose whether or not to ignore any cases where ${d}_{i}=0$ by removing them before or after ranking (see the description of the parameter ZER in Section 5). (c) The number of nonzero ${d}_{i}$ is found. (d) To each rank is affixed the sign of the ${d}_{i}$ to which it corresponds. Let ${s}_{i}=\mathrm{sign}\left({d}_{i}\right){r}_{i}$. (e) The sum of the positive-signed ranks, $W=\sum _{{s}_{i}>0}\phantom{\rule{0.25em}{0ex}}{s}_{i}=\sum _{i=1}^{n}\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({s}_{i},0.0\right)$, is calculated.
G08AGF returns
(a) the test statistic $W$;
(b) the number ${n}_{1}$ of nonzero ${d}_{i}$;
(c) the approximate Normal test statistic $z$, where
 $z= W- n1n1+14-signW- n1n1+14 × 12 14∑i=1nsi2 ;$
(d) the tail probability, $p$, corresponding to $W$, depending on the choice of the alternative hypothesis, ${H}_{1}$.
If ${n}_{1}\le 80$, $p$ is computed exactly; otherwise, an approximation to $p$ is returned based on an approximate Normal statistic corrected for continuity according to the tail specified.
The value of $p$ can be used to perform a significance test on the median against the alternative hypothesis. Let $\alpha$ be the size of the significance test (that is, $\alpha$ is the probability of rejecting ${H}_{0}$ when ${H}_{0}$ is true). If $p<\alpha$ then the null hypothesis is rejected. Typically $\alpha$ might be $0.05$ or $0.01$.
Conover W J (1980) Practical Nonparametric Statistics Wiley
Neumann N (1988) Some procedures for calculating the distributions of elementary nonparametric teststatistics Statistical Software Newsletter 14(3) 120–126
Siegel S (1956) Non-parametric Statistics for the Behavioral Sciences McGraw–Hill

## 5  Parameters

1:     N – INTEGERInput
On entry: $n$, the size of the sample.
Constraint: ${\mathbf{N}}\ge 1$.
2:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: the sample observations, ${x}_{1},{x}_{2},\dots ,{x}_{n}$.
3:     XME – REAL (KIND=nag_wp)Input
On entry: the median test value, ${X}_{\mathrm{med}}$.
4:     TAIL – CHARACTER(1)Input
On entry: indicates the choice of tail probability, and hence the alternative hypothesis.
${\mathbf{TAIL}}=\text{'T'}$
A two tailed probability is calculated and the alternative hypothesis is ${\mathrm{H}}_{1}$: population median $\text{}\ne {X}_{\mathrm{med}}$.
${\mathbf{TAIL}}=\text{'U'}$
An upper tailed probability is calculated and the alternative hypothesis is ${\mathrm{H}}_{1}$: population median $\text{}>{X}_{\mathrm{med}}$.
${\mathbf{TAIL}}=\text{'L'}$
A lower tailed probability is calculated and the alternative hypothesis is ${\mathrm{H}}_{1}$: population median $\text{}<{X}_{\mathrm{med}}$.
Constraint: ${\mathbf{TAIL}}=\text{'T'}$, $\text{'U'}$ or $\text{'L'}$.
5:     ZER – CHARACTER(1)Input
On entry: indicates whether or not to include the cases where ${d}_{i}=0.0$ in the ranking of the ${d}_{i}$'s.
${\mathbf{ZER}}=\text{'Y'}$
All ${d}_{i}=0.0$ are included when ranking.
${\mathbf{ZER}}=\text{'N'}$
All ${d}_{i}=0.0$, are ignored, that is all cases where ${d}_{i}=0.0$ are removed before ranking.
Constraint: ${\mathbf{ZER}}=\text{'Y'}$ or $\text{'N'}$.
6:     W – REAL (KIND=nag_wp)Output
On exit: the Wilcoxon rank sum statistic, $W$, being the sum of the positive ranks.
7:     WNOR – REAL (KIND=nag_wp)Output
On exit: the approximate Normal test statistic, $z$, as described in Section 3.
8:     P – REAL (KIND=nag_wp)Output
On exit: the tail probability, $p$, as specified by the parameter TAIL.
9:     N1 – INTEGEROutput
On exit: the number of nonzero ${d}_{i}$'s, ${n}_{1}$.
10:   WRK($3×{\mathbf{N}}$) – REAL (KIND=nag_wp) arrayWorkspace
11:   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{TAIL}}\ne \text{'T'}$, $\text{'U'}$ or $\text{'L'}$. or ${\mathbf{ZER}}\ne \text{'Y'}$ or $\text{'N'}$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{N}}<1$.
${\mathbf{IFAIL}}=3$
The whole sample is identical to the given median test value.

## 7  Accuracy

The approximation used to calculate $p$ when ${n}_{1}>80$ will return a value with a relative error of less than $10%$ for most cases. The error may increase for cases where there are a large number of ties in the sample.

The time taken by G08AGF increases with ${n}_{1}$, until ${n}_{1}>80$, from which point on the approximation is used. The time decreases significantly at this point and increases again modestly with ${n}_{1}$ for ${n}_{1}>80$.

## 9  Example

This example performs the Wilcoxon signed rank test on two matched samples of size $8$, taken from two populations. The distribution of the differences between pairs of observations from the two populations is assumed to be symmetric. The test is used to test whether the medians of the two distributions of the populations are equal or not. The test statistic, the approximate Normal statistic and the two tailed probability are computed and printed.

### 9.1  Program Text

Program Text (g08agfe.f90)

### 9.2  Program Data

Program Data (g08agfe.d)

### 9.3  Program Results

Program Results (g08agfe.r)