为什么这段代码中 statsmodel 库的 cochrans Q 测试输出没有变化?

Why is there no variation in output of cochrans Q test from statsmodel library in this code?

我正在使用以下代码:

from statsmodels.stats.contingency_tables import cochrans_q 
res = cochrans_q([[1,4,5],[9,6,8]])
print(res)

输出为:

df          2
pvalue      0.36787944117144245
statistic   2.0

但是,[[10,4,5],[9,6,8]][[55,88,77],[99,46,88]]

的输出保持不变

statsmodels 文档页面是 here and the Wikipedia page on Cochran's Q test is here

问题出在哪里,如何解决?感谢您的帮助。

以下是每个结果的定义:


df: In statistics, the degrees of freedom (DF) indicate the number of independent values that can vary in an analysis without breaking any constraints. It is an essential idea that appears in many contexts throughout statistics including hypothesis tests, probability distributions, and regression analysis.


pvalue: In statistics, the p-value is the probability of obtaining results as extreme as the observed results of a statistical hypothesis test, assuming that the null hypothesis is correct.


statistic: A statistic (singular) or sample statistic is any quantity computed from values in a sample that is used for a statistical purpose. Statistical purposes include estimating a population parameter, describing a sample, or evaluating a hypothesis.


如您所见,定义与每个元素的值无关,重要的是数量。

你的程序返回同样的东西
[[1,4,5],[9,6,8]][[10,4,5],[9,6,8]][[55,88,77],[99,46,88]]
因为每个列表中有 3 个元素。

Cochran's Q 检验是一种非参数 方法,用于查找三个或更多频率或比例的匹配组中的差异。这意味着在执行 Cochran 的 Q 检验时,每个元素的值无关紧要

cochrans_q取二进制数据不计数。

在 statsmodels 中,文档通常不是很明确,但可以从单元测试中看出预期的行为。

以下单元测试展示了如何将频率数据转换为 statsmodels 所需的格式。

来源:https://github.com/statsmodels/statsmodels/blob/master/statsmodels/stats/tests/test_nonparametric.py#L190

def test_cochransq3():
    # another example compared to SAS
    # in frequency weight format
    dt = [('A', 'S1'), ('B', 'S1'), ('C', 'S1'), ('count', int)]
    dta = np.array([('F', 'F', 'F', 6),
                    ('U', 'F', 'F', 2),
                    ('F', 'F', 'U', 16),
                    ('U', 'F', 'U', 4),
                    ('F', 'U', 'F', 2),
                    ('U', 'U', 'F', 6),
                    ('F', 'U', 'U', 4),
                    ('U', 'U', 'U', 6)], dt)

    cases = np.array([[0, 0, 0],
                      [1, 0, 0],
                      [0, 0, 1],
                      [1, 0, 1],
                      [0, 1, 0],
                      [1, 1, 0],
                      [0, 1, 1],
                      [1, 1, 1]])
    count = np.array([ 6,  2, 16,  4,  2,  6,  4,  6])
    data = np.repeat(cases, count, 0)

    res = cochrans_q(data)
    assert_allclose([res.statistic, res.pvalue], [8.4706, 0.0145], atol=5e-5)