秩和检验，Wilcoxon-Matt-Whitney test (or Wilcoxon rank sum test, orMann-Whitney U-test) 用于比较两个并不满足正态分布群组的均值比较：这是一个非参数检验（non-parametrical test）。其与应用于独立样本的t-test相当，但t-test需要数据为正态分布，这个检验不需要数据为正态分布。

## 一、算法：

In this example, we have a set of 20 reads, 10 of which support the reference allele and 10 of which support the alternate allele. At first glance, that looks like a clear heterozygous 0/1 site. But to be thorough in our analysis and to account for any technical bias, we want to determine if there is a significant difference in the base qualities of the bases that support the reference allele vs. the bases that support the alternate allele.

Before we proceed, we must define our null hypothesis and alternate hypothesis.

–*Null hypothesis:* There is **no** difference in the base qualities that support the reference allele and the base qualities that support the alternate allele.

–*Alternate hypothesis:* There **is** a difference in the base qualities that support the reference allele and the base qualities that support the alternate allele.

### Step 1: List the relevant observations

Reference allele base qualities: 20, 25, 26, 30, 32, 40, 47, 50, 53, 60 Alternate allele base qualities: 0, 7, 10, 17, 20, 21, 30, 34, 40, 45

### Step 2: Rank the observations

First, we arrange all the observations (base qualities) into a list of values ordered from lowest to highest (reference bases are in bold).

0, 7, 10, 17, **20**, 20, 21, **25**, **26**, **30**, 30, **32**, 34, **40**, 40, 45, **47**, **50**, **53**, **60**

Next we determine the ranks of the values. Since there are 20 observations (the base qualities), we have 20 ranks to assign. Whenever there are ties between observations for the rank, we take the rank to be equal to the midpoint of the ranks. For example, for 20(ref) and 20(alt), we have a tie in values, so we assign each observation a rank of (5+6)/2 = 5.5.

The ranks from the above list are (reference ranks are in bold):

1, 2, 3, 4, **5.5**, 5.5, 7, **8**, **9**, **10.5**, 10.5, **12**, 13, **14.5**, 14.5, 16, **17**, **18**, **19**, **20**

### Step 3: Add up the ranks for each group

We now need to add up the ranks for the base qualities that came from the reference allele and the alternate allele.

### Step 4: Calculate U for each group

U is a statistic that tells us the difference between the two rank totals. We can use the U statistic to calculate the z-score (explained below), which will give us our p-value.

Calculate U for each group (n = number of observations in each sample)

$$ U*{ref} = \frac{ n*{ref} *n{alt} + n{ref} *(n*{ref}+ 1) }{ 2 } – Rank*{ref} $$

$$ U*{alt} = \frac{ n*{alt} *n{ref} + n{alt} *(n*{alt} + 1) }{ 2 } – Rank*{alt} $$

$$ U_{ref} = \frac{ 10 *10 + 10 *11 }{ 2 } – 133.5 = 21.5 $$

$$ U_{alt} = \frac{ 10 *10 + 10 *11 }{ 2 } – 76.5 = 78.5 $$

### Step 5: Calculate the overall z-score

Next, we need to calculate the z-score which will allow us to get the p-value. The z-score is a normalized score that allows us to compare the probability of the U score occurring in our distribution. https://statistics.laerd.com/statistical-guides/standard-score.php

The equation to get the z-score is:

Breaking this equation down:

$$ mu = \text{mean of the U scores above} = \frac{ n*{ref} * n*{alt} }{ 2 } $$

$$ u = \text{standard deviation of U} = \sqrt{ \frac{n*{ref} * n*{alt} * (n*{ref} + n*{alt} + 1) }{ 12 } } $$

To calculate our z:

$$ u = \sqrt{ \frac{10 *10 *(10 + 10 + 1) }{ 12 } } = 13.229 $$

So altogether we have:

### Step 6: Calculate and interpret the p-value

The p-value is the probability of obtaining a z-score at least as extreme as the one we got, assuming the null hypothesis is true. In our example, the p-value gives us the probability that there is no difference in the base qualities that support the reference allele and the base qualities that support the alternate allele. The lower the p-value, the less likely it is that there is no difference in the base qualities.

Going to the z-score table, or just using a p-value calculator, we find the p-value to be 0.0312.

This means there is a .0312 chance that the base quality scores of the reference allele and alternate allele are the same. Assuming a p-value cutoff of 0.05, meaning there is less than 5% chance there is no difference in the two groups, and greater than or equal to 95% chance that there is a difference between the two groups, we have enough evidence to **reject our null hypothesis** that there is no difference in the base qualities of the reference and alternate allele. This indicates there is some bias and that the alternate allele is less well supported by the data than the allele counts suggest.

## 二、R的具体实现

1 2 3 4 5 6 7 8 9 10 |
a = c(6, 8, 2, 4, 4, 5) b = c(7, 10, 4, 3, 5, 6) wilcox.test(a,b, correct=FALSE) Wilcoxon rank sum test data: a and b W = 14, p-value = 0.5174 alternative hypothesis: true location shift is not equal to 0 |

p-value大于0.05，因此我们可接受null hypothesis H0，即两个群组的均值统计相等。

参考资料：

https://software.broadinstitute.org/gatk/guide/article?id=8031

http://blog.csdn.net/timothyzh/article/details/7657470

https://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U_test