如何只绘制图样或ggplot2中的残差?

问题描述

如果我只想绘制残差,我可以做

plot(model$residuals)

,我将得到一个不错的散点图。如何在ggplot2或plotly中执行相同的操作? 我不想绘制残油vs拟合值。

谢谢, 阿迪

解决方法

也许是这样吗?

model <- lm(Sepal.Width ~ Petal.Length,data = iris)

ggplot(data.frame(x = seq(model$residuals),y = model$residuals)) +
  geom_point(aes(x,y)) +
  labs(x = "Index",y = "Residuals",title = paste("Residuals of",format(model$call)))

![enter image description here

比以下哪个更好:

plot(model$residuals)

enter image description here

,

类似于@AllanCameron,您可以使用broom包,该包还提供有关模型结果的其他选项(df中的变量):

library(ggplot2)
library(broom)
#Data
data("iris")
#Model
m1 <- lm(Sepal.Length~Sepal.Width,data=iris)
df <- augment(m1)
ggplot(df,aes(x = 1:nrow(df),y = .resid)) + geom_point() + xlab('x')

enter image description here