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_wilcoxon (g08ag)

## Purpose

nag_nonpar_test_wilcoxon (g08ag) performs the Wilcoxon signed rank test on a single sample of size n$n$.

## Syntax

[w, wnor, p, n1, ifail] = g08ag(x, xme, tail, zer, 'n', n)
[w, wnor, p, n1, ifail] = nag_nonpar_test_wilcoxon(x, xme, tail, zer, 'n', n)

## 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$n$ observations denoted by x1,x2,,xn${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, H0${\mathrm{H}}_{0}$, often called the null hypothesis, is that the median is equal to some given value (Xmed)$\left({X}_{\mathrm{med}}\right)$, and this is to be tested against an alternative hypothesis H1${H}_{1}$ which is
• H1${H}_{1}$: population median Xmed$\text{}\ne {X}_{\mathrm{med}}$; or
• H1${H}_{1}$: population median > Xmed$\text{}>{X}_{\mathrm{med}}$; or
• H1${H}_{1}$: population median < Xmed$\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 [Parameters]).
The Wilcoxon test differs from the Sign test (see nag_nonpar_test_sign (g08aa)) 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 xi${x}_{\mathit{i}}$, for i = 1,2, … ,n$\mathit{i}=1,2,\dots ,n$, the signed difference di = xi − Xmed${d}_{i}={x}_{i}-{X}_{\mathrm{med}}$ is found, where Xmed${X}_{\mathrm{med}}$ is a given test value for the median of the sample. (b) The absolute differences |di|$|{d}_{i}|$ are ranked with rank ri${r}_{i}$ and any tied values of |di|$|{d}_{i}|$ are assigned the average of the tied ranks. You may choose whether or not to ignore any cases where di = 0${d}_{i}=0$ by removing them before or after ranking (see the description of the parameter zer in Section [Parameters]). (c) The number of nonzero di${d}_{i}$ is found. (d) To each rank is affixed the sign of the di${d}_{i}$ to which it corresponds. Let si = sign(di)ri${s}_{i}=\mathrm{sign}\left({d}_{i}\right){r}_{i}$. (e) The sum of the positive-signed ranks, W = ∑ si > 0 si = ∑ i = 1nmax (si,0.0)$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.
nag_nonpar_test_wilcoxon (g08ag) returns
(a) the test statistic W$W$;
(b) the number n1${n}_{1}$ of nonzero di${d}_{i}$;
(c) the approximate Normal test statistic z$z$, where
 z = ( (W − (n1(n1 + 1))/4) − sign(W − (n1(n1 + 1))/4) × (1/2))/(sqrt((1/4) ∑ i = 1nsi2)); $z= (W- n1(n1+1)4)-sign(W- n1(n1+1)4) × 12 14∑i=1nsi2 ;$
(d) the tail probability, p$p$, corresponding to W$W$, depending on the choice of the alternative hypothesis, H1${H}_{1}$.
If n180${n}_{1}\le 80$, p$p$ is computed exactly; otherwise, an approximation to p$p$ is returned based on an approximate Normal statistic corrected for continuity according to the tail specified.
The value of p$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 H0${H}_{0}$ when H0${H}_{0}$ is true). If p < α$p<\alpha$ then the null hypothesis is rejected. Typically α$\alpha$ might be 0.05$0.05$ or 0.01$0.01$.

## References

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

## Parameters

### Compulsory Input Parameters

