Skip to content

Instantly share code, notes, and snippets.

@ljwolf
Created October 13, 2016 20:00
Show Gist options
  • Save ljwolf/acc7a13676843c8e0d8bcb1d0d5e0542 to your computer and use it in GitHub Desktop.
Save ljwolf/acc7a13676843c8e0d8bcb1d0d5e0542 to your computer and use it in GitHub Desktop.
retrodesign.py, from Gelman & Carlin (2014), Beyond Power Calculations.
import numpy as np
import scipy.stats as st
import warnings
def retrodesign(theta, se, alpha=.05 , df=1, n_sims = 10000):
"""
Implements the "retrodesign" function from Gelman \& Carlin (2014).
This computes the power, Sign error rate, and exaggeration ratio.
Arguments
----------
theta : float
true effect size
se : float
standard error of effect estimator sampling distribution
alpha : float
error rate for the analysis
df : int
degrees of freedom in the estimate
n_sims : int
number of simulations to conduct to estimate the exaggeration ratio
"""
if not np.isfinite(df) and df > 0:
warn("SciPy's 't' distribution does not support infinite degrees of freedom. Setting sufficiently large")
df = 1000000
if df < 1:
raise ValueError('Degrees of freedom is less than 1.')
z = st.t.ppf(1 - alpha/2, df=df)
upper = 1 - st.t.cdf(z - theta/se, df=df)
lower = st.t.cdf(-z - theta/se, df=df)
power = upper + lower
type_S = lower / power
simulations = theta + se*st.t.rvs(df=df, size=n_sims)
sig_filter= np.abs(simulations) > se*z
sig_mags = np.abs(simulations[sig_filter])
exaggeration = sig_mags.mean() / theta
return power, type_S, exaggeration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment