Scipy stats t 检验的均值和自由度

问题描述 投票:0回答:2

我正在使用 scipy 的 stats 模块,特别是函数

ttest_ind
。当我应用此测试时,我想提取与自由度相关的信息。根据 SciPy v1.11.4 文档link,提到返回以下值:

  • 统计量:t统计量
  • pvalue:与给定替代方案相关的 p 值
  • df:用于计算 t 统计量的自由度

但是使用以下可重现的示例,我认为这是不可能的:

from scipy.stats import ttest_ind

# Example data for two groups
group1 = [25, 30, 22, 28, 32]
group2 = [18, 24, 20, 26, 19]

t_statistic, p_value, degrees_of_freedom = ttest_ind(group1, group2, permutations=None)
#> Traceback (most recent call last):
#> Cell In[6], line 1
#> ----> 1 t_statistic, p_value, degrees_of_freedom = ttest_ind(group1, group2, permutations=None)
#> ValueError: not enough values to unpack (expected 3, got 2)

这是文档中的错误还是有办法获得自由度?

python probability scipy.stats
2个回答
0
投票

前面的部分解释了如何处理这个问题:

退货:

结果T测试结果
具有以下属性的对象:

换句话说,该函数返回一个包含数据的对象,而不是包含数据的元组。

您可以像这样从该对象中获取数据:

from scipy.stats import ttest_ind

# Example data for two groups
group1 = [25, 30, 22, 28, 32]
group2 = [18, 24, 20, 26, 19]

result = ttest_ind(group1, group2, permutations=None)
print("t", result.statistic)
print("pvalue", result.pvalue)
print("df", result.df)

0
投票

请检查您的 scipy 版本

https://docs.scipy.org/doc/scipy/reference/ generated/scipy.stats.ttest_ind.html#scipy-stats-ttest-ind

df
1.11.0 version

中的新内容
Code in scipy-1.10.0

from scipy.stats import ttest_ind

# Example data for two groups
group1 = [25, 30, 22, 28, 32]
group2 = [18, 24, 20, 26, 19]

ttest_ind(group1, group2, permutations=None)

#output
Ttest_indResult(statistic=2.553769592276246, pvalue=0.03397476483217163) 

scipy-1.11.4中的代码

from scipy.stats import ttest_ind

# Example data for two groups
group1 = [25, 30, 22, 28, 32]
group2 = [18, 24, 20, 26, 19]

ttest_ind(group1, group2, permutations=None)

#output
TtestResult(statistic=2.553769592276246, pvalue=0.03397476483217163, df=8.0)

© www.soinside.com 2019 - 2024. All rights reserved.