Last updated: 2026-02-04

Checks: 7 0

Knit directory: muse/

This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20200712) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version bfee866. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rproj.user/
    Ignored:    data/1M_neurons_filtered_gene_bc_matrices_h5.h5
    Ignored:    data/293t/
    Ignored:    data/293t_3t3_filtered_gene_bc_matrices.tar.gz
    Ignored:    data/293t_filtered_gene_bc_matrices.tar.gz
    Ignored:    data/5k_Human_Donor1_PBMC_3p_gem-x_5k_Human_Donor1_PBMC_3p_gem-x_count_sample_filtered_feature_bc_matrix.h5
    Ignored:    data/5k_Human_Donor2_PBMC_3p_gem-x_5k_Human_Donor2_PBMC_3p_gem-x_count_sample_filtered_feature_bc_matrix.h5
    Ignored:    data/5k_Human_Donor3_PBMC_3p_gem-x_5k_Human_Donor3_PBMC_3p_gem-x_count_sample_filtered_feature_bc_matrix.h5
    Ignored:    data/5k_Human_Donor4_PBMC_3p_gem-x_5k_Human_Donor4_PBMC_3p_gem-x_count_sample_filtered_feature_bc_matrix.h5
    Ignored:    data/97516b79-8d08-46a6-b329-5d0a25b0be98.h5ad
    Ignored:    data/Parent_SC3v3_Human_Glioblastoma_filtered_feature_bc_matrix.tar.gz
    Ignored:    data/brain_counts/
    Ignored:    data/cl.obo
    Ignored:    data/cl.owl
    Ignored:    data/jurkat/
    Ignored:    data/jurkat:293t_50:50_filtered_gene_bc_matrices.tar.gz
    Ignored:    data/jurkat_293t/
    Ignored:    data/jurkat_filtered_gene_bc_matrices.tar.gz
    Ignored:    data/pbmc20k/
    Ignored:    data/pbmc20k_seurat/
    Ignored:    data/pbmc3k.csv
    Ignored:    data/pbmc3k.csv.gz
    Ignored:    data/pbmc3k.h5ad
    Ignored:    data/pbmc3k/
    Ignored:    data/pbmc3k_bpcells_mat/
    Ignored:    data/pbmc3k_export.mtx
    Ignored:    data/pbmc3k_matrix.mtx
    Ignored:    data/pbmc3k_seurat.rds
    Ignored:    data/pbmc4k_filtered_gene_bc_matrices.tar.gz
    Ignored:    data/pbmc_1k_v3_filtered_feature_bc_matrix.h5
    Ignored:    data/pbmc_1k_v3_raw_feature_bc_matrix.h5
    Ignored:    data/refdata-gex-GRCh38-2020-A.tar.gz
    Ignored:    data/seurat_1m_neuron.rds
    Ignored:    data/t_3k_filtered_gene_bc_matrices.tar.gz
    Ignored:    r_packages_4.4.1/
    Ignored:    r_packages_4.5.0/

Untracked files:
    Untracked:  .claude/
    Untracked:  CLAUDE.md
    Untracked:  analysis/bioc.Rmd
    Untracked:  analysis/bioc_scrnaseq.Rmd
    Untracked:  analysis/chick_weight.Rmd
    Untracked:  analysis/likelihood.Rmd
    Untracked:  bpcells_matrix/
    Untracked:  data/Caenorhabditis_elegans.WBcel235.113.gtf.gz
    Untracked:  data/GCF_043380555.1-RS_2024_12_gene_ontology.gaf.gz
    Untracked:  data/SeuratObj.rds
    Untracked:  data/arab.rds
    Untracked:  data/astronomicalunit.csv
    Untracked:  data/femaleMiceWeights.csv
    Untracked:  data/lung_bcell.rds
    Untracked:  m3/
    Untracked:  women.json

Unstaged changes:
    Modified:   analysis/isoform_switch_analyzer.Rmd
    Modified:   analysis/linear_models.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/odds_ratio.Rmd) and HTML (docs/odds_ratio.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd bfee866 Dave Tang 2026-02-04 Add more background material
html 34a78ed Dave Tang 2025-12-05 Build site.
Rmd 222d2c4 Dave Tang 2025-12-05 Odds ratio

Introduction

Odds ratios are widely used to measure the strength of association between two variables, particularly in medical research, epidemiology, and case-control studies. Before understanding odds ratios, we need to understand the difference between probability and odds.

Probability vs Odds

Probability and odds are related but different:

  • Probability: The chance of something happening out of all possibilities (ranges from 0 to 1)
  • Odds: The ratio of something happening to it NOT happening (ranges from 0 to infinity)
sick <- 2
healthy <- 8
total <- sick + healthy

probability <- sick / total
probability
[1] 0.2
odds <- sick / healthy
odds
[1] 0.25

In this example:

  • The probability of being sick is 2/10 = 0.2 (or 20%)
  • The odds of being sick are 2:8 = 0.25 (read as “1 to 4” or “0.25 to 1”)

Converting Between Probability and Odds

You can convert between probability (P) and odds using these formulas:

\[\text{Odds} = \frac{P}{1 - P}\]

\[P = \frac{\text{Odds}}{1 + \text{Odds}}\]

Convert probability to odds.

prob <- 0.2
odds_from_prob <- prob / (1 - prob)
odds_from_prob
[1] 0.25
# Convert odds back to probability
prob_from_odds <- odds_from_prob / (1 + odds_from_prob)
prob_from_odds
[1] 0.2

Example: Different Probability Values

To build intuition, let’s see how odds change across different probabilities:

probs <- c(0.1, 0.2, 0.5, 0.8, 0.9)
odds_values <- probs / (1 - probs)

data.frame(
  Probability = probs,
  Odds = round(odds_values, 3),
  Interpretation = c("1 to 9", "1 to 4", "1 to 1 (even)", "4 to 1", "9 to 1")
)
  Probability  Odds Interpretation
1         0.1 0.111         1 to 9
2         0.2 0.250         1 to 4
3         0.5 1.000  1 to 1 (even)
4         0.8 4.000         4 to 1
5         0.9 9.000         9 to 1

Notice that when P = 0.5, the odds are 1 (even odds). When P > 0.5, odds > 1; when P < 0.5, odds < 1.

What is an Odds Ratio?

An odds ratio (OR) compares the odds of an event between two groups. It answers the question: “How many times higher (or lower) are the odds of the outcome in one group compared to another?”

Interpreting odds ratio values:

  • OR = 1: No association between exposure and outcome
  • OR > 1: Exposure is associated with higher odds of the outcome
  • OR < 1: Exposure is associated with lower odds of the outcome

Smoking and Lung Cancer Example

Let’s work through a classic example examining the association between smoking and lung cancer. We have a sample of 200 people broken down into a 2x2 contingency table:

data <- matrix(
  c(20, 80, 2, 98),
  nrow = 2,
  byrow = TRUE,
  dimnames = list(
    c("Smokers", "Non-smokers"),
    c("Cancer", "No Cancer")
  )
)

data
            Cancer No Cancer
Smokers         20        80
Non-smokers      2        98

The general structure of a 2x2 table is:

Disease+ Disease-
Exposed a b
Not Exposed c d

In our example: a=20, b=80, c=2, d=98.

Step 1: Calculate odds for each group

# Odds of cancer for smokers = a/b
odds_smokers <- data[1, 1] / data[1, 2]
odds_smokers
[1] 0.25
# Odds of cancer for non-smokers = c/d
odds_nonsmokers <- data[2, 1] / data[2, 2]
odds_nonsmokers
[1] 0.02040816
  • Smokers: 20 have cancer, 80 don’t → odds = 20/80 = 0.25 (or 1:4)
  • Non-smokers: 2 have cancer, 98 don’t → odds = 2/98 ≈ 0.02 (or about 1:49)

Step 2: Calculate odds ratio

The odds ratio is simply the ratio of these two odds:

odds_ratio <- odds_smokers / odds_nonsmokers
odds_ratio
[1] 12.25

Interpretation: Smokers have 12.3 times the odds of developing lung cancer compared to non-smokers. This is a strong positive association.

Cross-Product Formula

For a 2x2 table, the odds ratio can also be calculated directly using the cross-product formula:

\[OR = \frac{a \times d}{b \times c}\]

Using the cross-product formula.

or_cross <- (data[1, 1] * data[2, 2]) / (data[1, 2] * data[2, 1])
or_cross
[1] 12.25

This gives the same result and is often more convenient for quick calculations.

Confidence Intervals

A point estimate of the odds ratio alone doesn’t tell us how precise our estimate is. We need confidence intervals to understand the range of plausible values for the true odds ratio.

Using Fisher’s Exact Test

The fisher.test() function calculates the odds ratio with a 95% confidence interval:

result <- fisher.test(data)
result

    Fisher's Exact Test for Count Data

data:  data
p-value = 5.091e-05
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
   2.809877 110.218221
sample estimates:
odds ratio 
  12.12786 

Key outputs:

  • Odds ratio: 12.13 (similar to our manual calculation; slight difference due to conditional maximum likelihood estimation)
  • 95% CI: [2.81, 110.22]
  • p-value: 5.09e-05

Interpreting the confidence interval: Since the 95% CI does not include 1, we can conclude there is a statistically significant association between smoking and lung cancer at the 0.05 significance level.

Rule of Thumb for Confidence Intervals

  • If the 95% CI excludes 1: The association is statistically significant (p < 0.05)
  • If the 95% CI includes 1: The association is not statistically significant

Logistic Regression and Odds Ratios

Logistic regression is commonly used to model binary outcomes (yes/no, disease/no disease). The coefficients from logistic regression are log odds ratios, which can be converted to odds ratios by exponentiation.

Why Log Odds?

Logistic regression models the log odds (also called logit) of the outcome:

\[\log\left(\frac{P}{1-P}\right) = \beta_0 + \beta_1 X_1 + \beta_2 X_2 + ...\]

The coefficients (\(\beta\)) represent the change in log odds for a one-unit increase in the predictor.

Example: Disease Prediction

Let’s simulate data where disease risk depends on age and smoking status:

set.seed(1984)
n <- 200
age <- rnorm(n, mean = 50, sd = 15)
smoker <- rbinom(n, 1, 0.3)
# True model: log odds = -5 + 0.05*age + 2*smoker
disease <- rbinom(n, 1, plogis(-5 + 0.05*age + 2*smoker))

model <- glm(disease ~ age + smoker, family = binomial)
summary(model)

Call:
glm(formula = disease ~ age + smoker, family = binomial)

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept) -4.53700    0.87994  -5.156 2.52e-07 ***
age          0.04302    0.01493   2.881  0.00396 ** 
smoker       1.83998    0.40800   4.510 6.49e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 188.56  on 199  degrees of freedom
Residual deviance: 157.34  on 197  degrees of freedom
AIC: 163.34

