我有一个样本1和样本2的平均值,标准差dev和n-样本取自样本总体,但由不同的实验室测量。
样本1和样本2的n是不同的。我想做一个加权(考虑n)双尾t检验。
我尝试通过使用np.random.normal
创建我的数字来使用scipy.stat模块,因为它只接受数据,而不是像mean和std dev这样的stat值(有什么方法可以直接使用这些值)。但它不起作用,因为数据数组必须具有相同的大小。
任何关于如何获得p值的帮助都将不胜感激。
发布于 2014-03-24 15:15:01
如果原始数据是数组a
和b
,则可以使用带有参数equal_var=False
的scipy.stats.ttest_ind
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)计算t值。
下面的脚本显示了这些可能性。
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
发布于 2014-03-24 14:58:49
使用Scipy 0.12.0的最新版本,此功能是内置的(并且实际上可以在不同大小的样本上运行)。在scipy.stats
中,当标志equal_var
设置为False
时,ttest_ind
函数执行韦尔奇的t检验。
例如:
>>> import scipy.stats as stats
>>> sample1 = np.random.randn(10, 1)
>>> sample2 = 1 + np.random.randn(15, 1)
>>> t_stat, p_val = stats.ttest_ind(sample1, sample2, equal_var=False)
>>> t_stat
array([-3.94339083])
>>> p_val
array([ 0.00070813])
https://stackoverflow.com/questions/22611446
复制相似问题