2

在此处输入图像描述

ggplot(data=Dane, aes(x=reg$fitted.values, y=reg$residuals))+ 
geom_smooth(method="lm", se=TRUE, level=0.95)+ 
theme(panel.background = element_rect(fill = "white", colour = "grey50"))+ 
geom_point()
4

1 回答 1

3

下面将使用内置数据集的一个子集iris,其中Species == "setosa".

请注意,为了获得预测的置信带,必须在绘图之前拟合线性模型。

library(ggplot2)

data(iris)

subdf <- iris[iris$Species == "setosa", ]

pred <- predict(lm(Sepal.Width ~ Sepal.Length, subdf), 
                se.fit = TRUE, interval = "confidence")
limits <- as.data.frame(pred$fit)

ggplot(subdf, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  theme(panel.background = element_rect(fill = "white", 
                                       colour = "grey50"))+
  geom_smooth(method = "lm") +
  geom_line(aes(x = Sepal.Length, y = limits$lwr), 
            linetype = 2) +
  geom_line(aes(x = Sepal.Length, y = limits$upr), 
            linetype = 2)

在此处输入图像描述

于 2018-12-03T17:01:29.153 回答