Number of Fisher Scoring iterations: 5

Converting Coefficients to Odds Ratios

The model coefficients are log odds ratios. To get odds ratios, we exponentiate them:

or_coef <- exp(coef(model))
or_coef
(Intercept)         age      smoker 
 0.01070543  1.04395510  6.29641114 

Interpretation:

  • Age: OR = 1.044. For each additional year of age, the odds of disease increase by about 4.4%
  • Smoker: OR = 6.3. Smokers have about 6.3 times the odds of disease compared to non-smokers

Odds Ratios with Confidence Intervals

For a complete picture, we should report odds ratios with confidence intervals:

# Get confidence intervals for coefficients
ci <- confint(model)
Waiting for profiling to be done...
# Combine into a nice table
or_table <- data.frame(
  OR = exp(coef(model)),
  Lower_CI = exp(ci[, 1]),
  Upper_CI = exp(ci[, 2])
)
round(or_table, 3)
               OR Lower_CI Upper_CI
(Intercept) 0.011    0.002    0.054
age         1.044    1.015    1.076
smoker      6.296    2.878   14.392

Summary

Key points about odds ratios:

  1. Odds are the ratio of an event occurring to it not occurring
  2. Odds ratios compare odds between two groups
  3. OR = 1 means no association; OR > 1 means positive association; OR < 1 means negative association
  4. Always report confidence intervals alongside point estimates
  5. In logistic regression, exponentiate coefficients to obtain odds ratios
  6. The cross-product formula (ad/bc) provides a quick way to calculate OR from 2x2 tables

sessionInfo()
R version 4.5.0 (2025-04-11)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 24.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Etc/UTC
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] lubridate_1.9.4 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4    
 [5] purrr_1.0.4     readr_2.1.5     tidyr_1.3.1     tibble_3.3.0   
 [9] ggplot2_3.5.2   tidyverse_2.0.0 workflowr_1.7.1

loaded via a namespace (and not attached):
 [1] sass_0.4.10        generics_0.1.4     stringi_1.8.7      hms_1.1.3         
 [5] digest_0.6.37      magrittr_2.0.3     timechange_0.3.0   evaluate_1.0.3    
 [9] grid_4.5.0         RColorBrewer_1.1-3 fastmap_1.2.0      rprojroot_2.0.4   
[13] jsonlite_2.0.0     processx_3.8.6     whisker_0.4.1      ps_1.9.1          
[17] promises_1.3.3     httr_1.4.7         scales_1.4.0       jquerylib_0.1.4   
[21] cli_3.6.5          rlang_1.1.6        withr_3.0.2        cachem_1.1.0      
[25] yaml_2.3.10        tools_4.5.0        tzdb_0.5.0         httpuv_1.6.16     
[29] vctrs_0.6.5        R6_2.6.1           lifecycle_1.0.4    git2r_0.36.2      
[33] fs_1.6.6           pkgconfig_2.0.3    callr_3.7.6        pillar_1.10.2     
[37] bslib_0.9.0        later_1.4.2        gtable_0.3.6       glue_1.8.0        
[41] Rcpp_1.0.14        xfun_0.52          tidyselect_1.2.1   rstudioapi_0.17.1 
[45] knitr_1.50         farver_2.1.2       htmltools_0.5.8.1  rmarkdown_2.29    
[49] compiler_4.5.0     getPass_0.2-4