# 假设检验[4]--秩和检验

## 一、配对资料的符号秩和检验

### 非参数检验的适用条件:

1. 总体分布类型不明
2. 总体分布呈偏态分布
3. 数据一端或两端有不确定值的资料
4. 总体方差不齐
5. 有序分类变量资料

### 配对资料的符号秩和检验（Wilcoxon signed­rank test, Wilcoxon符号秩和检验）

Wilcoxon符号秩和检验

1. 建立检验假设，确定检验水准

H0: 差值的总体中位数等于0 H1: 差值的总体中位数不等于0 α =0.05

2. 计算检验统计量T值

3. 求差值d

4. 编秩:依差值的绝对值由小到大编秩 ; 差值为0，不编秩，且总的对子数相应减少;差值的绝对值相等，称为相持，取平均秩。

5. 分别求正、负秩和 T+=43.5，T­=11.5

6. 确定统计量T :T=43.5或T=11.5

7. 确定P值，做出推断

(1) 查表法(n≤50)

(2) 正态近似法(n>50):作正态近似检验

## 二、两组独立样本比较的秩和检验（Wilcoxon rank sum test）

### 2.1 两组连续型变量资料的秩和检验

1.建立检验假设，确定检验水准

H0:两种药物杀灭钉螺死亡率的总体中位数相等
H1:两种药物杀灭钉螺死亡率的总体中位数不相等
α =0.05


2.计算检验统计量T值

1. 编秩:将两组数据混合，由小到大统一编秩;不同组遇到相同数 据取平均秩次。
2. 求各组秩和:以样本例数较小者为n1，其秩和为T1。
3. 确定检验统计量T值 : 若n1≠n2，则T=T1;若n1=n2，则T=T1或T=T2。

3.确定P值，做出推断

(1) 查表法

### 2.2 两组有序分类变量资料的秩和检验

1.建立检验假设，确定检验水准

H0:夏冬两个季节居民体内核黄素含量的总体分布位置相同
H1:夏冬两个季节居民体内核黄素含量的总体分布位置不同
α =0.05


2.计算检验统计量T值

(1) 编秩:将两组数据合并，按等级由小到大统一编秩。 先计算各等级合计数，并确定各等级秩次范围，求出各等级的平均秩次。

(1)求各组秩和:各等级的平均秩次分别乘以各组在各等级的例数，再求和，即得到各组秩和。

n1=40, n2=44, N=n1+n2=84 T1=16.5×10+48.5×14+74.5×16=2036 T2=16.5×22+48.5×18+74.5×4=1534


(3) 确定统计量T值 : T=T1=2036。

3.确定P值，做出推断

P<0.001。按照a = 0.05水准，拒绝H0，接受H1，故可认为夏冬两个 季节居民体内核黄素含量有差别。

## 三、多组独立样本比较的秩和检验

### 3.1多组独立样本比较的秩和检验（Kruskal­Wallis H检验）

1.建立检验假设，确定检验水准

H0:3种方法治疗后患者生存月数的总体中位数相等
H1:3种方法治疗后患者生存月数的总体中位数不全相等
α =0.05


2.计算检验统计量H值

1).编秩 将三组数据合并，其余步骤同两组定量变量资料 2).求各组秩和Ri

3). 确定检验统计量H值 :

3、确定P值，做出推断

(1) 查H界值表

• 当组数k=3，且各组例数ni≤5时，可查H界值表得到P值。
• P<0.05。按照a=0.05水准，拒绝H0，接受H1，故可认为3种方法治疗后胰腺癌患者的生存月数有差别。

(2) 查χ2界值表

### 3.2 有序变量多组独立样本的秩和检验

1.建立检验假设，确定检验水准

H0:3种治疗方法治疗效果的总体分布位置相同
H1:3种治疗方法治疗效果的总体分布位置不全相同
α =0.05


2.计算检验统计量H值

(1) 编秩 同两组有序分类变量资料

(2) 求各组秩和:各组各等级的频数与平均秩次的乘积之和。

R1 =32.5×24+96.5×26+183.5×72+358.5×186 = 83182
R2 =32.5×20+96.5×16+183.5×24+358.5×32 = 18070
R3 =32.5×20+96.5×22+183.5×14+358.5×22 = 13229


(3)计算检验统计量H值

c = 1­-[(643-­64)+(6433-­64)+(11033-­110)+(2403-­­240)]/(4783-­­478)=0.856 Hc=44.011/0.856=51.41

3.确定P值，做出推断

k=3，各组例数均大于5，可由 v =3-­1=2 查χ2界值表，得P<0.005。 按照 α = 0.05水准，拒绝H0，接受H1，故可认为3种方法治疗慢性喉 炎的效果有差别。

## 四、多个独立样本间的多重比较

1、建立检验假设，确定检验水准

H0: 第i种与第j种方法疗效的总体分布位置相同
H1: 第i种与第j种方法疗效的总体分布位置不同
α =0.05


2、计算检验统计量 t 值

(1)求各组平均秩次 Ri

(2)列出两两比较计算表，求得 t 值

3、确定P值，做出推断

## 总结

• 非参数检验是不依赖总体分布类型，也不对总体参数进行 推断的一类统计方法。
• 非参数检验不受总体分布的限制，适用范围广，但对服从 参数检验条件的资料采用非参数检验进行分析时，会降低 检验效能，增加犯II类错误的概率。

1. 总体分布类型不明
2. 总体分布呈偏态分布
3. 数据一端或两端有不确定值的资料
4. 总体方差不齐
5. 有序分类变量资料
• 秩和检验是将原数据转换为秩次，比较各组秩和的非参数检验。
• 有序分类变量资料选用非参数检验，可推断各等级强度的 差别，而用R×C列联表χ2检验，只能比较频数分布之间的差别。

## 讨论

### 一、算法：

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.

Rankref=133.5Rankref=133.5
Rankalt=76.5Rankalt=76.5


### 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:

z=U−muuz=U−muu


Breaking this equation down:

z=z−scorez=z−score


U=lowest of the U scores calculated in previous stepsU=lowest of the U scores calculated in previous steps

$$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=21.5U=21.5

mu=10∗102=50mu=10∗102=50

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


So altogether we have:

z=21.5−5013.229=−2.154z=21.5−5013.229=−2.154


### 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的具体实现

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，即两个群组的均值统计相等。