From f464c08ec52976942e36a4f6c988a96c43f416fe Mon Sep 17 00:00:00 2001 From: Kevin Carriere Date: Tue, 21 Jun 2016 21:15:18 -0400 Subject: [PATCH] Add GGPlot2 code to markdown This markdown had the base plot in twice, and again the lecture is taught in ggplot2, so copied off the video and included the proper code in the file. --- 07_RegressionModels/01_07_inference/index.Rmd | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) 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 + ``` ---