R:如何使用bpower函数计算双样本二项式测试功率

时间:2015-06-18 20:55:33

标签: r statistics

library(Hmisc)
#10% difference
n1 = 30
n2 = 30
n = 60
p1 = seq(0.1, 0.9, 0.1)
p2 = p1 + 0.1
> bpower(p1, p2, n, n1, n2, alpha = 0.05)
   Power1    Power2    Power3    Power4    Power5    Power6    Power7    Power8    Power9 
0.9997976 0.9992461 0.9933829 0.9670958 0.8995984 0.7799309 0.6141349 0.4211642 0.2252629 



#20% difference
n1 = 30
n2 = 30
n = 60
p1 = seq(0.1, 0.8, 0.1)
p2 = p1 + 0.2
> bpower(p1, p2, n, n1, n2, alpha = 0.05)
   Power1    Power2    Power3    Power4    Power5    Power6    Power7     Power8 
0.9997976 0.9992461 0.9933829 0.9670958 0.8995984 0.7799309 0.6141349 0.4211642 

这里我使用bpower中的Hmisc函数来计算双样本二项式检验的功效。我的假设是:H0:p1 = p2对H1:p1!= p2。在第一种情况下,样本比例相差0.1(即p2-p1 = 0.1),而在第二种情况下,样本比例相差0.2(p2-p1 = 0.2)。但是,当我计算两种情况的功率时,值是完全相同的?我在代码中犯了错误吗?

1 个答案:

答案 0 :(得分:1)

该功能的签名是

args(bpower)
# function (p1, p2, odds.ratio, percent.reduction, n, n1, n2, alpha = 0.05) 

因此,如果未命名,则第三个参数将被解释为优势比。是的,你在代码中犯了一个错误。您应该明确命名参数以避免此问题。

bpower(p1, p2, n=n, n1=n1, n2=n2, alpha = 0.05)

使用它,我得到了

# diff 0.1
Power1    Power2    Power3    Power4 ..
0.1893951 0.1437292 0.1268406 0.1204777 ...

# diff 0.2
Power1    Power2    Power3    Power4   ...
0.4903583 0.3912451 0.3495370 0.3376908