interpret_or2: Help with interpreting odds ratios (using 2x2 table values)

View source: R/interpret_or2.R

interpret_or2R Documentation

Help with interpreting odds ratios (using 2x2 table values)

Description

I always get tripped up interpreting odds ratios. Especially when trying to make sense of results from logistic regression. This function seems to help me out.

Unlike lamsic::interpret_or, this function take the cells (a, b, c, and d) of a 2x2 contingency table as inputs directly rather than columns of a data frame.

The function returns a list with a 2x2 table, a sample interpretation, and the odds ratio with Wald confidence interval. This can then be compared to logisitc regression results and make sure that thing are making sense.

Much of this is owed to the ExploringDataBlog

Usage

interpret_or2(
  a,
  b,
  c,
  d,
  dim_names = list(exposure_status = c("Exposed", "Unexposed"), outcome_status =
    c("Postive", "Negative")),
  alpha = 0.05
)

Arguments

a

Count of the upper left quadrant of 2x2 contingency table

b

Count of upper right

c

Count of lower left

d

Count of lower right

dim_names

A list; labels or the rows and columns of the 2x2 contingency table. Default is dim_names = list(exposure_status = c("Exposed", "Unexposed"), outcome_status = c("Positive", "Negative"))

alpha

Default = 0.05. The significance level for the two-sided Wald confidence interval.

Value

A list with the following:

table

2x2 contingency table

interpretation

Sample interpretation of the odds ratio of the outcome and the exposure levels

results

Odds ratio and Wald confidence interval

fishers

Results of Fisher's test

chisq

Results of Chi-square test

References

https://exploringdatablog.blogspot.com/2011/05/computing-odds-ratios-in-r.html

https://stats.idre.ucla.edu/other/mult-pkg/faq/general/faq-how-do-i-interpret-odds-ratios-in-logistic-regression/

Examples

library(dplyr)
library(forcats)
library(readr)
library(broom)
library(janitor)

#### Example 1 --------------------------------
mydata <- admissions
mydata <- mydata %>%
  mutate(rank = factor(rank),
         rank = forcats::fct_collapse(rank,
                                      "1" = c("1", "2"),
                                      "2" = c("3", "4")),
         admit = factor(admit,
                        levels = c(1, 0),
                        labels = c("Yes", "No")))

glm((admit == "Yes") ~ rank,
    data = mydata,
    family = binomial(link = "logit")) %>%
  broom::tidy(., exponentiate = TRUE)

# Get a 2x2 table
janitor::tabyl(dat = mydata,
               rank,
               admit)

# Note that I flip the values to match the refernce level in the logistic
# regression
interpret_or2(a = 40,
              b = 148,
              c = 87,
              d = 125,
              dim_names = list(rank = c("2", "1"),
                               admit = c("Yes", "No")))

#### Example 2 --------------------------------

dis_df <- tibble::tibble(
  Outcome = sample(c("Diseased", "Non-diseased"),
                   size = 100,
                   replace = TRUE,
                   prob = c(0.25, 0.75)),
  Exposure = sample(c("Exposed", "Unexposed"),
                   size = 100,
                   replace = TRUE,
                   prob = c(0.40, 0.60))) %>%
  mutate_all(.tbl = .,
             .funs = list(~ factor(.)))

# Get a 2x2 table
janitor::tabyl(dat = dis_df,
               Exposure,
               Outcome) %>%
  janitor::adorn_title(placement = "combined")


interpret_or2(a = 11,
              b = 28,
              c = 15,
              d = 46,
              dim_names = list(Exposure = c("Exposed", "Unexposed"),
                               Outcome = c("Diseased", "Non-diseased")))


#### Example 3 --------------------------------

sample_df <- hsb_sample
sample_df <- sample_df %>%
  mutate(female = factor(female,
                         levels = c(0, 1),
                         labels = c("male", "female")))

xtabs(~ female + hon,
      data = sample_df)

glm((hon == 1) ~ female,
    data = sample_df,
    family = binomial(link = "logit")) %>%
  broom::tidy(., exponentiate = TRUE)

interpret_or2(a = 32,
              b = 77,
              c = 17,
              d = 74,
              dim_names = list(Sex = c("Female", "Male"),
                               Honors = c("Yes", "No")))

emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.