2016-04-19 153 views
4

我想有一个关于我从lm()模型中得到的残差的好的情节。目前我使用plot(model$residuals),但我想要更好的东西。如果我尝试用ggplot绘制它,我得到错误信息:如何用ggplot绘制lm()的残差?

GGPLOT2不知道如何应对数字

回答

16

强化不再推荐,根据哈德利可能会被弃用。

可以使用扫帚包做类似的(更好)的东西:为gg版本的回归诊断图的

library(broom) 
y <-rnorm(10) 
x <-1:10 
mod <- lm(y ~ x) 
df <- augment(mod) 
ggplot(df, aes(x = .fitted, y = .resid)) + geom_point() 
2

ggplot想要一个data.frame类的数据。 fortify将为您制作一个。

y <-rnorm(10) 
x <-1:10 
mod <- lm(y ~ x) 
modf <- fortify(mod) 
ggplot(modf, aes(x = .fitted, y = .resid)) + geom_point() 
+0

你也可以通过MOD直接 – user20650

+0

向上最新的选择是使用'扫帚::整洁()' – ikashnitsky

6

使用ggfortify::autoplot()。看到这个vignette


fit <- lm(mpg ~ hp, data = mtcars) 

library(ggfortify) 
autoplot(fit) 

enter image description here