Created
September 9, 2023 06:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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