Reputation: 1355
I am conducting a t-test using stats.ttest_1samp and then I am calculating the t-test manually but each method comes up with different results. I am having some trouble figuring out how numpy is doing this calculation. Can someone tell me why I am getting different results?
Here is my code:
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
Here is my output:
>>> 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
>>>
Upvotes: 1
Views: 1719
Reputation: 2439
You should use n-1 instead of n because the number of degrees of freedom is 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
Results:
t-statistic = 0.84969783903281959 p-value = 0.410
0.84969783903281959
Upvotes: 1
Reputation: 77951
If a function is implemented in python you can easily get its source code:
>>> import inspect
>>> print(inspect.getsource(ttest_1samp))
also inspect.getfile
, returns the path to the file which implements the method/function. As for ttest_1samp
, dropping the doc string this is the source code which should be easy to compare with yours:
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
Upvotes: 2