Skip to content

Instantly share code, notes, and snippets.

View timriffe's full-sized avatar

Tim Riffe timriffe

View GitHub Profile
@timriffe
timriffe / arriaga_comp.R
Last active January 18, 2024 20:19
Idea to do an Arriaga decomposition that includes a component for compositional change. What do you think?
# testing Arriaga with composition.
# A little script I wrote for Michael Lachanski. And the world
# simiulate 2 groups at two time points,
# Gompertz mort, with parameters a and b
# we will use functions from a package in development coddecomp
# install like so:
# remotes::install_github("timriffe/coddecomp")
library(coddecomp)
@timriffe
timriffe / prevalence_shareable.R
Last active December 12, 2023 15:46
A script to smooth age difference prevalence
library(tidyverse)
library(splines)
N <- 2000
x <- rnorm(N,mean = 2, sd = 5) |>
round() |>
table()
age_diff <- x |> names() |> as.integer()
@timriffe
timriffe / ecm.R
Last active December 6, 2023 21:31
extinct cohort method in tidy-speak
library(HMDHFDplus)
library(tidyverse)
library(janitor)
d <- readHMDweb("ESP","Deaths_lexis",
username = Sys.getenv("us"),
password = Sys.getenv("pw"))
p <- readHMDweb("ESP","Population",
username = Sys.getenv("us"),
password = Sys.getenv("pw"))
@timriffe
timriffe / JPN_check.R
Created November 17, 2023 11:03
Check JPN old age mort using census denominators
library(tidyverse)
pop <- read_csv("Data/JPNpop.csv")
Exp <-
pop |>
filter(Type == "C",
Year > 1970,
Age != "TOT") |>
select(Sex, Year, Age, Population) |>
@timriffe
timriffe / M_spline.R
Created November 14, 2023 13:00
hacky M estimation from single-age data
devtools::load_all()
# library(HMDHFDplus)
# mx <- readHMDweb("USA","mltper_1x1",
# username = Sys.getenv("us"),
# password = Sys.getenv("pw")) |>
# filter(Year == 2000) |>
# pull(mx)
mx <- c(0.00791, 0.00056, 0.00038, 0.00029, 0.00022, 0.00021, 0.00019,
@timriffe
timriffe / sen_arriaga_instantaneous.R
Created November 6, 2023 11:37
arriaga sensitivity, an experiment
library(tidyverse)
mx_to_lx <- function(mx){
mx[is.na(mx)] <- 0
lx <- exp(-cumsum(mx))
lx <- c(1,lx)
lx[1:length(mx)]
}
lx_to_dx <- function(lx){
-diff(c(lx,0))
@timriffe
timriffe / ine_diagnostics.R
Created October 30, 2023 15:43
quick diagnostic of INE COD data
# https://www.ine.es/jaxiT3/Tabla.htm?t=7947
library(tidyverse)
ine <- read_tsv("Data/INE7947.csv",locale = locale(grouping_mark = "."))
ine |>
rename(cause = `Cause of death`) |>
mutate(cause = if_else(cause=="001-102 I-XXII.All causes","All",cause)) |>
filter(!grepl("-",cause),
Age != "All ages") |>
mutate(
@timriffe
timriffe / sullivan_decomp_mort_check.R
Created September 9, 2023 06:38
testing whether the mortality component of a sullivan decompo ought to be same for different prev scenarios
# Testing whether mort component should be identical in decomps where
# only prev swaps out
library(tidyverse)
library(HMDHFDplus)
library(DemoDecomp)
mlt <- readHMDweb("USA", "mltper_1x1", username = Sys.getenv("us"), password = Sys.getenv("pw"))
flt <- readHMDweb("USA", "fltper_1x1", username = Sys.getenv("us"), password = Sys.getenv("pw"))
mx2Lx <- function(mx){
@timriffe
timriffe / coale.R
Created July 7, 2023 07:05
test data for Coale's r estimation function
srb <- 1.05
pfb <- 1/ (1 + srb)
fxf <- c(0.00000,0.00003,0.00038,0.00146,0.00436,0.01006,0.01963,0.03576,
0.06036,0.09112,0.12532,0.15873,0.18765,0.20418,0.20929,0.20586,
0.19741,0.18698,0.16816,0.14313,0.12684,0.11769,0.11308,0.10262,
0.08831,0.07622,0.06532,0.05493,0.04629,0.03623,0.02455,0.01515,
0.00881,0.00494,0.00305,0.00219,0.00174,0.00140,0.00124,0.00065,
0.00019,0.00005,0.00001,0.00000) * pfb
fxf <- c(rep(0,12),fxf,rep(0,55))
@timriffe
timriffe / cause_deletion_gap_test.R
Created May 26, 2023 05:53
Cause-deleted lifetables can only result in an equal gap (before and after) if the deleted causes are *not* proportional
library(HMDHFDplus)
library(tidyverse)
mlt <- readHMDweb("ESP","mltper_1x1",username = Sys.getenv("us"),password = Sys.getenv("pw"))
# 1950 Spanish males
mx1 <- c(0.08382, 0.01361, 0.00764, 0.00464, 0.0032, 0.00245, 0.00222,