ggplot2 中的 qqnorm 和 qqline
假设有一个线性模型 LM,我想要残差的 qq 图。通常我会使用 R 基础图形:
qqnorm(residuals(LM), ylab="Residuals")
qqline(residuals(LM))
我可以弄清楚如何获得情节的 qqnorm 部分,但我似乎无法管理 qqline:
ggplot(LM, aes(sample=.resid)) +
stat_qq()
我怀疑我错过了一些非常基本的东西,但似乎应该有是一种简单的方法来做到这一点。
编辑:非常感谢以下解决方案。我已经修改了代码(非常轻微)以从线性模型中提取信息,以便该图的工作方式类似于 R 基础图形包中的便利图。
ggQQ <- function(LM) # argument: a linear model
{
y <- quantile(LM$resid[!is.na(LM$resid)], c(0.25, 0.75))
x <- qnorm(c(0.25, 0.75))
slope <- diff(y)/diff(x)
int <- y[1L] - slope * x[1L]
p <- ggplot(LM, aes(sample=.resid)) +
stat_qq(alpha = 0.5) +
geom_abline(slope = slope, intercept = int, color="blue")
return(p)
}
Say have a linear model LM that I want a qq plot of the residuals. Normally I would use the R base graphics:
qqnorm(residuals(LM), ylab="Residuals")
qqline(residuals(LM))
I can figure out how to get the qqnorm part of the plot, but I can't seem to manage the qqline:
ggplot(LM, aes(sample=.resid)) +
stat_qq()
I suspect I'm missing something pretty basic, but it seems like there ought to be an easy way of doing this.
EDIT: Many thanks for the solution below. I've modified the code (very slightly) to extract the information from the linear model so that the plot works like the convenience plot in the R base graphics package.
ggQQ <- function(LM) # argument: a linear model
{
y <- quantile(LM$resid[!is.na(LM$resid)], c(0.25, 0.75))
x <- qnorm(c(0.25, 0.75))
slope <- diff(y)/diff(x)
int <- y[1L] - slope * x[1L]
p <- ggplot(LM, aes(sample=.resid)) +
stat_qq(alpha = 0.5) +
geom_abline(slope = slope, intercept = int, color="blue")
return(p)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
下面的代码将为您提供您想要的情节。 ggplot 包似乎不包含用于计算 qqline 参数的代码,所以我不知道是否可以在(可理解的)单行代码中实现这样的图。
The following code will give you the plot you want. The ggplot package doesn't seem to contain code for calculating the parameters of the qqline, so I don't know if it's possible to achieve such a plot in a (comprehensible) one-liner.
您还可以使用此函数添加置信区间/置信带(部分代码复制自
car:::qqPlot
)示例:
You can also add confidence Intervals/confidence bands with this function (Parts of the code copied from
car:::qqPlot
)Example:
从版本 3.0 开始,相当于以下内容的
stat_qq_line
已找到了进入官方 ggplot2 代码 。旧版本:
从2.0版本开始,ggplot2就有了完善的扩展接口;所以我们现在可以轻松地自己为 qqline 编写一个新的统计数据(这是我第一次这样做,所以改进是 welcome):
这也概括了分布(与
stat_qq
完全相同),并且可以按如下方式使用:(不幸的是,由于 qqline 位于单独的层上,我找不到“重用”分布参数的方法,但这应该只是一个小问题。)
Since version 3.0, a
stat_qq_line
equivalent to the below has found its way into the officialggplot2
code.Old version:
Since version 2.0, ggplot2 has a well-documented interface for extension; so we can now easily write a new stat for the qqline by itself (which I've done for the first time, so improvements are welcome):
This also generalizes over the distribution (exactly like
stat_qq
does), and can be used as follows:(Unfortunately, since the qqline is on a separate layer, I couldn't find a way to "reuse" the distribution parameters, but that should only be a minor problem.)
线性模型的标准 QQ 诊断绘制了标准化残差的分位数与 N(0,1) 的理论分位数。 @Peter 的 ggQQ 函数绘制残差。下面的代码片段对此进行了修正,并添加了一些修饰性的更改,使绘图更像是从
plot(lm(...))
中得到的结果。使用示例:
The standard Q-Q diagnostic for linear models plots quantiles of the standardized residuals vs. theoretical quantiles of N(0,1). @Peter's ggQQ function plots the residuals. The snippet below amends that and adds a few cosmetic changes to make the plot more like what one would get from
plot(lm(...))
.Example of use:
在最新的ggplot2版本(>=3.0)中,实现了新函数
stat_qq_line
(https://github.com/tidyverse/ggplot2/blob/master/NEWS.md)和模型残差的 qq 行可以添加:rstandard(model)<需要 /code> 才能获得标准化残差。 (感谢 @jlhoward 和 @qwr)
如果您收到“stat_qq_line() 中的错误:无法找到函数“stat_qq_line””,则说明您的 ggplot2 版本太旧,您可以通过升级 ggplot2 软件包来修复它:
install。包(“ggplot2”)
。With the latest ggplot2 version (>=3.0), new function
stat_qq_line
is implemented (https://github.com/tidyverse/ggplot2/blob/master/NEWS.md) and a qq line for model residuals can be added with:rstandard(model)
is needed to get the standardized residual. (credit @jlhoward and @qwr)If you get an 'Error in stat_qq_line() : could not find function "stat_qq_line"', your ggplot2 version is too old and you can fix it by upgrading your ggplot2 package:
install.packages("ggplot2")
.为什么不是下面的呢?
给定一些向量,比如说,
但我们必须使用传统的图形函数来支撑 ggplot2,这似乎很奇怪。
难道我们不能通过从我们想要分位数图的向量开始,然后在 ggplot2 中应用适当的“stat”和“geom”函数来获得相同的效果吗?
Hadley Wickham 会监控这些帖子吗?也许他可以向我们展示更好的方法。
Why not the following?
Given some vector, say,
But it seems strange that we have to use a traditional graphics function to prop up ggplot2.
Can't we get the same effect somehow by starting with the vector for which we want the quantile plot and then applying the appropriate "stat" and "geom" functions in ggplot2?
Does Hadley Wickham monitor these posts? Maybe he can show us a better way.
你可以从那些用正态概率纸做这件事的老前辈那里借用一页。仔细观察 ggplot()+stat_qq() 图形表明可以使用 geom_abline() 添加参考线,如下所示
You could steal a page from the old-timers who did this stuff with normal probability paper. A careful look at a ggplot()+stat_qq() graphic suggests that a reference line can be added with geom_abline(), like this
ggplot2 v.3.0.0 现在有 qqline 统计数据。从帮助页面:
!ggplot2 v3.0.0 相当于 qqnorm 加 abline 的统计示例]1
ggplot2 v.3.0.0 now has an qqline stat. From the help page:
!ggplot2 v3.0.0 Example stats equivalent to qqnorm plus abline]1