Last active
January 6, 2023 15:42
-
-
Save martinctc/ba9f84f25a23a34f310f89e08e4c95a1 to your computer and use it in GitHub Desktop.
[Power analysis with python] #python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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