# Calculate bonferroni-adjusted confidence intervals for a one-way layout
ci.bonf <-
function(response, group, alpha=0.05)
{
# calculate anova table
anova.out <- anova(aov(response~group))
# within-group mean square
ms <- anova.out[2,3]
# number of individuals per group
n <- tapply(response, group, length)
# se's: different for each pair if the sample sizes are different
se <- sqrt(ms * outer(n,n, function(a,b) 1/a + 1/b))
# pick out lower triangle
se <- se[lower.tri(se)]
# multipier from t distribution
df <- anova.out[2,1] # degrees of freedom
k <- length(n)
ntests <- choose(k, 2) # number of pairs
tmult <- qt(1 - alpha/ntests, df)
# calculate the pairwise differences between the sample means
me <- tapply(response, group, mean)
diffs <- outer(me,me,"-")
# pick out the negative of the lower triangle
d <- -diffs[lower.tri(diffs)]
# assign names to these
rn <- rownames(diffs)[row(diffs)[lower.tri(row(diffs))]]
cn <- colnames(diffs)[col(diffs)[lower.tri(col(diffs))]]
names(d) <- paste(cn,rn,sep=" - ")
cbind(est=d, lower=d-tmult*se, upper=d+tmult*se)
}