Skip to content

Instantly share code, notes, and snippets.

@timriffe
Created September 9, 2023 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timriffe/450f8f874729eb14d8a74e3dd3644358 to your computer and use it in GitHub Desktop.
Save timriffe/450f8f874729eb14d8a74e3dd3644358 to your computer and use it in GitHub Desktop.
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){
# (lazy-man's approximation)
# we're not sacrificing too much precision here. just pretend...
lx <- c(1, exp(-cumsum(mx)))
(lx[-1] + lx[-length(lx)]) / 2
}
mxm <- mlt |> filter(Year == 2000) |> pull(mx)
mxf <- flt |> filter(Year == 2000) |> pull(mx)
sully <- function(mx, prev){
Lx <- mx2Lx(mx)
sum(Lx * prev)
}
sully_vec <- function(pars){
dim(pars) <- c(111,2)
sully(mx = pars[,1], prev = pars[,2])
}
prev_f_1 <- seq(0,.5,length=111)
prev_m_1 <- prev_f_1 * .8
prev_f_2 <- seq(.2,.6,length=111)
prev_m_2 <- prev_f_2 * .9
cc1 <- horiuchi(sully_vec, c(mxm,prev_m_1), c(mxf,prev_f_1), N = 20)
cc2 <- horiuchi(sully_vec, c(mxm,prev_m_2), c(mxf,prev_f_2), N = 20)
dim(cc1) <- c(111,2)
dim(cc2) <- c(111,2)
# first element is the mortality contribution
colSums(cc1)
colSums(cc2)
# no I'm curious what the mort-only difference is
mx2ex <- function(mx){
sum(mx2Lx(mx))
}
mx2ex(mxf) - mx2ex(mxm)
horiuchi(mx2ex,mxm, mxf, N = 1000) |> sum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment