The Wilcoxon signedrank test is a nonparametric statistical hypothesis test used either to test the location of a population based on a sample of data, or to compare the locations of two populations using two matched samples.^{[1]} The onesample version serves a purpose similar to that of the onesample Student's ttest.^{[2]} For two matched samples, it is a paired difference test like the paired Student's ttest (also known as the "ttest for matched pairs" or "ttest for dependent samples"). The Wilcoxon test can be a good alternative to the ttest when population means are not of interest; for example, when one wishes to test whether a population's median is nonzero, or whether there is a better than 50% chance that a sample from one population is greater than a sample from another population.
The test is named for Frank Wilcoxon (1892–1965) who, in a single paper, proposed both it and the ranksum test for two independent samples.^{[3]} The test was popularized by Sidney Siegel (1956) in his influential textbook on nonparametric statistics.^{[4]} Siegel used the symbol T for the test statistic, and consequently, the test is sometimes referred to as the Wilcoxon Ttest.
There are two variants of the signedrank test. From a theoretical point of view, the onesample test is more fundamental because the paired sample test is performed by converting the data to the situation of the onesample test. However, most practical applications of the signedrank test arise from paired data.
For a paired sample test, the data consists of samples . Each sample is a pair of measurements. In the simplest case, the measurements are on an interval scale. Then they may be converted to real numbers, and the paired sample test is converted to a onesample test by replacing each pair of numbers by its difference .^{[5]} In general, it must be possible to rank the differences between the pairs. This requires that the data be on an ordered metric scale, a type of scale that carries more information than an ordinal scale but may have less than an interval scale.^{[6]}
The data for a onesample test is a set of real number samples . Assume for simplicity that the samples have distinct absolute values and that no sample equals zero. (Zeros and ties introduce several complications; see below.) The test is performed as follows:^{[7]}^{[8]}
The ranks are defined so that is the number of for which . Additionally, if is such that , then for all .
The signedrank sum is closely related to two other test statistics. The positiverank sum and the negativerank sum are defined by^{[9]}
The positiverank sum and negativerank sum have alternative interpretations that are useful for the theory behind the test. Define the Walsh average to be . Then:^{[11]}
The onesample Wilcoxon signedrank test can be used to test whether data comes from a symmetric population with a specified median.^{[12]} If the population median is known, then it can be used to test whether data is symmetric about its center.^{[13]}
To explain the null and alternative hypotheses formally, assume that the data consists of independent and identically distributed samples from a distribution . If and are IID distributed random variables, define to be the cumulative distribution function of . Set
The alternative hypothesis being tested depends on whether the test statistic is used to compute a onesided or twosided pvalue (and if onesided, which side). If is a fixed, predetermined quantity, then the test can also be used as a test for the value of by subtracting from every data point.
The above null and alternative hypotheses are derived from the fact that is a consistent estimator of .^{[15]} It can also be derived from the description of and in terms of Walsh averages, since that description shows that the Wilcoxon test is the same as the sign test applied to the set of Walsh averages.^{[16]}
Restricting the distributions of interest can lead to more interpretable null and alternative hypotheses. One mildly restrictive assumption is that has a unique median. This median is called the pseudomedian of ; in general it is different from the mean and the median, even when all three exist. If the existence of a unique pseudomedian can be assumed true under both the null and alternative hypotheses, then these hypotheses can be restated as:
Most often, the null and alternative hypotheses are stated under the assumption of symmetry. Fix a real number . Define to be symmetric about if a random variable with distribution satisfies for all . If has a density function , then is symmetric about if and only if for every .^{[17]}
If the null and alternative distributions of can be assumed symmetric, then the null and alternative hypotheses simplify to the following:^{[18]}
If in addition , then is a median of . If this median is unique, then the Wilcoxon signedrank sum test becomes a test for the location of the median.^{[19]} When the mean of is defined, then the mean is , and the test is also a test for the location of the mean.^{[20]}
The restriction that the alternative distribution is symmetric is highly restrictive, but for onesided tests it can be weakened. Say that is stochastically smaller than a distribution symmetric about zero if an distributed random variable satisfies for all . Similarly, is stochastically larger than a distribution symmetric about zero if for all . Then the Wilcoxon signedrank sum test can also be used for the following null and alternative hypotheses:^{[21]}^{[22]}
The hypothesis that the data are IID can be weakened. Each data point may be taken from a different distribution, as long as all the distributions are assumed to be continuous and symmetric about a common point . The data points are not required to be independent as long as the conditional distribution of each observation given the others is symmetric about .^{[23]}
Because the paired data test arises from taking paired differences, its null and alternative hypotheses can be derived from those of the onesample test. In each case, they become assertions about the behavior of the differences .
Let be the joint cumulative distribution of the pairs . If is continuous, then the most general null and alternative hypotheses are expressed in terms of
Like the onesample case, under some restrictions the test can be interpreted as a test for whether the pseudomedian of the differences is located at zero.
A common restriction is to symmetric distributions of differences. In this case, the null and alternative hypotheses are:^{[24]}^{[25]}
These can also be expressed more directly in terms of the original pairs:^{[26]}
The null hypothesis of exchangeability can arise from a matched pair experiment with a treatment group and a control group. Randomizing the treatment and control within each pair makes the observations exchangeable. For an exchangeable distribution, has the same distribution as , and therefore, under the null hypothesis, the distribution is symmetric about zero.^{[27]}
Because the onesample test can be used as a onesided test for stochastic dominance, the paired difference Wilcoxon test can be used to compare the following hypotheses:^{[28]}
In real data, it sometimes happens that there is a sample which equals zero or a pair with . It can also happen that there are tied samples. This means that for some , we have (in the onesample case) or (in the paired sample case). This is particularly common for discrete data. When this happens, the test procedure defined above is usually undefined because there is no way to uniquely rank the data. (The sole exception is if there is a single sample which is zero and no other zeros or ties.) Because of this, the test statistic needs to be modified.
Wilcoxon's original paper did not address the question of observations (or, in the paired sample case, differences) that equal zero. However, in later surveys, he recommended removing zeros from the sample.^{[29]} Then the standard signedrank test could be applied to the resulting data, as long as there were no ties. This is now called the reduced sample procedure.
Pratt^{[30]} observed that the reduced sample procedure can lead to paradoxical behavior. He gives the following example. Suppose that we are in the onesample situation and have the following thirteen observations:
The reduced sample procedure removes the zero. To the remaining data, it assigns the signed ranks:
This has a onesided pvalue of , and therefore the sample is not significantly positive at any significance level . Pratt argues that one would expect that decreasing the observations should certainly not make the data appear more positive. However, if the zero observation is decreased by an amount less than 2, or if all observations are decreased by an amount less than 1, then the signed ranks become:
This has a onesided pvalue of . Therefore the sample would be judged significantly positive at any significance level . The paradox is that, if is between and , then decreasing an insignificant sample causes it to appear significantly positive.
Pratt therefore proposed the signedrank zero procedure. This procedure includes the zeros when ranking the samples. However, it excludes them from the test statistic, or equivalently it defines . Pratt proved that the signedrank zero procedure has several desirable behaviors not shared by the reduced sample procedure:^{[31]}
Pratt remarks that, when the signedrank zero procedure is combined with the average rank procedure for resolving ties, the resulting test is a consistent test against the alternative hypothesis that, for all , and differ by at least a fixed constant that is independent of and .^{[32]}
The signedrank zero procedure has the disadvantage that, when zeros occur, the null distribution of the test statistic changes, so tables of pvalues can no longer be used.
When the data is on a Likert scale with equally spaced categories, the signedrank zero procedure is more likely to maintain the Type I error rate than the reduced sample procedure.^{[33]}
From the viewpoint of statistical efficiency, there is no perfect rule for handling zeros. Conover found examples of null and alternative hypotheses that show that neither Wilcoxon's and Pratt's methods are uniformly better than the other. When comparing a discrete uniform distribution to a distribution where probabilities linearly increase from left to right, Pratt's method outperforms Wilcoxon's. When testing a binomial distribution centered at zero to see whether the parameter of each Bernoulli trial is , Wilcoxon's method outperforms Pratt's.^{[34]}
When the data does not have ties, the ranks are used to calculate the test statistic. In the presence of ties, the ranks are not defined. There are two main approaches to resolving this.
The most common procedure for handling ties, and the one originally recommended by Wilcoxon, is called the average rank or midrank procedure. This procedure assigns numbers between 1 and n to the observations, with two observations getting the same number if and only if they have the same absolute value. These numbers are conventionally called ranks even though the set of these numbers is not equal to (except when there are no ties). The rank assigned to an observation is the average of the possible ranks it would have if the ties were broken in all possible ways. Once the ranks are assigned, the test statistic is computed in the same way as usual.^{[35]}^{[36]}
For example, suppose that the observations satisfy
Under the average rank procedure, the null distribution is different in the presence of ties.^{[37]}^{[38]} The average rank procedure also has some disadvantages that are similar to those of the reduced sample procedure for zeros. It is possible that a sample can be judged significantly positive by the average rank procedure; but increasing some of the values so as to break the ties, or breaking the ties in any way whatsoever, results in a sample that the test judges to be not significant.^{[39]}^{[40]} However, increasing all the observed values by the same amount cannot turn a significantly positive result into an insignificant one, nor an insignificant one into a significantly negative one. Furthermore, if the observations are distributed symmetrically, then the values of which the test does not reject form an interval.^{[41]}^{[42]}
The other common option for handling ties is a tiebreaking procedure. In a tiebreaking procedure, the observations are assigned distinct ranks in the set . The rank assigned to an observation depends on its absolute value and the tiebreaking rule. Observations with smaller absolute values are always given smaller ranks, just as in the standard ranksum test. The tiebreaking rule is used to assign ranks to observations with the same absolute value. One advantage of tiebreaking rules is that they allow the use of standard tables for computing pvalues.^{[43]}
Random tiebreaking breaks the ties at random. Under random tiebreaking, the null distribution is the same as when there are no ties, but the result of the test depends not only on the data but on additional random choices. Averaging the ranks over the possible random choices results in the average rank procedure.^{[44]} One could also report the probability of rejection over all random choices.^{[45]} Random tiebreaking has the advantage that the probability that a sample is judged significantly positive does not decrease when some observations are increased.^{[46]} Conservative tiebreaking breaks the ties in favor of the null hypothesis. When performing a onesided test in which negative values of tend to be more significant, ties are broken by assigning lower ranks to negative observations and higher ranks to positive ones. When the test makes positive values of significant, ties are broken the other way, and when large absolute values of are significant, ties are broken so as to make as small as possible. Pratt observes that when ties are likely, the conservative tiebreaking procedure "presumably has low power, since it amounts to breaking all ties in favor of the null hypothesis."^{[47]}
The average rank procedure can disagree with tiebreaking procedures. Pratt gives the following example.^{[48]} Suppose that the observations are:
The average rank procedure assigns these the signed ranks
This sample is significantly positive at the onesided level . On the other hand, any tiebreaking rule will assign the ranks
At the same onesided level , this is not significant.
Two other options for handling ties are based around averaging the results of tiebreaking. In the average statistic method, the test statistic is computed for every possible way of breaking ties, and the final statistic is the mean of the tiebroken statistics. In the average probability method, the pvalue is computed for every possible way of breaking ties, and the final pvalue is the mean of the tiebroken pvalues.^{[49]}
Computing pvalues requires knowing the distribution of under the null hypothesis. There is no closed formula for this distribution.^{[50]} However, for small values of , the distribution may be computed exactly. Under the null hypothesis that the data is symmetric about zero, each is exactly as likely to be positive as it is negative. Therefore the probability that under the null hypothesis is equal to the number of sign combinations that yield divided by the number of possible sign combinations . This can be used to compute the exact distribution of under the null hypothesis.^{[51]}
Computing the distribution of by considering all possibilities requires computing sums, which is intractable for all but the smallest . However, there is an efficient recursion for the distribution of .^{[52]}^{[53]} Define to be the number of sign combinations for which . This is equal to the number of subsets of which sum to . The base cases of the recursion are , for all , and for all or . The recursive formula is
If is the probability that under the null hypothesis when there are samples, then satisfies a similar recursion:^{[55]}
For very large , even the above recursion is too slow. In this case, the null distribution can be approximated. The null distributions of , , and are asymptotically normal with means and variances:^{[57]}
Better approximations can be produced using Edgeworth expansions. Using a fourthorder Edgeworth expansion shows that:^{[58]}^{[59]}
The moment generating function of has the exact formula:^{[60]}
When zeros are present and the signedrank zero procedure is used, or when ties are present and the average rank procedure is used, the null distribution of changes. Cureton derived a normal approximation for this situation.^{[61]}^{[62]} Suppose that the original number of observations was and the number of zeros was . The tie correction is
Wilcoxon^{[63]} originally defined the Wilcoxon ranksum statistic to be . Early authors such as Siegel^{[64]} followed Wilcoxon. This is appropriate for twosided hypothesis tests, but it cannot be used for onesided tests.
Instead of assigning ranks between 1 and n, it is also possible to assign ranks between 0 and . These are called modified ranks.^{[65]} The modified signedrank sum , the modified positiverank sum , and the modified negativerank sum are defined analogously to , , and but with the modified ranks in place of the ordinary ranks. The probability that the sum of two independent distributed random variables is positive can be estimated as .^{[66]} When consideration is restricted to continuous distributions, this is a minimum variance unbiased estimator of .^{[67]}

