Wilcoxon Rank Sum Test for Independent Samples

When the requirements for the t-test for two independent samples are not satisfied, the Wilcoxon Rank-Sum non-parametric test can often be used provided the two independent samples are drawn from populations with an ordinal distribution.

For this test we use the following null hypothesis:

H0: the observations come from the same population

From a practical point of view, this implies:

H0: if one observation is made at random from each population (call them x0 and y0), then the probability that x0 > y0 is the same as the probability that x0 < y0, and so the populations for each sample have the same medians.

We illustrate the technique with the following examples.

Example 1: Repeat Example 2 from Two Sample t Test with Unequal Variances to test whether a new hay fever drug is effective, but this time using the data from Figure 1.

Two samples data

Figure 1 – Data for Example 1

When we look at the QQ Plot for the Control group we see that it is not very normal, but more concerning is that the Box Plot for the group that took the drug shows that the data is not very symmetric (see Figure 2). We therefore decide to use the Wilcoxon Sign-Rank test instead of the t-test.

Assumptions t test

Figure 2 – QQ Plot and Box Plots for data in Example 1

The results of the Wilcoxon Rank-Sum test are displayed in Figure 3.

Wilcoxon rank sum Excel

Figure 3 – Wilcoxon Rank-Sum Test for Example 1

We begin by calculating the ranks of the combined 24 raw scores using the supplemental RANK_AVG function (or the standard RANK.AVG function in Excel 2010). See Ranking for details. E.g., the contents of cell D6 is the rank of the first participant in the Control group, namely RANK_AVG(A6,$A$6:$B$17,1) which is the same as

=RANK(A6,$A$6:$B$17,1) + (COUNTIF($A$6:$B$17,A6)-1)/2.

using the standard Excel 2007 rank function (see Ranking).

We then calculate the sum of the ranks for each group to arrive at the rank sums R1 = 119.5 and R2 = 180.5. Since the sample sizes are equal, the value of the test statistic W = the smaller of R1 and R2, which for this example means that W = 119.5 (cell H10).

We next compare W with the critical value Wcrit, which can be found in the Wilcoxon Rank-Sum Table. Since the sample sizes are both 12, we look up the critical value in the table for α = .05 (two-tail) where n1 = n2 = 12, and find that Wcrit = 115. This represents the smallest value we could expect to obtain for W if the null hypothesis were true. Since W = 119.5 > 115 = Wcrit, we cannot reject the null hypothesis, and so conclude there is no significant difference between the effectiveness of the drug and the control.

Example 2: Repeat Example 1 with the last data element for the group that took the drug removed.

We again use the Wilcoxon Rank-Sum test, but this time the sample sizes are unequal. The test is as in Figure 4.

Wilcoxon rank sum test

Figure 4 – Wilcoxon Rank-Sum Test for Example 2

The rank sums are calculated as in the previous example, although since some of the data may be blank, we need to use a formula such as

=IF(A6<>””,RANK_AVG(A6,$A$6:$B$17,1),””).

Since the sample sizes are different, a bit more care is required. Essentially W represents the left tail statistic and so we need to also evaluate the right tail statistic W′, which can be obtained by using reverse ranking as described in Figure 5:

Reverse ranking Excel

Figure 5 – Calculation of W′ using reverse ranks

The value of W′ is therefore the sum of the ranks for the smaller sample, i.e. 105.5. Fortunately, because of symmetry, W’ can more easily be obtained via the formula

image933

where  (the smaller sample size) and  (the larger sample size). Thus we obtain

W′ = 11(11+12+1) –158.5 = 105.5 (the value in cell H11)

For the two tailed test, which is what we usually require, we compare the smaller of W and W′ with Wcrit. To find the value of Wcrit, we again use the Wilcoxon Rank-Sum Table for α = .05 (two-tail) where n1 = 11 and n2 = 12 to obtain Wcrit = 99. Since min(W,W′) = min(158.5,105.5) = 105.5 > 99 = Wcrit , once again we cannot reject the null hypothesis.

Observation: When n1 = n2, then W′ = R2, i.e. the rank sum of the larger sample. Thus in Example 1, W′ = 180.5

Property 1: Suppose sample 1 has size n1 and rank sum R1 and sample 2 has size n2 and rank sum R2, then R1 + R2 = n(n+1)/2 where n = n1 + n2.

Property 2: When the two samples are sufficiently large (say of size > 10, although some say 20), then the W statistic is approximately normal N(μ, σ) where

image945

Observation: Click here for a proof of Property 1 or 2.

Observation: Using Property 2, for samples sufficiently large, we can test W using the techniques from Sampling Distributions. Note that the result is the same whether we use W or W′.

