numpy中ttest是如何计算的
我正在使用 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
>>>
2 个回答
1
你应该用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
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