Home > Net >  Python: p value from scipy is not correct
Python: p value from scipy is not correct

Time:11-29

i want to calculate a p value with the package scipy. this is the code is used:

x = st.ttest_1samp(df_efw.stack(),np.round(np.mean(df_lw).mean(),2))

This is my output:

Ttest_1sampResult(statistic=-1.3939917717040629, pvalue=0.16382682901590806)

I also calculated it manually and my statistic value is correct but the p value is not..? The p value can be read on the standard normal distribution table. So the problem is: if you read the table you will see that -1,39399 has a p value of 0,0823 and not 0,1638. So i am thinking that i did the code wrong or i am interpreting something wrong. What is it?

CodePudding user response:

By default, ttest_1samp returns the two-sided or two-tailed p-value, which is twice the single-sided p-value due to the symmetry about 0 of the t distribution. Consistent with this, your manually computed single-sided p-value is (roughly) half of SciPy's p-value.

One solution is just to divide the two-sided p-value from ttest_1samp by 2. In SciPy 1.6.0 and later, you can pass the argument alternative='greater' or alternative='less' to get a single-sided p-value.

Further Reading

ttest_1samp documentation: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_1samp.html

The GitHub issue where the alternative argument was proposed: https://github.com/scipy/scipy/pull/12597

The resulting pull request: https://github.com/scipy/scipy/pull/12597

  • Related