Observation: Since it compares rank sums, the Wilcoxon Rank-Sum test is more robust than the t-test as it is less likely to indicate spurious results based on the presence of outliers. Even for large samples where the assumptions for the t-test are met, the Wilcoxon Rank-Sum test is only a little less efficient than the t-test.

Example 3: The objective of a study was to determine whether there is a significant difference in the median life expectancy between smokers and non-smokers. 38 smokers and 40 non-smokers were chosen at random and their age at death recorded in Figure 6.

image946

Figure 6 – Life expectancy for both groups

A table of ranks is created and the values of W and W′ are calculated as in Examples 1 and 2. Since the sample sizes are sufficiently large, we can test W (or W′) using the normal distribution as described in Figure 7.

Wilcoxon rank sum normal

Figure 7 – Wilcoxon rank-sum test using normal approximation

Since there are fewer smokers than non-smokers, W = the rank sum for the smokers = 1227 (cell U8). We calculate the mean (cell U14) and variance (cell U15) for W using the formulas =U6*(T6+U6+1)/2 and =U14*T6/6 respectively. The standard deviation (cell U16) is then given by the formula =SQRT(U15) as usual.

We now calculate the p-value (cell U17) using the formula =NORMDIST(U8, U14, U16, TRUE) since W < . If W > , as usual we would use the formula =1 – NORMDIST(U8, U14, U16, TRUE). Alternatively, we could have created the z-score and calculated the p-value using NORMSDIST.

Since p-value = .03 < .05 = α, we reject the null hypothesis (one tail test) and conclude that there is a significant difference between the life expectancy of smokers and non-smokers.

Note that if we had used W′ (column T of Figure 7), we would get the same p-value and come to the same conclusion.

Real Statistics Excel Functions: The following functions are provided in the Real Statistics Pack:

RANK_COMBINED(x, R1, R2, d) = the ranging of element x in the combination of ranges R1 and R2. If d = 0 (or is omitted), then the ranking is in decreasing order; otherwise it is in increasing order. The rank is corrected for ties as in RANK.AVG or RANK_AVG (see Ranking).

RANK_SUM(R1, R2, d) = sum of the ranks of all the elements in range R1 based on the combination of ranges R1 and R2. If d = 0 (or is omitted), then the ranking is in decreasing order; otherwise it is in increasing order. Rankings are corrected for ties as in RANK.AVG or RANK_AVG (see Ranking).

RANK_SUM(R1, kd) = sum of the ranks of all the elements in the kth column of range R1. If d = 0 (or is omitted), then the ranking is in decreasing order; otherwise it is in increasing order. Rankings are corrected for ties as in RANK.AVG or RANK_AVG (see Ranking).

WILCOXON(R1, R2) = minimum of W and W′ for the samples contained in ranges R1 and R2

WILCOXON(R1, n) = minimum of W and W′ for the samples contained in the first n columns of range R1 and the remaining columns of range R1. If the second argument is omitted it defaults to 1.

WTEST(R1, R2, t) = p-value of the Wilcoxon rank-sum test for the samples contained in ranges R1 and R2; t = the # of tails: t = 1 (default) or t = 2.

WTEST(R1, n, t) = p-value of the Wilcoxon rank-sum test for the samples contained in the first n columns of range R1 and the remaining columns of range R1. If the second argument is omitted it defaults to 1. t = the # of tails: t = 1 (default) or t = 2.

WCRIT(n1, n2, α, t) = critical value of the Wilcoxon Rank-Sum test for samples of size n1 and n2, for the given value of alpha and t = 1 (one tail) or 2 (two tails) based on the Wilcoxon Rank Sum Table.

WPROB(x, n1, n2, tails, iter) = an approximate p-value for Wilcoxon rank-sum test x (= the minimum of W and W′) for samples of size n1 and n2 and tails = 1 (one tail) or 2 (two tails, default) based on a linear interpolation of the values in the Wilcoxon Rank Sum Table using iter number of iterations (default = 40).

Note that the values for α in the Wilcoxon Rank Sum Table range from .01 to .2 for tails = 2 and .005 to .1 for tails = 1. If the p-value is less than .01 (tails = 2) or .005 (tails = 1) then the p-value is given as 0 and if the p-value is greater than .2 (tails = 2) or .1 (tails = 1) then the p-value is given as 1.

The WILCOXON and WTEST functions ignore any empty or non-numeric cells.

Caution: For releases of the Real Statistics Resource Pack prior to Release 2.16.1, n1 ≥ n2 in WCRIT, while n1 ≤ n2 in WPROB. Also there are some errors in the calculation of the WPROB function. These shortcomings are eliminated starting with Release 2.16.1.

