Created
March 17, 2019 09:51
-
-
Save chums2020/7dec9b864d96aeed3b76c747223c51d1 to your computer and use it in GitHub Desktop.
Translates Gelman's retrodesign function from R to 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
from scipy.stats import t | |
def retroDesign(A, s, alpha=0.05, df=1000000, n_sims=10000): | |
""" | |
:param A: the hypothesized true effect size | |
:param s: standard error | |
:param alpha: confidence level | |
:param df: degree of freedom | |
:param n_sims: number of simulations | |
:return: power, typeS, exaggeration | |
""" | |
z = t.ppf(1-alpha/2, df) | |
p_high = 1-t.cdf(z-A/s, df) | |
p_low = t.cdf(-z-A/s, df) | |
power = p_high + p_low | |
typeS = p_low/power | |
estimate = A + s*t.rvs(df, size=n_sims) | |
significant = [e for e in estimate if abs(e)>s*z] | |
exaggeration = sum([abs(x)/A for x in significant])/len(significant) | |
return power, typeS, exaggeration | |
if __name__ == '__main__': | |
# Example: true effect size of 0.1, standard error 3.28, alpha=0.05 | |
print(retroDesign(.1, 3.28)) | |
# Example: true effect size of 2, standard error 8.1, alpha=0.05 | |
print(retroDesign(2, 8.1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment