负样本大小 statsmodels.TTestIndPower().solve_power

Negative sample size by statsmodels.TTestIndPower().solve_power

我正在使用来自 statsmodels 的 TTestIndPower 的 solve_power 方法来解决所需的保留大小。通常,我们用它来解决处理大小。但是这里我需要解决控制大小,剩下的人口将被处理。 statsmodels 在这种情况下似乎效果不佳,因为它 returns 样本量为负,而所需样本量应为 210。

from statsmodels.stats.power import TTestIndPower

effect_size = 0.2
alpha = 0.05
power = 0.8
total_size = 3617

ideal_holdout_size = math.ceil(total_size - 
                               TTestIndPower().solve_power(effect_size=effect_size, nobs1=None, alpha=alpha, power=power, 
                                                                             ratio=(total_size - nobs1) / nobs1, alternative='two-sided'))

print(f'Out of total {total_size} stores, the ideal holdout size is: {ideal_holdout_size}')

这是上面代码的结果。

有人可以帮忙解决这个问题吗?谢谢!

无法使用 solve_power 解决此设置中的 nobs1,因为在这种情况下 nobs1 和 nobs_ratio 都需要更改。 solve_power 只能搜索一个给定其他关键字的值。

需要更改用于求根的函数,以便它针对给定的总大小求解 nobs1。 在这种情况下,根 (*) 可能并不总是存在,或者可能存在多个根。

在下文中,我检查幂在 nobs1 的相关范围内是单调的,并且它包括在此 nobs1 范围内的所需幂 0.8。 然后我们可以使用 scipy rootfinder 来解决 nobs1.

nobs1 = np.arange(100, 500, 10)
pow_ = TTestIndPower().power(effect_size=effect_size, nobs1=nobs1, alpha=alpha,
                             ratio=(total_size - nobs1) / nobs1, alternative='two-sided')
array([0.50469817, 0.54182567, 0.57681571, 0.60967519, 0.64043692,
       0.6691538 , 0.69589402, 0.72073695, 0.74376976, 0.76508462,
       0.78477648, 0.80294111, 0.81967375, 0.83506793, 0.84921453,
       0.86220121, 0.87411189, 0.88502644, 0.89502052, 0.90416541,
       0.91252805, 0.92017113, 0.92715306, 0.93352823, 0.93934712,
       0.94465644, 0.9494994 , 0.95391587, 0.95794252, 0.96161315,
       0.96495878, 0.96800784, 0.97078643, 0.97331846, 0.97562573,
       0.97772825, 0.97964426, 0.98139039, 0.98298186, 0.9844325 ])

from scipy import optimize
def power_func(nobs1):
    pow_ = TTestIndPower().power(effect_size=effect_size, nobs1=nobs1, alpha=alpha,
                                 ratio=(total_size - nobs1) / nobs1, alternative='two-sided')
    return pow_ - 0.8

optimize.brentq(power_func, 100, 500)
208.32446507391262

(*) 作为快速检查总样本量是否足够大以达到所需的功效,我们可以在 nobs_ratio 等于 1 时计算所需的样本量。使用相同的样本量将具有固定总大小的最大功效。

在示例中,我们至少需要 787 的总样本量才能达到所需的 0.8 功效。

nobs1 = TTestIndPower().solve_power(effect_size=effect_size, nobs1=None, alpha=alpha, power=power, 
                                   ratio=1, alternative='two-sided')
nobs1, 2 * nobs1
(393.4056989990322, 786.8113979980644)

(旁白:我从未在文献中或其他包中见过这个用例,因此 statsmodels 样本大小求解器不是为这种情况设计的。)