library(MASS)
galaxies
ng<-length(galaxies)

hist(galaxies,nclass=25)

iqr.actual<-quantile(galaxies,prob=c(0.25,0.75))
abline(v=iqr.actual,col="red")

nboot<-5000
iqr.boot<-matrix(0,nrow=nboot,ncol=2)
for(iboot in 1:nboot){
	boot.gal<-sample(galaxies,size=ng,rep=T)
	iqr.boot[iboot,]<-quantile(boot.gal,prob=c(0.25,0.75))
}

rug(iqr.boot[,1],col="green")
rug(iqr.boot[,2],col="blue")

library(boot)

set.seed(101)
gal.boot.lower <- boot(galaxies, function(x, i) quantile(x[i],prob=c(0.25)), R = 5000)
gal.boot.upper <- boot(galaxies, function(x, i) quantile(x[i],prob=c(0.75)), R = 5000)


lower.interval<-boot.ci(gal.boot.lower, conf = 0.95,type = c("norm","basic","perc","bca"))
names(lower.interval)
hist(iqr.boot[,1],xlab="Lower Quartile",
     main="Lower Quartile 5000 bootstrap resamples",col="black",border="white")
abline(v=lower.interval$normal,col="green")
abline(v=lower.interval$percent,col="yellow")
abline(v=lower.interval$bca,col="red")

upper.interval<-boot.ci(gal.boot.upper, conf = 0.95,type = c("norm","basic","perc","bca"))
hist(iqr.boot[,2],xlab="Upper Quartile",
     main="Upper Quartile 5000 bootstrap resamples",col="black",border="white")
abline(v=upper.interval$normal,col="green")
abline(v=upper.interval$percent,col="yellow")
abline(v=upper.interval$library,col="red")

#####################################################################

library(multicore)
gal.boot.upper <- boot(galaxies, function(x, i) quantile(x[i],prob=c(0.75)), R = 5000)
gal.boot.upper.para <- boot(galaxies, function(x, i) quantile(x[i],prob=c(0.25)), R = 5000,parallel="multicore")

