100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Python_4组数据看线性回归的假设检验问题

Python_4组数据看线性回归的假设检验问题

时间:2020-08-01 08:08:49

相关推荐

Python_4组数据看线性回归的假设检验问题

一般情况下,当H0:β1=0H_0: \beta_1 = 0H0​:β1​=0 被接受的时候,表明 yyy 的取值倾向不随 xxx 的值按线性关系变化。这种情况的原因可能是变量 yyy 与 xxx 之间的相关关系不显著,也可能是 yyy 与 xxx 并非线性相关。

当H0:β1=0H_0: \beta_1 = 0H0​:β1​=0 被拒绝的时候,如果没有其它信息,只能认为因变量 yyy 对 xxx 的线性回归是有效的,但并没有说明回归的有效程度,不能断言 yyy 与 xxx 之间一定是线性相关关系,而不是曲线关系或其他关系。这时候图形表现就很重要了。

4组数据示例

1-数据准备

import numpy as npx1 = list(range(4,15))x4 = [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 19]y1 = [4.26, 5.68, 7.24, 4.82, 6.95, 8.81, 8.04, 8.33, 10.84, 7.58, 9.96]y2 = [3.10, 4.74, 6.13, 7.26, 8.14, 8.77, 9.14, 9.26, 9.13, 8.74, 8.10]y3 = [5.39, 5.73, 6.08, 6.44, 6.77, 7.11, 7.46, 7.81, 8.15, 12.74, 8.84]y4 = [6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 5.56, 7.91, 6.89, 12.5]x1_in = np.array(x1).reshape(-1,1)x4_in = np.array(x4).reshape(-1,1)y1_in = np.array(y1).reshape(-1,1)y2_in = np.array(y2).reshape(-1,1)y3_in = np.array(y3).reshape(-1,1)y4_in = np.array(y4).reshape(-1,1)

图示

import matplotlib.pyplot as pltplt.style.use('ggplot')plt.subplot(2,2,1)plt.xlim(0,20),plt.ylim(0,15)plt.scatter(x1_in, y1_in,s = 8)plt.subplot(2,2,2)plt.xlim(0,20),plt.ylim(0,15)plt.scatter(x1_in, y2_in, s = 8)plt.subplot(2,2,3)plt.xlim(0,20),plt.ylim(0,15)plt.scatter(x1_in, y3_in,s = 8)plt.subplot(2,2,4)plt.xlim(0,20),plt.ylim(0,15)plt.scatter(x4_in, y4_in,s = 8)plt.show()

2-回归

from sklearn.linear_model import LinearRegression lrg1 = LinearRegression()lrg1.fit(x1_in,y1_in)lrg2 = LinearRegression()lrg2.fit(x1_in,y2_in)lrg3 = LinearRegression()lrg3.fit(x1_in,y3_in)lrg4 = LinearRegression()lrg4.fit(x4_in,y4_in)get_lr_stats(x1_in, y1_in, lrg1)get_lr_stats(x1_in, y2_in, lrg2)get_lr_stats(x1_in, y3_in, lrg3)get_lr_stats(x4_in, y4_in, lrg4)

四个模型参数几乎一样(get_lr_stats在Python_一元线性回归及回归显著性 中)

但是并非全都是线性回归

>>> get_lr_stats(x1_in, y1_in, lrg1)一元线性回归方程为:y=3.000090909090906 + 0.5000909090909094*x相关系数(R^2): 0.6665424595087752;回归分析(SSR): 27.51000090909094;残差(SSE): 13.76269;F : 17.989942967676996; pf : 0.002169628873078789t : 4.689105252775333;pt : 0.0005687504416628528>>> get_lr_stats(x1_in, y2_in, lrg2)一元线性回归方程为:y=3.0009090909090883 + 0.5000000000000002*x相关系数(R^2): 0.6662420337274844;回归分析(SSR): 27.500000000000014; 残差(SSE): 13.776290909090912;F : 17.965648492271313; pf : 0.002178816236910796t : 4.685937987627148;pt : 0.0005712964612135407>>> get_lr_stats(x1_in, y3_in, lrg3)一元线性回归方程为:y=3.007545454545453 + 0.49936363636363645*x相关系数(R^2): 0.6660467267232798;回归分析(SSR): 27.430044545454564; 残差(SSE): 13.753319090909097;F : 17.949878082322083; pf : 0.0021848056073100444t : 4.683880856554066;pt : 0.0005729566449371534>>> get_lr_stats(x4_in, y4_in, lrg4)一元线性回归方程为:y=3.0017272727272726 + 0.4999090909090909*x相关系数(R^2): 0.6667072568984653;回归分析(SSR): 27.490000909090913; 残差(SSE): 13.742490000000004;F : 18.003288209183207; pf : 0.0021646023471972213t : 4.690844158819928;pt : 0.0005673577949779548

3-回归图示

## 回归后图示xl = np.array(list(range(0,21))).reshape(-1,1)plt.subplot(2,2,1)plt.xlim(0,20),plt.ylim(0,15)plt.scatter(x1_in, y1_in,s = 8)plt.plot(xl, lrg1.predict(xl),c='steelblue', alpha=0.7, lw=1)plt.subplot(2,2,2)plt.xlim(0,20),plt.ylim(0,15)plt.scatter(x1_in, y2_in, s = 8)plt.plot(xl, lrg1.predict(xl),c='steelblue', alpha=0.7, lw=1)plt.subplot(2,2,3)plt.xlim(0,20),plt.ylim(0,15)plt.scatter(x1_in, y3_in,s = 8)plt.plot(xl, lrg1.predict(xl),c='steelblue', alpha=0.7, lw=1)plt.subplot(2,2,4)plt.xlim(0,20),plt.ylim(0,15)plt.scatter(x4_in, y4_in,s = 8)plt.plot(xl, lrg1.predict(xl),c='steelblue', alpha=0.7, lw=1)plt.show()

因此,在实际应用中,不应该局限于一种方法去分析判断。要得到,确实可信的结果,应该将F检验、散点图、残差分析等方法一起使用,得到一致的结果才可以下定论

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。