Check out the R Epi and epitools packages, which include many functions for computing exact and approximate CIs/p-values for various measures of association found in epidemiological studies, including relative risk (RR). I know there is also PropCIs, but I never tried it. Bootstraping is also an option, but generally these are exact or approximated CIs that are provided in epidemiological papers, although most of the explanatory studies rely on GLM, and thus make use of odds-ratio (OR) instead of RR (although, wrongly it is often the RR that is interpreted because it is easier to understand, but this is another story).
You can also check your results with online calculator, like on statpages.org, or Relative Risk and Risk Difference Confidence Intervals. The latter explains how computations are done.
By "exact" tests, we generally mean tests/CIs not relying on an asymptotic distribution, like the chi-square or standard normal; e.g. in the case of an RR, an 95% CI may be approximated as
$\exp\left[ \log(\text{rr}) - 1.96\sqrt{\text{Var}\big(\log(\text{rr})\big)} \right], \exp\left[ \log(\text{rr}) + 1.96\sqrt{\text{Var}\big(\log(\text{rr})\big)} \right]$,
where $\text{Var}\big(\log(\text{rr})\big)=1/a - 1/(a+b) + 1/c - 1/(c+d)$ (assuming a 2-way cross-classification table, with $a$, $b$, $c$, and $d$ denoting cell frequencies). The explanations given by @Keith are, however, very insightful.
For more details on the calculation of CIs in epidemiology, I would suggest to look at Rothman and Greenland's textbook, Modern Epidemiology (now in it's 3rd edition), Statistical Methods for Rates and Proportions, from Fleiss et al., or Statistical analyses of the relative risk, from J.J. Gart (1979).
You will generally get similar results with fisher.test()
, as pointed by @gd047, although in this case this function will provide you with a 95% CI for the odds-ratio (which in the case of a disease with low prevalence will be very close to the RR).
Notes:
- I didn't check your Excel file, for the reason advocated by @csgillespie.
- Michael E Dewey provides an interesting summary of confidence intervals for risk ratios, from a digest of posts on the R mailing-list.
Your question leave some considerable doubt about what, exactly, this in 'mimic this' consists of. You should be more explicit.
Do you want one sample confidence intervals for the means?
Then t.test
can do it easily, by doing it one sample at a time.
t.test(x,conf.int=TRUE)
t.test(y,conf.int=TRUE)
You can even extract the confidence interval part of the output and if you want, assign it to a variable:
t.test(y,conf.int=TRUE)$conf.int
(Or do you want to know how to produce a particular display? That's easy enough, once the values are calculated.)
Best Answer
You can use the Delta method to obtain an approximate distribution of your relative risk, as shown by that link. Then you can define a pivot and use this to obtain a CI.
I understand that there might be some confusion regarding the use of the Delta method, so here are a few simple steps that show how to construct an approximate CI for the relative risk.
Now you need the standard error. Using the Delta method for sample sizes $n$ and $m$ with probabilities $p$ and $q$ respectively, this is found to be
$$SE=\sqrt{\frac{1-p}{pn}+\frac{1-q}{qm}}$$
Of course you need to replace the unknown quantities with your estimates, let's denote them by $\widehat{p}$ and $\widehat{q}$. You might notice that this is the second approximation we are using.
Now that you have the formula, compute the standard error: $SE$
Calculate the lower and upper limits on the log scale: $\log(RR) scale: \log(RR) ± 1.96 \times SE \log(RR)$
Exponentiate!
You can find plenty such information throughout the internet and the above steps are taken from here. We all have Fisher to thank for these approximations!