bootstrap.R

richard — Oct 15, 2013, 12:44 PM

set.seed(1244)
data <- rgamma(100,shape=5,rate=2.3)
hist(data,freq=FALSE,breaks=20,xlim=c(0,6))

plot of chunk unnamed-chunk-1

mean(data)
[1] 2.141
sd(data)
[1] 1.04
qnorm(0.95)
[1] 1.645
interval <- c(mean(data)-qnorm(0.95)*sd(data)/sqrt(100),mean(data)-qnorm(0.05)*sd(data)/sqrt(100))
interval
[1] 1.970 2.312
nboot <- 1000
n <- 100
results <- numeric(1000)
for (i in 1:1000){
  bootdata <- sample(data,replace=TRUE)
  results[i] <- sqrt(n)*(mean(bootdata)-mean(data))/sd(bootdata)
}
hist(results)

plot of chunk unnamed-chunk-1

results.q <- sort(results)
interval.boot <- c(mean(data)-results.q[950]*sd(data)/sqrt(100),mean(data)-results.q[50]*sd(data)/sqrt(100))
interval.boot
[1] 1.972 2.333
interval
[1] 1.970 2.312
hist(data,freq=FALSE,breaks=20,xlim=c(0,6))
abline(v=mean(data),col="blue")
abline(v=interval.boot,col="red")
abline(v=interval,col="green")

plot of chunk unnamed-chunk-1