我有一组数据,除了找到每组列的平均值外,我还想找到置信区间。样本数据如下所示:
id <- c(1101:1108)
age <- c(12,15,14,12,3,1,2,5)
length <- c(52,62,63,58,79,45,65,25)
result <- c("TRUE","FALSE","TRUE","FALSE","TRUE","FALSE","TRUE","FALSE")
data<-data.frame(id, age, length, result)
id age length result
1 1101 12 52 TRUE
2 1102 15 62 FALSE
3 1103 14 63 TRUE
4 1104 12 58 FALSE
5 1105 3 79 TRUE
6 1106 1 45 FALSE
7 1107 2 65 TRUE
8 1108 5 25 FALSE
我想做的是计算每组结果的长度参数的平均值和0.95置信区间,所以我使用了下面的代码:
g <- data %>% select(length,result) %>% group_by(result) %>% summarise(Ave_length=mean(length, na.rm=TRUE))
为了计算每组的置信区间,我使用了gmodels
包中的以下函数
ci(data$length[data$result=="TRUE"], 0.95)
ci(data$length[data$result=="TRUE"], 0.95)
Howveer,我收到的是一条警告信息";警告消息:在ci.number(data$length[data$result=="TRUE"],0.95(中:没有类或未知类。使用默认计算">
你对我如何解决这个问题有什么建议吗?或者有没有其他函数可以用来计算置信区间
warning
消息无需担心。
methods('ci')
#[1] ci.binom ci.estimable* ci.lm* ci.lme* ci.numeric*
如果我们检查源代码,它以warning
开始,而不进行任何检查。
getAnywhere('ci.numeric')
function (x, confidence = 0.95, alpha = 1 - confidence, na.rm = FALSE,
...)
{
warning("No class or unkown class. Using default calcuation.") ####
est <- mean(x, na.rm = na.rm)
stderr <- sd(x, na.rm = na.rm)/sqrt(nobs(x))
ci.low <- est + qt(alpha/2, nobs(x) - 1) * stderr
ci.high <- est - qt(alpha/2, nobs(x) - 1) * stderr
retval <- c(Estimate = est, `CI lower` = ci.low, `CI upper` = ci.high,
`Std. Error` = stderr)
retval
}
开发人员将来可能会对其进行更改。此外,还有一些打字错误unkown
而不是unknown
这意味着numeric
类vector
正在获得此警告
ci(rnorm(10))
# Estimate CI lower CI upper Std. Error
# 0.3754708 -0.2600370 1.0109787 0.2809300
#Warning message:
#In ci.numeric(rnorm(10)) :
# No class or unkown class. Using default calcuation.
这个问题似乎只显示在numeric
类中。如果我们在lm
模型(ci.lm
(上应用ci
ci(lm(Sepal.Length ~ Species, iris))
# Estimate CI lower CI upper Std. Error p-value
#(Intercept) 5.006 4.8621258 5.149874 0.07280222 1.134286e-113
#Speciesversicolor 0.930 0.7265312 1.133469 0.10295789 8.770194e-16
#Speciesvirginica 1.582 1.3785312 1.785469 0.10295789 2.214821e-32
因为ci.lm
在开始时没有warning
getAnywhere('ci.lm')
function (x, confidence = 0.95, alpha = 1 - confidence, ...)
{
x <- summary(x)
est <- coef(x)[, 1]
ci.low <- est + qt(alpha/2, x$df[2]) * coef(x)[, 2]
ci.high <- est - qt(alpha/2, x$df[2]) * coef(x)[, 2]
retval <- cbind(Estimate = est, `CI lower` = ci.low, `CI upper` = ci.high,
`Std. Error` = coef(x)[, 2], `p-value` = coef(x)[, 4])
retval
}
可能的原因是,ci
方法主要检查lm
或lme
class
等,如果没有找到它们,则它将切换到默认模式,ci
用于numeric
类,而warning
在这方面有点误导