Search code examples
rconfidence-intervalbonferroni

Bonferroni Simultaneous Confidence Intervals of differences in means


I am trying to obtain Bonferroni simultaneous confidence intervals in R. I have the following data set that I made up for practice:

df2 <- read.table(textConnection(
  'group value
  1 25 
  2 36
  3 42
  4 50
  1 27
  2 35
  3 49
  4 57
  1 22
  2 37
  3 45
  4 51'), header = TRUE)

I have tried

aov(formula = value ~ group, data = df2)

However, this doesn't output simultaneous confidence intervals. Using SAS, the calculations should come out as:

enter image description here


Solution

  • There seem to be some conceptual/coding mistakes.

    1. df$group needs to be a categorical variable for your ANOVA to work. At the moment it is numeric.
    2. You want to perform what's called a post-hoc analysis, to correct ANOVA p-values for multiple group comparisons.

    Here is an example using the R package DescTools, based on the sample data you give:

    # Step 1: Make sure that group is a factor
    df2$group <- as.factor(df2$group);
    
    # Step 2: Perform ANOVA
    res <- aov(formula = value ~ group, data = df2)
    
    # Step 3: Perform post-hoc analysis
    require(DescTools);
    PostHocTest(res, method = "bonferroni");
    #
    #  Posthoc multiple comparisons of means : Bonferroni
    #    95% family-wise confidence level
    #
    #$group
    #         diff     lwr.ci   upr.ci    pval
    #2-1 11.333333  3.0519444 19.61472 0.00855 **
    #3-1 20.666667 12.3852778 28.94806 0.00014 ***
    #4-1 28.000000 19.7186111 36.28139 1.5e-05 ***
    #3-2  9.333333  1.0519444 17.61472 0.02648 *
    #4-2 16.666667  8.3852778 24.94806 0.00067 ***
    #4-3  7.333333 -0.9480556 15.61472 0.09062 .
    #
    #---
    #Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    

    The reported differences between the group means and confidence intervals match the SAS numbers you give.