提问人:PanchoPaz 提问时间:4/21/2023 最后编辑:KylePanchoPaz 更新时间:10/27/2023 访问量:37
scipy 执行相等性 t 检验
scipy to perform a t-test for equality
问:
我正在尝试执行 t 检验以确定两个数据样本的均值相等性。我知道我需要执行一个具有零差分假设的 t 检验并拒绝它:
$$ H_0 = \| \mu_1 - \mu_2\| > \delta $$
$$ H_a = \| \mu_1 - \mu_2\| < \delta $$
我知道scipy有ttest_ind但这个测试假设相等,所以它对我不起作用。
有没有办法直接用scipy执行这个测试?或者这怎么能做到呢?
找到了几个需要推导方程并使用 t 分布表的示例。我正在寻找一种解决方案,使用 scipy 或其他软件包中的内置统计函数来减少验证工作。
答:
1赞
Matt Haberland
10/27/2023
#1
听起来你正在寻找一个等价性检验,其中一个例子是“两个单侧检验”(TOST)。SciPy 没有内置的东西,但你可以执行两个单侧 t 检验。
import numpy as np
from scipy import stats
rng = np.random.default_rng(459324692354693456)
x = rng.normal(size=100)
y = rng.normal(size=100)
delta = 0.5 # "equivalence bound"
# H0: mean of distribution underlying `x` is equal to the mean of
# the distribution underlying `y - delta`.
# (some would replace "equal" with "less than or equal to"
# H1: mean of distribution underlying `x` is greater than the mean of
# the distribution underlying `y - delta`.
res1 = stats.ttest_ind(x, y - delta, alternative='greater')
print(res1)
# the other one-sided test
res2 = stats.ttest_ind(x, y + delta, alternative='less')
print(res2)
TtestResult(statistic=3.7153030134010545, pvalue=0.00013198904382128585, df=198.0)
TtestResult(statistic=-3.956379347715619, pvalue=5.298494654171639e-05, df=198.0)
用 Lakens 2018 的话来说,“当两个 p 值中的较大值小于 alpha 时,可以得出统计等价的结论”。
statsmodels
ttost_ind
,有一个功能。
from statsmodels.stats.weightstats import ttost_ind
ttost_ind(x, y, -delta, delta)
的第一个输出是两个 p 值中较大的一个,其余两个输出与上面 SciPy 的两个输出匹配。ttost_ind
(0.00013198904382128585,
(3.715303013401054, 0.00013198904382128585, 198.0),
(-3.956379347715618, 5.298494654171652e-05, 198.0))
评论