加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

为什么Python中的t-test(scipy,statsmodels)会给出与R,Stata或Ex

发布时间:2020-12-16 21:43:30 所属栏目:Python 来源:网络整理
导读:(问题已解决; x,y和s1,s2的大小不同) 在R: x 在STATA和Excel中获得相同的数字 t.test(x,y,alternative="less")t = -1.6229,p-value = 0.05758 无论我尝试哪种选项,我都无法使用statsmodels.stats.weightstats.ttest_ind或scipy.stats.ttest_ind复制相同的结

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

在R:

x <- c(373,398,245,272,238,241,134,410,158,125,198,252,577,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,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,equal_var=False)
(array(-1.8912081781378338),0.066740317997990892)

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

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

最佳答案
这是我得到的结果,默认等于var:

>>> x_ = (373,260)
>>> y_ = (411,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)

和不等var:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读