<p>cochrans_q采用二进制数据,不进行计数</p>
<p>在statsmodels中,文档通常不是很明确,但是可以从单元测试中看到预期的行为</p>
<p>下面的单元测试演示如何将频率数据转换为statsmodels所需的格式</p>
<p>资料来源:<a href="https://github.com/statsmodels/statsmodels/blob/master/statsmodels/stats/tests/test_nonparametric.py#L190" rel="nofollow noreferrer">https://github.com/statsmodels/statsmodels/blob/master/statsmodels/stats/tests/test_nonparametric.py#L190</a></p>
<pre><code>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)
</code></pre>