Last updated: 2026-04-03
Checks: 7 0
Knit directory: bioinformatics_tips/
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(20200503) 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 e8c428a. 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/
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/probability.Rmd) and HTML
(docs/probability.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 | e8c428a | Dave Tang | 2026-04-03 | Fix LaTeX |
| html | 379d8f1 | Dave Tang | 2026-04-03 | Build site. |
| Rmd | 555e3e3 | Dave Tang | 2026-04-03 | Bayes’ theorem |
| html | c17bafb | Dave Tang | 2026-04-03 | Build site. |
| Rmd | 73835bb | Dave Tang | 2026-04-03 | Marginal and conditional probability |
| html | 902bbc7 | Dave Tang | 2026-01-07 | Build site. |
| html | fe7d8b8 | Dave Tang | 2023-11-24 | Build site. |
| Rmd | 4503aed | Dave Tang | 2023-11-24 | Update |
| html | 97b7d02 | Dave Tang | 2023-06-27 | Build site. |
| html | e08ce00 | davetang | 2020-10-14 | Build site. |
| Rmd | 805d205 | davetang | 2020-10-14 | Probability basics |
A lot of bioinformatics tools use probabilistic models, so it’s important to have at least a working understanding of probability. In simpler cases, if you can figure out all the possible events, you can calculate the probability of a subset of events by dividing the subset by all possible events. For example, in a full deck of cards, there are 52 cards and the probability of picking a red card is the subset of red cards (26) divided by all the cards (52).
Another way to think about the probability of an event is that it is the proportion of times that the event occurs when we repeat the experiment an infinite number of times, independently and under the same conditions. The probability is the number of times an event occurred divided by the total number of times an experiment was repeated. Because of this, we can get a estimate of the probability of an event by running Monte Carlo simulations. This is particularly useful when it is difficult to work out the probability mathematically.
\(Pr(A)\) is the probability of event \(A\) happening; the general term event is used to refer to things that can happen by chance.
A probability distribution is the probabilities of all possible events.
For discrete variables, the probability distribution is calculated using a probability density function (PDF).
For continuous variables, the probability distribution is calculated using the cumulative distribution function (CDF).
Two events are independent if the outcome of one does not affect the other. The classic example is coin tosses; every time we toss a fair coin, the probability of seeing heads is 1/2 regardless of what previous events were.
When events are not independent, conditional probabilities are used.
\[ Pr(Card\ 2\ is\ a\ king\ |\ Card\ 1\ is\ a\ king) = \frac{3}{51} \]
The \(|\) means “given that” or “conditional on”. The mathematical definition of independence is:
\[ Pr(A\ |\ B) = Pr(A) \]
The fact that event \(B\) has occurred does not affect the probability of \(A\).
We use the multiplication rule for calculating the probability of two events occurring:
\[ Pr(A\ and\ B) = Pr(A) \cdot pr(B\ |\ A) \]
When there are three events:
\[ Pr(A\ and\ B\ and\ C) = Pr(A) \cdot Pr(B\ |\ A) \cdot Pr(C\ |\ A\ and\ B) \]
If the events are independent, then:
\[ Pr(A\ and\ B\ and\ C) = Pr(A) \cdot Pr(B) \cdot Pr(C) \]
General formula for computing conditional probabilities:
\[ Pr(B\ |\ A) = \frac{Pr(A\ and\ B)}{Pr(A)} \]
If event \(A\) and \(B\) are independent, then the probability just becomes \(Pr(B)\):
\[ Pr(B\ |\ A) = \frac{Pr(A) \cdot Pr(B)}{Pr(A)} = Pr(B) \]
Addition rule for calculating the probability of either event happening (but not both, which is why we subtract):
\[ Pr(A\ or\ B) = Pr(A) + Pr(B) - Pr(A\ and\ B) \]
Bayes’ theorem:
\[ Pr(A\ |\ B) = \frac{Pr(B\ |\ A) \cdot Pr(A)}{Pr(B)} \]
Again, if event \(A\) and \(B\) are independent, then the probability just becomes \(Pr(B)\):
\[ Pr(A) = \frac{Pr(B) \cdot Pr(A)}{Pr(B)} \]
One application of Bayes’ Theorem is for spam filtering where:
\[ Pr(spam\ |\ words) = \frac{Pr(words\ |\ spam) \cdot Pr(spam)}{Pr(words)} \]
A 2 by 2 contingency table can help illustrate marginal and conditional probability.
disease_table <- matrix(
c(40, 10, 20, 30),
nrow = 2,
dimnames = list(
"Test" = c("Test +", "Test -"),
"Disease" = c("Disease +", "Disease -")
)
)
disease_table_with_margins <- addmargins(disease_table)
disease_table_with_margins
Disease
Test Disease + Disease - Sum
Test + 40 20 60
Test - 10 30 40
Sum 50 50 100
Marginal probabilities are read directly from the totals
in the margins of the table, ignoring the other variable. Reading the
Sum, probability of disease (Disease +) is 0.5
and probability to test positive (Test +) is 0.6.
disease_table |>
prop.table() |>
addmargins()
Disease
Test Disease + Disease - Sum
Test + 0.4 0.2 0.6
Test - 0.1 0.3 0.4
Sum 0.5 0.5 1.0
Conditional probabilities restrict attention to a single row or column. Given a positive test, we restrict ourselves to the first row.
\(P(Disease+|Test+)\) is of those who tested positive, what fraction have disease?
\[ P(Disease+|Test+) = 0.4 / 0.6 = 0.67 \]
Given positive to disease, we restrict ourselves to the first column.
\(P(Test+|Disease+)\) is of those with disease, what fraction test positive?
\[ P(Test+|Disease+) = 0.4 / 0.5 = 0.80 \]
Note that \(P(Disease+|Test+)\) is not equal to \(P(Test+|Disease+)\); conditional probability is not symmetric. This asymmetry is the source of the base rate fallacy, which is commonly encountered in medical testing and legal reasoning, where \(P(Disease|Test+)\) is confused with \(P(Test+|Disease)\).
A cell in the two by two contingency table is simply a tally, a count of people who satisfy two conditions simultaneously. The top-left cell is how many people in our total of 100 had both a positive test and the disease.
There are two different ways to express the probability of landing in any single cell of the table.
disease_table |>
addmargins()
Disease
Test Disease + Disease - Sum
Test + 40 20 60
Test - 10 30 40
Sum 50 50 100
Consider the top-left cell with 40 people who are both Test+ and Disease+.
If we pull aside everyone with disease, we end up with 50 people. Then from those 50, if we ask how many tested positive we will get 40.
\[ 50\%×80\%=40\% \]
Again, if we pull aside everyone who tested positive, we end up with 60 people. Then from those 60, if we ask how many actually have the disease we get 40 again.
\[ 60\%×67\%=40\% \]
\[ P(Disease+) \times P(Test+∣Disease+) \]
\[ P(Test+) \times P(Disease+∣Test+) \]
Therefore:
\[ P(Disease+) \times P(Test+∣Disease+) = P(Test+) \times P(Disease+∣Test+) \]
So if we want to know \(P(Disease+ | Test+)\) but only have \(P(Test+ | Disease+)\), we simply rearrange:
\[ P(Test+) \times P(Disease+∣Test+) = P(Disease+) \times P(Test+∣Disease+) \\ P(Disease+∣Test+) = \frac{P(Disease+) \times P(Test+∣Disease+)}{P(Test+)} \]
This is Bayes’ theorem. In the abstract form with A and B:
\[ P(A | B) = \frac{P(B | A) \times P(A)}{P(B)} \]
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.1.0 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.11.0
[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