首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检验的P值

检验的P值
EN

Stack Overflow用户
提问于 2020-04-01 20:29:58
回答 1查看 2.8K关注 0票数 2

我正在计算一个测试统计量,它是作为一个1自由度的卡方分布的。我还使用来自scipy.stats的两种不同技术来计算对应于此的P值。

我有观察值和期望值作为numpy数组。

代码语言:javascript
复制
observation = np.array([  9.21899399e-04,   4.04363991e-01,   3.51713820e-02,
         3.00816946e-03,   1.80976731e-03,   6.46172153e-02,
         8.61549065e-05,   9.41395390e-03,   1.00946008e-03,
         1.25621846e-02,   1.06806251e-02,   6.66856795e-03,
         2.67380732e-01,   0.00000000e+00,   1.60859798e-02,
         3.63681803e-01,   1.06230978e-05])

expectation = np.array([ 0.07043956,  0.07043956,  0.07043956,  0.07043956,  0.07043956,
        0.07043956,  0.07043956,  0.07043956,  0.07043956,  0.07043956,
        0.07043956,  0.07043956,  0.07043956,  0.07043956,  0.07043956,
        0.07043956,  0.07043956])

对于第一种方法,我提到了堆栈溢出帖子。以下是我在第一种方法中所做的工作:

代码语言:javascript
复制
from scipy import stats

chi_sq = np.sum(np.divide(np.square(observation - expectation), expectation)) 
p_value = 1 - stats.chi2.cdf(chi_sq, 1)

print(chi_sq, p_value)

>> (4.1029225303927959, 0.042809154353783851)

在第二种方法中,我使用了来自spicy.statsspicy.stats方法。更具体地说,我使用的是链接。这就是我如何实现第二种方法。

代码语言:javascript
复制
from scipy import stats
print( stats.chisquare(f_obs=observation, f_exp=expectation, ddof=0) )

>> Power_divergenceResult(statistic=4.1029225303927959, pvalue=0.99871467077385223)

在这两种方法(即statistic=4.1029225303927959)中,我得到相同的卡方统计量,但p-值不同。在第一种方法中,我得到了p_value=0.042809154353783851。在第二种方法中,我得到了pvalue=0.99871467077385223

,为什么我在这两种方法中没有得到相同的p值呢?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-02 12:13:18

对于stats.chisquare,ddof定义为

代码语言:javascript
复制
ddofint, optional
“Delta degrees of freedom”: adjustment to the degrees of freedom for the p-value. 
The p-value is computed using a chi-squared distribution with 
k - 1 - ddof degrees of freedom, 
where k is the number of observed frequencies. The default value of ddof is 0.

你所做的基本上是一个皮尔逊卡方检验,自由度是k-1,其中n是观察的数目。据我所见,你的期望基本上是观测值的平均值,这意味着你估计了一个参数,所以ddof在0是正确的。但是对于stats.chi2.cdf,df应该是16。

所以:

代码语言:javascript
复制
chi_sq = np.sum(np.divide(np.square(observation - expectation), expectation)) 
[1 - stats.chi2.cdf(chi_sq, len(observation)-1),
stats.chisquare(f_obs=observation, ddof=0)[1]]

[0.9987146707738522, 0.9987146706997099]

差别很小,但比例多少是正确的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60979770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档