Paired Samples Wilcoxon Test in R

The paired samples Wilcoxon test is a non-parametric alternative to paired t-test used to compare paired data. It’s used when data are not normally distributed.

Implementation in R

To perform Paired Samples Wilcoxon-test, the R provides a function wilcox.test() that can be used as follow:

Syntax: wilcox.test(x, y, paired = TRUE, alternative = “two.sided”)

Parameters: 

  • x, y: numeric vectors
  • paired: a logical value specifying that we want to compute a paired Wilcoxon test
  • alternative: the alternative hypothesis. Allowed value is one of “two.sided” (default), “greater” or “less”.

Example: Here, let’s use an example data set, which contains the weight of 10 rabbits before and after the treatment. We want to know, if there is any significant difference in the median weights before and after treatment? 

R




# R program to illustrate
# Paired Samples Wilcoxon Test
 
# The data set
# Weight of the rabbit before treatment
before <-c(190.1, 190.9, 172.7, 213, 231.4,
        196.9, 172.2, 285.5, 225.2, 113.7)
 
# Weight of the rabbit after treatment
after <-c(392.9, 313.2, 345.1, 393, 434,
        227.9, 422, 383.9, 392.3, 352.2)
 
# Create a data frame
myData <- data.frame(
group = rep(c("before", "after"), each = 10),
weight = c(before, after)
)
 
# Print all data
print(myData)
 
# Paired Samples Wilcoxon Test
result = wilcox.test(before, after, paired = TRUE)
 
# Printing the results
print(result)


Output: 

   group weight
1  before  190.1
2  before  190.9
3  before  172.7
4  before  213.0
5  before  231.4
6  before  196.9
7  before  172.2
8  before  285.5
9  before  225.2
10 before  113.7
11  after  392.9
12  after  313.2
13  after  345.1
14  after  393.0
15  after  434.0
16  after  227.9
17  after  422.0
18  after  383.9
19  after  392.3
20  after  352.2

    Wilcoxon signed rank test

data:  before and after
V = 0, p-value = 0.001953
alternative hypothesis: true location shift is not equal to 0

In the above output, the p-value of the test is 0.001953, which is less than the significance level alpha = 0.05. We can conclude that the median weight of the mice before treatment is significantly different from the median weight after treatment with a p-value = 0.001953.

If one wants to test whether the median weight before treatment is less than the median weight after treatment, then the code will be: 

R




# R program to illustrate
# Paired Samples Wilcoxon Test
 
# The data set
# Weight of the rabbit before treatment
before <-c(190.1, 190.9, 172.7, 213, 231.4,
        196.9, 172.2, 285.5, 225.2, 113.7)
 
# Weight of the rabbit after treatment
after <-c(392.9, 313.2, 345.1, 393, 434,
        227.9, 422, 383.9, 392.3, 352.2)
 
# Create a data frame
myData <- data.frame(
group = rep(c("before", "after"), each = 10),
weight = c(before, after)
)
 
# Paired Samples Wilcoxon Test
result = wilcox.test(weight ~ group,
                    data = myData,
                    paired = TRUE,
                    alternative = "less")
 
# Printing the results
print(result)


Output: 

Wilcoxon signed rank test

data:  weight by group
V = 55, p-value = 1
alternative hypothesis: true location shift is less than 0

Or, If one wants to test whether the median weight before treatment is greater than the median weight after treatment, then the code will be: 

R




# R program to illustrate
# Paired Samples Wilcoxon Test
 
# The data set
# Weight of the rabbit before treatment
before <-c(190.1, 190.9, 172.7, 213, 231.4,
        196.9, 172.2, 285.5, 225.2, 113.7)
 
# Weight of the rabbit after treatment
after <-c(392.9, 313.2, 345.1, 393, 434,
        227.9, 422, 383.9, 392.3, 352.2)
 
# Create a data frame
myData <- data.frame(
group = rep(c("before", "after"), each = 10),
weight = c(before, after)
)
 
# Paired Samples Wilcoxon Test
result = wilcox.test(weight ~ group,
                    data = myData,
                    paired = TRUE,
                    alternative = "greater")
 
# Printing the results
print(result)


Output: 

Wilcoxon signed rank test

data:  weight by group
V = 55, p-value = 0.0009766
alternative hypothesis: true location shift is greater than 0


Wilcoxon Signed Rank Test in R Programming

The Wilcoxon signed-rank test is a non-parametric statistical hypothesis test used to compare two related samples, matched samples, or repeated measurements on a single sample to estimate whether their population means ranks differ e.g. it is a paired difference test. It can be applied as an alternative to the paired Student’s t-test also known as “t-test for matched pairs” or “t-test for dependent samples” when the distribution of the difference between the two samples’ means cannot be assumed to be normally distributed. A Wilcoxon signed-rank test is a nonparametric test that can be used to determine whether two dependent samples were selected from populations having the same distribution.

Similar Reads

Wilcoxon Signed-Rank Test in R

This test can be divided into two parts:...

One-Sample Wilcoxon Signed Rank Test

The one-sample Wilcoxon signed-rank test is a non-parametric alternative to a one-sample t-test when the data cannot be assumed to be normally distributed. It’s used to determine whether the median of the sample is equal to a known standard value i.e. a theoretical value. In R Language one can perform this test very easily....

Paired Samples Wilcoxon Test in R

...

Contact Us