Suppose I have two sets of three numbers: $x_1, x_2, x_3$ and $y_1, y_2, y_3$ and I want to test the Null hypothesis that they are drawn from the same distribution using the Wilcoxon-Mann-Whitney test.
So what I would do with pen and paper is the following:
- Compute the test statistic $W_{observed}$ for my sample
- Make a list of all 20 ways of assigning the label '$x$' to three of my 6 numbers and '$y$' to the three others
- Remember that under the Null hypothesis all 20 assignments in my list are equally likely
- Compute the statistic $W$ for all of them.
- Count how many of these $W$'s are at least as extreme (defined as 'far from the median of my list of 20 $W$-values') as the value $W_{observed}$ of step 1
- Divide this number by 20
- Interpret (with step 3 in mind) the outcome of step 6 as the probability under the Null-hypothesis (conditioned on observing these 6 numbers) that I would have observed a $W$-value at least as extreme as the one I did observe.
- Remember that the probability described in step 7 is the definition of $p$-value
- Conclude that, in step 6, I computed the $p$-value of my test
- Feel pleased with myself because I did this computation 'exactly'
Alternatively, I can feed my six numbers in to R as wilcox.test(c(x_1, x_2, x_3), c(y_1, y_2, y_3))
Non-surprisingly, if all 6 numbers are distinct, both methods give the same answer, with R being slightly faster.
However, when some $x_i$ equals some $y_j$ and even when some $x_i$ equals some $x_j$ (this seems even more absurd to me, although I cannot quite pinpoint why) R will complain that it 'cannot compute exact $p$-values with ties.'
My question is why?
Obviously the problem cannot be in step 2 through 7 so it must be in step 1 or 8. Indeed I found an internet-source claiming that the test statistic does not exist in case of ties (supporting the thesis that the problem lies in step 1): because the statistic is defined as a sum of ranks, it becomes undefined when some of the number are equal, as well-defined ranks do no longer exist in that case.
However this argument is invalid because, and here I quote the R helpfile of wilcox test
:
R's value can also be computed as the number of all pairs (x[i], y[j]) for which y[j] is not greater than x[i], the most common definition of the Mann-Whitney test.
Indeed, with this definition we can compute $W$ whether there are ties or not. So why does R still have trouble with computing $p$-values?
(Side note: there is a strange irony in the above quote form the help file, in that, in case of ties, R does not use above definition, but instead computes $W$ as the number of all pairs (x[i], y[j]) for which y[j] is strictly smaller than x[i] plus one half of all pairs (x[i], y[j]) for which y[j] is equal to x[i]. However that seems irrelevant to my question since as long as we have a way of computing $W$ we can run through the 10 steps I wrote down to obtain an exact $p$-value.)
Y = c(1,2,3,4,5,1,6,7,8,9); Group = factor(c(rep("A",5), rep("B", 5))); library(exactRankTests); wilcox.exact(Y ~ Group, exact=TRUE); library(coin); wilcox_test(Y ~ Group, distribution = "exact")
$\endgroup$