我有一个矢量:
vector <- c(12, 17, 24, 35, 23, 34, 56)
如何在R?
中计算此向量的置信区间(90%,99%,95%)这是我想要的结果示例: enter image description here
答案 0 :(得分:4)
这是一个根据t分布计算置信区间的函数:
confidence_interval <- function(vector, interval) {
# Standard deviation of sample
vec_sd <- sd(vector)
# Sample size
n <- length(vector)
# Mean of sample
vec_mean <- mean(vector)
# Error according to t distribution
error <- qt((interval + 1)/2, df = n - 1) * vec_sd / sqrt(n)
# Confidence interval as a vector
result <- c("lower" = vec_mean - error, "upper" = vec_mean + error)
return(result)
}
您提供的向量和间隔的示例用法:
> vector <- c(12, 17, 24, 35, 23, 34, 56)
> confidence_interval(vector, 0.90)
lower upper
17.97255 39.45602
> confidence_interval(vector, 0.95)
lower upper
15.18797 42.24060
> confidence_interval(vector, 0.99)
lower upper
8.219946 49.208626
我this is the tutorial从中开发了这种方法。