笔记&代码 | 统计学——基于R(第四版) 第九章一元线性回归

一元线性回归

在这里插入图片描述

确定变量间关系

变量间的关系

相关关系描述

  • 绘制散点图描述销售收入与广告支出的关系
    在这里插入图片描述
> library(carData)
> library(car)
> scatterplot(销售收入~广告支出,data=example9_1,pch=19,xlab="广告支出",ylab="销售收入",cex.lab=0.8)

在这里插入图片描述
观测点分布在一条直线周围,有线性相关关系
两个箱线图基本上对称分布,两个变量为线性关系

关系强度的度量

  1. 相关系数
  2. 相关系数的检验
    t检验
  • 计算销售收入与广告支出的相关系数,并检验其显著性(a=0.05)

step1:提出假设
H0:p=0(两个变量的线性关系不显著)
H1:显著
step2:检验
step3:决策。如果p<a,拒绝H0表示线性关系显著

#计算相关系数
cor(example9_1[,2],example9_1[,3])
#检验相关系数 默认皮尔森系数
cor.test(example9_1[,2],example9_1[,3])
> cor(example9_1[,2],example9_1[,3])
[1] 0.937114
> cor.test(example9_1[,2],example9_1[,3])
	Pearson's product-moment correlation
data:  example9_1[, 2] and example9_1[, 3]
t = 11.391, df = 18, p-value = 1.161e-09
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.8450142 0.9752189
sample estimates:
     cor 
0.937114 

模型估计和检验

回归模型与回归方程

参数的最小二乘估计

  • 求销售收入(因变量y)与广告支出(自变量x)的估计的回归方程
    lm函数:用于拟合线性模型
#回归模型的拟合
> model<-lm(销售收入~广告支出,data=example9_1)
> summary(model)
Call:
lm(formula = 销售收入 ~ 广告支出, data = example9_1)
Residuals:
    Min      1Q  Median      3Q     Max 
-766.30 -273.85  -26.79  174.73  900.66 
Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 2343.8916   274.4825   8.539 9.56e-08 ***
广告支出       5.6735     0.4981  11.391 1.16e-09 ***
---
Signif. codes:  
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 394 on 18 degrees of freedom
Multiple R-squared:  0.8782,	Adjusted R-squared:  0.8714 
F-statistic: 129.8 on 1 and 18 DF,  p-value: 1.161e-09

回归方程:
y=2343.8916+5.6725*广告支出
estimate下的两个参数

#输出方差分析表
> anova(model)
Analysis of Variance Table
Response: 销售收入
          Df   Sum Sq  Mean Sq F value    Pr(>F)    
广告支出   1 20139304 20139304  129.76 1.161e-09 ***
Residuals 18  2793629   155202                      
---
Signif. codes:  
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

模型的拟合优度

  1. 决定系数R^2
    上方代码块 Multiple R-squared: 0.8782
    表示在销售收入取值的总误差中,有87.82%可以由销售收入与广告支出之间的线性关系来解释,可见模型的拟合程度较高
  2. 残差的标准误Se
    上方代码块 Residual standard error: 394
    表示用广告支出来预测销售收入时平均的预测误差为394万元

模型的显著性检验

  1. 线性关系检验
    F检验
    上方代码块 F-statistic: 129.8 on 1 and 18 DF, p-value: 1.161e-09
    p<a,拒绝H0,表示两个变量之间的线性关系显著
  2. 回归系数的检验和推断
    t检验
    上方代码块 t=11.391 p=1.16e-09,拒绝H0
    在一元线性回归中,回归系数的检验与线性关系的检验是等价的,所以数值相同
  3. 区间估计
#计算回归系数的置信区间
> confint(model,level=0.95)
                  2.5 %      97.5 %
(Intercept) 1767.225152 2920.558006
广告支出       4.627092    6.719825

表示广告支出每变动1万元,销售收入的平均改变量在 4.627092~6.719825万元之间

利用回归方程进行预测

在这里插入图片描述

均值的置信区间

  • 求20家企业收入95%的置信区间和预测区间
#求点预测值、置信区间、预测区间
> model<-lm(销售收入~广告支出,data=example9_1)
> x0<-example9_1$广告支出
> pre_model<-predict(model)
> con_int<-predict(model,data.frame(广告支出=x0),interval="confidence",level=0.95)
> pre_int<-predict(model,data.frame(广告支出=x0),interval="prediction",level=0.95)
> data.frame(销售收入=example9_1$销售收入,点预测值=pre_model,置信下限=con_int[,2],置信上限=con_int[,3],预测下限=pre_int[,2],预测上限=pre_int[,3])
   销售收入 点预测值 置信下限 置信上限 预测下限 预测上限
