Advertisement
Guest User

Untitled

a guest
Aug 18th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. # Author: tim
  2. ###############################################################################
  3.  
  4. stand <- function(x){
  5. x/sum(x)
  6. }
  7. mx2lx <- function(mx, trunc = 0){
  8. # pretend mx is continuous hazard, cheap trick
  9. # ought to work fine if we left truncate too
  10. x <- 1:length(mx) - 1
  11. mx <- mx[x >= trunc]
  12.  
  13. c(1,exp(-cumsum(mx)))[1:length(mx)]
  14. }
  15.  
  16. mx2dx <- function(mx, trunc = 0){
  17. x <- 1:length(mx) - 1
  18. mx <- mx[x >= trunc]
  19. # use previous function, first diff gives dx,
  20. # just need to peg on the zero to close out
  21. lx <- mx2lx(mx)
  22. -diff(c(lx,0))
  23. }
  24.  
  25. mx2ex <- function(mx, trunc = 0){
  26. x <- 1:length(mx) - 1
  27. mx <- mx[x >= trunc]
  28. lx <- mx2lx(mx)
  29. lx <- c(lx,0)
  30. # spline interpolate to get Lx. force monotonic decline.
  31. # works best in log, then unlog :-)
  32. Lx <- exp(splinefun(log(lx)~c(x,x[length(x)+1]),method="monoH.FC")((x+.5)))
  33. # Tx are all the years lived above age x
  34. Tx <- rev(cumsum(rev(Lx)))
  35. # conditioned on survival, this give ex
  36. Tx / lx[-length(lx)]
  37. }
  38.  
  39. # essentially same as excercise edagger, but we need it to start from mx
  40. # this is why we needed all the above functions
  41. mx2edagger <- function(mx, trunc = 0){
  42.  
  43. # this is the new part
  44. dx <- mx2dx(mx)
  45. ex <- mx2ex(mx)
  46.  
  47. age <- 1:length(mx)-1
  48. ex <- ex[age >= trunc]
  49. dx <- dx[age >= trunc]
  50. dx <- stand(dx)
  51. age <- age[age >= trunc]
  52. # assume constant hazard after 110 = same ex
  53. ex <- c(ex,ex[length(ex)])
  54. age2 <- c(age,age[length(age)]+1)
  55. # linear interp would give the same thing.
  56. ex.5 <- splinefun(ex~age2,method="monoH.FC")((age+.5))
  57. sum(ex.5 * dx)
  58. }
  59.  
  60. library(DecompHoriuchi)
  61. DecompContinuousOrig()
  62.  
  63. library(HMDHFDplus)
  64. SWE <- readHMDweb("SWE","mltper_1x1",username=us,password=pw)
  65. RUS <- readHMDweb("RUS","mltper_1x1",username=us,password=pw)
  66.  
  67. # OK, sort of experimental here to select years, but let's just compare
  68. # for years of similar e(0). Could also compare within years...
  69.  
  70. e0swe <- SWE$ex[SWE$Age == 0]
  71. e0rus <- RUS$ex[RUS$Age == 0]
  72.  
  73. yrswe <- sort(unique(SWE$Year))
  74. yrrus <- sort(unique(RUS$Year))
  75.  
  76. # problem, there is basically no overlap!!!
  77. plot(yrrus, e0swe[yrswe %in% yrrus], ylim = c(55,80))
  78. lines(yrrus, e0rus)
  79.  
  80. # literally, we'd need to go back to close to 1900 to find
  81. # a SWE e(0) close to Russia's
  82.  
  83. # so here's a decomp within year:
  84. Year <- 2000
  85. mxrus <- RUS$mx[RUS$Year == Year]
  86. mxswe <- SWE$mx[SWE$Year == Year]
  87.  
  88.  
  89. # compare edaggers
  90. (edswe <- mx2edagger(mxswe))
  91. (edrus <- mx2edagger(mxrus))
  92. edrus - edswe # by this measure, deaths of Swedes result in 5 fewer years of life lost
  93. # than those of Russians (year 2000)
  94. # N = time steps over which to integrate. Bigger isn't necessarily better...
  95. Diff <- DecompContinuousOrig(mx2edagger, mxswe, mxrus, N = 20)
  96.  
  97. sum(Diff); edrus - edswe # pretty close. close enough says I.
  98.  
  99. barplot(Diff, main = "Age pattern to difference in edagger", sub =
  100. "(where differences are assigned to ages in
  101. which mortality rate differences occur)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement