Python – Python Package for Power Analysis Calculator

pythonstatistical-power

I would like to use the following power analysis formula into a python notebook

https://clincalc.com/stats/samplesize.aspx

Any idea which python package would replicate the same results ?

Edit:

I tried the following function from statsmodel module but can't seem to get anything close to the numbers in the webpage. Also , the formula in the webpage doesn't require a standard deviation . Why ?

statsmodels.stats.power.tt_ind_solve_power¶

Best Answer

I think what's confusing you here is that the power function in the statsmodels in Python takes as an input Cohen's d for the effect size. Cohen's d scales the effect size in terms of pooled standard deviation. For your problem, you need to add the variance of the control and sample groups divided by two and then take the square root. This gives you Cohen's d.

I've modified the code from here to address you problem. You can change the input to 'two.sided' instead of 'larger' to see the required sample for the two-sided test.

p_c = 0.01
p_t = 0.0105

v_c = p_c * (1 - p_c)
v_t = p_t * (1 - p_t)
  
# calculate the pooled standard deviation 
# Cohen's d)
s = sqrt((v_c + v_t) / 2)
  
# calculate the effect size
d = (p_t - p_c) / s
print(f'Effect size: {d}')
  
# factors for power analysis
alpha = 0.05
power = 0.8
  
# perform power analysis to find sample size 
# for given effect
obj = TTestIndPower()
n = obj.solve_power(effect_size=d, alpha=alpha, power=power, 
                    ratio=1, alternative='larger')
  
print('Sample size/Number needed in each group: {:.3f}'.format(n))

Again, you'll see that the required sample size is 501771.

Related Question