Software and Example Code for Bootstrap Confidence Intervals for Means and Differences in Means
The following uses the simple nonparametric bootstrap percentile approach in
R
. To obtain 0.95 confidence intervals for a single mean use
library(Hmisc)
smean.cl.boot(x, B=2000)
To get the confidence interval for the difference in two means, say between treatments A and B:
bootdif <- function(y, g) {
g <- as.factor(g)
a <- attr(smean.cl.boot(y[g==levels(g)[1]], B=2000, reps=TRUE),'reps')
b <- attr(smean.cl.boot(y[g==levels(g)[2]], B=2000, reps=TRUE),'reps')
meandif <- diff(tapply(y, g, mean, na.rm=TRUE))
a.b <- quantile(b-a, c(.025,.975))
res <- c(meandif, a.b)
names(res) <- c('Mean Difference','.025','.975')
res
}
bootdif(blood.pressure, treatment)
To show bootstrap nonparametric CLs for 3 group means and for pairwise differences on the same graph:
set.seed(1)
group <- sample(c('a','b','d'), 200, TRUE)
y <- runif(200) + .25*(group=='b') + .5*(group=='d')
cla <- smean.cl.boot(y[group=='a'],reps=TRUE)
a <- attr(cla,'reps')
clb <- smean.cl.boot(y[group=='b'],reps=TRUE)
b <- attr(clb,'reps')
cld <- smean.cl.boot(y[group=='d'],reps=TRUE)
d <- attr(cld,'reps')
a.b <- quantile(a-b,c(.025,.975))
a.d <- quantile(a-d,c(.025,.975))
b.d <- quantile(b-d,c(.025,.975))
errbar(c('a','b','d','a - b','a - d','b - d'),
c(cla[1], clb[1], cld[1],
cla[1]-clb[1], cla[1]-cld[1], clb[1]-cld[1]),
c(cla[3], clb[3], cld[3], a.b[2], a.d[2], b.d[2]),
c(cla[2], clb[2], cld[2], a.b[1], a.d[1],b.d[1]),
Type=c(1,1,1,2,2,2))
Bootstrap confidence intervals for means and differences in means