Odds Ratio & Relative Risk in R: Effect Sizes for 2×2 Tables

Quantify association in a 2×2 table — the odds ratio and the relative risk, with confidence intervals, and which to use for cohort vs case-control studies

Biostatistics

Learn to compute the odds ratio and the relative risk in R for a 2×2 contingency table — the standard effect sizes in epidemiology and clinical research. Compute both with base R (and their confidence intervals from the log scale), understand when to use each (cohort vs case-control study designs), and why the odds ratio approximates the relative risk for rare outcomes.

Published

June 23, 2026

Modified

July 7, 2026

TipKey takeaways
  • For a 2×2 table (exposure × outcome), the odds ratio (OR) and relative risk (RR) measure how strongly the exposure is associated with the outcome — the effect sizes behind chi-square/Fisher.
  • Relative risk = ratio of the probabilities of the outcome; odds ratio = ratio of the odds. Both equal 1 when there is no association.
  • Use the relative risk for cohort / experimental studies (you can estimate risk); use the odds ratio for case-control studies (where risk can’t be estimated) and as the natural output of logistic regression.
  • For a rare outcome the OR ≈ the RR — which is why ORs are reported as if they were risk ratios.
  • Report both with a 95% confidence interval (computed on the log scale); a CI that excludes 1 means a significant association.

Introduction

A chi-square or Fisher test tells you whether two categorical variables are associated; for a 2×2 table you also want how strongly — and in which direction. The two standard measures are the relative risk and the odds ratio, the workhorses of epidemiology and clinical research.

For a 2×2 table of exposure (yes/no) × outcome (event/no event), label the cells:

Event No event
Exposed a b
Unexposed c d

This lesson computes both effect sizes with base R, with confidence intervals, using the famous Physicians’ Health Study (aspirin and heart attack).

The data: aspirin and heart attack

In a randomized trial, 104 of 11037 on aspirin had a heart attack (MI), vs 189 of 11034 on placebo:

# a = aspirin & MI, b = aspirin & no MI, c = placebo & MI, d = placebo & no MI
a <- 104; b <- 10933
c <- 189; d <- 10845

tab <- matrix(c(a, b, c, d), nrow = 2, byrow = TRUE,
              dimnames = list(group = c("Aspirin", "Placebo"), outcome = c("MI", "no MI")))
tab
         outcome
group      MI no MI
  Aspirin 104 10933
  Placebo 189 10845

Relative risk

The relative risk is the ratio of the probability of the event in the exposed group to that in the unexposed group:

a <- 104; b <- 10933; c <- 189; d <- 10845

risk_exposed   <- a / (a + b)
risk_unexposed <- c / (c + d)
RR <- risk_exposed / risk_unexposed

# 95% CI on the log scale
se_logRR <- sqrt(1/a - 1/(a + b) + 1/c - 1/(c + d))
RR_ci <- exp(log(RR) + c(-1, 1) * 1.96 * se_logRR)

c(RR = RR, lower = RR_ci[1], upper = RR_ci[2])
       RR     lower     upper 
0.5501150 0.4336712 0.6978247 

The relative risk is 0.55 (95% CI 0.43–0.70) — aspirin takers had about half the risk of a heart attack. The CI is entirely below 1, so the protective effect is significant.

Odds ratio

The odds ratio is the ratio of the odds of the event — for a 2×2 table this is the cross-product (a × d) / (b × c):

a <- 104; b <- 10933; c <- 189; d <- 10845

OR <- (a * d) / (b * c)

# 95% CI on the log scale
se_logOR <- sqrt(1/a + 1/b + 1/c + 1/d)
OR_ci <- exp(log(OR) + c(-1, 1) * 1.96 * se_logOR)

c(OR = OR, lower = OR_ci[1], upper = OR_ci[2])
       OR     lower     upper 
0.5458355 0.4290391 0.6944271 

The odds ratio is 0.55 (95% CI 0.43–0.69) — almost identical to the relative risk, because a heart attack is rare here (≈ 1–2%). When the outcome is rare, the OR approximates the RR.

Tip

fisher.test() also gives an odds ratio. For a 2×2 table, fisher.test(tab)$estimate returns the conditional maximum-likelihood odds ratio (slightly different from the sample OR above) with an exact CI — a convenient alternative that also gives the significance test.

Which one — OR or RR?

Study design Can you estimate risk? Report
Cohort / RCT (follow exposed vs unexposed forward) yes relative risk (and OR if you wish)
Case-control (start from cases vs controls, look back) no odds ratio (RR is not estimable)
Logistic regression odds ratio (exp(coef))

The relative risk is more intuitive (“half the risk”), but it can only be computed when you can estimate the actual probability of the outcome — i.e. in a cohort or experimental study. In a case-control study you fix the number of cases and controls, so risk is not estimable and the odds ratio is the only valid measure. For a rare outcome they nearly coincide.

Plot the effect sizes

A forest-style plot makes the two estimates and their intervals easy to compare against the no-effect line at 1:

library(ggpubr)

a <- 104; b <- 10933; c <- 189; d <- 10845
RR <- (a / (a + b)) / (c / (c + d)); se_logRR <- sqrt(1/a - 1/(a + b) + 1/c - 1/(c + d))
OR <- (a * d) / (b * c);             se_logOR <- sqrt(1/a + 1/b + 1/c + 1/d)
est <- data.frame(
  measure = c("Relative risk", "Odds ratio"),
  value   = c(RR, OR),
  lower   = c(exp(log(RR) - 1.96 * se_logRR), exp(log(OR) - 1.96 * se_logOR)),
  upper   = c(exp(log(RR) + 1.96 * se_logRR), exp(log(OR) + 1.96 * se_logOR))
)

