Python:统计T检验

时间:2017-06-23 16:18:37

标签: python python-3.x scipy statistics t-test

我正在使用python 3.6对数据集运行一些统计测试。我想要完成的是  在数据集和趋势线之间运行t检验以确定统计显着性。我使用scipy来做这个但是我不确定我应该在测试中包含哪些变量来获得我需要的结果。

到目前为止,这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

p = np.load('data.npy')

#0=1901
start=0
end=100

plt.figure()
plt.plot(a,annualmean,  '-')
slope, intercept, r_value, p_value, std_err = stats.linregress(a,annualmean)
plt.plot(a,intercept+slope*a, 'r')

annualmean=[]
for n in range(start,end):
    annualmean.append(np.nanmean(p[n]))

#Trendline Plots
a=range(start,end)
year1 = 1901

print(stats.ttest_ind(annualmean,a))

现在代码正在运行,没有错误信息,但是我得到了一个令人难以置信的小p值,我认为这是不正确的。如果有人知道我应该在t检验中写出哪些变量会非常有帮助。 谢谢!

2 个答案:

答案 0 :(得分:1)

我没有评论的声誉,但根据您的代码,您正在进行t检验,比较年度平均数据与0-100之间的数据之间的平均值。 scipy.stats.ttest采用两个大小相等的数组,你想要比较它们的平均值。

根据documentation

scipy.stats.ttest_ind(a, b, axis=0, equal_var=True)[source]

Parameters: 
a, b : array_like
The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default).

另外请注意,在趋势线和原始数据之间进行t检验是没有意义的,但这是another forum的问题

答案 1 :(得分:0)

事实证明我对如何测试统计显着性感到困惑。我已经找到了该行数据的p值:

slope, intercept, r_value, p_value, std_err = stats.linregress(a,annualmean)

我需要做的就是: 打印(P_VALUE)