0
$\begingroup$

I've implemented a simple one sample t-test in python:

import numpy as np
from math import sqrt
from scipy.stats import t, ttest_1samp

def one_sample_ttest(sample, global_mean, alpha=0.05):
    n = sample.size
    std = np.std(sample)
    mean = np.mean(sample)
    serr = std / sqrt(n)
    tscore = (mean - global_mean) / serr
    pval = (t.sf(x=tscore, df=n-1)) * 2  # alternatively = (1 - t.cdf(x=tstat, df=n-1)) * 2

    return tscore, pval

However, when comparing the computed t-score with ttest_1samp from scipy.stats module, I'm getting a different result:

data = np.array([5.473, 2.967, 5.337, -1.054])

print(one_sample_ttest(data, 0.0)[0])
print(ttest_1samp(data, 0.0).statistic)

######################
2.4094767021440173
2.08666803388347

Through trial-and-error, I've found this is likely caused by the calculation of the standard error in the denominator of the test formula:

$$ t=\frac{\bar{x} - \mu_0}{\frac{s}{\sqrt{n}}} $$

which becomes (using the number of DoF instead of sample size):

$$ t=\frac{\bar{x} - \mu_0}{\frac{s}{\sqrt{n-1}}} $$

Is there a particular reason for this? What are the practical implications?

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.