1    4597.5 4264.925 3998.348 4531.501 3395.383 5134.467
2    6611.0 6945.066 6590.492 7299.641 6044.643 7845.490
3    7349.3 6448.639 6168.060 6729.217 5574.703 7322.575
4    5525.7 5260.049 5074.789 5445.310 4411.897 6108.201
5    4675.9 4763.054 4552.697 4973.412 3909.069 5617.039
6    4418.6 4762.487 4552.080 4972.894 3908.490 5616.484
7    5845.4 6196.170 5948.675 6443.664 5332.287 7060.053
8    7313.0 7151.013 6763.533 7538.493 6237.130 8064.896
9    5035.4 5015.523 4822.893 5208.154 4165.731 5865.315
10   4322.6 4578.100 4349.549 4806.650 3719.452 5436.747
11   6389.5 6320.986 6057.644 6584.328 5452.430 7189.542
12   4152.2 4011.888 3709.980 4313.796 3130.873 4892.904
13   5544.8 4854.964 4652.116 5057.813 4002.798 5707.131
14   6095.1 5946.538 5726.896 6166.179 5090.218 6802.857
15   3626.2 3821.828 3491.525 4152.130 2930.682 4712.973
16   3745.4 4074.296 3781.397 4367.196 3196.327 4952.266
17   5121.8 5888.101 5674.071 6102.132 5033.204 6742.998
18   5674.5 5747.967 5545.680 5950.254 4895.934 6600.000
19   4256.6 4043.660 3746.360 4340.960 3164.212 4923.107
20   5803.7 6008.946 5782.898 6234.994 5150.961 6866.931
#绘制置信带和预测带
> library(investr)
> plotFit(model,interval = "both",level=0.95,shade=TRUE,col.conf="skyblue3",col.pred="lightskyblue2",col.fit="red2")
> legend(x="topleft",legend=c("回归线","置信区间","预测区间"),col=c("red2","skyblue3","skyblue2"),cex=0.8)

在这里插入图片描述
红线:回归直线
深蓝:置信带
浅蓝:预测带

个别值的预测区间

  • 求x0=500时销售收入的点预测值、置信区间和预测区间
>  x0<-data.frame(广告支出=500)
> predict(model,newdata=x0)
       1 
5180.621 
> predict(model,data.frame(广告支出=500),interval="confidence",level=0.95)
       fit      lwr      upr
1 5180.621 4994.127 5367.115
> predict(model,data.frame(广告支出=500),interval="prediction",level=0.95)
       fit      lwr      upr
1 5180.621 4332.199 6029.043

回归模型的诊断

残差与残差图

  • 求回归预测值pre,残差res,标准化残差zre
> pre<-fitted(model)
> res<-residuals(model)
> zre<-model$residuals/(sqrt(deviance(model)/df.residual(model)))
> mysummary<-data.frame(销售收入=example9_1$销售收入,点预测值=pre,残差=res,标准化残差=zre)
> mysummary
   销售收入 点预测值       残差 标准化残差
1    4597.5 4264.925  332.57536  0.8441934
2    6611.0 6945.066 -334.06646 -0.8479784
3    7349.3 6448.639  900.66117  2.2861954
4    5525.7 5260.049  265.65073  0.6743151
5    4675.9 4763.054  -87.15430 -0.2212283
6    4418.6 4762.487 -343.88696 -0.8729063
7    5845.4 6196.170 -350.76993 -0.8903777
8    7313.0 7151.013  161.98700  0.4111801
9    5035.4 5015.523   19.87679  0.0504543
10   4322.6 4578.100 -255.49955 -0.6485479
11   6389.5 6320.986   68.51398  0.1739126
12   4152.2 4011.888  140.31161  0.3561603
13   5544.8 4854.964  689.83567  1.7510460
14   6095.1 5946.538  148.56225  0.3771033
15   3626.2 3821.828 -195.62753 -0.4965716
16   3745.4 4074.296 -328.89643 -0.8348550
17   5121.8 5888.101 -766.30113 -1.9451423
18   5674.5 5747.967  -73.46670 -0.1864844
19   4256.6 4043.660  212.94024  0.5405174
20   5803.7 6008.946 -205.24580 -0.5209861

检验模型假定

  1. 检验线性关系
    绘制成分残差图
> library(car)
> crPlots(model )

在这里插入图片描述
横坐标:自变量的实际观测值
纵坐标:因变量与残差之和
图中显示销售收入与广告支出没有明显的非线性模式,说明二者之间的线性关系假定成立
2. 检验正态性
Q-Q图

> par(mfrow=c(2,2),cex=0.8,cex.main=0.7)
> plot(model)   # 绘制模型诊断图

在这里插入图片描述
右上Q-Q图,各个点基本上在直线周围随机分布,没有固定模式
左上与成分残差图作用类似
左下位置尺度图,与散布-水平图作用类似
右下残差与杠杆图,超纲了
3. 检验方差齐性

> ncvTest(model)
Non-constant Variance Score Test 
Variance formula: ~ fitted.values 
Chisquare = 1.126441, Df = 1, p = 0.28854

不拒绝原假设,回归模型满足方差齐性
4. 检验独立性

> durbinWatsonTest(model)
 lag Autocorrelation D-W Statistic p-value
   1       0.1330482      1.679232   0.506
 Alternative hypothesis: rho != 0

不拒绝原假设,显示残差无自相关

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值