LearnToGrow
LearnToGrow

Reputation: 1750

Wrong P_value given by ttest_1samp

Here is a one sample t-test example:

from scipy.stats import ttest_1samp
import numpy as np

ages = [32., 34., 29., 29., 22., 39., 38., 37.,38, 36, 30, 26, 22, 22.]
ages_mean = np.mean(ages)
ages_std = np.std(ages, ddof=1)
print(ages_mean)
print(ages_std)
ttest, pval = ttest_1samp(ages, 30)
print("ttest: ", ttest)
print("p_value: ", pval)
#31.0
#6.2634470725607025
#ttest:  0.5973799001456603
#p_value:  0.5605155888171379

# check analytically:
my_ttest = (ages_mean - 30.0)/(ages_std/np.sqrt(len(ages)))
print(t)
#0.5973799001456603

check the p_value

by definition p_value = P(t>=0.59) = 1 - P(t<=.59).
Using the Z-table, we got p_value = 1 - 0.7224 = 0.2776 # 0.56!!!

Upvotes: 2

Views: 536

Answers (1)

StupidWolf
StupidWolf

Reputation: 46888

If you check the vignette of ttest_1samp, it writes:

enter image description here

So it's a two-sided p-value, meaning what the sum of probabilities of getting a absolute t-statistic more extreme than this.

The t distribution is symmetric, so we can take the -abs(t stat) and multiply by two for a 2 sided test, and the p-value will be:

from scipy.stats import t
2*t.cdf(-0.5973799001456603, 13)
0.5605155888171379

Your derived value will be correct for a one-sided t-test :)

Upvotes: 2

Related Questions