我需要计算cohen's d来确定实验的效果大小。在我可以使用的声音库中是否有任何实现?如果不是,什么是一个好的实现?

最佳答案

在两组大小相等的特殊情况下,上述实现是正确的。根据WikipediaRobert Coe's article中的公式得出的更通用的解决方案是下面所示的第二种方法。注意分母是集合标准差,通常只有当两组的总体标准差相等时才适用:

from numpy import std, mean, sqrt

#correct if the population S.D. is expected to be equal for the two groups.
def cohen_d(x,y):
    nx = len(x)
    ny = len(y)
    dof = nx + ny - 2
    return (mean(x) - mean(y)) / sqrt(((nx-1)*std(x, ddof=1) ** 2 + (ny-1)*std(y, ddof=1) ** 2) / dof)

#dummy data
x = [2,4,7,3,7,35,8,9]
y = [i*2 for i in x]
# extra element so that two group sizes are not equal.
x.append(10)

#correct only if nx=ny
d = (mean(x) - mean(y)) / sqrt((std(x, ddof=1) ** 2 + std(y, ddof=1) ** 2) / 2.0)
print ("d by the 1st method = " + str(d))
if (len(x) != len(y)):
    print("The first method is incorrect because nx is not equal to ny.")

#correct for more general case including nx !=ny
print ("d by the more general 2nd method = " + str(cohen_d(x,y)))

输出将是:
d按第1种方法=-0.559662109472
第一个方法不正确,因为nx不等于ny。
D采用更一般的第二种方法=-0.572015604666

关于python - 如何在Python中计算cohen的d?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21532471/

10-09 16:45