我正在使用stats.ttest_1samp进行t检验,然后我手动计算t检验,但每种方法都会产生不同的结果。我在弄清楚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
>>>
答案 0 :(得分:2)
如果在python中实现了一个函数,你可以很容易地得到它的源代码:
>>> import inspect
>>> print(inspect.getsource(ttest_1samp))
也inspect.getfile
,返回实现方法/函数的文件的路径。至于ttest_1samp
,删除文档字符串这是源代码,应该很容易与你的比较:
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
答案 1 :(得分:0)
您应该使用 n-1 而不是 n ,因为自由度的数量是 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