forked from duty-machine/duty-machine
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
15 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
title: "ggplot2高效绘制残差图" | ||
date: 2023-10-09T16:54:33Z | ||
draft: ["false"] | ||
tags: [ | ||
"fetched", | ||
"R语言数据分析指南" | ||
] | ||
categories: ["Acdemic"] | ||
--- | ||
ggplot2高效绘制残差图 by R语言数据分析指南 | ||
------ | ||
<div><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com"><h3 data-tool="mdnice编辑器"><span></span><span><span></span>欢迎关注R语言数据分析指南</span><span></span></h3><p data-tool="mdnice编辑器">本节分享一个小案例,如何使用ggplot2中的<strong>「stat_smooth」</strong>函数来快速绘制残差图。</p><h3 data-tool="mdnice编辑器"><span></span><span><span></span>关注下方公众号下回更新不迷路</span><span></span></h3><section><mp-common-profile data-pluginname="mpprofile" data-id="Mzg3MzQzNTYzMw==" data-headimg="http://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAZF0rpeZII9Ltl26VbVagriczTria1fib3XgjwwHEHFjPzkmGpqWDVVHBSzhENictUM2iavAKiaM5lc9USw/0?wx_fmt=png" data-nickname="R语言数据分析指南" data-alias="YanJANtwo" data-signature="R语言重症爱好者,喜欢绘制各种精美的图表,喜欢的小伙伴可以关注我,跟我一起学习" data-from="0" data-is_biz_ban="0"></mp-common-profile></section><blockquote data-tool="mdnice编辑器"><span>❝</span><p>残差图是一种用于回归分析的图形工具,它显示了模型的预测值与实际观测值之间的差异,即残差。残差是观测值与模型预测值之间的差值。</p><span>❞</span></blockquote><p data-tool="mdnice编辑器"><strong>「残差图的主要目的是:」</strong></p><ol data-tool="mdnice编辑器"><li><section><strong>「检查线性回归模型的假设」</strong>:线性回归模型有几个关键的假设,如误差项的独立性、常数方差(同方差性)和误差项的正态性。残差图可以帮助我们检查这些假设是否得到满足。</section></li><li><section><strong>「识别模型中的异常值」</strong>:如果某些点在残差图上显著偏离其他点,它们可能是异常值或杠杆点,可能会影响模型的准确性。</section></li><li><section><strong>「检查模型的拟合情况」</strong>:如果残差图显示出某种模式或趋势,而不是随机分布的点,这可能意味着模型没有充分捕捉到数据中的某些信息或关系。</section></li></ol><p data-tool="mdnice编辑器">常见的残差图有:</p><ul data-tool="mdnice编辑器"><li><section><strong>「基本残差图」</strong>:y轴表示残差,x轴表示预测值或观测值。</section></li><li><section><strong>「标准化残差图」</strong>:y轴表示标准化残差,x轴表示预测值。</section></li><li><section><strong>「QQ图」</strong>:用于检查残差的正态分布假设。</section></li><li><section><strong>「Scale-Location图」</strong>:用于检查同方差性的假设。</section></li></ul><h3 data-tool="mdnice编辑器"><span></span><span><span></span>具体代码</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code><span># 使用mtcars数据集</span><br>mtcars %>% <br> ggplot(aes(wt,mpg)) +<br> geom_point() + <span># 添加散点图层,显示每辆车的重量和每加仑的英里数</span><br> geom_smooth() + <span># 基于wt和mpg的数据点拟合回归曲线</span><br> stat_smooth(geom=<span>"point"</span>,color=<span>"blue"</span>,xseq=mtcars$wt) +<br> <span># 添加一个平滑层,以线段的形式表示,x轴的序列和结束点都是mtcars$wt,y轴的结束点是mtcars$mpg</span><br> stat_smooth非常的高效,本(geom=<span>"segment"</span>,color=<span>"red"</span>,<br> xseq=mtcars$wt,<br> xend=mtcars$wt,<br> yend=mtcars$mpg) <span># 这些线段代表残差,即每个观测值与模型预测值之间的差异</span><br><br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.7" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXE0wYIYibTjDZ3RicibClVrpFcStjaMEQYia4RlCeAo1D97joHtQqZx8zTg/640?wx_fmt=png" data-type="png" data-w="1080" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXE0wYIYibTjDZ3RicibClVrpFcStjaMEQYia4RlCeAo1D97joHtQqZx8zTg/640?wx_fmt=png"></figure><blockquote data-tool="mdnice编辑器"><span>❝</span><p>可以看到使用stat_smooth非常的高效,本节内容介绍到此结束过程仅供参考;有需要学习个性化数据可视化的朋友,欢迎到小编的<strong>「淘宝店铺」</strong> <strong>「R语言数据分析指南」</strong>购买<strong>「2023年度会员文档」</strong>同步更新中<strong>「售价149元」</strong>,内容主要包括各种<strong>「高分论文的图表分析复现以及一些个性化图表的绘制」</strong>均包含数据+代码;按照往年数据小编年产出约在150+以上</p><span>❞</span></blockquote><p data-tool="mdnice编辑器">购买后微信发小编订单截图即邀请进新的会员交流群,小编的文档为按年售卖,只包含当年度的<strong>「除系列课程外」</strong>的文档,有需要往年文档的朋友也可下单购买,需要了解更多信息的朋友欢迎交流咨询。</p><h3 data-tool="mdnice编辑器"><span></span><span><span></span>淘宝店铺(淘宝扫一扫)</span><span></span></h3><p><img data-galleryid="" data-ratio="1.0210420841683367" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/EibnicgwScTAbvhPDLGT8NaialEsht92PTYNJWpmVLfoYGic1uha5FyBrDCibibZCLjiazgvpT1XcdwibfVywD2el0VAgg/640?wx_fmt=jpeg" data-type="jpeg" data-w="998" src="https://mmbiz.qpic.cn/mmbiz_jpg/EibnicgwScTAbvhPDLGT8NaialEsht92PTYNJWpmVLfoYGic1uha5FyBrDCibibZCLjiazgvpT1XcdwibfVywD2el0VAgg/640?wx_fmt=jpeg"></p><h3 data-tool="mdnice编辑器"><span></span><span><span></span>2023会员群案例展示</span><span></span></h3><p data-tool="mdnice编辑器"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKX8Q3wmDncicvmLWaTqujgMBjicGsoqjXatx0FMGG9HmmjriaZsz3fjDLqQ/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKX8Q3wmDncicvmLWaTqujgMBjicGsoqjXatx0FMGG9HmmjriaZsz3fjDLqQ/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXZwKaoPDnkdxWWTEZLj7ic0cCdIqKh7d8ICfTLLTRLK6j6LLnSKITRNQ/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXZwKaoPDnkdxWWTEZLj7ic0cCdIqKh7d8ICfTLLTRLK6j6LLnSKITRNQ/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXoeuCVXS9vT0zN6Dwq9NcpicJ5qpb56azavboMEsHhbrJPQWI8IMzP3A/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXoeuCVXS9vT0zN6Dwq9NcpicJ5qpb56azavboMEsHhbrJPQWI8IMzP3A/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXfKCVKWiaVPiaeOGNoWL5s5KjQDcFoyDvb1K9oUyrRqpUSY0fAB73HkicA/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXfKCVKWiaVPiaeOGNoWL5s5KjQDcFoyDvb1K9oUyrRqpUSY0fAB73HkicA/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXbeTyzUosSFDWMYH6aj7hrDibLgHL7Qhbut6ribC99bCDQibWoC6iaN7Syg/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXbeTyzUosSFDWMYH6aj7hrDibLgHL7Qhbut6ribC99bCDQibWoC6iaN7Syg/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXeibRAUJYHUXazHibURMYvXG2emJByZzkXRHKEkrmGPfRdfzXH6JNTZwQ/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXeibRAUJYHUXazHibURMYvXG2emJByZzkXRHKEkrmGPfRdfzXH6JNTZwQ/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXUstBIaTvUWSbZTo8hG5CwQbF0t1lyI8x9avxuOltwqnWJKERuxiaOzA/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXUstBIaTvUWSbZTo8hG5CwQbF0t1lyI8x9avxuOltwqnWJKERuxiaOzA/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXhicqRloZsMBUqLMZpTqattTg7VYS2HDPfSbtYG8XVfFKibYxDRafmt5g/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbGyOibTO9tzzjFict8XmWEKXhicqRloZsMBUqLMZpTqattTg7VYS2HDPfSbtYG8XVfFKibYxDRafmt5g/640?wx_fmt=png"></p></section><p><br></p><p><mp-style-type data-value="3"></mp-style-type></p></div> | ||
<hr> | ||
<a href="https://mp.weixin.qq.com/s/mFH32sVu-h3I42Wh3gv_NA",target="_blank" rel="noopener noreferrer">原文链接</a> |