1:     x(n) – double array
n, the dimension of the array, must satisfy the constraint n1${\mathbf{n}}\ge 1$.
The sample observations, x1,x2,,xn${x}_{1},{x}_{2},\dots ,{x}_{n}$.
2:     xme – double scalar
The median test value, Xmed${X}_{\mathrm{med}}$.
3:     tail – string (length ≥ 1)
Indicates the choice of tail probability, and hence the alternative hypothesis.
tail = 'T'${\mathbf{tail}}=\text{'T'}$
A two tailed probability is calculated and the alternative hypothesis is H1${\mathrm{H}}_{1}$: population median Xmed$\text{}\ne {X}_{\mathrm{med}}$.
tail = 'U'${\mathbf{tail}}=\text{'U'}$
An upper tailed probability is calculated and the alternative hypothesis is H1${\mathrm{H}}_{1}$: population median > Xmed$\text{}>{X}_{\mathrm{med}}$.
tail = 'L'${\mathbf{tail}}=\text{'L'}$
A lower tailed probability is calculated and the alternative hypothesis is H1${\mathrm{H}}_{1}$: population median < Xmed$\text{}<{X}_{\mathrm{med}}$.
Constraint: tail = 'T'${\mathbf{tail}}=\text{'T'}$, 'U'$\text{'U'}$ or 'L'$\text{'L'}$.
4:     zer – string (length ≥ 1)
Indicates whether or not to include the cases where di = 0.0${d}_{i}=0.0$ in the ranking of the di${d}_{i}$'s.
zer = 'Y'${\mathbf{zer}}=\text{'Y'}$
All di = 0.0${d}_{i}=0.0$ are included when ranking.
zer = 'N'${\mathbf{zer}}=\text{'N'}$
All di = 0.0${d}_{i}=0.0$, are ignored, that is all cases where di = 0.0${d}_{i}=0.0$ are removed before ranking.
Constraint: zer = 'Y'${\mathbf{zer}}=\text{'Y'}$ or 'N'$\text{'N'}$.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array x.
n$n$, the size of the sample.
Constraint: n1${\mathbf{n}}\ge 1$.

wrk

### Output Parameters

1:     w – double scalar
The Wilcoxon rank sum statistic, W$W$, being the sum of the positive ranks.
2:     wnor – double scalar
The approximate Normal test statistic, z$z$, as described in Section [Description].
3:     p – double scalar
The tail probability, p$p$, as specified by the parameter tail.
4:     n1 – int64int32nag_int scalar
The number of nonzero di${d}_{i}$'s, n1${n}_{1}$.
5:     ifail – int64int32nag_int scalar
${\mathrm{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:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

ifail = 1${\mathbf{ifail}}=1$
 On entry, tail ≠ 'T'${\mathbf{tail}}\ne \text{'T'}$, 'U'$\text{'U'}$ or 'L'$\text{'L'}$. or zer ≠ 'Y'${\mathbf{zer}}\ne \text{'Y'}$ or 'N'$\text{'N'}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, n < 1${\mathbf{n}}<1$.
W ifail = 3${\mathbf{ifail}}=3$
The whole sample is identical to the given median test value.

## Accuracy

The approximation used to calculate p$p$ when n1 > 80${n}_{1}>80$ will return a value with a relative error of less than 10%$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 nag_nonpar_test_wilcoxon (g08ag) increases with n1${n}_{1}$, until n1 > 80${n}_{1}>80$, from which point on the approximation is used. The time decreases significantly at this point and increases again modestly with n1${n}_{1}$ for n1 > 80${n}_{1}>80$.

## Example

```function nag_nonpar_test_wilcoxon_example
x = [19;
27;
-1;
6;
7;
13;
-4;
3];
xme = 0;
tail = 'Two-tail';
zer = 'Nozeros';
[w, wnor, p, n1, ifail] = nag_nonpar_test_wilcoxon(x, xme, tail, zer)
```
```

w =

32

wnor =

1.8904

p =

0.0547

n1 =

8

ifail =

0

```
```function g08ag_example
x = [19;
27;
-1;
6;
7;
13;
-4;
3];
xme = 0;
tail = 'Two-tail';
zer = 'Nozeros';
[w, wnor, p, n1, ifail] = g08ag(x, xme, tail, zer)
```
```

w =

32

wnor =

1.8904

p =

0.0547

n1 =

8

ifail =

0

```