[英]Confidence Interval for t-test (difference between means) in Python
我正在尋找一種快速方法來獲取 Python 中的 t 檢驗置信區間,以了解均值之間的差異。 在 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
出去:
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
下一個:
>> print(c(t_res$conf.int[1], t_res$conf.int[2]))
[1] -2.539749 17.355816
我在 statsmodels 或 scipy 中都沒有發現任何類似的東西,這很奇怪,考慮到假設檢驗中顯着性區間的重要性(以及最近只報告 p 值的做法受到了多少批評)。
這里如何使用StatsModels的CompareMeans
計算置信區間的差異意味着:
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')
輸出是
(-10.414599391793885, -5.5854006082061138)
並匹配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
使用pingouin
的替代答案(基本上從這里復制代碼並適應使用Ulrich Stern的變量)
import pingouin as pg
x1, x2 = np.arange(10,21), np.arange(20,26.5,.5)
res = pg.ttest(x1, x2, paired=False)
print(res)
印刷
T dof tail p-val CI95% cohen-d BF10 power
T-test -7.039 15.58 two-sided 0.000003 [-10.41, -5.59] 3.009 2.251e+04 1.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.