R 和 Python 之间的不同 t 检验 p 值

Different t-test pvalues between R and Python

我目前是 python 新手,正在尝试了解有关倾向得分匹配的更多信息。我从 Stanford.edu 找到了一个很棒的教程(因为这是我的第一个 post 堆栈溢出不会让我 post 两个链接但是 google 斯坦福倾向得分匹配)涵盖了这个.我的目标是在 python 中重新创建这一切并了解正在发生的事情。

我的问题是当我进入第 1.2 节均值差异:预处理协变量并开始 运行 t 检验时。我不明白为什么相同测试和相同数据的 R 和 Python 之间的 p 值如此不同。

R代码: with(ecls, t.test(race_white ~ catholic, var.equal=FALSE))

R输出:

Welch Two Sample t-test

data:  race_white by catholic
t = -13.453, df = 2143.3, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.1936817 -0.1444003
sample estimates:
mean in group 0 mean in group 1 
      0.5561246       0.7251656

当我在 python 中执行相同的思考时,我的 t-stat 和自由度是相同的,但我的 p 值相差甚远。

Python代码:

cath=dat[dat['catholic']==1]['race_white']
noncath=dat[dat['catholic']==0]['race_white']
fina =sms.ttest_ind(noncath,cath,alternative='two-sided', usevar='unequal')
print(fina)
print("The t-statistic is %.3f the p-value is %.3f and the df is %.3f"%fina) 

Python 输出: (-13.45342570302274, 1.1413329198468439e-39, 2143.2902027156415) The t-statistic is -13.453 the p-value is 0.000 and the df is 2143.290'

我使用的是完全相同的数据集,只是无法弄清楚为什么两者不同。我在另一个类似的 SO 主题中看到,但他们的结论是尺寸不同。这是使用相同的数据集,因此大小没有不同。

可在 here 中找到用于 python 和 R 的数据文件 (ecls.csv) 的数据文件。关于为什么 p 值不同的任何帮助非常感谢此 t 检验。

R 不会打印低于 2.2e-16 的 p 值,但会计算并存储它们。为您的 R 代码试试这个:

with(ecls, t.test(race_white ~ catholic, var.equal=FALSE))$p.value
[1] 1.141333e-39

该值实际上为零,这就是为什么当您使用 Python 将其打印到小数点后 3 位时,您会看到 0.000。尝试在 Python 中打印未修改的 p 值(不要使用 %.3f - 事实上你已经这样做了!print(fina))我希望你会看到与 R 相同的值(事实上​​你知道!)