diff --git a/07_RegressionModels/01_07_inference/index.Rmd b/07_RegressionModels/01_07_inference/index.Rmd index 57a40b612..5eeeada7a 100644 --- a/07_RegressionModels/01_07_inference/index.Rmd +++ b/07_RegressionModels/01_07_inference/index.Rmd @@ -151,17 +151,26 @@ lines(xVals, yVals - 2 * se2) --- ## Plotting the prediction intervals +## GGPlot2 + ```{r, fig.height=5, fig.width==5, echo = FALSE, results='hide'} -plot(x, y, frame=FALSE,xlab="Carat",ylab="Dollars",pch=21,col="black", bg="lightblue", cex=2) -abline(fit, lwd = 2) -xVals <- seq(min(x), max(x), by = .01) -yVals <- beta0 + beta1 * xVals -se1 <- sigma * sqrt(1 / n + (xVals - mean(x))^2/ssx) -se2 <- sigma * sqrt(1 + 1 / n + (xVals - mean(x))^2/ssx) -lines(xVals, yVals + 2 * se1) -lines(xVals, yVals - 2 * se1) -lines(xVals, yVals + 2 * se2) -lines(xVals, yVals - 2 * se2) +library(ggplot2) +newx = data.frame(x = seq(min(x), max(x), length=100)) +p1 = data.frame(predict(fit, newdata= newx,interval = ("confidence"))) +p2 = data.frame(predict(fit, newdata = newx,interval = ("prediction"))) +p1$interval = "confidence" +p2$interval = "prediction" +p1$x = newx$x +p2$x = newx$x +dat = rbind(p1, p2) +names(dat)[1] = "y" + +g = ggplot(dat, aes(x = x, y = y)) +g = g + geom_ribbon(aes(ymin = lwr, ymax = upr, fill = interval), alpha = 0.2) +g = g + geom_line() +g = g + geom_point(data = data.frame(x = x, y=y), aes(x = x, y = y), size = 2) +g + ``` ---