Observation: If R1 represents the first n columns of range R and R2 represents the remaining columns in range R, then WILCOXON(R, n) = WILCOXON(R1, R2) and WTEST(R, n) = WTEST(R1, R2). Of course, WILCOXON(R1, R2) and WTEST(R1, R2) can also be used when the two ranges are not contiguous.

Similarly, if R1 represents the first n columns of range R and R2 represents the remaining columns in range R, then RANK_COMBINED(x, R1, R2, d) = RANK_AVG(x, R, d). The RANK_COMBINED function is especially useful, however, when R1 and R2 are not contiguous.

Observation: In Example 2, we can use the supplemental function to arrive at the same value for the minimum of W and W′, namely WILCOXON(A6:B17) = 105.5. Also RANK_COMBINED(34, A6:A17, B6:B7, 1) = 2.5, RANK_SUM(A6:A17, B6:B17) = 170.5 and RANK_SUM(B6:B17, A6:A17) = 105.5.

Also WCRIT(H5,I5,H8,H9) = WCRIT(12, 11, .05, 2) = 99 (the value in cell H12 of Figure 4). Finally note that the p-value = WPROB(H11,I5,H5,H9) = WPROB(105.5, 11, 12, 2) = .125 > .05 = α, and so once again we can’t reject the null hypothesis.

Similarly in Example 3, we can use the supplemental function to arrive at the same value for the minimum of W and W′, namely WILCOXON(J6:Q15, 4) = WILCOXON(J6:M15, N6:Q15) = 1227, as well as the same p-value (assuming a normal approximation), namely WTEST(J6:Q15, 4) = WTEST(J6:M15, N6:Q15) = 0.003081. Also RANK_COMBINED(72, J6:M15, N6:Q15, 1) = 37, RANK_SUM(J6:M15, N6:Q15) = 1854 and RANK_SUM(N6:Q15, J6:M15) = 1227

Observation: The effect size for the Wilcoxon Rank Sum test is given by the correlation coefficient  (see Basic Concepts of Correlation). The correlation coefficient for the Wilcoxon Rank Sum test is given by the formula

effect-size-wilcoxon

where the z-score is

z.score Wilcozon

For Example 3,

image7003

and so

image7004

As described in Correlation in Relation to t-test, a rough estimate of effect size is that r.5 represents a large effect size, r = .3 represents a medium effect size and r = .1 represents a small effect. Thus, for Example 3 we have a medium sized effect.

Also see Mann-Whitney Test (including Figure 2) for more information about how to calculate the effect size r in Excel.

Exact Test

Click here for a description of the exact version of the Wilcoxon Rank-Sum Exact Test using the permutation function.

10 Responses to Wilcoxon Rank Sum Test for Independent Samples

  1. Colin says:

    Sir
    At the end of Example 1, you wrote:” Since W = 119.5 > 115 = Wcrit, we cannot reject the null hypothesis, and so conclude there is no significant difference between the effectiveness of the drug and the control.”
    Is that right?

    • Charles says:

      Colin,
      Yes. It is correct. When W > W-crit you cannot reject the null hypothesis.
      Charles

      • Tze says:

        Charles:

        Indeed, well explained, but I am still not sure why we cannot reject the null hypothesis (as oppose to t-test) because W = 119.5 and 115 = W-crit. According to your eariler tutorial “Hypothesis Testing”, my understanding is to reject the null hypothesis since W-value is within the critical region.

        • Charles says:

          For this and other non-parametric tests the critical region is the area less than the critical value. You can think of W-crit as the critical value on the left tail.
          Charles

  2. Colin says:

    Sir

    In Real Statistics Excel Functions, when d = o or omitted the ranking is in descending order.

    Colin

  3. Jean-Pierre Baeyens says:

    First of all, congratulations with your site.

    I have a question related to the use of the W score in the Wilcoxon rank sum test.
    If you define W as the smallest of R1 and R2, why do you use a two-tailed test and not just a one tailed?

    • Charles says:

      Jean-Pierre,

      If n1 = n2, you will get the same test result whether you use R1 or R2. If I remember correctly one should be compared with the left critical value and the other with the right critical value. The smaller one corresponds to the left critical value, which can be compared with the values in the critical values.

      This very similar to the t test where negative t value is compared with the left critical value and the positive t value is compared with the right critical value. Given symmetry to do a two-sided test you just pick one side and compare with the t-critical value determined by halving the value of alpha. A similar thing happens in the Wilcoxon Rank Sum test.

      Charles

  4. kembo says:

    suppose I have two samples with unequal sizes, how can I compare them using with Wilcoxon rank sum?

    • Charles says:

      Kembo,
      Examples 2 and 3 on the referenced webpage compare two samples of unequal size. I suggest that you look at these.
      Charles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>