Relative Risk Example

Plain vanilla R does not have a built in function for computing confidence intervals for relative risk. But we can make our own relative risk function.

In [7]:
RRvariance = function(s1,s2,n1,n2) {
    # Returns the variance of the natural log of the relative risk.
    # s1 and s2 are the numbers of successess in groups 1 and 2, respectively. n1 and n2 are the totals for the two groups.
    1/s1-1/n1+1/s2-1/n2
}
In [5]:
var = RRvariance(1392,1748,5348,8471)
var
Out[5]:
0.000985437578357588

Then to compute the 95% confidence interval, we use the formula $RRe^{\pm z^* \sigma }$. Remember that the standard deviation $\sigma$ is the square root of the variance.

In [6]:
RR = (1392/5348)/(1748/8471)
RR*exp(-1.96*sqrt(var))
RR*exp(1.96*sqrt(var))
Out[6]:
1.18609630397013
Out[6]:
1.34141212519129
In [ ]: