wilcoxonSignRank¶
Performs a Wilcoxon signed rank test.
Synopsis¶
wilcoxonSignRank (x)
Required Arguments¶
- float
x[]
(Input) - Array of length
nObservations
containing the data.
Return Value¶
An array of length two containing the values described below.
The asymptotic probability of not exceeding the standardized (to an asymptotic variance of 1.0) minimum of (W+, W-) using method 1 under the null hypothesis that the distribution is symmetric about 0.0.
And, the asymptotic probability of not exceeding the standardized (to an asymptotic variance of 1.0) minimum of (W+, W-) using method 2 under the null hypothesis that the distribution is symmetric about 0.0.
Optional Arguments¶
fuzz
, float (Input)Nonnegative constant used to determine ties in computing ranks in the combined samples. A tie is declared when two observations in the combined sample are within
fuzz
of each other.Default value for
fuzz
is 0.0.stat
(Output)- An array of length 10 containing the following statistics:
Row | Statistics |
---|---|
0 | The positive rank sum, W+, using method 1. |
1 | The absolute value of the negative rank sum, W-, using method 1. |
2 | The standardized (to an asymptotic variance of 1.0) minimum of (W+, W-) using method 1. |
3 | The asymptotic probability of not exceeding stat[2] under
the null hypothesis that the distribution is symmetric about
0.0. |
4 | The positive rank sum, W+, using method 2. |
5 | The absolute value of the negative rank sum, W-, using method 2. |
6 | The standardized (to an asymptotic variance of 1.0) minimum of (W+, W-) using method 2. |
7 | The asymptotic probability of not exceeding stat[6] under
the null hypothesis that the distribution is symmetric about
0.0. |
8 | The number of zero observations. |
9 | The total number of observations that are tied, and that are not within fuzz of zero. |
nMissing
(Output)- Number of missing values in
y
.
Description¶
Function wilcoxonSignRank
performs a Wilcoxon signed rank test of
symmetry about zero. In one sample, this test can be viewed as a test that
the population median is zero. In matched samples, a test that the medians
of the two populations are equal can be computed by first computing
difference scores. These difference scores would then be used as input to
wilcoxonSignRank
. A general reference for the methods used is Conover
(1980).
Function wilcoxonSignRank
computes statistics for two methods for
handling zero and tied observations. In the first method, observations
within fuzz
of zero are not counted, and the average rank of tied
observations is used. (Observations within fuzz
of each other are said
to be tied.) In the second method, observations within fuzz
of zero are
randomly assigned a positive or negative sign, and the ranks of tied
observations are randomly permuted.
The W+ and W- statistics are computed as the sums of the ranks of the positive observations and the sum of the ranks of the negative observations, respectively. Asymptotic probabilities are computed using standard methods (see, e.g., Conover 1980, page 282).
The W+ and W- statistics may be used to test the following hypotheses about the median, M. In deciding whether to reject the null hypothesis, use the bracketed statistic if method 2 for handling ties is preferred. Possible null hypotheses and alternatives are given as follows:
\(H_0 : M\leq 0\) \(H_1 : M>0\)
Reject if
stat[0]
[orstat[4]
] is too large.\(H_0 : M\geq 0\) \(H_1 : M<0\)
Reject if
stat[1]
[orstat[5]
] is too large.\(H_0 : M=0\) \(H_1 : M\neq 0\)
Reject if
stat[2]
[orstat[6]
] is too small. Alternatively, if an asymptotic test is desired, reject if 2 *stat[3]
[or 2 *stat[7]
] is less than the significance level.
Tabled values of the test statistic can be found in the references. If
possible, tabled values should be used. If the number of nonzero
observations is too large, then the asymptotic probabilities computed by
wilcoxonSignRank
can be used.
The assumptions required for the hypothesis tests are as follows:
- The distribution of each \(X_i\) is symmetric.
- The \(X_i\) are mutually independent.
- All \(X_i\)’s have the same median.
- An ordering of the observations exists (i.e., \(X_1>X_2\) and \(X_2>X_3\) implies that \(X_1>X_3\)).
If other assumptions are made, related hypotheses that are more (or less) restrictive can be tested.
Example¶
This example illustrates the application of the Wilcoxon signed rank test to
a test on a difference of two matched samples (matched pairs) {X1 = 223,
216, 211, 212, 209, 205, 201; and X2 = 208, 205, 202, 207, 206, 204, 203}. A
test that the median difference is 10.0 (rather than 0.0) is performed by
subtracting 10.0 from each of the differences prior to calling
wilcoxonSignRank
. As can be seen from the output, the null hypothesis is
rejected. The warning error will always be printed when the number of
observations is 50 or less unless printing is turned off for warning errors.
from __future__ import print_function
from numpy import *
from pyimsl.stat.wilcoxonSignRank import wilcoxonSignRank
stat = []
nmiss = []
fuzz = .0001
x = array([-25., -21., -19., -15., -13., -11., -8.])
result = wilcoxonSignRank(x,
nMissing=nmiss,
fuzz=fuzz,
stat=stat)
print("\nStatistic\t\t\tMethod 1\tMethod 2")
print("W+\t\t\t\t %3.0f\t\t %3.0f" % (stat[0], stat[4]))
print("W-\t\t\t\t %3.0f\t\t %3.0f" % (stat[1], stat[5]))
print("Standardized Minimum\t\t%6.4f\t\t%6.4f" % (stat[2], stat[6]))
print("p-value\t\t\t\t %6.4f\t\t %6.4f\n" % (stat[3], stat[7]))
print("Number of zeros\t\t\t%3.0f" % stat[8])
print("Number of ties\t\t\t%3.0f" % stat[9])
print("Number of missing\t\t %d" % nmiss[0])
Output¶
***
*** Warning error issued from IMSL function wilcoxonSignRank:
*** "n_observations" = 7. The number of observations is less than 50, and exact tables should be referenced for probabilities.
***
Statistic Method 1 Method 2
W+ 0 0
W- 28 28
Standardized Minimum -2.3664 -2.3664
p-value 0.0090 0.0090
Number of zeros 0
Number of ties 0
Number of missing 0