Python中的Kendall协调系数(W)

2024-06-07 09:34:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从我的数据中计算出肯德尔和谐系数。有人知道在Python包中实现的函数,比如R(http://cc.oulu.fi/~jarioksa/softhelp/vegan/html/kendall.global.html)的“vegan”包中实现的函数,包括置换测试吗?在

Kendall的W不难计算,但我找不到一个Python函数来将它与置换测试结合起来。在


Tags: 数据函数httphtmlglobalficckendall
2条回答

如果有“m”评分器和“n”项,不应该是“n”的乘法而不是“S”中的“m”?在

S = n*np.var(rating_sums)

我相信它没有被注意到,因为你在例子中使用了4个评分者和4个项目,所以'm=n'。我注意到了,因为我使用了这个代码,并且得到了大于1的值。在

我也不知道。但是,您可以用这种方法在Python中计算置换测试。请注意,我没有在公式“W”中包括对捆绑值的修正。在

import numpy as np

def kendall_w(expt_ratings):
    if expt_ratings.ndim!=2:
        raise 'ratings matrix must be 2-dimensional'
    m = expt_ratings.shape[0] #raters
    n = expt_ratings.shape[1] # items rated
    denom = m**2*(n**3-n)
    rating_sums = np.sum(expt_ratings, axis=0)
    S = m*np.var(rating_sums)
    return 12*S/denom

the_ratings = np.array([[1,2,3,4],[2,1,3,4],[1,3,2,4],[1,3,4,2]])
m = the_ratings.shape[0]
n = the_ratings.shape[1]

W = kendall_w(the_ratings)

count = 0
for trial in range(1000):
    perm_trial = []
    for _ in range(m):
        perm_trial.append(list(np.random.permutation(range(1, 1+n))))
    count += 1 if kendall_w(np.array(perm_trial)) > W else 0

print ('Calculated value of W:', W, ' exceeds permutation values in', count, 'out of 1000 cases')

在这种情况下,结果是

^{pr2}$

您还应该注意,由于这些是随机排列,因此报告的值的数量会有一些变化。例如,在我做的一个试验中,我认为0.575的计算值只超过了1000个病例中的48个。在

相关问题 更多 >

    热门问题