简体   繁体   English

R中的置信区间计算

[英]Confidence interval calculation in R

I'm finding differences when trying to calculate the CI in R:我在尝试计算 R 中的 CI 时发现了差异:

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!谢谢!

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.您的 1.96 是来自标准正态分布的所需分位数的近似值,随着样本量趋于无穷大,它渐近等效于学生 t 分布。 With your sample size of N = 6, there are considerable differences between the standard normal and a student's t distribution.当您的样本量为 N = 6 时,标准正态分布和学生 t 分布之间存在相当大的差异。

Here is the calculation of the desired quantile as per Stéphane's comment:这是根据 Stéphane 的评论计算所需的分位数:

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)代表 package (v1.0.0) 于 2021 年 4 月 9 日创建

Additional to the-mad-statter and Stéphane.除了-mad-statter 和 Stéphane。

This is the function for the calculation of CI in Rmisc package:这是用于计算 Rmisc package 中 CI 的Rmisc

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在这里您可以找到更多更深入的信息: https://stats.stackexchange.com/questions/467015/what-is-the-confidence-interval-formula-that-rmisc-package-in-r-uses

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM