NAG FL Interface
g08agf (test_​wilcoxon)

Settings help

FL Name Style:

FL Specification Language:

1 Purpose

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

2 Specification

Fortran Interface
Subroutine g08agf ( n, x, xme, tail, zer, w, wnor, p, n1, wrk, ifail)
Integer, Intent (In) :: n
Integer, Intent (Inout) :: ifail
Integer, Intent (Out) :: n1
Real (Kind=nag_wp), Intent (In) :: x(n), xme
Real (Kind=nag_wp), Intent (Out) :: w, wnor, p, wrk(3*n)
Character (1), Intent (In) :: tail, zer
C Header Interface
#include <nag.h>
void  g08agf_ (const Integer *n, const double x[], const double *xme, const char *tail, const char *zer, double *w, double *wnor, double *p, Integer *n1, double wrk[], Integer *ifail, const Charlen length_tail, const Charlen length_zer)
The routine may be called by the names g08agf or nagf_nonpar_test_wilcoxon.

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 x1,x2,,xn. 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, often called the null hypothesis, is that the median is equal to some given value (Xmed), and this is to be tested against an alternative hypothesis H1 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 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
  1. (a)For each xi, for i=1,2,,n, the signed difference di=xi-Xmed is found, where Xmed is a given test value for the median of the sample.
  2. (b)The absolute differences |di| are ranked with rank ri and any tied values of |di| are assigned the average of the tied ranks. You may choose whether or not to ignore any cases where di=0 by removing them before or after ranking (see the description of the argument zer in Section 5).
  3. (c)The number of nonzero di is found.
  4. (d)To each rank is affixed the sign of the di to which it corresponds. Let si=sign(di)ri.
  5. (e)The sum of the positive-signed ranks, W=si>0si=i=1nmax(si,0.0), is calculated.
g08agf returns
  1. (a)the test statistic W;
  2. (b)the number n1 of nonzero di;
  3. (c)the approximate Normal test statistic z, where
    z= (W- n1(n1+1)4)-sign(W- n1(n1+1)4) × 12 14i=1nsi2 ;  
  4. (d)the tail probability, p, corresponding to W, depending on the choice of the alternative hypothesis, H1.
If n180, 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 H0 when H0 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: n Integer Input
On entry: n, the size of the sample.
Constraint: n1.
2: x(n) Real (Kind=nag_wp) array Input
On entry: the sample observations, x1,x2,,xn.
3: xme Real (Kind=nag_wp) Input
On entry: the median test value, Xmed.
4: tail Character(1) Input
On entry: indicates the choice of tail probability, and hence the alternative hypothesis.
A two tailed probability is calculated and the alternative hypothesis is H1: population median Xmed.
An upper tailed probability is calculated and the alternative hypothesis is H1: population median >Xmed.
A lower tailed probability is calculated and the alternative hypothesis is H1: population median <Xmed.
Constraint: tail='T', 'U' or 'L'.
5: zer Character(1) Input
On entry: indicates whether or not to include the cases where di=0.0 in the ranking of the dis.
All di=0.0 are included when ranking.
All di=0.0, are ignored, that is all cases where di=0.0 are removed before ranking.
Constraint: zer='Y' or '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 argument tail.
9: n1 Integer Output
On exit: the number of nonzero di's, n1.
10: wrk(3×n) Real (Kind=nag_wp) array Workspace
11: ifail Integer Input/Output
On entry: ifail must be set to 0, −1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of −1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value −1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. When the value -1 or 1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6 Error Indicators and Warnings

If on entry ifail=0 or −1, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
On entry, tail=value.
Constraint: tail='T', 'U' or 'L'.
On entry, zer=value.
Constraint: zer='Y' or 'N'.
On entry, n=value.
Constraint: n1.
All elements of the sample are equal to xme, i.e., variance=0.
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

The approximation used to calculate p when n1>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.

8 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
g08agf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9 Further Comments

The time taken by g08agf increases with n1, until n1>80, from which point on the approximation is used. The time decreases significantly at this point and increases again modestly with n1 for n1>80.

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

10.1 Program Text

Program Text (g08agfe.f90)

10.2 Program Data

Program Data (g08agfe.d)

10.3 Program Results

Program Results (g08agfe.r)