# Random Draw sample(1:10,10,replace=T) sample(1:10,10,replace=F) sample(1:10,10,replace=T,prob=1:10) rbinom(n=1,size=1,prob=0.3) rbinom(5,2,0.5) rmultinom(n=10,size=1,prob=rep(1,10)) rmultinom(10,1,rep(1,5)) # Bootstrap in R d.gas<-read.table("~/mywork/econ508/AUTO2.txt",header=T) attach(d.gas) gas<-ts(gas,start=1959,frequency=4) price<-ts(price,start=1959,frequency=4) income<-ts(income,start=1959,frequency=4) miles<-ts(miles,start=1959,frequency=4) price2<-price^2 princ<-price*income # 1. Residual Bootstrap fit.star<-lm(gas~income+price+price2+princ) uhat<-fit.star$resid R<-1000 # Number of Repetions h<-rep(0,R) for (i in 1:R){ gash <- fit.star$fit + sample(uhat,replace=TRUE) b <- lm(gash~income+price+price2+princ)$coef h[i] <- (-(1+b[3]+b[5]*log(15))/(2*b[4])) } quantile(h,probs=c(0.025,0.975)) # 2. Data (xy) Bootstrap n<-length(gas) R<-1000 # Number of Repetions a<-rep(0,R) for (i in 1:R){ s <- sample(1:n,replace=TRUE) f <- lm(gas[s]~income[s]+price[s]+price2[s]+princ[s]) coefs <- f$coefficients a[i] <- (-(1+coefs[3]+coefs[5]*log(15))/(2*coefs[4])) } quantile(a,probs=c(0.025,0.975))