ggdotchart(est, x = "measure", y = "value", color = "measure", palette = "jco",
           sorting = "none", rotate = TRUE, dot.size = 4, ylab = "Estimate (vs no effect = 1)", xlab = NULL) +
  geom_errorbar(aes(ymin = lower, ymax = upper, color = measure), width = 0.15) +
  geom_hline(yintercept = 1, linetype = "dashed")

A forest-style plot of the odds ratio and relative risk for aspirin and heart attack, both about 0.55 with confidence intervals (0.43–0.70) entirely below the dashed no-effect line at 1.

Report

Aspirin was associated with a reduced risk of myocardial infarction: relative risk 0.55 (95% CI 0.43–0.70) and odds ratio 0.55 (95% CI 0.43–0.69). The near-equality reflects the rarity of the outcome. Both intervals exclude 1, indicating a statistically significant protective effect.

For the 2×2 table above, risk in each row is \(a/(a+b)\) and \(c/(c+d)\), so \(\text{RR} = \dfrac{a/(a+b)}{c/(c+d)}\). Odds are \(a/b\) and \(c/d\), so \(\text{OR} = \dfrac{a/b}{c/d} = \dfrac{ad}{bc}\). Confidence intervals are built on the log scale (where the estimates are roughly normal) with standard errors \(\text{SE}(\log \text{OR}) = \sqrt{1/a + 1/b + 1/c + 1/d}\) and \(\text{SE}(\log \text{RR}) = \sqrt{1/a - 1/(a+b) + 1/c - 1/(c+d)}\), then exponentiated back. When the outcome is rare, \(a \ll b\) and \(c \ll d\), so \(a/(a+b) \approx a/b\) and the OR converges to the RR.

Try it live

Compute the odds ratio and relative risk for your own 2×2 table. Edit the four counts and re-run; the sandbox boots on first Run.

🟢 With an AI agent

Ask Prova “I have a 2×2 table — compute the odds ratio and relative risk with confidence intervals, and tell me which to report for my study design” — it answers with code you can run on your own counts. The runtime is the judge. Ask Prova →

Common issues

You reported a relative risk from a case-control study. Risk isn’t estimable in a case-control design (the case/control ratio is fixed by the sampling), so the odds ratio is the only valid measure there.

You mislabelled the cells. The cross-product (a×d)/(b×c) and the risks depend on which cell is which. Keep the layout exposure in rows, outcome in columns, with the event as the first column, so OR and RR both describe the event in the exposed group.

You computed the CI on the raw scale. The OR and RR are skewed, so the confidence interval is built on the log scale and exponentiated back — computing estimate ± 1.96·SE directly is wrong.

Frequently asked questions

Relative risk is the ratio of the probabilities of the outcome (exposed vs unexposed); odds ratio is the ratio of the odds. RR is more intuitive but only estimable in cohort/experimental studies; OR is valid in any design (including case-control) and is what logistic regression returns. Both equal 1 under no association.

Use the relative risk for cohort or randomized studies, where you can estimate the actual risk of the outcome. Use the odds ratio for case-control studies (risk isn’t estimable) and as the output of logistic regression. For a rare outcome the two are nearly equal.

When the outcome is rare, the number of events (a, c) is small relative to the non-events (b, d), so the probability \(a/(a+b)\) is close to the odds \(a/b\). The odds ratio (ratio of odds) then converges to the relative risk (ratio of probabilities) — which is why ORs from rare-disease studies are interpreted like risk ratios.

Test your understanding

  1. Run it. In the live cell, compute the OR and RR for the 2×2 table. Fill the blank. Are they close, or far apart — and why?
  2. Conceptual. You ran a case-control study. Why can you report the odds ratio but not the relative risk?

Fill the blank with OR. Compare the two values; here the “outcome” (event) is common, not rare. For question 2, recall what is fixed by the case-control sampling.

OR <- (9 * 7) / (1 * 3)            #> 21
RR <- (9/10) / (3/10)             #> 3
c(OR = OR, RR = RR)

They are far apart (OR 21 vs RR 3) because the event is common here (90% vs 30%) — the OR approximates the RR only when the outcome is rare. For question 2: a case-control study fixes the number of cases and controls by design, so you cannot estimate the probability (risk) of the outcome — only the odds ratio, which is invariant to that sampling, is valid.

TipWhich measure when?
  • Cohort / RCT, want intuitive riskrelative risk.
  • Case-control studyodds ratio (RR not estimable).
  • From a logistic regression modelodds ratio (exp(coef)) — see logistic regression.
  • Stratified by a third variable → the common odds ratio from Cochran-Mantel-Haenszel.

Conclusion

You can now compute the odds ratio and relative risk in R for a 2×2 table — the cross-product (a×d)/(b×c) for the OR, the ratio of risks for the RR, each with a log-scale confidence interval. Choose the relative risk for cohort/experimental designs and the odds ratio for case-control studies and logistic regression, and remember they nearly coincide when the outcome is rare. They are the effect sizes that turn a significant chi-square into a meaningful clinical statement.

Reuse

Citation

BibTeX citation:
@online{2026,
  author = {},
  title = {Odds {Ratio} \& {Relative} {Risk} in {R:} {Effect} {Sizes}
    for 2×2 {Tables}},
  date = {2026-06-23},
  url = {https://www.datanovia.com/learn/biostatistics/categorical/odds-ratio-and-relative-risk-in-r},
  langid = {en}
}
For attribution, please cite this work as:
“Odds Ratio & Relative Risk in R: Effect Sizes for 2×2 Tables.” 2026. June 23. https://www.datanovia.com/learn/biostatistics/categorical/odds-ratio-and-relative-risk-in-r.