lok*_*art 3 python r chi-squared p-value
作为普通的R用户,我正在学习使用python进行分析,我从卡方开始,并做了以下工作:
> 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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
对于test1,我很满意,因为python和R的测试显示了相似的结果,但test2事实并非如此,因为R具有参数correct,所以我将其更改为默认值,并且生成的p值不相同。
我的代码有什么问题吗?我应该“相信”哪一个?
感谢您的反馈。我知道不应该对值小于5的单元格使用卡方检验,而应该使用fisher精确检验,我担心的是为什么R和Python给出如此巨大差异的p值。
除了单元数小于5的问题外,根据我的经验,R和Python统计测试的实现通常都默认启用了各种更正(应该在基本方法上进行了改进)。关闭校正似乎会使scipyp值与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]]))
Run Code Online (Sandbox Code Playgroud)
这适用于t检验等,默认情况下可能会或可能不会假设均等方差。基本上,每当您在统计软件之间无法匹配输出时,就开始查看默认参数以查看是否应启用或禁用这些调整。