【问题标题】:Why does t-test in Python (scipy, statsmodels) give results different from R, Stata, or Excel?为什么 Python(scipy、statsmodels)中的 t-test 给出的结果与 R、Stata 或 Excel 不同?
【发布时间】:2014-01-09 16:51:35
【问题描述】:

(问题已解决;x,y 和 s1,s2 大小不同)

在 R 中:

x <- c(373,398,245,272,238,241,134,410,158,125,198,252,577,272,208,260)
y <- c(411,471,320,364,311,390,163,424,228,144,246,371,680,384,279,303)
t.test(x,y)
t = -1.6229, df = 29.727, p-value = 0.1152

在 STATA 和 Excel 中得到相同的数字

t.test(x,y,alternative="less")
t = -1.6229, df = 29.727, p-value = 0.05758

无论我尝试哪种选项,我都无法使用 statsmodels.stats.weightstats.ttest_ind 或 scipy.stats.ttest_ind 复制相同的结果。

statsmodels.stats.weightstats.ttest_ind(s1,s2,alternative="two-sided",usevar="unequal")
(-1.8912081781378358, 0.066740317997990656, 35.666557473974343)

scipy.stats.ttest_ind(s1,s2,equal_var=False)
(array(-1.8912081781378338), 0.066740317997990892)

scipy.stats.ttest_ind(s1,s2,equal_var=True)
(array(-1.8912081781378338), 0.066664507499812745)

一定有成千上万的人使用 Python 来计算 t-test。我们都得到不正确的结果吗? (我通常依赖 Python,但这次我使用 STATA 检查了我的结果)。

【问题讨论】:

  • 我刚刚运行了 stats.ttest_ind(x,y,equal_var=True) 并得到了数组 (-1.6229, 0.1152)。在您的示例中检查 s1/s2 == x/y。
  • 你使用的是什么版本的 scipy?
  • 进一步研究这个问题,我发现 R 给出 df = 29.727,而 Python 给出 df=35.666。所以我怀疑错误一定是由df计算引起的......沃伦,我仍然得到stats.ttest_ind(s1,s2,equal_var = True)(数组(-1.8912081781378338),0.066664507499812745)。我正在使用最新的 Enthought Canopy Python 安装。
  • 和@tnknepp 一样,我从ttest_ind(x, y, equal_var=True) 得到(array(-1.62292672368488), 0.11506840827144681)
  • 显示完整的python代码。也许s1s2 与R 示例中的xy 不同。

标签: python scipy statsmodels


【解决方案1】:

这是我得到的结果,默认等于 var:

>>> x_ = (373,398,245,272,238,241,134,410,158,125,198,252,577,272,208,260)
>>> y_ = (411,471,320,364,311,390,163,424,228,144,246,371,680,384,279,303)

>>> from scipy import stats
>>> stats.ttest_ind(x_, y_)
(array(-1.62292672368488), 0.11506840827144681)

>>> import statsmodels.api as sm
>>> sm.stats.ttest_ind(x_, y_)
(-1.6229267236848799, 0.11506840827144681, 30.0)

和不相等的变量:

>>> statsmodels.stats.weightstats.ttest_ind(x_, y_,alternative="two-sided",usevar="unequal")
(-1.6229267236848799, 0.11516398707890187, 29.727196553288369)
>>> stats.ttest_ind(x_, y_, equal_var=False)
(array(-1.62292672368488), 0.11516398707890187)

【讨论】:

    【解决方案2】:

    简短的回答是,Python 中提供的 t 检验与 R 和 Stata 中提供的结果相同,您只是在 Python 数组中多了一个元素。

    I wouldn't bank on Excel's robustness, however.

    【讨论】:

      猜你喜欢
      • 2021-03-14
      • 2020-08-30
      • 2018-06-04
      • 2016-05-06
      • 2015-07-30
      • 1970-01-01
      • 2018-09-16
      • 2020-10-15
      • 2018-12-01
      相关资源
      最近更新 更多