Paired samples Wilcoxon test in R

By Data Tricks, 28 July 2020

What is a paired samples Wilcoxon test?

A paired samples Wilcoxon test is the non-parametric alternative of a paired samples t-test. It assumes that the data are not normally distributed.

A paired samples Wilcoxon test has a null hypothesis that the mean difference is equal to zero, and an alternative hypothesis that it is not equal. This is called a two-tailed test.

We can also perform a one-tailed t-test if we have a prior belief that the median difference is either larger or smaller than zero.

Example in R

First let’s create a set of values to use in this example:

randomsequence <- sample(c(-4:5), 100, replace = TRUE)
data <- data.frame(id = c(1:100),
                   before = sample(c(1:50), 100, replace = TRUE))
data$after <- data$before + randomsequence

Our null hypothesis is that the median difference between the pairings of after and before are zero, whilst the alternative hypothesis is that the median is different.

test <- wilcox.test(data$after, data$before, paired = TRUE, alternative = "two.sided", = TRUE)

Now analyse the result of the test:

> test
       Wilcoxon signed rank test with continuity correction

data: data$after and data$before
V = 2957.5, p-value = 0.0005856
alternative hypothesis: true location shift is not equal to 0
95 percent confidence interval:
 0.4999899 1.5000280
sample estimates:


The p-value is 0.00059, below the 5% significance level and therefore the null hypothesis can be rejected.

95% confidence interval

The 95% confidence interval for our test is 0.50 to 1.50. This means that at the 5% significance level, the true median difference lies between 0.50 and 1.50.

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Please note that your first comment on this site will be moderated, after which you will be able to comment freely.