Wilcoxon signed-rank test may be used to compare the means of two dependent samples when the assumption of normal distribution may be a problem. This is a non-parametric test which is a good alternative to Student’s paired t-test.

The function to be used is the same as for Mann-Whitney U test (Wilcoxon rank-sum test) but with an extra argument which sets the test for dependent samples: wilcox.test(x, y, paired = TRUE) where x and y are the two paired samples and paired = TRUE the argument that turns the original test into a paired test.

Let’s take an example similar to the one used for Student’s paired t-test. Here, daily mean temperatures are recorded during the first 20 days of May 2019 at Florida, close to Bergen City Centre. Temperatures are retrieved simultaneously from 2 different sources, GFI and Yr; the data set thus consists of pairs of measurements.

The first test below confirms that the samples do not originate from a normal distribution:

## 
##  Shapiro-Wilk normality test
## 
## data:  GFI
## W = 0.85544, p-value = 0.006584
## 
##  Shapiro-Wilk normality test
## 
## data:  Yr
## W = 0.85092, p-value = 0.005522



Now, let’s visualize the data with a boxplot:

Here, the medians are close to each other but not equal, and the spread is rather similar. Let’s look at the data with a line plot.

It seems like there is a rather small, but constant gap between the lines. Is that enough to declare that the 2 samples are significantly different? Let’s test it with Wilcoxon signed-rank test:

wilcox.test(GFI, Yr, paired = TRUE)
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  GFI and Yr
## V = 5, p-value = 0.0002017
## alternative hypothesis: true location shift is not equal to 0

So, the test results in a p-value less than 0.05, thus indicating that the null hypothesis (the sample means are not different) may be rejected.

Note: If you wonder whether the argument paired = TRUE makes a difference or not, let’s try the test without it:

wilcox.test(GFI, Yr)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  GFI and Yr
## W = 170.5, p-value = 0.4326
## alternative hypothesis: true location shift is not equal to 0

This time, the p-value is greater than 0.05 and H0 cannot be rejected anymore… So it is clearly important to know about the dependency of your samples; the conclusion of the test relies on that.