20 Apr 2011

Bootsrap Confidence Intervals, Stratified Bootstrap

 Here's a worked example for comparing group averages with bootstrap confidence intervals and allowing for different subsample sizes by calling the strata argument within the bootstrap function.
The data (simulated) is set up analogous to an before-after impact experiment conducted on plots across 4 levels of a grouping factor ('stage'). Similarities were calculated for each composition before and after an impact and will be averaged over the grouping factor. Our hypothesis was that the levels of the grouping factor would show significantly different average similarities - that is, a higher/lower impact on composition. As plots were aggregated in different sites within the 'stages', this dependency had to be allowed for by use of the "strata" argument in the boot.ci call.

The conclusion from this simulated example would be that the averages similarities at stages C and D are significantly different from stages A and B. That is, as the similarities are higher in C and D than in A and B, impact on composition is significantly lower in C and D.

library(boot)
library(Hmisc)
 
### a factor 'stage' and a variable 'MH-Index'
stage<-sort(as.factor(rep(LETTERS[1:4], 30)))
site<-gl(12, 10)
MH.Index<-runif(4*30, 0, 1)
 
### simulate effect:
n = 30
eff <- c(rep(0.25, n), rep(0.4, n), rep(0.5, n), rep( 0.65,30))
 
MH.Index <- MH.Index*eff
 
### dataframe:
print(sim<-data.frame(stage, site, MH.Index))
 
### function needed for the bootstrap:
mean.fun <- function(x, index){mean(x[index])}
 
### set up dataframe to collect results for plotting:
pldat<-data.frame(mean=rep(NA,4),low=rep(NA,4),upp=rep(NA,4))
rownames(pldat)<-c("A","B","C","D")
pldat
 
for(i in c("A","B","C","D")){
### the boot sample and the ci's:
    boot<-boot(sim$MH.Index[sim$stage==i], mean.fun, R=1000, strata=sim$site[sim$stage==i])
    ci<-boot.ci(boot,type = "norm")
### get ci's (method: normal)
    ci[2]->pldat[i,"mean"] # mean
    ci$normal[1,2]->pldat[i,"low"]  # lower ci
    ci$normal[1,3]->pldat[i,"upp"]} # upper ci

### plot:
errbar(c(1:4),pldat$mean,pldat$upp,pldat$low,ylab="MH-Similarity Index",xlab="Stage",
       pch=15,xaxt="n",xlim=c(0.5,4.5))
axis(1,c(1:4),labels=row.names(pldat))
legend("topleft","errorbars =\n95%-normal\nbootstrap\nconf. int",bty="n")
###

No comments :

Post a Comment