Home > Software engineering >  Are the confidence intervals in compareGroups R package accurate?
Are the confidence intervals in compareGroups R package accurate?

Time:09-05

I've been using R package "compareGroups" for a while now. But I've been curious and playing with it and I don't understand how compareGroups computes confidence intervals.

data <- data.frame(a = c(1:20, 1:20), b = c(rep("A",20), rep("B",20)))

I get (with conf.level = 0.95 as default)

> compareGroups(b~a , data = data.frame(a = c(1:20, 1:20), b = c(rep("A",20), rep("B",20)))) %>% 
    createTable(digits = 2, show.ci = T)

--------Summary descriptives table by 'b'---------

_________________________________________________ 
          A                  B          p.overall 
         N=20               N=20                  
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
a 10.50 [7.73;13.27] 10.50 [7.73;13.27]   1.000   
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 

But then, if i try manually, I get those results :

> mean(1:20) - qnorm(0.975)*sd(1:20)/sqrt(20)
[1] 7.907211
> mean(1:20)   qnorm(0.975)*sd(1:20)/sqrt(20)
[1] 13.09279

Which are somehow different than what I got from the compareGroups %>% createTable function

Does any of you know how this package works ? Or maybe am I wrong somewhere calculating my confidence interval ?

Thank you for your help.

CodePudding user response:

I think compareGroups calculates the confidence interval using a t-test by default, so it assumes a t-distribution rather than the normal distribution.

If you use:

mean(1:20) - qt(0.975, df = 19)*sd(1:20)/sqrt(20)
[1] 7.731189

mean(1:20)   qt(0.975, df = 19)*sd(1:20)/sqrt(20)
[1] 13.26881

You get the same values as from compareGroups.

See this for more information.

  • Related