Two sample Wilcoxon paired test
If one samples have larger values than the others, on average, the ranks will be smaller than in the other sample. Under the null hypothesis, on the other hand, the ranks will be similar in both samples.
It is a non-parametric version of the paired t-test that does not require Normality or equality of variance of the 2 populations.
wilcox.test{stats}
- var1 is a vector containing the different numerical observations in the first sample
- var2 is a vector containing the different numerical observations in the second sample
- alternative = one of “two.sided”, “less”, “greater”. The parameters defines to define the type of alternative hypothesis to be used: two.sided for bilateral test and “greater” or “less” for unilateral tests.
- Greater means the the ranks associated with var1 are larger than those associated with var2.
- If the option exact = TRUE is used, R will calculate the exact p-values as long as the number of observations < 50 and there are no ties in rank. If exact = FALSE, then an approximation using a normal distribution is used.
Wilcoxon Tests
M Claereboudt
October 30, 2015
This version of the Wicoxon test deals with 2 linked samples. We would like to know if the two samples were extracted from population with similar values. It this is the case, the ranks of both samples should be quite similar.
Let’s first create two vectors that represents the values observed in the 2 samples. These could be part of a larger data frame.
var1 <- c(1.8, 0.5, 1.6, 2.5, 1.7, 1.9, 1.6, 3.1, 1.3)
var2 <- c(0.9, 0.7, 0.6, 2.1, 1.1, 1.3, 1.1, 3.1, 1.3)
We can now apply the wicoxon non parametric test.
wilcox.test(var1, var2, paired = TRUE, alternative = "two.sided")
## Warning in wilcox.test.default(var1, var2, paired = TRUE, alternative =
## "two.sided"): cannot compute exact p-value with ties
## Warning in wilcox.test.default(var1, var2, paired = TRUE, alternative =
## "two.sided"): cannot compute exact p-value with zeroes
##
## Wilcoxon signed rank test with continuity correction
##
## data: var1 and var2
## V = 27, p-value = 0.03429
## alternative hypothesis: true location shift is not equal to 0
The p-value of the test is 0.034 and suggest thus that the ranks of the values in the two samples differ too much to have been taken from the same population.
Note also the two error messages. These do not invalidate the test but warn that the exact probability could not have been calculated because of the presence of ties in the data and two because no difference between paired values. To remove the messages, we could simply add the parameter exact = FALSE which forces R to use a Normal approximation to calculate the p-value.
wilcox.test(var1, var2, paired = TRUE, exact = FALSE, alternative = "two.sided")
##
## Wilcoxon signed rank test with continuity correction
##
## data: var1 and var2
## V = 27, p-value = 0.03429
## alternative hypothesis: true location shift is not equal to 0