Perform 2 sample t-test(执行 2 个样本 t 检验)
问题描述
我有样本 1 和样本 2 的均值、标准差和 n - 样本取自样本总体,但由不同实验室测量.
I have a the mean, std dev and n of sample 1 and sample 2 - samples are taken from the sample population, but measured by different labs.
样本 1 和样本 2 的 n 不同.我想做一个加权(考虑 n)双尾 t 检验.
n is different for sample 1 and sample 2. I want to do a weighted (take n into account) two-tailed t-test.
我尝试使用 scipy.stat 模块通过使用 np.random.normal 创建我的数字,因为它只需要数据而不是像 mean 和 std dev 这样的统计值(有没有办法直接使用这些值).但它不起作用,因为数据数组必须具有相同的大小.
I tried using the scipy.stat module by creating my numbers with np.random.normal, since it only takes data and not stat values like mean and std dev (is there any way to use these values directly). But it didn't work since the data arrays has to be of equal size.
任何有关如何获得 p 值的帮助将不胜感激.
Any help on how to get the p-value would be highly appreciated.
推荐答案
如果你有原始数据作为数组 a 和 b,你可以使用 scipy.stats.ttest_ind 带有参数 equal_var=False:
If you have the original data as arrays a and b, you can use scipy.stats.ttest_ind with the argument equal_var=False:
t, p = ttest_ind(a, b, equal_var=False)
如果你只有两个数据集的汇总统计,你可以使用scipy.stats.ttest_ind_from_stats(在 0.16 版中添加到 scipy)或来自公式(http://en.wikipedia.org/wiki/Welch%27s_t_test).
If you have only the summary statistics of the two data sets, you can calculate the t value using scipy.stats.ttest_ind_from_stats (added to scipy in version 0.16) or from the formula (http://en.wikipedia.org/wiki/Welch%27s_t_test).
以下脚本显示了可能性.
The following script shows the possibilities.
from __future__ import print_function
import numpy as np
from scipy.stats import ttest_ind, ttest_ind_from_stats
from scipy.special import stdtr
np.random.seed(1)
# Create sample data.
a = np.random.randn(40)
b = 4*np.random.randn(50)
# Use scipy.stats.ttest_ind.
t, p = ttest_ind(a, b, equal_var=False)
print("ttest_ind: t = %g p = %g" % (t, p))
# Compute the descriptive statistics of a and b.
abar = a.mean()
avar = a.var(ddof=1)
na = a.size
adof = na - 1
bbar = b.mean()
bvar = b.var(ddof=1)
nb = b.size
bdof = nb - 1
# Use scipy.stats.ttest_ind_from_stats.
t2, p2 = ttest_ind_from_stats(abar, np.sqrt(avar), na,
bbar, np.sqrt(bvar), nb,
equal_var=False)
print("ttest_ind_from_stats: t = %g p = %g" % (t2, p2))
# Use the formulas directly.
tf = (abar - bbar) / np.sqrt(avar/na + bvar/nb)
dof = (avar/na + bvar/nb)**2 / (avar**2/(na**2*adof) + bvar**2/(nb**2*bdof))
pf = 2*stdtr(dof, -np.abs(tf))
print("formula: t = %g p = %g" % (tf, pf))
输出:
ttest_ind: t = -1.5827 p = 0.118873
ttest_ind_from_stats: t = -1.5827 p = 0.118873
formula: t = -1.5827 p = 0.118873
这篇关于执行 2 个样本 t 检验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:执行 2 个样本 t 检验
基础教程推荐
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
