Skip to content

Instantly share code, notes, and snippets.

View timriffe's full-sized avatar

Tim Riffe timriffe

View GitHub Profile
@timriffe
timriffe / Saddaf_lx_smooth.R
Created April 27, 2023 10:35
Code for Saddaf Akhtar's email inquiry
library(tidyverse)
library(DemoTools)
library(readxl)
IN <-read_excel("Data/Saddaf_New_file.xlsx")|>
mutate(age = parse_number(Age),
lx = parse_number(lx))
lx2qx <- function(lx){
-diff(c(lx,0)) / lx
}
@timriffe
timriffe / dudel_discount_single_decrement.R
Created April 19, 2023 13:50
Does the dudel discount work for single-decrement lifetables? Looks like it's not a bad approximation.
ex = c(84.18, 83.55, 82.58, 81.59, 80.6, 79.61, 78.62, 77.62,
76.63, 75.64, 74.64, 73.64, 72.65, 71.66, 70.67, 69.68, 68.7,
67.71, 66.72, 65.74, 64.76, 63.78, 62.8, 61.81, 60.83, 59.85,
58.88, 57.89, 56.91, 55.94, 54.96, 53.98, 53, 52.03, 51.06,
50.08, 49.11, 48.14, 47.17, 46.2, 45.24, 44.27, 43.3, 42.34,
41.39, 40.44, 39.48, 38.53, 37.59, 36.65, 35.72, 34.79, 33.87,
32.95, 32.03, 31.12, 30.22, 29.32, 28.43, 27.54, 26.66, 25.79,
24.92, 24.06, 23.21, 22.37, 21.53, 20.7, 19.88, 19.06, 18.27,
17.47, 16.68, 15.92, 15.18, 14.44, 13.71, 12.99, 12.28, 11.59,
10.93, 10.29, 9.65, 9.03, 8.44, 7.88, 7.33, 6.81, 6.32, 5.85,
@timriffe
timriffe / non-monotonic-prevalence.R
Created March 3, 2023 12:34
A multistate model using Gompertz mortality but that produces a non-monotonic prevalence pattern
gompmx <- function(a,b,x){
a * exp(b*x)
}
a1 <- .0002
a2 <- .0001
b1 <- .06
b2 <- .1
x <- 0:110
@timriffe
timriffe / forRebecca.R
Created July 7, 2022 13:59
attempt to get DemoTools::lt_abrdiged to work with wpp2010 rates
library(DemoTools)
library(wpp2010)
library(sjmisc)
library(tidyverse)
library(janitor)
help(wpp2010)
#age-sex-specific mortality rates
data(mxM)
data(mxF)
@timriffe
timriffe / ponderacio_tim_marc.R
Created July 7, 2022 06:24
una funcio que fa la ponderacio de forma mes facil, rapid, i robust, a veure
library(tidyverse)
# dd <-
# structure(list(n = structure(c(10, 10, 10, 10, 10, 20, 5, 1), format.spss = "F8.2"),
# contin = c(1, 2, 3, 4, 5, 1, 2, 3), esp = c(0, 0.5, 0.25,
# 1, 1, 0, 0.5, 0.25), eur = c(0.5, 0, 0.75, 1, 1, 0.5, 0,
# 0.75), ame = c(0.25, 0.75, 0, 0.75, 0.75, 0.25, 0.75, 0),
# afr = c(1, 1, 0.75, 0, 0.75, 1, 1, 0.75), asi = c(1, 1, 0.75,
# 0.75, 0, 1, 1, 0.75), cont_def = c(NA, NA, NA, NA, NA, NA,
@timriffe
timriffe / code_that_does_stuff.R
Created June 28, 2022 10:59
toy example research code that we want to convert into a reproducibility repository. Used in SICSS Covenant workshop, summer 2022
"https://covid.ourworldindata.org/data/owid-covid-data.csv"
download.file("https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/EXCEL_FILES/3_Mortality/WPP2019_MORT_F07_2_LIFE_EXPECTANCY_0_MALE.xlsx",
destfile = "Data/wpp2019_male_e0.xlsx")
download.file("https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/EXCEL_FILES/3_Mortality/WPP2019_MORT_F07_3_LIFE_EXPECTANCY_0_FEMALE.xlsx",
destfile = "Data/wpp2019_female_e0.xlsx")
library(readr)
library(readxl)
library(tidyverse)
@timriffe
timriffe / vglm_comparisons.R
Last active June 7, 2022 20:13
Comparisons of refstate leverage, as well as
# a little exercise for Angelo and me
library(VGAM)
library(markovchain)
library(tidyverse)
source("https://raw.githubusercontent.com/timriffe/PHDS_HLE_2021/main/Functions.R")
# read in original transition estimates
TR <- read_csv("https://raw.githubusercontent.com/timriffe/PHDS_HLE_2021/main/Data/TRv6.csv")
@timriffe
timriffe / monotonic_interpolation_tests.R
Created May 19, 2022 15:26
testing methods for monotonic interpolation
library(lubridate)
# options for a truly monotonic case:
set.seed(1)
values <- runif(100) %>% cumsum() %>% '*'(100) %>% round()
all_dates <- seq(dmy("01.01.2020"),today(), by = "days")
my_dates <- sample(all_dates, size = 100, replace = FALSE) %>% sort()
plot(my_dates, main = "irregularly spaced dates")
sum(day(my_dates) == 1) # just 4 days are 1st of month
@timriffe
timriffe / birth_offsets.R
Created April 7, 2022 18:04
A script to test splitting age-grouped census data using a long prior birth series
library(tidyverse)
library(HMDHFDplus)
library(ungroup)
getwd()
# B <- readRDS("Data/SWE_final.Rdata")
# Bt <- B %>%
# group_by(Year) %>%
# summarize(Births = sum(Births))
#
# SWEpop <- readHMDweb("SWE","Population5")
@timriffe
timriffe / UK.R
Created February 23, 2022 12:31
Needed this snapshot, which at the moment was failing in COVerAGE-DB for reasons yet to be fixed
UK <- structure(list(country = c("United Kingdom", "United Kingdom",
"United Kingdom", "United Kingdom", "United Kingdom", "United Kingdom",
"United Kingdom", "United Kingdom", "United Kingdom", "United Kingdom",
"United Kingdom", "United Kingdom", "United Kingdom", "United Kingdom",
"United Kingdom", "United Kingdom", "United Kingdom", "United Kingdom",
"United Kingdom", "United Kingdom", "United Kingdom", "United Kingdom",
"United Kingdom", "United Kingdom", "United Kingdom", "United Kingdom",
"United Kingdom", "United Kingdom", "United Kingdom", "United Kingdom",
"United Kingdom", "United Kingdom", "United Kingdom", "United Kingdom",
"United Kingdom", "United Kingdom", "United Kingdom", "United Kingdom"