25
h"p://www.rtutor.com/ 1

hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

h"p://www.r-­‐tutor.com/  

1  

Page 2: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

“residuals”

Least squares: minimize the sum of the squared residuals

“fitted values”

2  

Page 3: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

3  

Page 4: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

4  

Page 5: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

5  

Page 6: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

6  

Page 7: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

7  

Page 8: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

8  

Page 9: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

9  

Page 10: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

10  

Page 11: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

11  

Page 12: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

12  

Page 13: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

13  

Page 14: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

50 60 70 80 90

1.5

2.0

2.5

3.0

3.5

4.0

4.5

5.0

waiting

eruptions

14  

Page 15: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

140 150 160 170 180

4060

80100

120

Height

Weight

15  

Page 16: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

140 150 160 170 180

4060

80100

120

Height

Weight

16  

Page 17: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

50 60 70 80

-20

020

4060

Fitted values

Residuals

lm(Weight ~ Height)

Residuals vs Fitted

99

205

267

17  

Page 18: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

-3 -2 -1 0 1 2 3

-20

24

6

Theoretical Quantiles

Sta

ndar

dize

d re

sidu

als

lm(Weight ~ Height)

Normal Q-Q

99

205

267

18  

Page 19: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

The  Bootstrap  

foo  <-­‐  rnorm(100)  t.test(foo)    b_means<-­‐rep(0,1000)  for  (i  in  1:1000)  {b_means[i]  <-­‐  mean(sample(foo,replace=TRUE))}  b_means  <-­‐  sort(b_means)  b_means[25]  b_means[975]  

19  

Page 20: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

bootstrapping the linear model

plot(sale.price ~ area, data=houseprices)houseprices.lm <- lm (sale.price ~ area, data=houseprices)houseprices.fn <- function(houseprices, index) { house.resample <- houseprices[index,] house.lm <- lm(sale.price ~ area, data=house.resample) abline(house.lm,lwd=0.3) coef(house.lm)[2]} houseprices.boot <- boot(houseprices, R=99, statistic=houseprices.fn)abline(houseprices.lm,col="red")

20  

Page 21: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

21  

Page 22: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

> houseprices.bootORDINARY NONPARAMETRIC BOOTSTRAPCall:boot(data = houseprices, statistic = houseprices.fn, R = 99)Bootstrap Statistics : original bias std. errort1* 0.1877769 0.01020854 0.07837381> summary(houseprices.lm)Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 70.7504 60.3477 1.172 0.2621 area 0.1878 0.0664 2.828 0.0142 *

22  

Page 23: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

houseprices.fn <- function(houseprices, index) { house.resample <- houseprices[index,] house.lm <- lm(sale.price ~ area, data=house.resample) predict(house.lm, newdata=data.frame(area=1200))}houseprices.boot <- boot(houseprices, R=99, statistic=houseprices.fn)boot.ci(houseprices.boot, type="perc")

BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONSBased on 99 bootstrap replicatesCALL : boot.ci(boot.out = houseprices.boot, type = "perc")Intervals : Level Percentile 95% (247.4, 363.1 ) Calculations and Intervals on Original ScaleSome percentile intervals may be unstable

23  

Page 24: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

houseprices2.fn <- function(houseprices, index) { house.resample <- houseprices[index,] house.lm <- lm(sale.price ~ area, data=house.resample) houseprices$sale.price - predict(house.lm,houseprices)} R<-200houseprices2.boot <- boot(houseprices, R=R, statistic=houseprices2.fn)par(mfrow=c(1,2))n <- length(houseprices$area)house.fac <- factor(rep(1:n,rep(R,n)))plot(house.fac,as.vector(houseprices2.boot$t),ylab="Prediction Errors",xlab="House")bootse <- apply(houseprices2.boot$t,2,sd)usualse <- predict(houseprices.lm,houseprices, se.fit=TRUE)$se.fitplot(bootse/usualse,ylab="ratio of bootse to usualse")abline(h=1)

24  

Page 25: hp:// )tutor.com/madigan/W2025/notes/... · The/Bootstrap/ foo

25