作为普通的R用户,我正在学习使用python进行分析,我从卡方开始,并做了以下工作:
[R
> chisq.test(matrix(c(10,20,30,40),nrow = 2))$p.value # test1
[1] 0.5040359
> chisq.test(matrix(c(1,2,3,4),nrow = 2))$p.value # test2
[1] 1
Warning message:
In chisq.test(matrix(c(1, 2, 3, 4), nrow = 2)) :
Chi-squared approximation may be incorrect
> chisq.test(matrix(c(1,2,3,4),nrow = 2),correct = FALSE)$p.value # test3
[1] 0.7781597
Warning message:
In chisq.test(matrix(c(1, 2, 3, 4), nrow = 2), correct = FALSE) :
Chi-squared approximation may be incorrect
Python
In [31]:
temp = scipy.stats.chi2_contingency(np.array([[10, 20], [30, 40]])) # test1
temp[1] # pvalue
Out[31]:
0.50403586645250464
In [30]:
temp = scipy.stats.chi2_contingency(np.array([[1, 2], [3, 4]])) # test2
temp[1] # pvalue
Out[30]:
0.67260381744151676
对于test1,我感到满意,因为来自python和R的测试显示了相似的结果,但test2并非如此,因为R的参数正确,所以我将其更改为默认值,并且生成的p值不相同.
我的代码有什么问题吗?我应该“相信”哪一个?
更新01
感谢您的反馈.我知道不应该对值小于5的单元格使用卡方检验,而应该使用fisher精确检验,我担心的是为什么R和Python给出如此巨大差异的p值.