假设有一个线性模型LM,我想要一个残差的qq图.通常我会使用R基础图形:
qqnorm(residuals(LM), ylab="Residuals")
qqline(residuals(LM))
Run Code Online (Sandbox Code Playgroud)
我可以弄清楚如何获得情节的qqnorm部分,但我似乎无法管理qqline:
ggplot(LM, aes(sample=.resid)) +
stat_qq()
Run Code Online (Sandbox Code Playgroud)
我怀疑我遗漏了一些非常基本的东西,但似乎应该有一种简单的方法来做到这一点.
编辑:非常感谢下面的解决方案.我已经修改了代码(非常轻微)以从线性模型中提取信息,以便绘图的工作方式类似于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)
}
Run Code Online (Sandbox Code Playgroud)