简体   繁体   English

线性回归的单侧t检验?

[英]One sided t-test for linear regression?

I have problems with this. 我有这个问题。 I am trying to do a linear regression and test the slope. 我正在尝试进行线性回归并测试斜率。 The t-test checks if the slope is far away from 0. The slope can be negative or positive. t检验检查斜率是否远离0。斜率可以为负或正。 I am only interested in negative slopes. 我只对负坡感兴趣。

In this example, the slope is positive which I am not interested in, so the P value should be large. 在此示例中,斜率是正的,我不感兴趣,因此P值应较大。 But it is small because right now it tests if the slope is far away from 0, in either direction. 但这很小,因为现在它会测试斜率在任一方向上是否都远离0。 (I am forcing an intercept of zero, which is what I want). (我强迫截距为零,这是我想要的)。 Can someone help me with the syntax to see if the slope is only negative. 有人可以通过语法帮助我看看斜率是否仅为负数。 In this case the P value should be large. 在这种情况下,P值应较大。

And how can I change to, to say 99% confidence level or 95% or...? 我该如何更改为置信度为99%或95%或...?

import statsmodels.api as sm
import matplotlib.pyplot as plt
import numpy
X = [-0.013459134, 0.01551033, 0.007354476, 0.014686473, -0.014274754, 0.007728445, -0.003034186, -0.007409397]
Y = [-0.010202462, 0.003297546, -0.001406498, 0.004377665, -0.009244517, 0.002136552, 0.006877126, -0.001494624]
regression_results = sm.OLS (Y, X, missing = "drop").fit ()
P_value = regression_results.pvalues [0]
R_squared = regression_results.rsquared
K_slope = regression_results.params [0]
conf_int = regression_results.conf_int ()
low_conf_int = conf_int [0][0]
high_conf_int = conf_int [0][1]
fig, ax = plt.subplots ()
ax.grid (True)
ax.scatter (X, Y, alpha = 1, color='orchid')
x_pred = numpy.linspace (min (X), max (X), 40)
y_pred = regression_results.predict (x_pred)
ax.plot (x_pred, y_pred, '-', color='darkorchid', linewidth=2)

p-value for the two-way t-test is calculated by: 双向t检验的p值由以下公式计算:

import scipy.stats as ss
df = regression_results.df_resid
ss.t.sf(regression_results.tvalues[0], df) * 2 # About the same as (1 - cdf) * 2.
# see @user333700's comment
Out[12]: 0.02903685649821508

Your modification would just be: 您的修改将是:

ss.t.cdf(regression_results.tvalues[0], df)
Out[14]: 0.98548157175089246

since you are interested in the left-tail only. 因为您只对左尾感兴趣。

For confidence interval, you just need to pass the alpha parameter: 对于置信区间,您只需要传递alpha参数:

regression_results.conf_int(alpha=0.01)

for a 99% confidence interval. 置信区间为99%。

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

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