Confidence interval calculation in R

Viewed 244

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 Answers

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)

Related