简体   繁体   English

ttest_1samp 给出的 P_value 错误

[英]Wrong P_value given by ttest_1samp

Here is a one sample t-test example:这是一个样本 t 检验示例:

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检查 p_value

by definition p_value = P(t>=0.59) = 1 - P(t<=.59).根据定义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!!!使用 Z 表,我们得到p_value = 1 - 0.7224 = 0.2776 # 0.56!!!

If you check the vignette of ttest_1samp, it writes:如果您检查 ttest_1samp 的小插图,它会写道:

在此处输入图像描述

So it's a two-sided p-value, meaning what the sum of probabilities of getting a absolute t-statistic more extreme than this.所以它是一个双边 p 值,这意味着获得比这更极端的绝对 t 统计量的概率总和。

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: t 分布是对称的,因此我们可以取 -abs(t stat) 并乘以 2 进行 2 边检验,p 值为:

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:)对于单面 t 检验,您的派生值将是正确的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM