Reputation: 49
How to calculate relative risk in R?
In input i have
names(DS)
Tab<-table(DS[,5],DS[,11],DS[,3])
No Yes
No 4 16
Yes 40 168
I am new in R programming language...
Upvotes: 3
Views: 25515
Reputation: 226332
This is a fairly straightforward calculation; the relative risk is just (pos1/total1)/(pos2/total2)
where pos1
is the number of cases in the first group, pos2
in the second group, and the total
variables are the group totals
However, you're probably interested in the epitab
function of the epitools
package:
## maybe:
## install.packages("epitools")
library("epitools")
See ?epicalc
for input formats, but this should work for your example:
tab <- matrix(c(4,16,40,168),byrow=TRUE,nrow=2)
epitab(tab,method="riskratio")
## $tab
## Outcome
## Predictor Disease1 p0 Disease2 p1 riskratio lower upper
## Exposed1 4 0.2000000 16 0.8000000 1.000000 NA NA
## Exposed2 40 0.1923077 168 0.8076923 1.009615 0.8030206 1.269361
## Outcome
## Predictor p.value
## Exposed1 NA
## Exposed2 1
You should of course double-check that the results make sense; e.g. p0
is 4/20=0.2 for the first group, 40/208=0.192 for the second group. The risk ratio is ((16/20)/(168/208))=0.8/0.8076=1.009.
Upvotes: 26