nag_wilcoxon_test (g08agc) (PDF version)
g08 Chapter Contents
g08 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_wilcoxon_test (g08agc)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_wilcoxon_test (g08agc) performs the Wilcoxon signed rank test on a single sample of size n .

2  Specification

#include <nag.h>
#include <nagg08.h>
void  nag_wilcoxon_test (Integer n, const double x[], double median, Nag_TailProbability tail, Nag_IncSignZeros zeros, double *w, double *z, double *p, Integer *non_zero, NagError *fail)

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 consist of a single sample of n  observations denoted by x 1 , x 2 , , 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 , often called the null hypothesis, is that the median is equal to some given value X med , and this is to be tested against an alternative hypothesis H 1  which is 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 Section 5).
The Wilcoxon test differs from the Sign test (see nag_sign_test (g08aac)) 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 , for i=1,2,,n, the signed difference d i = x i - X med  is found, where X med  is a given test value for the median of the sample.
(b) The absolute differences d i  are ranked with rank r i  and any tied values of d i  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 zeros in Section 5).
(c) The number of nonzero d i 's is found.
(d) To each rank is affixed the sign of the d i  to which it corresponds. Let s i = sign d i r i .
(e) The sum of the positive-signed ranks, W = s i > 0 s i = i=1 n max s i ,0.0 , is calculated.
nag_wilcoxon_test (g08agc) returns:
(a) The test statistic W ;
(b) The number n 1  of nonzero d i 's;
(c) The approximate Normal test statistic z , where
z = W - n 1 n 1 + 1 4 - sign W - n 1 n 1 + 1 4 × 1 2 1 4 i=1 n s i 2
(d) The tail probability, p , corresponding to W , depending on the choice of the alternative hypothesis, H 1 .
If n 1 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 α  be the size of the significance test (that is, α  is the probability of rejecting H 0  when H 0  is true). If p<α  then the null hypothesis is rejected. Typically α  might be 0.05 or 0.01.

4  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

5  Arguments

1:     nIntegerInput
On entry: the size of the sample, n .
Constraint: n1 .
2:     x[n]const doubleInput
On entry: the sample observations, x 1 , x 2 , , x n .
3:     mediandoubleInput
On entry: the median test value, X med .
4:     tailNag_TailProbabilityInput
On entry: indicates the choice of tail probability, and hence the alternative hypothesis.
tail=Nag_TwoTail
A two tailed probability is calculated and the alternative hypothesis is H 1 : population median X med .
tail=Nag_UpperTail
An upper tailed probability is calculated and the alternative hypothesis is H 1 : population median > X med .
tail=Nag_LowerTail
A lower tailed probability is calculated and the alternative hypothesis is H 1 : population median < X med .
Constraint: tail=Nag_TwoTail, Nag_UpperTail or Nag_LowerTail.
5:     zerosNag_IncSignZerosInput
On entry: indicates whether or not to include the cases where d i = 0.0  in the ranking of the d i 's.
zeros=Nag_IncSignZerosY
All d i = 0.0  are included when ranking.
zeros=Nag_IncSignZerosN
All d i = 0.0 , are ignored, that is all cases where d i = 0.0  are removed before ranking.
Constraint: zeros=Nag_IncSignZerosY or Nag_IncSignZerosN.
6:     wdouble *Output
On exit: the Wilcoxon rank sum statistic, W , being the sum of the positive ranks.
7:     zdouble *Output
On exit: the approximate Normal test statistic, z , as described in Section 3.
8:     pdouble *Output
On exit: the tail probability, p , as specified by the argument tail.
9:     non_zeroInteger *Output
On exit: the number of nonzero d i 's, n 1 .
10:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_BAD_PARAM
On entry, argument tail had an illegal value.
On entry, argument zeros had an illegal value.
NE_G08AG_SAMP_IDEN
The whole sample is identical to the given median test value.
NE_INT_ARG_LT
On entry, n=value.
Constraint: n1.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.

7  Accuracy

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

8  Parallelism and Performance

Not applicable.

9  Further Comments

The time taken by nag_wilcoxon_test (g08agc) 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 .

10  Example

The example program 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.

10.1  Program Text

Program Text (g08agce.c)

10.2  Program Data

Program Data (g08agce.d)

10.3  Program Results

Program Results (g08agce.r)


nag_wilcoxon_test (g08agc) (PDF version)
g08 Chapter Contents
g08 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2014