【问题标题】:Kendall's coefficient of concordance (W) in PythonPython 中的 Kendall 一致性系数 (W)
【发布时间】:2018-07-31 07:30:22
【问题描述】:

我正在尝试根据我的数据计算 Kendall 一致性系数 (W)。有没有人知道在 Python 包中实现的函数,如 R (http://cc.oulu.fi/~jarioksa/softhelp/vegan/html/kendall.global.html) 的“vegan”包中的函数,包括置换测试?

Kendall 的 W 不难计算,但我找不到允许将其与置换测试结合的 Python 函数。

【问题讨论】:

    标签: python r function statistics


    【解决方案1】:

    请注意:感谢 Boris 在此代码中发现错误。在计算S 的行中,我无意中乘以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 个。

    【讨论】:

    • the_ratings 到底是什么,你是怎么得到它的?
    • @Boris:四个 S 分别被要求对四个项目进行排名。 the_ratings 是结果数据。 the_ratings 中的四个子列表中的每一个都是一个 S 的排名集合。
    • 所以所有子列表都以相同的顺序排列元素对吧? the_ratings[0][1] 对应 the_ratings[1][1]?
    • BelI 刚刚使用 exprratings = [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]] 进行了尝试,得到的测量值为 0.4。我认为可能有问题。
    • @Boris:非常感谢您的礼貌通知并在此问题上与我保持联系。我已经编辑了我的答案。
    【解决方案2】:

    如果有“m”个评分者和“n”个项目,不应该是乘以“n”而不是“S”中的“m”吗?

    S = n*np.var(rating_sums)
    

    我相信它没有引起注意,因为您在示例中使用了 4 个评估者和 4 个项目,所以“m = n”。我注意到是因为我正在使用此代码并获得超过一的值。

    【讨论】:

    • 是的,应该。我终于看到了这个。由于您似乎对 SO 不熟悉,因此您可能不知道作为对我的回答的评论会更好。那么我会早点看到它。感谢您对此的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 2018-04-02
    • 1970-01-01
    • 2023-03-31
    • 2019-12-09
    • 1970-01-01
    相关资源
    最近更新 更多