2013-12-10 30 views
5

我正在嘗試使用scipy.stats.chisquare。我建立了一個玩具例子:Python scipy chisquare返回的值不同於R 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 

我在做什麼錯?

由於

回答

9

對於本chisq.test呼叫蟒當量是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)