有人可以帮助我使用 scipy.stats.chisquare 吗?我没有统计/数学背景,我正在使用来自https://en.wikipedia.org/wiki/Chi-squared_test 的数据集学习 scipy.stats.chisquare
维基百科文章以下表为例,说明基于它的卡方值约为24.6。我将使用 scipy.stats 来验证这个值并计算相关的 p 值。
我在这里找到了最有可能帮助我的公式解决方案
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.chisquare.html
由于我是统计学的新手,以及 scipy.stats.chisquare 的使用,我只是不确定最好的方法,以及如何最好地将提供的表中的数据输入到数组中,以及是否提供预期值?来自维基百科。
该数据是列联表。SciPy 具有将scipy.stats.chi2_contingency卡方检验应用于列联表的功能。它基本上只是一个规则卡方检验,但是当应用于列联表时,预期频率是在独立性假设下计算的(chi2_contingency为您做这件事),并且自由度取决于行数和列数(chi2_contingency也会为你计算这个)。
以下是将卡方检验应用于该表的方法:
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)
Run Code Online (Sandbox Code Playgroud)
输出:
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]]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1556 次 |
| 最近记录: |