2
votes

I'm finding differences when trying to calculate the CI in R :

x=c(25,30,15,45,22,54)

#IC 1
install.packages("Rmisc")
library(Rmisc)
CI(x,0.95) # [16.30429  ; 47.36238]

#IC2
lclm=mean(x)-(1.96*sd(x)/sqrt(length(x))) #19.99285
Uclm=mean(x)+(1.96*sd(x)/sqrt(length(x))) #43.67382

I want to know why I don't get same intervals with the two ways. Thank you!

2
1.96 is not the correct factor. It should be a quantile of a Student t-distribution. Take a look at the way to get a confidence interval for the mean of a Gaussian sample.Stéphane Laurent

2 Answers

1
votes

Your 1.96 is an approximation of the desired quantile from the standard normal distribution which is asymptotically equivalent to a student t-distribution as the sample size tends toward infinity. With your sample size of N = 6, there are considerable differences between the standard normal and a student's t distribution.

Here is the calculation of the desired quantile as per Stéphane's comment:

library(Rmisc)

x <- c(25, 30, 15, 45, 22, 54)

#IC 1
CI(x, 0.95) 
#>    upper     mean    lower 
#> 47.36238 31.83333 16.30429

#IC2
m <- mean(x)
s <- sd(x)
n <- length(x)
q <- qt(1 - 0.05 / 2, n - 1)
c(
  "upper" = m + q * s / sqrt(n), 
  "mean"  = m, 
  "lower" = m - q * s / sqrt(n)
)
#>    upper     mean    lower 
#> 47.36238 31.83333 16.30429

Created on 2021-04-09 by the reprex package (v1.0.0)

1
votes

Additional to the-mad-statter and Stéphane.

This is the function for the calculation of CI in Rmisc package:

function (x, ci = 0.95) 
{
    a <- mean(x)
    s <- sd(x)
    n <- length(x)
    error <- qt(ci + (1 - ci)/2, df = n - 1) * s/sqrt(n)
    return(c(upper = a + error, mean = a, lower = a - error))
}

Here you can find more deeper information: https://stats.stackexchange.com/questions/467015/what-is-the-confidence-interval-formula-that-rmisc-package-in-r-uses