3

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

JayPeerachai
  • 3,499
  • 3
  • 14
  • 29
Remy M
  • 599
  • 1
  • 4
  • 17

1 Answers1

8

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.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • Fairly new to R. Does factor basically mean categorical? – Remy M Feb 01 '18 at 22:38
  • @RemyM Yes, that's basically correct, see e.g. [here](https://www.stat.berkeley.edu/classes/s133/factors.html) for more details. – Maurits Evers Feb 01 '18 at 22:39
  • Having some difficulty installing DescTools. Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) : namespace ‘Rcpp’ 0.12.9 is already loaded, but >= 0.12.10 is required Error: package or namespace load failed for ‘DescTools’ – Remy M Feb 01 '18 at 22:44
  • Can you try quitting R, and then doing `install.packages("DescTools")` from a fresh R terminal? What is your R version? – Maurits Evers Feb 01 '18 at 22:45
  • It worked and I obtained the necessary confidence intervals. Thank you very much for your help! – Remy M Feb 01 '18 at 22:47
  • Great! Glad to help @RemyM. – Maurits Evers Feb 01 '18 at 22:48