理解 scipy.stats.chisquare

understanding scipy.stats.chisquare

有人可以帮我 scipy.stats.chisquare 吗?我没有统计/数学背景,我正在学习 scipy.stats.chisquare 使用来自 https://en.wikipedia.org/wiki/Chi-squared_test

的数据集

维基百科文章以下面的table为例,说明基于它的卡方值约为24.6。我要用scipy.stats来验证这个值并计算相关的p值。

我找到了最有可能帮助我的公式解决方案

https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.chisquare.html

因为我是统计学的新手,而且 scipy.stats.chisquare 的使用我只是不确定最好的方法,以及如何最好地将提供的 table 中的数据输入数组,以及是否提供期望值?来自维基百科。

该数据是 contingency table. SciPy has the function scipy.stats.chi2_contingency 将 chi-square 测试应用于意外事件 table。它基本上只是一个规则 chi-square 测试,但是当应用于意外事件 table 时,预期频率是在独立假设下计算的(chi2_contingency 为您做的),并且度数自由度取决于行数和列数(chi2_contingency 也会为您计算)。

以下是如何将 chi-square 测试应用于 table:

import numpy as np
from scipy.stats import chi2_contingency


table = np.array([[90, 60, 104, 95],
                  [30, 50,  51, 20],
                  [30, 40,  45, 35]])

chi2, p, dof, expected = chi2_contingency(table)

print(f"chi2 statistic:     {chi2:.5g}")
print(f"p-value:            {p:.5g}")
print(f"degrees of freedom: {dof}")
print("expected frequencies:")
print(expected)

输出:

chi2 statistic:     24.571
p-value:            0.00040984
degrees of freedom: 6
expected frequencies:
[[ 80.53846154  80.53846154 107.38461538  80.53846154]
 [ 34.84615385  34.84615385  46.46153846  34.84615385]
 [ 34.61538462  34.61538462  46.15384615  34.61538462]]