第三章 一元线性回归

批量和需要劳动工时数在这里插入图片描述

0 导入库&加载数据

Jupyter中文字体乱码显示问题

!apt-get install ttf-wqy-zenhei -y

导入库

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.sandbox.regression.predstd import wls_prediction_std #计算预测的标准差和置信区间

from matplotlib.font_manager import FontProperties
font_set = FontProperties(fname=r"/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", size=16) # 解决中文乱码问题

导入数据

x = [80, 30, 50, 90, 70, 60, 120, 80, 100, 50, 40, 70, 90, 20, 110, 100, 30, 50] # 批量大小
y = [399, 121, 221, 376, 361, 224, 546, 352, 353, 157, 160, 242, 389, 113, 435, 420, 212, 268] # 劳动工时数

1 散点图

plt.scatter(x, y) # 散点图
plt.xlabel("批量大小", fontproperties=font_set)
plt.ylabel("劳动工时数", fontproperties=font_set)

在这里插入图片描述
观察该散点图可以得出,需要的劳工时数与批量大小呈现很强的线性关系

2 估计的回归方程

x_ = sm.add_constant(x) # 添加一列1
mo = sm.OLS(y, x_) # 最小二乘法
result = mo.fit() # 拟合数据

print(result.summary())

在这里插入图片描述

result.params # 参数的估计值

在这里插入图片描述
y ^ = 28.12706767 + 3.90541353 x \hat y = 28.12706767 + 3.90541353 x y^=28.12706767+3.90541353x

参数含义

β ^ 0 \hat \beta_0 β^0: 当批量为0时,平均工时数为28.12706767
β ^ 0 \hat \beta_0 β^0: 当批量每增加一个单位,劳动工时数平均增加3.90541353个单位

3 t检验

百度
在这里插入图片描述

step1

原假设: H 0 : β 1 = 0 H_0: \beta_1=0 H0:β1=0

对立假设: H 1 : β 1 ≠ 0 H_1: \beta_1 \neq 0 H1:β1=0

step2:检验统计量

t = β ^ 1 σ 2 / L x x ∼ t ( n − 2 ) t = \cfrac {\hat\beta_1} {\sqrt {{\sigma^2}/{L_xx}}} \sim t(n-2) t=σ2/Lxx β^1t(n2)

step3:检验统计量的实现值

t 0 = β ^ 1 σ 2 / L x x t_0 = \cfrac {\hat\beta_1} {\sqrt {{\sigma^2}/{L_xx}}} t0=σ2/Lxx β^1

step4: 得出决策

P值<0.05,拒绝原假设,认为批量大小与劳工时数的线性关系显著

4 平方和分解

print('SST', result.centered_tss ) # 总平方和
print('SSR', result.ess) # 回归平方和
print('SSE', result.ssr) # 残差平方和

在这里插入图片描述
∑ i = 1 n ( y i − y ‾ ) 2 = ∑ i = i n ( y i ^ − y ) 2 + ∑ i = i n ( y − y i ^ ) 2 \sum_{i=1}^{n}{(y_i - \overline{y})^2} = \sum^n_{i=i}(\hat{y_i} - y)^2 + \sum^n_{i=i}(y - \hat{y_i})^2 i=1n(yiy)2=i=in(yi^y)2+i=in(yyi^)2

SST = SSE + SSR

5 拟合优化

在这里插入图片描述
线性回归决定系数 R 2 R^2 R2

R 2 = S S R S S T R^2 = \cfrac {SSR}{SST} R2=SSTSSR

(225394.43308270676 / 256936.5) 

模型拟合良好,工时数变差的87.7%能够由批量大小解释

6 F检验

在这里插入图片描述
原假设: H 0 : β 1 = 0 H_0: \beta_1=0 H0:β1=0

对立假设: H 1 : β 1 ≠ 0 H_1: \beta_1 \neq 0 H1:β1=0

检查统计量: F = S S R / 1 S S R / n − 2 F = \cfrac {SSR/1} {SSR/n-2} F=SSR/n2SSR/1

(225394.43308270676/1) / (31542.066917293232/(18-2)) # n=18

在这里插入图片描述

拒绝原假设,批量大小和所需工时数的线性关系显著

P { F > F 0 } = P { ∣ t ∣ > t 0 } P\{F > F_0\} = P\{|t| > t_0\} P{F>F0}=P{t>t0}

F检验与t检验等价

  1. 检验统计量关系 F 0 = t 0 2 F_0 = t^2_0 F0=t02 -> 114.3 = 10.69 3 2 10.693^2 10.6932
  2. P值相等 -> P { F > F 0 } = P { ∣ t ∣ > t 0 } P\{F > F_0\} = P\{|t| > t_0\} P{F>F0}=P{t>t0}
  3. 结论相同 -> 拒绝原假设,认为线性关系显著

7 预测

点预测&区间预测

plt.scatter(x, y, label='orgin') # 原始数据
plt.plot(x, y_fitted, c='r', label='fit') # 预测值
plt.plot(x, confidence_interval_lower, 'y--', label='lower') # 下限
plt.plot(x, confidence_interval_upper, 'g--', label='upper') # 上限
plt.xlabel("批量大小", fontproperties=font_set)
plt.ylabel("劳动工时数", fontproperties=font_set)
plt.legend()

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值