Mastering Non-Parametric Testing: The Mann-Whitney U Test in R

Introduction

Welcome to the second lesson on Mastering Hypothesis Testing with R! Our focus today is on the Mann-Whitney U test. We've engaged with T-tests previously and have now set our sights on the Mann-Whitney U test — a valuable tool when data do not meet the T-test's normality assumption. In this lesson, we'll unpack the nuances of the Mann-Whitney U test by applying it to a realistic dataset using R's wilcox.test() function.

Mann-Whitney U Test Overview

We'll begin with non-parametric tests. They're also known as distribution-free tests because they cater to data that does not follow a normal distribution. We resort to them when our data is either skewed, ordinal, or has outliers. Ordinal data is a particular type in which the order of data points matters, though the difference between the data points does not. For example, the sequence in which runners finish a race matters, but the exact time difference between each runner does not necessarily matter.

The Mann-Whitney U test is used to compare two independent groups when the dependent variable is either ordinal or continuous but does not follow a normal distribution. By ranking the values from both groups and summing the ranks, equivalent sums of ranks suggest that the two groups do not differ significantly.

The Mann-Whitney U test yields two values: the U-statistic and the p-value. The U-statistic reflects the rank sum difference between the two groups in relation to their observed data values. Essentially, a larger U-statistic indicates a greater separation or difference between the data of the two groups. The p-value conveys the same information as in the T-test: If the p-value is less than 0.05, the difference is statistically significant and not due to chance.

Performing the Test in R

To perform the U test, we use R's wilcox.test() function. This function takes two data samples as inputs and outputs a test statistic (W) and a p-value (p). Check out this code for a better insight:

R
# Define two distinct data samples
data1 <- c(5, 22, 15, 18, 12, 17, 14)
data2 <- c(25, 24, 30, 19, 23)

# Perform the Mann-Whitney U test
result <- wilcox.test(data1, data2, exact = FALSE)

# Print the test statistic and p-value
print(paste('W-value:', result$statistic))  # 1.5
print(paste('p-value:', result$p.value))  # 0.0117

If the p-value is less than 0.05, this result suggests that we should reject the null hypothesis.

The exact = FALSE parameter in the wilcox.test() function instructs R not to use the exact distribution method for computing the p-value. This is particularly useful when dealing with larger samples, as calculating the exact p-value can become computationally intensive. By setting exact = FALSE, the function instead approximates the p-value using normal distribution assumptions, making the computation more efficient for larger datasets.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal