我正在尝试通过我的数据来计算kendall系数(w(。任何人都知道像R(http://cc.oulu.fi/~jarioksa/softhelp/softhelp/vegan/html/kendall.global.html(一样,在Python软件包中实现的功能测试?
肯德尔的W并不难计算,但是我找不到允许将其与排列测试相结合的python函数。
请注意:我要感谢鲍里斯(Boris(在此代码中找到错误。在计算S
的线路中,i无意间乘以m
而不是n
。
我也不知道一个。但是,您可以以这种方式计算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 = n*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')
在这种情况下,结果是
Calculated value of W: 0.575 exceeds permutation values in 55 out of 1000 cases.
您还应注意,由于这些是随机排列,因此报告的值数量会有所变化。例如,在我进行的一次试验中,我认为计算值为0.575的值仅超过1000例中的48个。
如果有'm'评估者和'n'项目,是否应该是乘以'n'而不是's'm'的乘法。
S = n*np.var(rating_sums)
我相信这是没有注意到的,因为您在示例中使用了4个评分者和4个项目,因此" m = n"。我注意到是因为我正在使用此代码并获得一个超过一个代码。