Skip to content

Instantly share code, notes, and snippets.

@jeffwong
Created March 27, 2014 08:21
Show Gist options
  • Select an option

  • Save jeffwong/9802843 to your computer and use it in GitHub Desktop.

Select an option

Save jeffwong/9802843 to your computer and use it in GitHub Desktop.
Confidence Interval for the ratio of two means. http://en.wikipedia.org/wiki/Fieller%27s_theorem
fieller = function(x,y) {
x.mean = mean(x)
y.mean = mean(y)
x.sd = sd(x)
y.sd = sd(y)
xy.cov = cov(x,y)
print(sprintf("x has mean %s, y has mean %s", x.mean, y.mean))
print(sprintf("ratio of means is %s", x.mean/y.mean))
g = pt(.05,length(y) - 1)^2*(y.sd^2)/(y.mean^2)
lower = (1 / (1-g)) * ((x.mean/y.mean) - g*xy.cov/(y.sd^2) - pt(.05, length(y) - 1)/y.mean * sqrt (x.sd^2 - 2*x.mean/y.mean*xy.cov + x.mean^2/y.mean^2 * y.sd^2 - g*(x.sd^2 - xy.cov^2/y.sd^2)))
upper = (1 / (1-g)) * ((x.mean/y.mean) - g*xy.cov/(y.sd^2) + pt(.05, length(y) - 1)/y.mean * sqrt (x.sd^2 - 2*x.mean/y.mean*xy.cov + x.mean^2/y.mean^2 * y.sd^2 - g*(x.sd^2 - xy.cov^2/y.sd^2)))
list(lower, upper)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment