2014-04-01 133 views
1

我正在使用stats.ttest_1samp進行t檢驗,然後手動計算t檢驗,但每種方法都會產生不同的結果。我在計算numpy是如何計算的時候遇到了一些麻煩。有人能告訴我爲什麼我得到不同的結果嗎?ttest如何在numpy中計算

這裏是我的代碼:

import numpy as np 
from scipy import stats 
import math 

#our sample 
x=[21, 33, 28, 17, 23, 30, 26, 27, 28, 31, 29, 23, 28, 27, 25] 
x_bar = np.mean(x)#x_bar = 26.3999 
mu0 = 25.5 
n = len(x) 
s = np.std(x) 
se = s/math.sqrt(n) 
print "t-statistic = %6.3f p-value = %6.3f" % stats.ttest_1samp(x, mu0) 
t_manual = (x_bar-mu0)/se 
t_manual 

這裏是我的輸出:

>>> print "t-statistic = %6.3f p-value = %6.3f" % stats.ttest_1samp(x, mu0) 
t-statistic = 0.850 p-value = 0.410 
>>> t_manual = (x_bar-mu0)/se 
>>> t_manual 
0.87952082184625846 
>>> 

回答

1

如果一個函數在Python實現的,你可以很容易地得到它的源代碼:

>>> import inspect 
>>> print(inspect.getsource(ttest_1samp)) 

inspect.getfile,返回到實現方法/函數的文件的路徑。至於ttest_1samp,下降doc字符串這是源代碼,應該很容易與您進行比較:

def ttest_1samp(a, popmean, axis=0): 
    """ 
    ... doc string ... 
    """ 
    a, axis = _chk_asarray(a, axis) 
    n = a.shape[axis] 
    df = n - 1 

    d = np.mean(a, axis) - popmean 
    v = np.var(a, axis, ddof=1) 
    denom = np.sqrt(v/float(n)) 

    t = np.divide(d, denom) 
    t, prob = _ttest_finish(df, t) 

    return t,prob 

def _ttest_finish(df,t): 
    """Common code between all 3 t-test functions.""" 
    prob = distributions.t.sf(np.abs(t), df) * 2 # use np.abs to get upper tail 
    if t.ndim == 0: 
     t = t[()] 

    return t, prob 
0

您應該使用N-1代替ň因爲自由度數是n-1

se = s/math.sqrt(n-1) # use n-1 instead of n 
print "t-statistic = %6.12f p-value = %6.3f" % stats.ttest_1samp(x, mu0) 
t_manual = (x_bar-mu0)/se 
print t_manual 

結果:

t-statistic = 0.84969783903281959 p-value = 0.410 
0.84969783903281959