Skip to content

Instantly share code, notes, and snippets.

@g-leech g-leech/effect_sizes.py
Last active Mar 21, 2020

Embed
What would you like to do?
import math
# assumes bivariate normal, dichotomised groups
def dichotomy_r_to_d(r) :
d = 2*r / (math.sqrt(1 - r**2))
return d
# Equation 9
# https://sci-hub.tw/10.1037/1082-989X.11.4.386
def r_to_d(r, n1, n2) :
N = n1 + n2
p1 = n1 / N
p2 = n2 / N
d = r / math.sqrt( (1 - r**2) * p1*p2 )
return d
# r.Lakens = CohenDs / sqrt(CohenDs^2 + (N^2 - 2*N)/(n1*n2)
def cohens_d(experimentalMean, controlMean, \
experimentalSd, controlSd, \
experimentalN, controlN, \
correctForBias=True) :
sd = pooled_sd(experimentalSd, controlSd)
d = (experimentalMean - controlMean) / sd
if correctForBias :
N = experimentalN + controlN
correction = ( (N - 3)/(N - 2.25) ) \
* math.sqrt( (N-2)/N )
d = d * correction
return d
def pooled_sd(sd1, sd2) :
sumSquaredSigma = sd1**2 + sd2**2
return math.sqrt(sumSquaredSigma/2)
# Correction from Lakens
# https://www.frontiersin.org/articles/10.3389/fpsyg.2013.00863/full
def hedges_g(experimentalMean, controlMean, \
experimentalSd, controlSd, \
experimentalN, controlN) :
dof = experimentalN + controlN
approxCorrection = 1 - ( 3 / (4*dof - 9) )
d = cohens_d(experimentalMean, controlMean, \
experimentalSd, controlSd, \
correctForBias=True)
return d * approxCorrection
# Elderly priming
doyen = { "primed_mean" : 6.27, \
"primed_sd" : 2.15,\
"primed_n" : 60,\
"control_mean" : 6.39,\
"control_sd" : 1.11,\
"control_n" : 60,
}
cohens_d(doyen["primed_mean"], doyen["control_mean"], \
doyen["primed_sd"], doyen["control_sd"], \
doyen["primed_n"], doyen["control_n"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.