# Wilcoxon test

6 mins

## Overview

The Wilcoxon test is a non-parametric alternative to the t-test for comparing averages in one or two (dependent or independent) samples. There are different types of wilcoxon tests, depending on the type of sample: One-sample Wilcoxon signed rank test, Wilcoxon matched-pairs test for a dependent sample and Wilcoxon rank sum test (Mann-Whitney U test) for an independent sample. In this building block, you will learn how to perform these tests on R using wilcox.test().

## One-sample Wilcoxon signed-rank test

The one-sample Wilcoxon signed-rank test is used to assess whether the median of the sample is equal to a known standard or theoretical value. It is a non-parametric test equivalent to a one-sample t-test and is appropriate for continuous and ordinal data. To perform this test on R, you can use the inbuilt function wilcox.test(), in the following way:

where:

• x denotes a numeric vector containing your data values.
• mu is the theoretical mean/median value. The default is 0 but you can change it.
• alternative defines the alternative hypothesis. It can be â€śtwo.sidedâ€ť (default), â€śgreaterâ€ť or â€ślessâ€ť.
• exact determines whether to compute an exact p-value for small sample sizes. When exact = TRUE, R will compute an exact p-value using the exact distribution of the test statistic rather than relying on an asymptotic approximation.
• correct is a boolean that indicates whether to apply continuity correction in the normal approximation for the p-value. It is TRUE by default.

Suppose you recorded the heights of 100 trees in a dataset called tree_heights to check if the median height is less than 85ft.

Let’s first check if this data set follows a normal distribution via a histogram and the Shapiro-Wilk test.

The shape of the histogram shows that the data does not follow a normal distribution and the p-value of the Shapiro-Wilk tests confirms it (since we reject the null hypothesis of normality for both distributions at the 5% significance level). Since the normality assumption is violated we cannot use parametric tests such as Studentâ€™s t-test and should instead utilise the Wilcoxon test.

This test can be performed on R using wilcox.test(). Since you are interested in analysing if the median height is less than 85ft you should set mu = 85 and alternative = “less”.

The hypothesis for this test will be set as:

• H0: Median height = 85
• H1: Median height < 85

The output of the code gives a p-value of 0.008 which is less than 0.01. Thus, you can reject the null hypothesis at a 1% significance level which is highly suggestive of the median height of the trees being less than 85ft.

Tip

If your sample size is large, an asymptotic approximation is often used because otherwise, the exact calculation might be computationally expensive. Thus, given the large sample size of the dataset in this example, we set exact = FALSE.

It is important to note that in the case of large samples, normality is not required. By the central limit theorem, sample means of large samples are often well-approximated by a normal distribution even if the data are not normally distributed. Therefore, we typically use the wilcoxon test for smaller samples where normality is unlikely to hold.

For the sake of this building block, in the following two examples, we will assume that normality does not hold even with a large sample.

## Wilcoxon matched-pairs test

The Wilcoxon matched-pairs signed-rank test (or simply the Wilcoxon matched-pairs test) is a nonparametric method to compare before-after or matched subjects. For performing this test, wilcox.test() has the following syntax:

where:

• y is another (optional) numeric vector with data values.
• paired is a logical value specifying whether you want a paired test. It is FALSE by default, so you need to set it to TRUE for performing the Wilcoxon matched-pairs test.

To understand how to use this test, let us look at the dataset mice2 from the package datarium. It contains the weights of ten mice before and after a treatment. Suppose you want to assess if there is a significant difference between the weights of the mice before and after the treatment (paired samples) you can use the Wilcoxon matched-pairs test. The hypothesis will be set as:

• H0: Weights before = Weights after
• H1: Weights before â‰  Weights after

The output of this test gives a p-value equal to 0.002 which is less than 0.01. This suggests that at a 1% significance level, the difference in the weights of the mice before and after the treatment is highly significant.

Tip

If your dataset does not have the ‘Before’ and ‘After’ values in separate columns like so:

id before after
1 187.2 429.5

but instead in the form of, say

id group weight
1 before 187.2
1 after 429.5

you can specify your data values as

wilcox.test(weight ~ group)

Warning

For an independent sample, it is sufficient to check for normality individually on the two samples. However, for paired samples (when in the presence of a small sample), normality must be checked on the differences between the two paired samples.

## Wilcoxon rank sum test

The Wilcoxon rank sum test is a nonparametric test to compare two unmatched (independent) groups. It is also known as the Mann-Whitney U test. The syntax of wilcox.test() for performing this test is the same as that for the matched-pairs test.

Tip

There is no need to explicitly specify the exact argument for this test. That argument is more relevant when you are dealing with paired samples. For the Mann-Whitney U test (which is essentially a Wilcoxon rank-sum test for independent samples), the default behaviour is to use an asymptotic approximation.

For this let us look at another dataset from the datarium package, called genderweight. It contains the weights by sex (M for male; F for female). The genders are specified under the column ‘group’. Suppose you want to answer the question of whether the average womanâ€™s weight significantly differs from the average manâ€™s weight you can use the Mann-Whitney U test. The hypothesis will be set as:

• H0: Female weight = Male weight
• H1: Female weight â‰  Male weight

The output of this test gives a p-value less than 0.0001 so you can reject the null hypothesis. This suggests that the difference in the average weights of the two genders is significant.

Warning

It is not always ideal to default to non-parametric tests to avoid normality checks. The reason is that non-parametric tests are usually less powerful than corresponding parametric tests when the normality assumption holds. This means that ceteris paribus, with a non-parametric test you are less likely to reject the null hypothesis when it is false if the data follows a normal distribution. It is thus preferred to use the parametric version of a statistical test when the assumptions are met.

Summary
• The Wilcoxon test family provides versatile options for comparing samples without making strong assumptions about the underlying distribution.

• One-sample Wilcoxon Signed-Rank test is used when you have one sample and want to test if its distribution is symmetrically centred around a hypothesized median.
• Wilcoxon matched-pairs test is used when you want to compare averages of two paired (dependent) samples.
• Wilcoxon rank sum test is used when you have two independent samples.
• These tests can be performed on R using its inbuilt test wilcox.test()