PDF version (NAG web site
, 64bit version, 64bit version)
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
Description
The Wilcoxon onesample 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 x_{1},x_{2}, … ,x_{n}${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,
H_{0}${\mathrm{H}}_{0}$, often called the null hypothesis, is that the median is equal to some given value
(X_{med})$\left({X}_{\mathrm{med}}\right)$, and this is to be tested against an alternative hypothesis
H_{1}${H}_{1}$ which is
 H_{1}${H}_{1}$: population median ≠ X_{med}$\text{}\ne {X}_{\mathrm{med}}$; or
 H_{1}${H}_{1}$: population median > X_{med}$\text{}>{X}_{\mathrm{med}}$; or
 H_{1}${H}_{1}$: population median < X_{med}$\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 x_{i}${x}_{\mathit{i}}$, for i = 1,2, … ,n$\mathit{i}=1,2,\dots ,n$, the signed difference d_{i} = x_{i} − X_{med}${d}_{i}={x}_{i}{X}_{\mathrm{med}}$ is found, where X_{med}${X}_{\mathrm{med}}$ is a given test value for the median of the sample. 
(b) 
The absolute differences d_{i}$\left{d}_{i}\right$ are ranked with rank r_{i}${r}_{i}$ and any tied values of d_{i}$\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${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 d_{i}${d}_{i}$ is found. 
(d) 
To each rank is affixed the sign of the d_{i}${d}_{i}$ to which it corresponds. Let s_{i} = sign(d_{i})r_{i}${s}_{i}=\mathrm{sign}\left({d}_{i}\right){r}_{i}$. 
(e) 
The sum of the positivesigned ranks, W = ∑ _{si > 0} s_{i} = ∑ _{i = 1}^{n}max (s_{i},0.0)$W={\displaystyle \sum _{{s}_{i}>0}}\phantom{\rule{0.25em}{0ex}}{s}_{i}={\displaystyle \sum _{i=1}^{n}}\mathrm{max}\phantom{\rule{0.125em}{0ex}}({s}_{i},0.0)$, is calculated. 
nag_nonpar_test_wilcoxon (g08ag) returns
(a) 
the test statistic W$W$; 
(b) 
the number n_{1}${n}_{1}$ of nonzero d_{i}${d}_{i}$; 
(c) 
the approximate Normal test statistic z$z$, where

(d) 
the tail probability, p$p$, corresponding to W$W$, depending on the choice of the alternative hypothesis, H_{1}${H}_{1}$. 
If n_{1} ≤ 80${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 H_{0}${H}_{0}$ when H_{0}${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) Nonparametric 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
n ≥ 1${\mathbf{n}}\ge 1$.
The sample observations, x_{1},x_{2}, … ,x_{n}${x}_{1},{x}_{2},\dots ,{x}_{n}$.
 2:
xme – double scalar
The median test value, X_{med}${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 H_{1}${\mathrm{H}}_{1}$: population median ≠ X_{med}$\text{}\ne {X}_{\mathrm{med}}$.
 tail = 'U'${\mathbf{tail}}=\text{'U'}$
 An upper tailed probability is calculated and the alternative hypothesis is H_{1}${\mathrm{H}}_{1}$: population median > X_{med}$\text{}>{X}_{\mathrm{med}}$.
 tail = 'L'${\mathbf{tail}}=\text{'L'}$
 A lower tailed probability is calculated and the alternative hypothesis is H_{1}${\mathrm{H}}_{1}$: population median < X_{med}$\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
d_{i} = 0.0${d}_{i}=0.0$ in the ranking of the
d_{i}${d}_{i}$'s.
 zer = 'Y'${\mathbf{zer}}=\text{'Y'}$
 All d_{i} = 0.0${d}_{i}=0.0$ are included when ranking.
 zer = 'N'${\mathbf{zer}}=\text{'N'}$
 All d_{i} = 0.0${d}_{i}=0.0$, are ignored, that is all cases where d_{i} = 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:
n ≥ 1${\mathbf{n}}\ge 1$.
Input Parameters Omitted from the MATLAB Interface
 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 d_{i}${d}_{i}$'s, n_{1}${n}_{1}$.
 5:
ifail – int64int32nag_int scalar
ifail = 0${\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 n_{1} > 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.
Further Comments
The time taken by nag_nonpar_test_wilcoxon (g08ag) increases with n_{1}${n}_{1}$, until n_{1} > 80${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}${n}_{1}$ for n_{1} > 80${n}_{1}>80$.
Example
Open in the MATLAB editor:
nag_nonpar_test_wilcoxon_example
function nag_nonpar_test_wilcoxon_example
x = [19;
27;
1;
6;
7;
13;
4;
3];
xme = 0;
tail = 'Twotail';
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
Open in the MATLAB editor:
g08ag_example
function g08ag_example
x = [19;
27;
1;
6;
7;
13;
4;
3];
xme = 0;
tail = 'Twotail';
zer = 'Nozeros';
[w, wnor, p, n1, ifail] = g08ag(x, xme, tail, zer)
w =
32
wnor =
1.8904
p =
0.0547
n1 =
8
ifail =
0
PDF version (NAG web site
, 64bit version, 64bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013