一般情况下,H0:β1=0H_0: \beta_1 = 0接受的时候,表明 yy 的取值倾向不随 xx 的值按线性关系变化。这种情况的原因可能是变量 yyxx 之间的相关关系不显著,也可能是 yyxx 并非线性相关。
H0:β1=0H_0: \beta_1 = 0拒绝的时候,如果没有其它信息,只能认为因变量 yyxx 的线性回归是有效的,但并没有说明回归的有效程度,不能断言 yyxx 之间一定是线性相关关系,而不是曲线关系或其他关系。这时候图形表现就很重要了。


4组数据示例

1-数据准备

import numpy as np
x1 = 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 plt
plt.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()

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

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_statsPython_一元线性回归及回归显著性 中)
但是并非全都是线性回归

>>> 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.002169628873078789
           t : 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.002178816236910796
           t : 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.0021848056073100444
           t : 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.0021646023471972213
           t : 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()

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


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

相关文章:

  • 2021-10-20
  • 2021-11-18
  • 2021-12-23
  • 2021-09-09
  • 2021-07-02
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-24
  • 2021-11-07
  • 2021-07-16
  • 2022-01-05
  • 2022-01-19
  • 2021-04-25
相关资源
相似解决方案