我正在计算一个测试统计量,它是作为一个1自由度的卡方分布的。我还使用来自scipy.stats的两种不同技术来计算对应于此的P值。
我有观察值和期望值作为numpy数组。
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])对于第一种方法,我提到了这堆栈溢出帖子。以下是我在第一种方法中所做的工作:
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.stats的spicy.stats方法。更具体地说,我使用的是这链接。这就是我如何实现第二种方法。
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值呢?谢谢。
发布于 2020-04-02 12:13:18
对于stats.chisquare,ddof定义为
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。
所以:
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]差别很小,但比例多少是正确的。
https://stackoverflow.com/questions/60979770
复制相似问题