26

在 R 中,有一个非常有用的函数可以帮助确定双边 t 检验的参数,以获得目标统计功效。

该函数被调用power.prop.test

http://stat.ethz.ch/R-manual/R-patched/library/stats/html/power.prop.test.html

您可以使用以下方法调用它:

power.prop.test(p1 = .50, p2 = .75, power = .90)

它会告诉您获得这种功效所需的样本量。这对于阻止测试的样本量非常有用。

scipy包中是否有类似的功能?

4

4 回答 4

27

我已经设法使用下面的 n 公式和norm.isfscipy.stats中的逆生存函数来复制该函数

在此处输入图像描述

from scipy.stats import norm, zscore

def sample_power_probtest(p1, p2, power=0.8, sig=0.05):
    z = norm.isf([sig/2]) #two-sided t test
    zp = -1 * norm.isf([power]) 
    d = (p1-p2)
    s =2*((p1+p2) /2)*(1-((p1+p2) /2))
    n = s * ((zp + z)**2) / (d**2)
    return int(round(n[0]))

def sample_power_difftest(d, s, power=0.8, sig=0.05):
    z = norm.isf([sig/2])
    zp = -1 * norm.isf([power])
    n = s * ((zp + z)**2) / (d**2)
    return int(round(n[0]))

if __name__ == '__main__':

    n = sample_power_probtest(0.1, 0.11, power=0.8, sig=0.05)
    print n  #14752

    n = sample_power_difftest(0.1, 0.5, power=0.8, sig=0.05)
    print n  #392
于 2013-03-05T08:47:15.690 回答
16

一些基本的功率计算现在可以在 statsmodels 中使用

http://statsmodels.sourceforge.net/devel/stats.html#power-and-sample-size-calculations http://jpktd.blogspot.ca/2013/03/statistical-power-in-statsmodels.html

该博客文章尚未考虑对 statsmodels 代码的最新更改。此外,我还没有决定提供多少包装函数,因为许多功率计算只是减少到基本分布。

>>> import statsmodels.stats.api as sms
>>> es = sms.proportion_effectsize(0.5, 0.75)
>>> sms.NormalIndPower().solve_power(es, power=0.9, alpha=0.05, ratio=1)
76.652940372066908

在 R 统计中

> power.prop.test(p1 = .50, p2 = .75, power = .90)

     Two-sample comparison of proportions power calculation 

              n = 76.7069301141077
             p1 = 0.5
             p2 = 0.75
      sig.level = 0.05
          power = 0.9
    alternative = two.sided

 NOTE: n is number in *each* group 

使用 R 的pwr

> library(pwr)
> h<-ES.h(0.5,0.75)
> pwr.2p.test(h=h, power=0.9, sig.level=0.05)

     Difference of proportion power calculation for binomial distribution (arcsine transformation) 

              h = 0.5235987755982985
              n = 76.6529406106181
      sig.level = 0.05
          power = 0.9
    alternative = two.sided

 NOTE: same sample sizes 
于 2013-08-22T11:47:50.353 回答
9

Matt's answer for getting the needed n (per group) is almost right, but there is a small error.

Given d (difference in means), s (standard deviation), sig (significance level, typically .05), and power (typically .80), the formula for calculating the number of observations per group is:

n= (2s^2 * ((z_(sig/2) + z_power)^2) / (d^2)

As you can see in his formula, he has

n = s * ((zp + z)**2) / (d**2)

the "s" part is wrong. a correct function that reproduces r's functionality is:

def sample_power_difftest(d, s, power=0.8, sig=0.05):
    z = norm.isf([sig/2]) 
    zp = -1 * norm.isf([power])
    n = (2*(s**2)) * ((zp + z)**2) / (d**2)
    return int(round(n[0]))

Hope this helps.

于 2013-08-20T20:25:15.750 回答
6

你还有:

from statsmodels.stats.power import tt_ind_solve_power

并在您想要获得的值中输入“无”。例如,要获得 effect_size = 0.1、power = 0.8 等情况下的观察次数,您应该输入:

tt_ind_solve_power(effect_size=0.1, nobs1 = None, alpha=0.05, power=0.8, ratio=1, alternative='two-sided')

并获得: 1570.7330663315456 作为所需的观察次数。或者,要获得在其他值固定的情况下可以获得的功率:

tt_ind_solve_power(effect_size= 0.2, nobs1 = 200, alpha=0.05, power=None, ratio=1, alternative='two-sided')

你得到:0.5140816347005553

于 2018-10-26T14:00:32.763 回答