背景
现在,我正在创建一个多预测器线性模型,并生成诊断图以评估回归假设。 (这是我目前钟爱的多元回归分析统计信息类:-)
我的教科书(Cohen,Cohen,West和Aiken,2003年)建议针对残差绘制每个预测变量,以确保:
关于第(2)点,我的教科书上写着:
Some statistical packages allow the analyst to plot lowess fit lines at the mean of the residuals (0-line), 1 standard deviation above the mean, and 1 standard deviation below the mean of the residuals....In the present case {their example}, the two lines {mean + 1sd and mean - 1sd} remain roughly parallel to the lowess {0} line, consistent with the interpretation that the variance of the residuals does not change as a function of X. (p. 131)
如何修改黄土线?
我知道如何生成带有“0行”的散点图:
# First, I'll make a simple linear model and get its diagnostic stats
library(ggplot2)
data(cars)
mod <- fortify(lm(speed ~ dist, data = cars))
attach(mod)
str(mod)
# Now I want to make sure the residuals are homoscedastic
qplot (x = dist, y = .resid, data = mod) +
geom_smooth(se = FALSE) # "se = FALSE" Removes the standard error bands
但是,有谁知道我如何使用
ggplot2
和qplot
生成将叠加0线,“均值+ 1sd”和“均值-1sd”线的图?这是一个很奇怪/复杂的问题吗?
最佳答案
道歉
亲爱的,我要为自己的无知向您道歉。哈德利绝对是正确的,答案一直就在我面前。正如我所怀疑的那样,我的问题是统计上的而不是程序上的无知。
我们免费获得68%的置信区间geom_smooth()
默认为loess
平滑,并且在处理过程中将+ 1sd和-1sd线叠加在一起。这就是哈德利(Hadley)说的意思:“这不只是68%的置信区间吗?”我只是完全忘记了68%的间隔,并一直在寻找我已经知道该怎么做的东西。通过指定geom_smooth(se = FALSE)
实际上关闭了代码中的置信区间并没有帮助。
我的示例代码应该看起来像什么
# First, I'll make a simple linear model and get its diagnostic stats.
library(ggplot2)
data(cars)
mod <- fortify(lm(speed ~ dist, data = cars))
attach(mod)
str(mod)
# Now I want to make sure the residuals are homoscedastic.
# By default, geom_smooth is loess and includes the 68% standard error bands.
qplot (x = dist, y = .resid, data = mod) +
geom_abline(slope = 0, intercept = 0) +
geom_smooth()
我学到了什么
哈德利(Hadley)实现了一种非常漂亮,简单的方法来获得我一直想要的东西。但是因为我专注于黄土线,所以我没有看到68%的置信区间受我需要的线限制的事实。抱歉给大家带来麻烦。
关于r - 如何在ggplot2 qplot上叠加经过修改的黄土线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2531402/