Skip to content

Instantly share code, notes, and snippets.

View dreidpath's full-sized avatar

Daniel D Reidpath dreidpath

View GitHub Profile
@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)
@dreidpath
dreidpath / LE_1870_2016.R
Last active January 21, 2017 07:31
Changes in country level life expectancy 1870-2016 based on data from Gapminder.org
######################################################################################################################################
# This is a small piece of R-Code to support a recent blog-post of mine on country-level inequalities in life expectancy. #
# If you are so inclined, you can read the blog-post here: http://wp.me/p8fOE1-13 #
# #
# For the purposes of re-use, the code is released under an MIT License (https://choosealicense.com/licenses/mit/) #
# The code is provided "as is" and without warranties. #
# #
####### Notes
@dreidpath
dreidpath / p-divide.R
Created June 17, 2016 06:40
The probability (P_n) of an event in 1/n-th of the time if the probability of the event over the whole time is P
# Imagine that the probability of dying over a whole year is 0.5. Assuming risk is constant,
# what is the probability of dying in any single month? This trivial function (thank you Mark)
# ensures that the monthly-p results in an annual probability of 0.5. I wanted somewhere to
# remember it
p-Divide <- function(P, n){
1-(1-P)^(1/n)
}