--- title: "Porsche" author: "Chris Parrish" date: "January 19, 2016" output: pdf_document --- Porsche reference: - Cannon, et al., Stat2, chapter 04, example 4.10 Import the data. ```{r} data <- read.csv("PorschePrice.csv", header=TRUE) head(data) dim(data) ``` Scatterplot matrix. ```{r} pairs(~ Price + Age + Mileage, data=data, col="darkred") ``` View the data. ```{r} plot(Price ~ Mileage, data=data, pch=20, col="darkred", las=1) Porsche.lm <- lm(Price ~ Mileage, data=data) abline(Porsche.lm, col="orange") ``` Slope of the regression line. ```{r} Porsche.lm ``` Bootstrap slope. ```{r} sample.idx <- sample(1:30, replace=TRUE) Porsche.bootstrap.lm <- lm(Price ~ Mileage, data=data[sample.idx, ]) Porsche.bootstrap.lm ``` Bootstrap sampling distribution for sample slope. ```{r} n.trials <- 5000 results <- rep(NA, n.trials) for (i in 1:n.trials){ sample.idx <- sample(1:30, replace=TRUE) Porsche.bootstrap.lm <- lm(Price ~ Mileage, data=data[sample.idx, ]) results[i] <- coef(Porsche.bootstrap.lm)[2] } hist(results, las=1, col="wheat") ``` Bootstrap CI for slope ... method 1 (use bootstrap SE). ```{r} point.estimate <- coef(Porsche.lm)[2] alpha <- 0.05 z.star <- qnorm(c(0.025, 0.975)) se.bootstrap <- sd(results) se.bootstrap ci1 <- point.estimate + z.star * se.bootstrap ci1 ``` Bootstrap CI for slope ... method 2 (use bootstrap quantiles). ```{r} ci2 <- quantile(results, c(0.025, 0.975)) ci2 ``` Bootstrap CI for slope ... method 3 (reverse the bootstrap quantiles). ```{r} qs <- quantile(results, c(0.025, 0.975)) qL <- qs[1] qU <- qs[2] ci3 <- c(point.estimate - (qU - point.estimate), point.estimate + (point.estimate - qL)) ci3 ``` Bootstrap CI for slope ... three methods. ```{r} CI.table <- rbind(ci1, ci2, ci3) colnames(CI.table) <- c("lwr", "upr") CI.table ```