Advertisement
Guest User

Untitled

a guest
Jan 17th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. library(HMDHFDplus) # on CRAN
  2. library(DecompHoriuchi) # on github
  3.  
  4. # define your HMD username and password as objects, us, and pw
  5. mlt <- readHMDweb("USA","mltper_1x1",username=us,password = pw)
  6. flt <- readHMDweb("USA","fltper_1x1",username=us,password = pw)
  7.  
  8. mxm1 <- mlt$mx[mlt$Year == 1950]
  9. mxm2 <- mlt$mx[mlt$Year == 2000]
  10. mxf1 <- flt$mx[flt$Year == 1950]
  11. mxf2 <- flt$mx[flt$Year == 2000]
  12.  
  13. # translate into initial conditions versus change.
  14. minit <- mxm1
  15. finit <- mxf1
  16. mchg <- mxm2 - mxm1
  17. fchg <- mxf2 - mxf1
  18.  
  19. # cheap e0 function of initial conditions and change
  20. mye0 <- function(mxinit, mxchg){
  21. mx <- mxinit + mxchg
  22. sum(exp(-cumsum(mx)))
  23. }
  24.  
  25. # need the same thing but that takes a single vector of rates.
  26. mye0vec <- function(mxvec){
  27. # clearly the vecs need to be of the same length
  28. mxinit <- mxvec[1:(length(mxvec)/2)]
  29. mxchg <- mxvec[(length(mxvec)/2+1):length(mxvec)]
  30. mye0(mxinit, mxchg)
  31. }
  32.  
  33. # now decompose:
  34. males <- c(minit,mchg)
  35. females <- c(finit,fchg)
  36.  
  37. results <- DecompContinuousOrig(mye0vec, females, males, 20)
  38.  
  39. initial <- results[1:111]
  40. change <- results[112:222]
  41.  
  42. # take a quick look.
  43. barplot(rbind(initial,change),space=0,border=NA)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement