gpt4 book ai didi

python - 单面一个样本 T 测试 Python

转载 作者:行者123 更新时间:2023-12-01 06:01:24 25 4
gpt4 key购买 nike

在 Python 中,我使用 SciPy 进行单样本 t 检验:

from scipy import stats

one_sample_data = [177.3, 182.7, 169.6, 176.3, 180.3, 179.4, 178.5, 177.2, 181.8, 176.5]

one_sample = stats.ttest_1samp(one_sample_data, 175.3)

这是一个双尾测试,但我在 scipy.stats.ttest_1samp 中看不到选项做一个单尾测试。

在 R 中,如果我使用 t.test()我会简单地设置 alternative="less" (或“更大”)。在 Python 中执行此操作的最简单方法是什么?

最佳答案

根据评论中提供的链接,我将执行以下操作:

from scipy import stats
def one_sample_one_tailed(sample_data, popmean, alpha=0.05, alternative='greater'):
t, p = stats.ttest_1samp(sample_data, popmean)
print ('t:',t)
print ('p:',p)
if alternative == 'greater' and (p/2 < alpha) and t > 0:
print ('Reject Null Hypothesis for greater-than test')
if alternative == 'less' and (p/2 < alpha) and t < 0:
print ('Reject Null Hypothesis for less-thane test')
sample_data = [177.3, 182.7, 169.6, 176.3, 180.3, 179.4, 178.5, 177.2, 181.8, 176.5]
one_sample_one_tailed(sample_data,175.3)

这给出了输出:
t: 2.295568968083183
p: 0.04734137339747034
Reject Null Hypothesis for greater-than test

此解决方案基于链接中已接受的答案:

It goes on to say that scipy always gives the test statistic as signed. This means that given p and t values from a two-tailed test, you would reject the null hypothesis of a greater-than test when p/2 < alpha and t > 0, and of a less-than test when p/2 < alpha and t < 0.

关于python - 单面一个样本 T 测试 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44947229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com