r语言中残差与回归值的残差图_R语言:多元线性回归和模型检验
利用swiss数据集进行多元线性回归研究
# 先查看各变量间的散点图
pairs(swiss, panel = panel.smooth, main = "swiss data",
col = 3 + (swiss$Catholic > 50))
# 利用全部变量建立多元线性回
a=lm(Fertility ~ . , data = swiss)
summary(a)
##
## Call:
## lm(formula = Fertility ~ ., data = swiss)
##
## Residuals:
## Min 1Q Median 3Q Max
## -15.274 -5.262 0.503 4.120 15.321
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 66.9152 10.7060 6.25 1.9e-07 ***
## Agriculture -0.1721 0.0703 -2.45 0.0187 *
## Examination -0.2580 0.2539 -1.02 0.3155
## Education -0.8709 0.1830 -4.76 2.4e-05 ***
## Catholic 0.1041 0.0353 2.95 0.0052 **
## Infant.Mortality 1.0770 0.3817 2.82 0.0073 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.17 on 41 degrees of freedom
## Multiple R-squared: 0.707, Adjusted R-squared: 0.671
## F-statistic: 19.8 on 5 and 41 DF, p-value: 5.59e-10
# 从结果看,Education变量的p值一颗星就都没有,说明对模型极不显著。
# R中提供了add1 drop1函数来针对线性模型进行变量的增减处理
drop1(a)
## Single term deletions
##
## Model:
## Fertility ~ Agriculture + Examination + Education + Catholic +
## Infant.Mortality
## Df Sum of Sq RSS AIC
## 2105 191
## Agriculture 1 308 2413 195
## Examination 1 53 2158 190
## Education 1 1163 3268 209
## Catholic 1 448 2553 198
## Infant.Mortality 1 409 2514 197
# 从结果看,去掉Education这个变量后,AIC最小,所以下一步可以剔除该变量进行建模。
b=update(a,.~.-Education)
summary(b)
##
## Call:
## lm(formula = Fertility ~ Agriculture + Examination + Catholic +
## Infant.Mortality, data = swiss)
##
## Residuals:
## Min 1Q Median 3Q Max
## -23.919 -3.553 -0.649 6.596 14.177
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 59.6027 13.0425 4.57 4.2e-05 ***
## Agriculture -0.0476 0.0803 -0.59 0.55669
## Examination -0.9680 0.2528 -3.83 0.00042 ***
## Catholic 0.0261 0.0384 0.68 0.50055
## Infant.Mortality 1.3960 0.4626 3.02 0.00431 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 8.82 on 42 degrees of freedom
## Multiple R-squared: 0.545, Adjusted R-squared: 0.501
## F-statistic: 12.6 on 4 and 42 DF, p-value: 8.27e-07
#从接下来的结果看,有两个变量不显著,R平方也仅有0.53,模型效果极不理想。需要进一步进行研究。
# 幸好R有step函数,可以对模型进行变量自动筛选,根据AIC最小原则进行
b=step(a,direction="backward")
## Start: AIC=190.7
## Fertility ~ Agriculture + Examination + Education + Catholic +
## Infant.Mortality
##
## Df Sum of Sq RSS AIC
## - Examination 1 53 2158 190
## 2105 191
## - Agriculture 1 308 2413 195
## - Infant.Mortality 1 409 2514 197
## - Catholic 1 448 2553 198
## - Education 1 1163 3268 209
##
## Step: AIC=189.9
## Fertility ~ Agriculture + Education + Catholic + Infant.Mortality
##
## Df Sum of Sq RSS AIC
## 2158 190
## - Agriculture 1 264 2422 193
## - Infant.Mortality 1 410 2568 196
## - Catholic 1 957 3115 205
## - Education 1 2250 4408 221
summary(b)
##
## Call:
## lm(formula = Fertility ~ Agriculture + Education + Catholic +
## Infant.Mortality, data = swiss)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.676 -6.052 0.751 3.166 16.142
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 62.1013 9.6049 6.47 8.5e-08 ***
## Agriculture -0.1546 0.0682 -2.27 0.0286 *
## Education -0.9803 0.1481 -6.62 5.1e-08 ***
## Catholic 0.1247 0.0289 4.31 9.5e-05 ***
## Infant.Mortality 1.0784 0.3819 2.82 0.0072 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.17 on 42 degrees of freedom
## Multiple R-squared: 0.699, Adjusted R-squared: 0.671
## F-statistic: 24.4 on 4 and 42 DF, p-value: 1.72e-10
接下来,对建模的变量和模型进行回归诊断的研究
首先,对自变量进行正态性检验
shapiro.test(swiss$Agriculture)
##
## Shapiro-Wilk normality test
##
## data: swiss$Agriculture
## W = 0.9664, p-value = 0.193
shapiro.test(swiss$Examination)
##
## Shapiro-Wilk normality test
##
## data: swiss$Examination
## W = 0.9696, p-value = 0.2563
shapiro.test(swiss$Education)
##
## Shapiro-Wilk normality test
##
## data: swiss$Education
## W = 0.7482, p-value = 1.312e-07
shapiro.test(swiss$Catholic)
##
## Shapiro-Wilk normality test
##
## data: swiss$Catholic
## W = 0.7463, p-value = 1.205e-07
shapiro.test(swiss$Infant.Mortality)
##
## Shapiro-Wilk normality test
##
## data: swiss$Infant.Mortality
## W = 0.9776, p-value = 0.4978
对各变量的正态性检验结果来看,变量Education和Catholic的p值小于0.05,故这两个变量数据不符合正态性分布。
现在,对模型的残差也进行正态性检验(回归模型的残差也要符合正态分布)
b.res
shapiro.test(b.res)
##
## Shapiro-Wilk normality test
##
## data: b.res
## W = 0.9766, p-value = 0.459
从结果来看,p值是0.459,模型残差符合正态分布
接下来,画出回归值与残差的残差图(应该符合均匀分布,即残差不管回归值如何,都具有相同分布)
par(mfrow=c(1,2))
# 画出残差图
plot(b.res~predict(b))
# 画出标准残差图
plot(rstandard(b)~predict(b))
par(mfrow=c(1,1))
从残差图来看,效果不太明显.
其实,可以直接画出残差图
par(mfrow=c(2,2))
plot(b)
par(mfrow=c(1,1))
End.
作者:谢佳标
来源:天善智能
本文均已和作者授权,如转载请与作者联系。
r语言中残差与回归值的残差图_R语言:多元线性回归和模型检验相关推荐
- r语言中残差与回归值的残差图_R语言逻辑回归、方差分析、伪R平方分析
原标题:R语言逻辑回归.方差分析.伪R平方分析 原文链接:http://tecdat.cn/?p=9589 目录 怎么做测试 假设条件 并非所有比例或计数都适用于逻辑回归分析 过度分散 伪R平方 测试 ...
- r语言中残差与回归值的残差图_R语言基础-数据分析及常见数据分析方法
R表达式中常用的符号 残差(Residuals) 残差是真实值与预测值之间的差,五个分位的值越小模型越精确 系数项与截距项(Coefficients & Intercept)和P值指标 残差标 ...
- r语言中残差与回归值的残差图_R语言-如何处理回归中的异常值点
R语言-如何处理回归中的异常值点 异常观测值 一个全面的回归分析要覆盖对异常值的分析,包括离群点.高杠杆值点和强影响点.这些数据点需要更深入的研究,因为它们在一定程度上与其他观测点不同,可能对结果产生 ...
- r语言中残差与回归值的残差图_独家 | 手把手教你用R语言做回归后的残差分析(附代码)-阿里云开发者社区...
作者:Abhijit Telang 翻译:张睿毅 校对:丁楠雅 文章来源:微信公众号 数据派THU 本文介绍了做残差分析的方法及其重要性,以及利用R语言实现残差分析. 在这篇文章中,我们通过探索残差分 ...
- r语言中残差与回归值的残差图_用R语言做回归分析_iris数据集/longley数据集
机器学习课程2 回归分析 [题目1] 使用R对内置鸢尾花数据集iris(在R提示符下输入iris回车可看到内容)进行回归分析,自行选择因变量和自变量,注意Species这个分类变量的处理方法. 解答 ...
- r语言中的或怎么表示什么不同_R语言中灵活运用if实现根据不同条件执行不同的语句...
原标题:R语言中灵活运用if实现根据不同条件执行不同的语句 在程序语言中,if一般属于一种分支结构,即根据某个条件执行相关的语句,在R中也不例外.同时,if语句与else配合可以运用到多种条件递归判断 ...
- r语言中的或怎么表示什么不同_R语言中$是什么意思
展开全部 $是S3类的引用方32313133353236313431303231363533e58685e5aeb931333431353430式,@是S4类的引用方式.$比较常用,@比较少用.通常我 ...
- C/C++ 语言中的表达式求值
转载地址:http://www.cnblogs.com/heyonggang/p/3340301.html 在此,首先向裘老师致敬! 裘宗燕:C/C++ 语言中的表达式求值 经常可以在一些讨论组里看到 ...
- 决定c++语言中函数的返回值类型的是,全国2009年10月高等教育自学考试C++程序设计试题及部分参考答案...
全国2009年10月高等教育自学考试 C++程序设计试题 课程代码:04737 一.单项选择题(本大题共20小题,每小题1分,共20分) 在每小题列出的四个备选项中只有一个是符合题目要求的,请将其代码 ...
最新文章
- RabbitMQ的集群模式
- 高房价可控制人口增长与人口素质?
- 树莓派外设开发基础(wiringPi库)
- python读写文件实例_python读写文件的简单示例
- javaScript设计模式之常用工厂模式
- 【ElasticSearch】Es 源码之 PeerRecoverySourceService 源码解读
- CCNP精粹系列之十三-----OSPF路由汇总
- 研究生开学需要准备的事宜(准研究生和保研必看)
- Unity3D学习1--Unity基础
- 我的她 —— 记我的 IBM R51
- Android Bluetooth HCI log 详解
- 华硕PRIME Z390-P主板设置开启虚拟化技术
- oracle 亿级数据存储方案
- 文本框的左视图不见了?
- QListWidgt QListView QTableWidget QTableView 去掉虚线框
- python下载arcgis地图_使用下载器下载适用于arcgis的离线地图切片
- hr 0x800401f0 尚未调用 CoInitialize
- js进阶之你必须要会的技术!
- win10+vs2019配置lite.ai.toolkit预编译库
- 机器学习—线性回归与R模拟
热门文章
- POW,POS,DPOS共识机制简单介绍
- java二进制对象_java 二进制搜索
- 破解360doc个人图书馆网站的右键、复制方法
- 液晶分子平行排列MATLAB程序,液晶百问:液晶分子排列方向如何改变
- VS2015正式版出炉-----
- html中加重点符号,html特殊符号对照表 html怎么插入特殊符号
- Unity使用RenderTexture实现实时阴影绘制
- c++ 摇摆序列 (贪心+状态自动机)
- 触摸屏手势控制镜头旋转与缩放
- 都是自动挡,AT/CVT/AMT双离合究竟谁最强?