Home » How to Perform a Mann-Whitney U Test in R

How to Perform a Mann-Whitney U Test in R

by Erma Khan

A Mann-Whitney U test (sometimes called the Wilcoxon rank-sum test) is used to compare the differences between two independent samples when the sample distributions are not normally distributed and the sample sizes are small (n

It is considered to be the nonparametric equivalent to the two-sample independent t-test.

This tutorial explains how to perform a Mann-Whitney U test in R.

Example: Mann-Whitney U Test in R

Researchers want to know whether or not a new drug is effective at preventing panic attacks. A total of 12 patients are randomly split into two groups of 6 and assigned to receive the new drug or the placebo. The patients then record how many panic attacks they have over the course of one month.

The results are shown below:

NEW DRUG PLACEBO
3 4
5 8
1 6
4 2
3 1
5 9

Conduct a Mann-Whitney U Test to determine if there is a difference in the number of panic attacks for the patients in the placebo group compared to the new drug group. Use a .05 level of significance. 

There are two different ways to perform the Mann-Whitney U test, but both methods use the wilcox.test() function and both lead to the same outcome.

Option 1: Enter the data as two separate vectors.

#create a vector for each group
new #perform the Mann Whitney U test
wilcox.test(new, placebo)

#output
Wilcoxon rank sum test with continuity correction

data:  new and placebo
W = 13, p-value = 0.468
alternative hypothesis: true location shift is not equal to 0

Option 2: Enter the data into one data frame with two columns. One column contains the number of panic attacks and the other contains the group.

#create a data frame with two columns, one for each group
drug_data #perform the Mann Whitney U test
wilcox.test(attacks~drug_group, data = drug_data)

#output
data:  attacks by drug_group
W = 13, p-value = 0.468
alternative hypothesis: true location shift is not equal to 0

Notice that both methods lead to the exact same result. Namely, the test statistic is W = 13 and the corresponding p-value is 0.468.

Since the p-value is greater than 0.05, we fail to reject the null hypothesis.

This means we do not have sufficient evidence to say that the number of panic attacks experienced by patients in the placebo group is different from the new drug group.

Notes on Using Wilcox.test()

By default, wilcox.test() assumes you want to run a two-tailed hypothesis test. However, you can specify alternative=”less” or alternative=”more” if you’d instead like to run a one-tailed test.

For example, suppose we’d like to test the hypothesis that the new drug leads to less panic attacks than the placebo. In this case, we could specify alternative=”less” in our wilcox.test() function:

#create a vector for each group
new #perform the Mann Whitney U test, specify alternative="less"
wilcox.test(new, placebo, alternative="less")

#output
	Wilcoxon rank sum test with continuity correction

data:  new and placebo
W = 13, p-value = 0.234
alternative hypothesis: true location shift is less than 0

Notice that the test statistic is still W = 13, but the p-value is now 0.234, which is exactly half as large as the previous p-value for the two-sided test.

Since the p-value is still greater than 0.05, we would still fail to reject the null hypothesis.

We do not have sufficient evidence to say that the number of panic attacks experienced by patients in the new drug group was less than that of the patients in the placebo group.

Additional Resources

A Guide to the Mann-Whitney U Test
Mann-Whitney U Test Calculator

Related Posts