## Rank tests

This post discusses non parametric testing methods: sign test, signed rank test and rank sum test.

### Sign Test

Sign test tests if the median of data is md. For example, given:

> x <- c(1, 3, 2, 7, 4, 6, 8, 2)


If $H_0: md = 5$, then the number of positive signs of x – 5 should be about 4 (= the number of data / 2 = 8 / 2):

> sign(x - 5)
[1] -1 -1 -1  1 -1  1  1 -1


If $H_0$ holds, number of positive sign is $B(8, \frac{1}{2})$ where 8 is the number of data. Thus, having observed 3 positive signs, we set $H_0: p=.5$ in $B(8, p)$ and $H_1: p \neq .5$.

Note that we should remove values with x = 5 as it does not fit binomial distribution we’re considering(plus or minus). But we don’t have 5 in this example.

Then, the p value is:

> 2 * pbinom(3, 8, .5)
[1] 0.7265625


Thus, we can not reject $H_0$.

We can do the same thing with binom.test():

> binom.test(3, 8)

Exact binomial test

data:  3 and 8
number of successes = 3, number of trials = 8, p-value = 0.7266
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
0.08523341 0.75513678
sample estimates:
probability of success
0.375


See that the p-value is the same as we calculated manually. BSDA package has a convenient function SIGN.test:

> install.packages("BSDA")
> library(BSDA)
> SIGN.test(x, md=5)

One-sample Sign-Test

data:  x
s = 3, p-value = 0.7266
alternative hypothesis: true median is not equal to 5
95 percent confidence interval:
1.675 7.325
sample estimates:
median of x
3.5

Conf.Level L.E.pt U.E.pt
Lower Achieved CI     0.9297  2.000  7.000
Interpolated CI       0.9500  1.675  7.325
Upper Achieved CI     0.9922  1.000  8.000


Based on the same idea, we can test if x and y has the median difference of 0:

> SIGN.test(c(1, 3, 2, 5, 6), c(2, 5, 3, 2, 1), md=0)

Dependent-samples Sign-Test

data:  c(1, 3, 2, 5, 6) and c(2, 5, 3, 2, 1)
S = 2, p-value = 1
alternative hypothesis: true median difference is not equal to 0
93.75 percent confidence interval:
-2  5
sample estimates:
median of x-y
-1


### Signed Rank Test

Wilcoxon’s signed rank test uses the magnitude of difference in addition to the sign. In other words , sign test looked at sign (+1 or -1) only. But in signed rank test, we look at difference magnitude and their rank as well. Also, signed rank test assumes the difference, i.e., x – 5, is symmetric for two-sided test. (See assumptions section in the wiki.)

> m <- rbind(abs(x-5), rank(abs(x-5)), sign(x-5))
> rownames(m) <- c("abs", "rank", "sign")
> m
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
abs     4  2.0    3  2.0  1.0  1.0    3    3
rank    8  3.5    6  3.5  1.5  1.5    6    6
sign   -1 -1.0   -1  1.0 -1.0  1.0    1   -1


Then, we get the sum of rank for sign = -1 (T_minus) and sum of rank for sign = +1 (T_plus):

> T_plus <- sum(m["rank", m["sign",] > 0])
> T_minus <- sum(m["rank", m["sign",] < 0])


Finally, we get min of T_plus and T_minus:

> T_test <- min(T_plus, T_minus)
> T_test
[1] 11


If median were md, then T_test should not be too different from 18 (= (1+2+…+8)/2) as half of x-5 should be larger than 0 and half of them should be smaller than 0.

Package exactRankTests has wilcox.test() and wilcox.exact(). Among them, wilcox.exact() considers tie in ranking properly:

> wilcox.exact(x, mu=5, exact=FALSE)

Asymptotic Wilcoxon signed rank test

data:  x
V = 11, p-value = 0.3234
alternative hypothesis: true mu is not equal to 5


We can’t reject $H_0$ that mu equals to 5. Please note that I’ve used exact=FALSE. It’s necessary when there’s tie in the data. If there’s tie, we can’t compute p-value exactly, and need to use approximation to gaussian by specifying exact=FALSE.

For paired test(test if median of difference is 5):

> x
[1] 1 3 2 7 4 6 8 2
> y
[1] 1 3 2 4 5 3 2 7
> wilcox.exact(x, y, mu=5, exact=FALSE, paired=T)

Asymptotic Wilcoxon signed rank test

data:  x and y
V = 1, p-value = 0.01661
alternative hypothesis: true mu is not equal to 5


### Rank Sum Test

Rank sum test is for samples taken independently from x and y. Basic idea is to combine x and y, then sort them to give them ranks. If x and y are not too different, rank sum of x and y shouldn’t be too different:

> m <- rbind(c(x,y), c(rep(0, 8), rep(1, 8)), rank(c(x, y)))
> rownames(m) <- c("data", "source", "rank")
> m
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
data    1.0    3  2.0  7.0  4.0    6    8  2.0  1.0     3   2.0   4.0     5
source  0.0    0  0.0  0.0  0.0    0    0  0.0  1.0     1   1.0   1.0     1
rank    1.5    8  4.5 14.5 10.5   13   16  4.5  1.5     8   4.5  10.5    12
[,14] [,15] [,16]
data       3   2.0   7.0
source     1   1.0   1.0
rank       8   4.5  14.5
> sum(m["rank", m["source",]==0])
[1] 72.5
> sum(m["rank", m["source",]==1])
[1] 63.5


More precisely, the sum of rank should be proportional to the number of data belongs to each source. But in our case, as the number of data in x and y is the same, they should be very similar.

wilcox.test() and wilcox.exact() performs rank sum test if paired = FALSE (which is default):

> wilcox.exact(x, y, exact=FALSE)

Asymptotic Wilcoxon rank sum test

data:  x and y
W = 36.5, p-value = 0.6322
alternative hypothesis: true mu is not equal to 0


As p > 0.05, if can not reject $H_0$. Thus, mu is equal to zero.

### Efficiency

According to [4], signed rank test and rank sum test is efficient enough compared to t-test for data from normal distribution. Compared to signed rank test and rank sum test, t-test needed 90% of data, meaning that those non parametric methods are not too inferior to the parametric testing.

References)
1. r-tutor.com: sign test
2. rtutor.com: wilcoxon signed rank test
3. 임동훈, R을 이용한 비모수 통계학, 자유아카데미.
4. 배도선 외, 통계학 이론과 응용, 청문각.
5. http://en.wikipedia.org/wiki/Sign_test
6. http://en.wikipedia.org/wiki/Wilcoxon_signed-rank_test
7. http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U: This is rank sum test called also as Mann–Whitney U test or Mann–Whitney–Wilcoxon (MWW) or Wilcoxon rank-sum test or Mann-Whitney test.
8. 안재형, R을 이용한 누구나하는 통계분석, 한나래.

Similar Posts: