[R] Confidence interval for coefficient of variation

From: Kevin Wright <kw.statr_at_gmail.com>
Date: Thu, 14 Jun 2007 08:41:45 -0500


This is a function I coded a few years ago to calculate a confidence interval for a coefficient of variation. The code is based on a paper by Mark Vangel in The American Statistician. I have not used the function much, but it could be useful for comparing cv's from different groups.

Kevin Wright

confint.cv <- function(x,alpha=.05, method="modmckay"){
# Calculate the confidence interval of the cv of the vector x
# Author: Kevin Wright
# See: Vangel, Mark. Confidence Intervals for a Normal Coefficient
# of Variation. American Statistician, Vol 15, No1, p. 21--26.
# x <- c(326,302,307,299,329)
# confint.cv(x,.05,"modmckay")

  x <- na.omit(x)
  v <- length(x)-1
  mu <- mean(x)
  sigma <- sqrt(var(x))
  k <- sigma/mu
# CV > .33 may give poor results, so warn the user
  if(k>.33) warning("Confidence interval may be very approximate.")

  method <- casefold(method) # In case we see "McKay"

  if(method=="mckay"){
    # McKay method. See equation 15.

    t1 <- qchisq(1-alpha/2,v)/v
    t2 <- qchisq(alpha/2,v)/v
    u1 <- v*t1
    u2 <- v*t2

    lower <- k/sqrt(( u1/(v+1) -1)*k*k + u1/v)     upper <- k/sqrt(( u2/(v+1) -1)*k*k + u2/v)   } else if (method=="naive"){
    # Naive method. See equation 17.
    t1 <- qchisq(1-alpha/2,v)/v
    t2 <- qchisq(alpha/2,v)/v
    lower <- k/sqrt(t1)
    upper <- k/sqrt(t2)
  } else {
    # Modified McKay method. See equation 16.     u1 <- qchisq(1-alpha/2,v)
    u2 <- qchisq(alpha/2,v)
    lower <- k/sqrt(( (u1+2)/(v+1) -1)*k*k + u1/v)     upper <- k/sqrt(( (u2+2)/(v+1) -1)*k*k + u2/v)   }
  ci <- c(lower,upper)
  attr(ci,"CV") <- k
  attr(ci,"alpha") <- alpha
  return(ci)
}

R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 14 Jun 2007 - 13:56:29 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 14 Jun 2007 - 14:32:02 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.