Yellow_truffle
Yellow_truffle

Reputation: 923

Calculating confidence interval using "ci" function

I have a set of data and besides finding the average of columns for each group, I want to find a confidence interval. the sample data is shown below:

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

what I want to do is calculating the average and 0.95 confidence interval for the length parameter for each group of the result, so I used the code below:

g <- data %>% select(length,result) %>% group_by(result) %>% summarise(Ave_length=mean(length, na.rm=TRUE))

and to calculate the confidence interval for each group, I used the following function from gmodels package

ci(data$length[data$result=="TRUE"], 0.95)
ci(data$length[data$result=="TRUE"], 0.95)

Howveer, what I got is an warning message " Warning message: In ci.numeric(data$length[data$result == "TRUE"], 0.95) : No class or unkown class. Using default calcuation."

Do you have any suggestion on how I can fix this? or is there any other function I can use to calculate the confidence interval

Upvotes: 3

Views: 1240

Answers (1)

akrun
akrun

Reputation: 886938

There is nothing to worry about the warning message.

methods('ci')
#[1] ci.binom      ci.estimable* ci.lm*        ci.lme*       ci.numeric*  

If we check the source code, it starts with a warning without any check.

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
}

It is possible that the developers may change it in the future. Also, there are some typos unkown instead of unknown

It means that numeric class vectors are getting this warning

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.

This issue seems to be showing only for numeric class. If we apply the ci on a lm model (ci.lm)

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

as ci.lm doesn't have the warning at the beginning

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
}

Possible reason is that the ci methods are primarily checking for lm or lme class etc. and if none of them are found, it is switching to default mode with ci for numeric class and the warning is kind of misleading in that respect

Upvotes: 4

Related Questions