order by absolute difference 

is the sign function, is the absolute value, and is the rank. Notice that pairs 3 and 9 are tied in absolute value. They would be ranked 1 and 2, so each gets the average of those ranks, 1.5.
Main article: Mann–Whitney_U_test § Rankbiserial_correlation 
To compute an effect size for the signedrank test, one can use the rankbiserial correlation.
If the test statistic T is reported, the rank correlation r is equal to the test statistic T divided by the total rank sum S, or r = T/S. ^{[68]} Using the above example, the test statistic is T = 9. The sample size of 9 has a total rank sum of S = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) = 45. Hence, the rank correlation is 9/45, so r = 0.20.
If the test statistic T is reported, an equivalent way to compute the rank correlation is with the difference in proportion between the two rank sums, which is the Kerby (2014) simple difference formula.^{[68]} To continue with the current example, the sample size is 9, so the total rank sum is 45. T is the smaller of the two rank sums, so T is 3 + 4 + 5 + 6 = 18. From this information alone, the remaining rank sum can be computed, because it is the total sum S minus T, or in this case 45 − 18 = 27. Next, the two ranksum proportions are 27/45 = 60% and 18/45 = 40%. Finally, the rank correlation is the difference between the two proportions (.60 minus .40), hence r = .20.
wilcox.test(x,y, paired=TRUE)
, where x and y are vectors of equal length.^{[69]}wilcoxon_test
function.