## 12.4 Estimate the odds ratio

We’ll use the oddsratio function from the epitools package to calculate the odds ratio ($$\hat{OR}$$) and its 95% confidence interval.

Check out the help file for the function:

?oddsratio

The oddsratio function expects the contingency table to be arranged exactly like this:

#            treatment control
#  sick          a        b
#  healthy       c        d

If you were calculating the odds ratio by hand, using the letters shown in the table above, the shortcut formula is:

$\hat{OR} = \frac{{a/c}}{{b/d}}$

Here’s the code for producing the appropriately formatted 2 x 2 table as so:

cancer %>%
tabyl(aspirinTreatment, response) %>%
select(Cancer, "No cancer")
##  Cancer No cancer
##    1438     18496
##    1427     18515

So thats what the oddsratio function is expecting as input.

However, it’s also expecting it in the form of a “matrix” object.

Here’s all the code at once, and we’ll store the output (which comes in the form of a “list”) in an object called “cancer.odds”:

cancer.odds <- cancer %>%
tabyl(aspirinTreatment, response) %>%
select(Cancer, "No cancer") %>%
as.matrix() %>%
oddsratio(method = "wald")

We’ve seen the first two lines before. Then:

• we select the two columns associated with the “Cancer” and “No cancer” data. NOTE that because there’s a space in the variable name “No cancer”, we need to use quotation marks around it
• Then we use the base as.matrix function to coerce the resulting 2 x 2 table that we’ve created into a matrix type object, which is what the oddsratio function is expecting.
• lastly we run the oddsratio function, with the argument “method = ‘wald’” (don’t worry about why)

Let’s have a look at the rather verbose output:

cancer.odds
## $data ## Cancer No cancer Total ## row1 1438 18496 19934 ## row2 1427 18515 19942 ## Total 2865 37011 39876 ## ##$measure
##                         NA
## odds ratio with 95% C.I. estimate     lower    upper
##                     [1,] 1.000000        NA       NA
##                     [2,] 1.008744 0.9349043 1.088415
##
## $p.value ## NA ## two-sided midp.exact fisher.exact chi.square ## [1,] NA NA NA ## [2,] 0.8224348 0.8310911 0.8223986 ## ##$correction
##  FALSE
##
## attr(,"method")
##  "Unconditional MLE & normal approximation (Wald) CI"

What we’re interested in is the information under the “$measure” part, and specifically the “odds ratio with 95% C.I.”. To limit the output to the relevant information, use this code: cancer.odds$measure[2,]
##  estimate     lower     upper
## 1.0087436 0.9349043 1.0884148
This isolates the actual estimate of the odds ratio ($$\hat{OR}$$) with its 95% confidence interval.