Search code examples
rconfidence-interval

confidence interval result not computing with DescTools::MedianCI


I ran this code to get the confidence intervals for the 4 different groups in my dataset. However for group 1, it seems that the upper and lower confidence intervals are not being computed.

 Valve.size valvemedian lowerci upperci
  <fct>            <dbl>   <dbl>   <dbl>
1 1                 5760    -Inf     Inf
2 2                 7150    6100    8090
3 3                 9210    8320   10400
4 4                10450    9520   13700

This is the code that I used to get to the result before - it worked for the other groups except the first one:

library(DescTools)
barplot2 <- barplot %>% 
  dplyr::group_by(Valve.size) %>% 
  dplyr::summarise(valvemedian = MedianCI(DAP, na.rm = TRUE)[1],
                   lowerci = MedianCI(DAP, na.rm = TRUE)[2],
                   upperci = MedianCI(DAP, na.rm = TRUE)[3])

Solution

  • Based on some quick experiments, it looks like the default method for DescTools::MedianCI ("exact") returns (-Inf, Inf) as the confidence intervals whenever there are 5 or fewer observations in the vector. In contrast, method = "boot" appears to return non-infinite confidence intervals down to n=2 (n=1 gives an error).

    If you do switch to using method = "boot" you should call set.seed(...) (with the integer value of your choice) to make sure the results are reproducible.

    It would be possible to dig further into the code and figure out why (the procedure starts (for 95% confidence intervals) by computing the critical value for a binomial test with probability = 0.5: qbinom(0.025, size = n, prob = 0.5, lower.tail = TRUE) &mdash which comes out to 0 for n=5 and 1 for n=6, that's probably where the trouble starts ...) What looks like a description of the rationale for the exact method (which may be more technical than you want) is in an answer to this CrossValidated question.