作为一个普通的R用户,我正在学习使用python进行分析,我从chi开始并做了以下工作:
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 incorrectPython
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有参数correct,所以我将它从默认情况下更改,生成的p值不一样。
我的代码有什么问题吗?我该相信哪一个?
更新01
感谢您的反馈。我知道,对于值小于5的单元格,不应该使用卡方检验,我应该使用fisher精确测试,我担心的是为什么R和Python给出的p值有这么大的差异。
发布于 2014-07-30 05:31:16
除了单元格数<5问题之外,根据我的经验,统计测试的R和Python实现通常都具有默认启用的各种校正(这些校正应该改进基本方法)。关闭校正似乎使scipy p值与R值匹配:
scipy.stats.chi2_contingency(np.array([[1, 2], [3, 4]]), correction=False)
Out[6]:
# p-val = 0.778159
(0.079365079365079388, 0.77815968617616582, 1, array([[ 1.2, 1.8],
[ 2.8, 4.2]]))这同样适用于t检验等,如果默认情况可能是或可能不是假设相同的方差。基本上,每当您在统计软件之间的输出匹配出现问题时,就开始查看默认参数,看看是否应该启用或禁用这些调整。
https://stackoverflow.com/questions/25028833
复制相似问题