线性回归的单侧t检验? [英] One sided t-test for linear regression?

查看:247
本文介绍了线性回归的单侧t检验?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此有疑问.我正在尝试进行线性回归并测试斜率. t检验检查斜率是否远离0.斜率可以为负或正.我只对负斜率感兴趣.

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. I am only interested in negative slopes.

在此示例中,我不感兴趣的斜率为正,因此P值应较大.但是它很小,因为现在它会测试斜率在任一方向上是否都远离0. (我要强制零截距,这就是我想要的).有人可以通过语法帮助我,看看斜率是否仅为负数.在这种情况下,P值应较大.

In this example, the slope is positive which I am not interested in, so the P value should be large. But it is small because right now it tests if the slope is far away from 0, in either direction. (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.

该如何更改为置信度为99%或95%或...?

And how can I change to, to say 99% confidence level or 95% or...?

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)

推荐答案

双向t检验的p值由以下公式计算:

p-value for the two-way t-test is calculated by:

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

您的修改将是:

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

因为您只对左尾感兴趣.

since you are interested in the left-tail only.

对于置信区间,您只需要传递alpha参数:

For confidence interval, you just need to pass the alpha parameter:

regression_results.conf_int(alpha=0.01)

置信区间为99%.

这篇关于线性回归的单侧t检验?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