Wilcoxon Signed Rank Test in R
Wilcoxon Signed Rank Test is based on the direction of differences and magnitudes. This test is more sensitive and powerful than an ordinary sign test.
The ordinary sign test was based only on the direction of differences ignoring their magnitudes.
How can one apply Wilcoxon signed-rank test for matched paired samples?
Under matched paired samples, the differences d within n paired values (xi,yi) for i= 1,2,3….n are assumed to have come from continuous and symmetric population differences.
If Md is the median of the population of differences and expected to possess a known value Md0, we test
H0: Md= Md0vs H1: Md≠Md0
This test comes under the nonparametric test used to compare related samples, matched samples, or repeated measurements on a single sample to assess whether their population means ranks differ.
Majorly used when your data are non-normally distributed.
The paired samples Wilcoxon test (also known as Wilcoxon signed-rank test) is a non-parametric alternative to paired t-test used to compare paired data.
The R function wilcox.test() can be used as follow:
wilcox.exact(x, y, paired = TRUE, alternative = "two.sided")
We want to example the efficacy of the product after treatment applied.
Pre <-c(19,20,13,14,15,16,17,18,19,20) Post <-c(20,22,15,16,15,19,22,23,19,18)
Now create a data frame
data <- data.frame(Pre,Post)
The ultimate aim is to identify the any significant difference in the median values of Pre and Post treatment?
Before testing the significance, let’s visualize the data.
library("ggpubr") library(reshape) data1<-melt(data)
ggboxplot(data1, x = "variable", y = "value", color = "variable", palette = c("#00AFBB", "#E7B800"), fill = "variable", order = c("Pre", "Post"), ylab = "Value", xlab = "Groups")
Let’s calculate the summary statistics.
Descriptive statistics by group group: Pre vars n mean sd median trimmed mad min max range skew kurtosis se variable* 1 10 1.0 0.00 1.0 1.00 0.00 1 1 0 NaN NaN 0.0 value 2 10 17.1 2.51 17.5 17.25 2.97 13 20 7 -0.29 -1.59 0.8 group: Post vars n mean sd median trimmed mad min max range skew kurtosis se variable* 1 10 2.0 0.00 2 2.00 0.00 2 2 0 NaN NaN 0.00 value 2 10 18.9 2.92 19 18.88 4.45 15 23 8 -0.06 -1.63 0.92
Post median value is higher than the Pre value.
Let’s calculate the p-value based on exactRankTests package.
library(exactRankTests) res <- wilcox.exact(Pre,Post, paired = TRUE) res
Exact Wilcoxon signed rank test
data: Pre and Post V = 3.5, p-value = 0.04688 alternative hypothesis: true mu is not equal to 0
print only the p-value
If you want to test whether the median value before treatment is less than the median value post-treatment, type this:
wilcox.exact(Pre, Post, data = data, paired = TRUE, alternative = "less")
data: Pre and Post V = 3.5, p-value = 0.02344 alternative hypothesis: true mu is less than 0
If you want to test whether the median value before treatment is greater than the median value post-treatment, type this:
wilcox.exact(Pre, Post, data = data, paired = TRUE, alternative = "greater")
The exact Wilcoxon signed-rank test
data: Pre and Post V = 3.5, p-value = 0.9922 alternative hypothesis: true mu is greater than 0
Based on the test, significant improvement was observed in the tested sample performance over the pre-value.