总体比率的假设检验实际上是业界最常用也是最需要的检验,例如在ABtest中,检验两个实验的转化率是否有显著差异,则需要用到比率检验。本文介绍比率检验的原理,以及R语言的实现代码。
单总体比率的假设检验
前提条件:
- 样本取自两点分布 X ∼ B ( 1 , p ) X \sim B(1,p) X∼B(1,p)
- 样本量 n n n很大,能够满足 n p > 5 np>5 np>5 且 n ( 1 − p ) > 5 n(1-p)>5 n(1−p)>5
记要检验的原假设为为 H 0 : p = p 0 H_0: p=p_0 H0:p=p0,则样本比率 p ~ \widetilde{\mathrm{p}} p 服从方差为 p ( 1 − p ) / n p(1-p)/n p(1−p)/n的正态分布,对应标准化的检验统计量近似服从 N ( 0 , 1 ) N(0,1) N(0,1) :
u = n ( p ~ − p 0 ) p 0 ( 1 − p 0 ) \mathrm{u}=\frac{\sqrt{\mathrm{n}}\left(\widetilde{\mathrm{p}}-\mathrm{p}_{0}\right)}{\sqrt{\mathrm{p}_0\left(1-\mathrm{p}_0\right)}} u=p0(1−p0)n(p −p0)
实际上,当样本量很少时,需要采用精确的比率检验,即直接使用二项分布来检验,具体实现见下文的R代码。
两个总体比率的假设检验
检验前提条件:
- 两总体互相独立
- 变量都取自两点分布,即两总体服从二项分布
- 两总体且每类的样本量满足大于5的要求,从而能用正态分布来近似
那么可知:
( p ~ 1 − p ~ 2 ) − ( p 1 − p 2 ) p 1 ( 1 − p 1 ) n 1 + p 2 ( 1 − p 2 ) n 2 ≈ ( p ~ 1 − p ~ 2 ) − ( p 1 − p 2 ) p 1 ( 1 − p ~ 1 ) n 1 + p ~ 2 ( 1 − p ~ 2 ) n 2 ∼ N ( 0 , 1 ) \frac{\left(\widetilde{\mathrm{p}}_1-\widetilde{\mathrm{p}}_2\right)-\left(\mathrm{p}_1-\mathrm{p}_2\right)}{\frac{\mathrm{p}_1\left(1-\mathrm{p}_1\right)}{\mathrm{n}_1}+\frac{\mathrm{p}_2\left(1-\mathrm{p}_2\right)}{\mathrm{n}_2}} \approx \frac{\left(\widetilde{\mathrm{p}}_1-\widetilde{\mathrm{p}}_2\right)-\left(\mathrm{p}_1-\mathrm{p}_2\right)}{\frac{\mathbb{p}_1\left(1-\widetilde{\mathrm{p}}_1\right)}{\mathrm{n}_1}+\frac{\widetilde{\mathrm{p}}_2\left(1-\widetilde{\mathrm{p}}_2\right)}{\mathrm{n}_2}} \sim \mathrm{N}(0,1) n1p1(1−p1)