2014-03-24 27 views
13

我有一個樣本1和樣本2的平均值,std dev和n--樣本來自樣本羣體,但是由不同的實驗室測量。執行2樣本t檢驗

樣本1和樣本2的n不同。我想做一個加權(考慮n)雙尾t檢驗。

我嘗試使用scipy.stat模塊,通過使用np.random.normal創建我的數字,因爲它只接受數據而不是stat和std dev之類的stat值(是否有任何方法可以直接使用這些值)。但是它不起作用,因爲數據數組必須具有相同的大小。

任何有關如何獲得p值的幫助將不勝感激。

+2

在據我瞭解,韋爾奇的t檢驗是未配對的情況下(即不相關的樣本)... – rroowwllaanndd

+0

你「(相關)」的問題標題。正如@rroowwllaanndd指出的,韋爾奇的t檢驗是針對獨立樣本的。如果你有其他想法,請解釋。 –

+0

我已經更新了這個問題。希望現在更清楚 – Norfeldt

回答

36

如果您有原始數據作爲數組ab,您可以使用scipy.stats.ttest_ind的說法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)計算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 
+0

非常感謝。特別是'stdtr' - 非常有用 – Norfeldt

+0

如果只有統計信息,可以使用scipy.stats.ttest_ind_from_stats(http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind_from_stats。 html#scipy.stats.ttest_ind_from_stats) –

+0

@JensdeBruijn感謝您的提醒。在回答最初編寫完成後,'ttest_ind_from_stats'被添加到了scipy中。我已經更新了包含它的答案。 –

2

使用最新版本的Scipy 0.12.0,該功能內置(實際上可以對不同大小的樣本進行操作)。在scipy.stats 函數執行韋爾奇的t檢驗,當標誌equal_var設置爲False

例如:

>>> 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])