一元线性回归

只有具备线性关系的数据才能够应用线性回归方法进行拟合,因此在进行回归分析之前首先要观察数据是否具有线性关系。对于没有明显线性关系的数据则没有必要做线性回归。现在我们拥有X、Y两组数据,我们首先观察一下数据是否具有线性关系。

x <- c(274, 180, 375, 205, 86, 265, 98, 800, 195, 53, 430, 372, 236, 157, 600, 370)
y <- c(162, 120, 223, 131, 67, 169, 81, 192, 116, 55, 252, 234, 144, 103, 250, 212)

lm.reg <- lm(y ~ x + 1)
plot(x,y)
summary(lm.reg)

从散点图上看,X、Y基本上呈现线性关系,从结果上看假设检验中p-value均小于0.05,说明数据符合一元线性关系。

Call:
lm(formula = y ~ x + 1)

Residuals:
    Min      1Q  Median      3Q     Max 
-96.801 -20.061  -0.433  23.398  59.526 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 80.52707   19.06532   4.224 0.000850 ***
x            0.26034    0.05449   4.778 0.000295 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 41.51 on 14 degrees of freedom
Multiple R-squared:  0.6198,    Adjusted R-squared:  0.5927 
F-statistic: 22.83 on 1 and 14 DF,  p-value: 0.0002946

我们看到Adjusted R-squared: 0.5927,这个值有点低,理论上讲这个值越接近1越好,值太小说明数据有异常点,需要我们进行残差分析去除异常点在进行回归分析。

res <- residuals(lm.reg)
plot(abs(res))
identify(abs(res))
残差图

我们用鼠标点选残差较大的点作为异常点,显示X、Y的第8个点为异常点。接着我们去除这个点。

x1 <- x[-8]
y1 <- y[-8]
lm.res(y1~x1+1)
summary(lm.res)

Call:
lm(formula = y1 ~ x1 + 1)

Residuals:
    Min      1Q  Median      3Q     Max 
-51.190  -8.939  -0.250  11.014  31.034 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 42.70434   10.72448   3.982  0.00156 ** 
x1           0.43081    0.03617  11.911 2.29e-08 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 20.03 on 13 degrees of freedom
Multiple R-squared:  0.9161,    Adjusted R-squared:  0.9096 
F-statistic: 141.9 on 1 and 13 DF,  p-value: 2.285e-08

我们看到p-value均小于0.05,并且Adjusted R-squared: 0.9096已经比较接近1了,高于上次的0.5927,说明去除异常点后回归质量进一步提升,方法有效可用。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容