gpt4 book ai didi

python - 使用 python chisquare 和使用卡方值表的不同结果

转载 作者:行者123 更新时间:2023-12-01 03:08:57 25 4
gpt4 key购买 nike

我需要通过卡方获得 p 值。我的程序是:

from scipy.stats import chisquare

c = chisquare([10,4,7,5],ddof=[0,1,2,3])

print(c)

结果是:

Power_divergenceResult(statistic=3.2307692307692308, pvalue=array([ 0.35739509,  0.19881419,  0.07226674,         nan]))

当我尝试使用卡方值表获取 p 值时(例如来自此站点 https://www.medcalc.org/manual/chi-square-table.php ),结果有所不同。在此示例中,使用自由度 = 1(ddof=0) 的 python p 值为 0.35739509,但使用表 p 值为 0.01。您能解释一下为什么结果不同吗?

最佳答案

函数chisquare执行Chi-squared hypothesis test但该表是关于 Chi-square distribution .

如果您想使用该发行版,您需要使用 scipy.stats.chi2 。特别是,要复制表中的值:

import scipy as sp

p = 0.1
df = 5

x = sp.stats.chi2.ppf(1-p, df=df)
print(x) # 9.23635689978

并获取给定 x 和自由度的 p 值:

p = 1 - sp.stats.chi2.cdf([10,4,7,5], df=[0,1,2,3])
print(p)
# [ nan 0.04550026 0.03019738 0.17179714]

请注意,该表将 p 定义为从 x 到无穷大的概率密度函数的积分。 scipy中的累积密度函数是从0到x的积分。因此,p = 1 - cdf

关于python - 使用 python chisquare 和使用卡方值表的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43086118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com