Skip to content

Instantly share code, notes, and snippets.

View dreidpath's full-sized avatar

Daniel D Reidpath dreidpath

View GitHub Profile
@dreidpath
dreidpath / ptolemaic-epicycles.R
Created March 10, 2026 12:31
Ptolemaic epicycles relied on a fourier analysis to describe planetary orbits. This is a small shiny-app to play with the idea of their scientific success.
# =============================================================================
# Epicycles: a Fourier curve-fitting toy for scientific epistemology
# =============================================================================
#
# Ptolemy's geocentric model used epicycles — circles on circles — to predict
# planetary motion. It worked remarkably well. The reason is that epicycles
# are mathematically equivalent to a Fourier series: any smooth periodic
# function can be approximated arbitrarily well by sums of circular motions.
# Ptolemy was doing harmonic analysis without knowing it.
#
@dreidpath
dreidpath / Laplace-demon.R
Last active March 5, 2026 15:30
A visualisation of why Laplace's demon fails: deterministic chaos and the quantisation problem illustrated with the logistic map in R.
# =============================================================================
# Laplace's Demon and the Quantisation Problem
# =============================================================================
# Laplace's demon is a thought experiment: a intelligence that knows the exact
# position and momentum of every particle in the universe could, in principle,
# predict its entire future. This script demonstrates why that fails.
#
# Any representation of a continuous value must truncate at some precision.
# In a chaotic system, that irreducible truncation — however small — is the
# foothold through which sensitive dependence enters. The demon's prediction
@dreidpath
dreidpath / probability_of_death.R
Last active June 19, 2023 06:03
Use life tables to calculate the probability of dying between two ages (e.g., 76.5 and 88.2).
################################################################################
# Using the US CDC life tables for White Males (2020), I wanted to estimate the
# Probability that if Joe Biden or Donald Trump became the next US president
# They would die in office. The general approach could be used for any interval.
# The life tables can be found here: https://stacks.cdc.gov/view/cdc/118055
################################################################################
library(tidyverse)
library(lubridate)
@dreidpath
dreidpath / firefox_pdf.md
Created August 13, 2022 15:50
Fixing poorly displayed PDF fonts in Firefox
@dreidpath
dreidpath / veRsion_update
Created May 13, 2022 03:56
Ensuring a new version of R has the same packages installed as the previous version
version_new <- list.files('~/R/x86_64-pc-linux-gnu-library/4.2')
version_old <- list.files('~/R/x86_64-pc-linux-gnu-library/4.1')
target <- version_old[!(version_old %in% version_new)]
install.packages(target)
#' In a dataframe, find all variable names that match var_pattern
#'
#' @param var_pattern a character string ("pattern" to match)
#' @param dframe a dataframe
#'
#' @return all variable names in the dataframe that match var_pattern
#' @export
#'
#' @examples
#' find_var(iris, "idth")
@dreidpath
dreidpath / DJT-BMI.R
Created February 15, 2019 10:57
Visual analysis of Donald Trumps likely BMI
# A quick visual analysis of Donald Trump's BMI assuming that his reported weight
# in 2018 and 2019 are correct, and his real height lies between his claimed
# height of 6'3" (1.9m) and a more likely height of 6'1" (1.854m)
# https://www.theguardian.com/us-news/2018/jan/17/a-tall-tale-accuracy-of-trumps-medical-report-and-new-height-questioned
# Include the ggplot library
library(ggplot2)
# Function to calculate BMI from height (m) and mass/weight (Kg)
bmicalc <- function(height, mass){
@dreidpath
dreidpath / phred.R
Created September 30, 2018 12:45
The R functions for the caluclation of the phred quality score, " a measure of the quality of the identification of the nucleobases generated by automated DNA sequencing" https://en.wikipedia.org/wiki/Phred_quality_score
phred <- function(q){
10^(-q/10)
}
inv_phred <- function(p){
10 * log10(p)
}
@dreidpath
dreidpath / SeaIce.R
Created December 13, 2017 13:24
A replotting of the recently published NOAA data from the Arctic Report Card 2017
# These data appeared in a graph on climate.gov: http://bit.ly/2AFNBJz.
# Paraphrasing from the website:
# The data are for each November since 1979 to 2017.
# The variable 'area' represents the combined November sea ice
# area for the Chukchi and Beaufort Seas, the two sub-basins of
# the Arctic Ocean that touch Alaska’s Arctic (northern) coast.
# Larger values means more sea ice in the combined basin in square kilometres.
# The variable 'temp' is the November temperature (Fahrenheit) at Utqiaġvik,
# also known as Barrow (71°17′26″N 156°47′19″W).
# The data were digitised using Plot Digitizer
@dreidpath
dreidpath / wbplots.R
Created July 12, 2017 13:26
Quick time series plot of World Bank Life Expectancy and Infant Mortality Rate data downloaded using the wbstats package
# This is a quick script to create two time series plots using World Bank data downloaded using the wbstats package
# and plotted using ggplot2.
# The first plot uses Malaysia data only, and is a time series plot showing Life Expectancy at Birth changes
# in the whole population and for males and females separately.
# The second plot uses data for a handful of Southeast Asian countries and Australia and shows changes in the
# Infant Mortality Rate over time.
library(wbstats)
library(tidyverse)