scipy.stats.ttest_ind_from_stats

scipy.stats.ttest_ind_from_stats(mean1, std1, nobs1, mean2, std2, nobs2, equal_var=True, alternative='two-sided')[源代码]

描述性统计中两个独立样本均值的t检验。

这是对两个独立样本具有相同平均值(期望值)的零假设的检验。

参数
mean1array_like

样本1的平均值。

std1array_like

样本1的标准偏差。

nobs1array_like

样本1的观测次数。

mean2array_like

样本2的平均值。

std2array_like

样本2的标准偏差。

nobs2array_like

样本2的观测次数。

equal_var布尔值,可选

如果为True(默认值),则执行假设总体方差相等的标准独立双样本测试 [1]. 如果为假,则执行韦尔奇t检验,该检验不假定总体方差相等 [2].

alternative{‘双面’,‘少’,‘大’},可选

定义了另一种假设。有以下选项可用(默认为‘双面’):

  • “两面性”:分配的平均数是不平等的。

  • “小于”:第一个分布的平均值小于第二个分布的平均值。

  • “大于”:第一个分布的平均值大于第二个分布的平均值。

1.6.0 新版功能.

退货
statistic浮点或数组

计算的t统计量。

pvalue浮点或数组

双尾p值。

注意事项

0.16.0 新版功能.

参考文献

1

https://en.wikipedia.org/wiki/T-test#Independent_two-sample_t-test

2

https://en.wikipedia.org/wiki/Welch%27s_t-test

示例

假设我们有两个示例的汇总数据,如下所示:

                 Sample   Sample
           Size   Mean   Variance
Sample 1    13    15.0     87.5
Sample 2    11    12.0     39.0

对此数据应用t检验(假设总体方差相等):

>>> from scipy.stats import ttest_ind_from_stats
>>> ttest_ind_from_stats(mean1=15.0, std1=np.sqrt(87.5), nobs1=13,
...                      mean2=12.0, std2=np.sqrt(39.0), nobs2=11)
Ttest_indResult(statistic=0.9051358093310269, pvalue=0.3751996797581487)

为了便于比较,以下是摘录这些汇总统计数据的数据。使用此数据,我们可以使用以下命令计算相同的结果 scipy.stats.ttest_ind

>>> a = np.array([1, 3, 4, 6, 11, 13, 15, 19, 22, 24, 25, 26, 26])
>>> b = np.array([2, 4, 6, 9, 11, 13, 14, 15, 18, 19, 21])
>>> from scipy.stats import ttest_ind
>>> ttest_ind(a, b)
Ttest_indResult(statistic=0.905135809331027, pvalue=0.3751996797581486)

假设我们有二进制数据,并希望应用t检验来比较两个独立组中1的比例:

                  Number of    Sample     Sample
            Size    ones        Mean     Variance
Sample 1    150      30         0.2        0.16
Sample 2    200      45         0.225      0.174375

样本均值 \(\hat{{p}}\) 是样本中1的比例,二元观测值的方差估计为 \(\hat{{p}}(1-\hat{{p}})\)

>>> ttest_ind_from_stats(mean1=0.2, std1=np.sqrt(0.16), nobs1=150,
...                      mean2=0.225, std2=np.sqrt(0.17437), nobs2=200)
Ttest_indResult(statistic=-0.564327545549774, pvalue=0.5728947691244874)

为了进行比较,我们可以使用0和1的数组来计算t统计量和p值 scipy.stat.ttest_ind ,如上所述。

>>> group1 = np.array([1]*30 + [0]*(150-30))
>>> group2 = np.array([1]*45 + [0]*(200-45))
>>> ttest_ind(group1, group2)
Ttest_indResult(statistic=-0.5627179589855622, pvalue=0.573989277115258)