Created
March 27, 2014 08:21
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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