Skip to content

Instantly share code, notes, and snippets.

@seekshreyas
Forked from chums2020/retroDesign.py
Created June 22, 2020 05:26
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 seekshreyas/5d47a4e9c245db70008b3de6ba9658a0 to your computer and use it in GitHub Desktop.
Save seekshreyas/5d47a4e9c245db70008b3de6ba9658a0 to your computer and use it in GitHub Desktop.
Translates Gelman's retrodesign function from R to Python
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