【问题标题】:Python scipy chisquare returns different values than R chisquarePython scipy chisquare 返回与 R chisquare 不同的值
【发布时间】:2013-12-10 10:50:01
【问题描述】:

我正在尝试使用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

我做错了什么?

谢谢

【问题讨论】:

    标签: python r numpy scipy chi-squared


    【解决方案1】:

    对于这个chisq.test,python 等效调用是chi2_contingency

    此函数计算观察频率 in the contingency table 观察到的独立性假设检验的卡方统计量和 p 值。

    >>> 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)
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 2012-05-09
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多