gpt4 book ai didi

Python scipy chisquare 返回与 R chisquare 不同的值

转载 作者:太空狗 更新时间:2023-10-30 00:45:14 25 4
gpt4 key购买 nike

我正在尝试使用 scipy.stats.chisquare。我已经构建了一个玩具示例:

In [1]: import scipy.stats as sps

In [2]: import numpy as np

In [3]: sps.chisquare(np.array([38,27,23,17,11,4]), np.array([98, 100, 80, 85,60,23]))
Out[11]: (240.74951271813072, 5.302429887719704e-50)

R 中的相同示例返回:

> chisq.test(matrix(c(38,27,23,17,11,4,98,100,80,85,60,23), ncol=2))

Pearson's Chi-squared test

data: matrix(c(38, 27, 23, 17, 11, 4, 98, 100, 80, 85, 60, 23), ncol = 2)
X-squared = 7.0762, df = 5, p-value = 0.215

我做错了什么?

谢谢

最佳答案

对于这个 chisq.test 调用 python 等价物是 chi2_contingency :

This function computes the chi-square statistic and p-value for the hypothesis test of independence of the observed frequencies in the contingency table observed.

>>> arr = np.array([38,27,23,17,11,4,98,100,80,85,60,23]).reshape(2,-1)
>>> arr
array([[ 38, 27, 23, 17, 11, 4],
[ 98, 100, 80, 85, 60, 23]])
>>> chi2, p, dof, expected = scipy.stats.chi2_contingency(arr)
>>> chi2, p, dof
(7.0762165124844367, 0.21503342516989818, 5)

关于Python scipy chisquare 返回与 R chisquare 不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20492419/

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