The Wilcoxon signed rank test is a non-parametric method used to compare two related groups. It works well when we have matched data or repeated measurements from the same group and want to see if there is a meaningful difference between them. This test is often used as an alternative to the paired t-test, especially when the data does not follow a normal distribution.
In simple terms, the Wilcoxon test helps us find out if the differences between two sets of related data are likely due to chance or if they suggest a real change.
Types of Wilcoxon Signed-Rank Test in R
This test can be divided into two parts:
- One-Sample Wilcoxon Signed Rank Test
- Paired Samples Wilcoxon Test
1. One-Sample Wilcoxon Signed Rank Test
The one-sample Wilcoxon signed-rank test is a non-parametric alternative to a one-sample t-test , when the data cannot be assumed to be normally distributed. It’s used to determine whether the median of the sample is equal to a known standard value (theoretical value) .
Implementation in R
To perform a one-sample Wilcoxon test, R provides a function wilcox.test() that can be used as follow:
Syntax: wilcox.test(x, mu = 0, alternative = "two.sided")
Parameters:
- x: a numeric vector containing your data values
- mu: the theoretical mean/median value. Default is 0 but you can change it.
- alternative: the alternative hypothesis. Allowed value is one of “two.sided” (default), “greater” or “less”.
Example: Is the median weight of the rabbit differs from 25g?
set.seed(1234)
myData <- data.frame(
name = paste0("R_", 1:10),
weight = round(rnorm(10, 30, 2), 1)
)
print(myData)
result <- wilcox.test(myData$weight, mu = 25)
print(result)
Output:
name weight
1 R_1 27.6
2 R_2 30.6
3 R_3 32.2
4 R_4 25.3
5 R_5 30.9
6 R_6 31.0
7 R_7 28.9
8 R_8 28.9
9 R_9 28.9
10 R_10 28.2
Wilcoxon signed rank test with continuity correction
data: myData$weight
V = 55, p-value = 0.005793
alternative hypothesis: true location is not equal to 25
Since the p-value of the test is 0.005793, which is less than the significance level alpha = 0.05, we can reject the null hypothesis and conclude that the average weight of the rabbit is significantly different from 25g.
To test whether the median is less than 25g,
wilcox.test(myData$weight, mu = 25, alternative = "less")
Output:
Wilcoxon signed rank exact test
data: myData$weight
V = 55, p-value = 0.9979
alternative hypothesis: true location is less than 25
To test whether the median is greater than 25g,
wilcox.test(myData$weight, mu = 25, alternative = "greater")
Output:
Wilcoxon signed rank exact test
data: myData$weight
V = 55, p-value = 0.002897
alternative hypothesis: true location is less than 25
2. Paired Samples Wilcoxon Test in R
The paired samples Wilcoxon test is a non-parametric alternative to paired t-test used to compare paired data. It’s used when you have two related measurements for the same subjects, like before and after a treatment. It compares whether there is a significant shift in the median.
Implementation in R
To perform Paired Samples Wilcoxon-test, the R provides a function wilcox.test() that can be used as follow:
Syntax: wilcox.test(x, y, paired = TRUE, alternative = "two.sided")
Parameters:
- x, y: numeric vectors
- paired: a logical value specifying that we want to compute a paired Wilcoxon test
- alternative: the alternative hypothesis. Allowed value is one of “two.sided” (default), “greater” or “less”.
Example: Compare weights before and after treatment
before <- c(190.1, 190.9, 172.7, 213, 231.4,
196.9, 172.2, 285.5, 225.2, 113.7)
after <- c(392.9, 313.2, 345.1, 393, 434,
227.9, 422, 383.9, 392.3, 352.2)
result <- wilcox.test(before, after, paired = TRUE)
print(result)
Output:
Wilcoxon signed rank test
data: before and after
V = 0, p-value = 0.001953
alternative hypothesis: true location shift is not equal to 0
In the above output, the p-value of the test is 0.001953, which is less than the significance level alpha = 0.05. We can conclude that the median weight of the mice before treatment is significantly different from the median weight after treatment with a p-value = 0.001953.
To check if weight before treatment is less than after:
wilcox.test(before, after, paired = TRUE, alternative = "less")
Output:
Wilcoxon signed rank test
data: weight by group
V = 55, p-value = 0.0009766
alternative hypothesis: true location shift is less than 0
To check if weight before treatment is greater than after:
wilcox.test(before, after, paired = TRUE, alternative = "greater")
Output:
Wilcoxon signed rank test
data: weight by group
V = 55, p-value = 1
alternative hypothesis: true location shift is greater than 0
In this article, we performed the Wilcoxon Signed Rank Test in R for both one-sample and paired sample scenarios, along with practical examples and interpretation of results.