I've been tasked with plotting the plot explained in the title here's my code so far:
model<-lm(y~.,data=data)
pred <- prediction(model,data=data,interval = 'confidence')
lwr<-pred[76:150]
upr<-pred[151:225]
interval<-cbind(lwr,upr)
I'm not sure where to go after this because I can't figure out how plot the interval.
You can do this with ggplot2
library(tidyverse)
data(mtcars)
mylm <- lm(mpg ~ wt, data = mtcars)
summary(mylm)
myc <- predict(mylm, newdata = mtcars$wt, interval = "confidence")
fit <- myc[,1]
low <- myc[,2]
high <- myc[,3]
myp <- predict(mylm, newdata = mtcars, interval = "predict")
mtcars %>%
ggplot() +
geom_point(aes(x = wt, y = mpg)) +
geom_point(aes(x = wt, y = fit), color = "green") +
geom_line(aes(x = wt, y = fit), color = "green") +
geom_point(aes(x = wt, y = low), color = "red") +
geom_line(aes(x = wt, y = low), color = "red") +
geom_point(aes(x = wt, y = high), color = "red") +
geom_line(aes(x = wt, y = high), color = "red") +
geom_point(aes(x = wt, y = myp[,1]), color = "blue") +
geom_line(aes(x = wt, y = myp[,1]), color = "blue") +
geom_point(aes(x = wt, y = myp[,2]), color = "darksalmon") +
geom_line(aes(x = wt, y = myp[,2]), color = "darksalmon") +
geom_point(aes(x = wt, y = myp[,3]), color = "darksalmon") +
geom_line(aes(x = wt, y = myp[,3]), color = "darksalmon")
Fitted are the blue, the confidence is the red, and the prediction the dark salmon, observed are the black.
By the way I know you can place wt and mpg into the ggplot()
and have it affect all geom's but I just prefer to do it this way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.