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

## 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$ 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 argument tail in Arguments).
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}_{\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 argument zer in Arguments). (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.
nag_nonpar_test_wilcoxon (g08ag) 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$.

## 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:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
The sample observations, ${x}_{1},{x}_{2},\dots ,{x}_{n}$.
2:     $\mathrm{xme}$ – double scalar
The median test value, ${X}_{\mathrm{med}}$.
3:     $\mathrm{tail}$ – string (length ≥ 1)
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'}$.
4:     $\mathrm{zer}$ – string (length ≥ 1)
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'}$.

### Optional Input Parameters

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

### Output Parameters

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

${\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$.
W  ${\mathbf{ifail}}=3$
The whole sample is identical to the given median test value.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## 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 nag_nonpar_test_wilcoxon (g08ag) 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$.

## 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.
```function g08ag_example

fprintf('g08ag example results\n\n');

% Data
x = [82 69 73 43 58 56 76 65];
y = [63 42 74 37 51 43 80 62];

fprintf('Wilcoxon one sample signed ranks test\n\n');
fprintf('Data values\n   ');
fprintf('%5.1f', x);
fprintf('\n   ');
fprintf('%5.1f', y);
fprintf('\n\n');

% Control parameters
xme = 0;
tail = 'Two-tail';
zer = 'Nozeros';

% z = differences
z = x - y;

[w, wnor, p, n1, ifail] = g08ag( ...
z, xme, tail, zer);

fprintf('Test statistic            = %8.4f\n', w);
fprintf('Normalized test statistic = %8.4f\n', wnor);
fprintf('Degrees of freedom        = %3d\n', n1);
fprintf('Two tail probability      = %8.4f\n', p);

```
```g08ag example results

Wilcoxon one sample signed ranks test

Data values
82.0 69.0 73.0 43.0 58.0 56.0 76.0 65.0
63.0 42.0 74.0 37.0 51.0 43.0 80.0 62.0

Test statistic            =  32.0000
Normalized test statistic =   1.8904
Degrees of freedom        =   8
Two tail probability      =   0.0547
```