Skip to content

Instantly share code, notes, and snippets.

@martinctc
Last active January 6, 2023 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinctc/ba9f84f25a23a34f310f89e08e4c95a1 to your computer and use it in GitHub Desktop.
Save martinctc/ba9f84f25a23a34f310f89e08e4c95a1 to your computer and use it in GitHub Desktop.
[Power analysis with python] #python
# estimate sample size via power analysis
from statsmodels.stats.power import TTestIndPower
# parameters for power analysis
effect = 0.8
alpha = 0.05
power = 0.8
# perform power analysis
analysis = TTestIndPower()
result = analysis.solve_power(effect, power=power, nobs1=None, ratio=1.0, alpha=alpha)
print('Sample Size: %.3f' % result)
# calculate power curves for varying sample and effect size
from numpy import array
from matplotlib import pyplot
from statsmodels.stats.power import TTestIndPower
# parameters for power analysis
effect_sizes = array([0.2, 0.5, 0.8])
sample_sizes = array(range(5, 100))
# calculate power curves from multiple power analyses
analysis = TTestIndPower()
analysis.plot_power(dep_var='nobs', nobs=sample_sizes, effect_size=effect_sizes)
pyplot.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment