python t检验显著差异_Python中t检验的置信区间(均值之间的差异)

1586010002-jmsa.png

I am looking for a quick way to get the t-test confidence interval in Python for the difference between means. Similar to this in R:

X1 <- rnorm(n = 10, mean = 50, sd = 10)

X2 <- rnorm(n = 200, mean = 35, sd = 14)

# the scenario is similar to my data

t_res <- t.test(X1, X2, alternative = 'two.sided', var.equal = FALSE)

t_res

Out:

Welch Two Sample t-test

data: X1 and X2

t = 1.6585, df = 10.036, p-value = 0.1281

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

-2.539749 17.355816

sample estimates:

mean of x mean of y

43.20514 35.79711

Next:

>> print(c(t_res$conf.int[1], t_res$conf.int[2]))

[1] -2.539749 17.355816

I am not really finding anything similar in either statsmodels or scipy, which is strange, considering the importance of significance intervals in hypothesis testing (and how much criticism the practice of reporting only the p-values recently got).

解决方案

Here how to use StatsModels' CompareMeans to calculate the confidence interval for the difference between means:

import numpy as np, statsmodels.stats.api as sms

X1, X2 = np.arange(10,21), np.arange(20,26.5,.5)

cm = sms.CompareMeans(sms.DescrStatsW(X1), sms.DescrStatsW(X2))

print cm.tconfint_diff(usevar='unequal')

Output is

(-10.414599391793885, -5.5854006082061138)

and matches R:

> X1 <- seq(10,20)

> X2 <- seq(20,26,.5)

> t.test(X1, X2)

Welch Two Sample t-test

data: X1 and X2

t = -7.0391, df = 15.58, p-value = 3.247e-06

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

-10.414599 -5.585401

sample estimates:

mean of x mean of y

15 23

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值