# How to Perform the Wilcoxon Signed-Rank Test in R

The Wilcoxon Signed-Rank Test is a non-parametric statistical hypothesis test used to compare two related samples, paired samples, or repeated measurements on a single sample to assess whether their population mean ranks differ. It is an alternative to the paired sample t-test when the normality assumption is in doubt.

In this article, we will walk through the rationale behind the Wilcoxon Signed-Rank Test, how to prepare your data, the steps to perform the test in R, and finally, how to interpret the results.

## Understanding the Wilcoxon Signed-Rank Test

The Wilcoxon Signed-Rank Test is based on the rank of the differences between paired observations. Here’s a step-by-step breakdown of the test:

1. For each pair, calculate the difference.
2. Rank the differences in ascending order, ignoring the signs.
3. Sum the ranks for the positive differences (called W+W+) and the ranks for the negative differences (called W−W−).
4. The test statistic WW is the smaller of W+W+ and W−W−.

This test is suitable for ordinal, interval, and ratio data.

Your data should be in paired format. This means you should have two columns of data, where each row represents a pair of related observations. This structure is commonly seen in before-and-after scenarios.

Let’s say you’re studying the effects of a new medication. You measure some health parameter before and after administering the medication to 20 patients. Your dataset might look like this:

# Create a sample dataset
set.seed(123)
before <- rnorm(20, mean = 50, sd = 10)
after <- before + rnorm(20, mean = 5, sd = 5)

data <- data.frame(before, after)

## Checking Assumptions

For the Wilcoxon Signed-Rank Test, we need to check:

1. Dependence: The data pairs are dependent. This is inherent in the design (e.g., before-and-after measurements on the same subjects).
2. Ordinal Scale: The differences between paired observations can be ranked. If you’re using interval or ratio data (like our example), this assumption is met.

## Performing the Wilcoxon Signed-Rank Test

Once your data is set up and assumptions are checked, you can perform the test using the wilcox.test() function in R with the paired argument set to TRUE. Here’s how you can do it with our dataset:

# Wilcoxon Signed-Rank Test
result <- wilcox.test(data$before, data$after, paired = TRUE)
print(result)

## Interpreting the Results

R will provide an output that includes the test statistic VV, and the two-sided p-value. Here’s an example of what the output might look like:

Wilcoxon signed rank test with continuity correction

data:  data$before and data$after
V = 45, p-value = 0.004419
alternative hypothesis: true location shift is not equal to 0

Here’s a breakdown of the output:

• V: This is the test statistic. It is the smaller of the sums of the positive and negative ranks, but how it is computed can differ based on software or reference material. In R, VV is typically the sum of the positive ranks.
• p-value: The p-value helps you determine the significance of your results in hypothesis testing. A small p-value (typically ≤ 0.05) indicates strong evidence against the null hypothesis. In this example, the p-value is 0.004419, which is significant at the 0.05 level. Thus, we would reject the null hypothesis.

Given the results, we have evidence to reject the null hypothesis and conclude that the new medication had a significant effect on the health parameter we measured.

## Conclusion

The Wilcoxon Signed-Rank Test offers a way to test for significant differences between two paired groups when the assumptions of the paired sample t-test are not met. It’s a valuable tool for researchers working with non-normally distributed data or data that’s ordinal in nature. When performing the test in R, the wilcox.test() function makes it straightforward, providing quick and interpretable results.

Posted in RTagged