## estimate standard errors and C.I.'s using bootstrapping ## data and part 2 from Data Analysis and Graphics Using R, ## by Maindonald and Braun source('A:/cuckoos.R'); cuckoos wren<-split(cuckoos$length,cuckoos$species)$wren #19.8 22.1 21.5 20.9 22.0 21.0 22.3 21.0 20.3 20.9 22.0 20.0 20.8 21.2 21.0 length(wren) #15 ########################## # 1. programming on my own ########################## nboot<-999 n<-length(wren) x<-NULL for (i in 1:nboot){ x<-c(x,median(sample(wren,n,replace=T))) } se<-sd(x); se #0.2054436 #95% confidence interval quantile(x,c(.025,.975)) #20.9 21.5 ############################### # 2. using R built-in functions ############################### library(boot) ## First define median.fun(), with two required arguments: ## data specifies the data vector, ## indices selects vector elements for a each resample median.fun <- function(data, indices){median(data[indices])} ## Call the boot() function, with statistic=median.fun wren.boot <- boot(data = wren, statistic = median.fun, R = nboot) wren.boot original bias std. error t1* 21 0.04954955 0.2219365 boot.ci(boot.out=wren.boot,type="perc") Intervals : Level Percentile 95% (20.8, 22.0 ) Calculations and Intervals on Original Scale