# Is the W statistic output by wilcox.test() in R the same as the U statistic?

I've recently been reading about the Mann-Whitney U test. It turns out that to carry out this test in R you actually need to run a Wilcoxon test!

My question: is the W statistic of wilcox.test in R identical to the U statistic?

Wilcoxon is generally credited with being the original inventor of the test*, though Mann and Whitney's approach was a great stride forward, and they extended the cases for which the statistic was tabulated. My preference is to refer to the test as the Wilcoxon-Mann-Whitney, to recognize both contributions (Mann-Whitney-Wilcoxon is also seen; I don't mind that either).

* However, the actual picture is a little more cloudy, with several other authors also coming up with the same or similar statistics about this time or earlier, or in some cases making contributions that are closely connected to the test. At least some of the credit should go elsewhere.

The Wilcoxon test and the Mann-Whitney U test are equivalent (and the help states that they are) in that they always reject the same cases under the same circumstances; at most their test statistics will only differ by a shift (and in some cases, just possibly a sign change).

The Wilcoxon test is defined in more than one way in the literature (and that ambiguity dates back to the original tabulation of the test statistic, more on than in a moment), so one must take care with which Wilcoxon test is being discussed.

The two most common forms of definition are discussed in this pair of posts:

Wilcoxon rank sum test in R

Different ways to calculate the test statistic for the Wilcoxon rank sum test

To address what, specifically, happens in R:

The statistic used by wilcox.test in R is defined in the help (?wilcox.test), and the question of the relationship to the Mann-Whitney U statistic is explained there:

The literature is not unanimous about the definitions of the Wilcoxon rank sum and Mann-Whitney tests

The two most common definitions correspond to the sum of the ranks of the first sample with the minimum value subtracted or not: R subtracts and S-PLUS does not, giving a value which is larger by m(m+1)/2 for a first sample of size m. (It seems Wilcoxon's original paper used the unadjusted sum of the ranks but subsequent tables subtracted the minimum.)

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.

This last sentence completely answers that aspect of your question - the version of W that R puts out* is also the value of U.

* The sum of the ranks in sample 1, minus the smallest value it can take (i.e. minus $$\frac{n_1(n_1+1)}{2}$$).

Both the Wilcoxon rank sum test and the Mann-Whitney test are the non-parametric equivalents of the independent t-test. In some cases the version of W that R gives, is also the valua of U. But not in all cases.

When you use: wilcox.test(df$var1 ~ df$var2, paired=FALSE) the given W is the same as U. So you may report it as the Mann-Whitney U statistic.

However when you use: wilcox.test(df$var1 ~ df$var2, paired=TRUE), you are actually performing a Wilcoxon signed rank test. The Wilcoxon signed rank test is the equivalent of the dependent t-test.

Source: "Dicovering statistics using R" by Andy Field (2013)

• A good idea to point out that if you use the argument paired=TRUE it isn't the Wilcoxon-Mann-Whitney but the signed rank. – Glen_b -Reinstate Monica Jul 28 '15 at 10:20

Note however, that the code: wilcox.test(df$var1 ~ df$var2, paired=FALSE) (using '~')

will produce a different W statistic than a: wilcox.test(df$var1, df$var2, paired=FALSE) (using ',')

• Welcome to the site, @Tilen. Was this intended as an answer to the OP's question, a comment requesting clarification from the OP or one of the answerers, or a new question of your own? Please only use the "Your Answer" field to provide answers to the original question. You will be able to comment anywhere when your reputation is >50. If you have a new question, click the gray ASK QUESTION at the top of the page & ask it there, then we can help you properly. Since you're new here, you may want to take our tour, which has information for new users. – gung - Reinstate Monica Sep 13 '16 at 16:41
• Many thanks @gung. Indeed, it was kind of both answer and question I guess. Thank you for the tips. I look forward to being part of this. – Tilen Sep 13 '16 at 17:21
• 'Answers' are for direct answers to the question only. Can you edit this to make it more purely an answer? – gung - Reinstate Monica Sep 13 '16 at 18:03
• Sorry about that. I edited it now. Does it look better now? – Tilen Sep 13 '16 at 18:09
• No need to apologize. I think your answer is implicit in what you've put here, but people will have to read between the lines. Can you make it explicit & tie it all up in a bow? However, I'm not sure this is right. To use the formula version, you need a vector of values & a grouping indicator. Eg, wilcox.test(values~ind, with(df, stack(var1=var1, var2=var2)), paired=FALSE). When I do that, I get the same W both ways. – gung - Reinstate Monica Sep 13 '16 at 18:19