簡體   English   中英

進行2個樣本t檢驗

[英]Perform 2 sample t-test

我有樣本1和樣本2的平均值,std dev和n - 樣本來自樣本群,但是由不同的實驗室測量。

n對於樣本1和樣本2是不同的。我想進行加權(考慮n)雙尾t檢驗。

我嘗試使用scipy.stat模塊通過創建我的號碼np.random.normal ,因為只需要數據,而不是像統計平均值和標准偏差值(有沒有辦法直接使用這些值)。 但它不起作用,因為數據陣列必須具有相同的大小。

任何有關如何獲得p值的幫助都將受到高度贊賞。

如果您將原始數據作為數組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/ )計算t值。 韋爾奇%27s_t_test )。

以下腳本顯示了可能性。

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

使用最新版本的Scipy 0.12.0,內置了此功能(實際上可以對不同大小的樣本進行操作)。 scipy.stats ,當標志equal_var設置為False時, ttest_ind函數執行Welch的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])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM