# 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:

set.seed(150)
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", conf.int = 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:
(pseudo)median
1.000035

#### p-value

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.

## Is a paired samples Wilcoxon test the right test?

Use our interactive tool to help you choose the right statistical test or read our article on how to choose the right statistical test.

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

## Free data science in R guide

Sign up to our newsletter and we will send you a series of guides containing tips and tricks on data science and machine learning in R.

No thanks