Guest User

Untitled

a guest
Dec 12th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. library("StMoMo")
  2.  
  3. label="U.S.A."
  4.  
  5. mx <- try(utils::read.table("C:/Users/myself/Downloads/USA/STATS/Mx_1x1.txt", skip = 2, header = TRUE,
  6. na.strings = "."), TRUE)
  7. mx = mx[mx$Year<=2014 & mx$Year>=1977,]
  8.  
  9. pop <- try(utils::read.table("C:/Users/myself/Downloads/USA/STATS/Exposures_1x1.txt", skip = 2, header = TRUE,
  10. na.strings = "."), TRUE)
  11. pop = pop[pop$Year<=2014 & pop$Year>=1977,]
  12. obj <- list(type = "mortality", label = label, lambda = 0)
  13. obj$year <- sort(unique(mx[, 1]))
  14. n <- length(obj$year)
  15. m <- length(unique(mx[, 2]))
  16. obj$age <- mx[1:m, 2]
  17. mnames <- names(mx)[-c(1, 2)]
  18. n.mort <- length(mnames)
  19. obj$rate <- obj$pop <- list()
  20. for (i in 1:n.mort) {
  21. obj$rate[[i]] <- matrix(mx[, i + 2], nrow = m, ncol = n)
  22. obj$rate[[i]][obj$rate[[i]] < 0] <- NA
  23. obj$pop[[i]] <- matrix(pop[, i + 2], nrow = m, ncol = n)
  24. obj$pop[[i]][obj$pop[[i]] < 0] <- NA
  25. dimnames(obj$rate[[i]]) <- dimnames(obj$pop[[i]]) <- list(obj$age,
  26. obj$year)
  27. }
  28. names(obj$pop) = names(obj$rate) <- tolower(mnames)
  29. obj$age <- as.numeric(as.character(obj$age))
  30. if (is.na(obj$age[m]))
  31. obj$age[m] <- 2 * obj$age[m - 1] - obj$age[m - 2]
  32. USATrain = (structure(obj, class = "demogdata"))
  33.  
  34. label="U.S.A."
  35.  
  36. mx <- try(utils::read.table("C:/Users/myself/Downloads/USA/STATS/Mx_1x1.txt", skip = 2, header = TRUE,
  37. na.strings = "."), TRUE)
  38.  
  39. pop <- try(utils::read.table("C:/Users/myself/Downloads/USA/STATS/Exposures_1x1.txt", skip = 2, header = TRUE,
  40. na.strings = "."), TRUE)
  41. obj <- list(type = "mortality", label = label, lambda = 0)
  42. obj$year <- sort(unique(mx[, 1]))
  43. n <- length(obj$year)
  44. m <- length(unique(mx[, 2]))
  45. obj$age <- mx[1:m, 2]
  46. mnames <- names(mx)[-c(1, 2)]
  47. n.mort <- length(mnames)
  48. obj$rate <- obj$pop <- list()
  49. for (i in 1:n.mort) {
  50. obj$rate[[i]] <- matrix(mx[, i + 2], nrow = m, ncol = n)
  51. obj$rate[[i]][obj$rate[[i]] < 0] <- NA
  52. obj$pop[[i]] <- matrix(pop[, i + 2], nrow = m, ncol = n)
  53. obj$pop[[i]][obj$pop[[i]] < 0] <- NA
  54. dimnames(obj$rate[[i]]) <- dimnames(obj$pop[[i]]) <- list(obj$age,
  55. obj$year)
  56. }
  57. names(obj$pop) = names(obj$rate) <- tolower(mnames)
  58. obj$age <- as.numeric(as.character(obj$age))
  59. if (is.na(obj$age[m]))
  60. obj$age[m] <- 2 * obj$age[m - 1] - obj$age[m - 2]
  61. FullData = (structure(obj, class = "demogdata"))
  62.  
  63. usaMale = StMoMoData(USATrain, series = "male")
  64. usaMaleStMoMo <- central2initial(usaMale)
  65. ages.fit <- 30:95
  66. LC = lc(link = "logit")
  67. APC = apc(link = "logit")
  68. M7 = m7()
  69. M6 = m6()
  70. M8 = m8(xc = 89)
  71. CBD = cbd()
  72. RH <- rh(link = "logit", cohortAgeFun = "1")
  73.  
  74. f2 <- function(x, ages) mean(ages) - x
  75.  
  76. constPlat <- function(ax, bx, kt, b0x, gc, wxt, ages){
  77. nYears <- dim(wxt)[2]
  78. x <- ages
  79. t <- 1:nYears
  80. c <- (1 - tail(ages, 1)):(nYears - ages[1])
  81. xbar <- mean(x)
  82. phiReg <- lm(gc ~ 1 + c + I(c ^ 2), na.action = na.omit)
  83. phi <- coef(phiReg)
  84. gc <- gc - phi[1] - phi[2] * c - phi[3] * c ^ 2
  85. kt[2, ] <- kt[2, ] + 2 * phi[3] * t
  86. kt[1, ] <- kt[1, ] + phi[2] * t + phi[3] * (t ^ 2 - 2 * xbar * t)
  87. ax <- ax + phi[1] - phi[2] * x + phi[3] * x ^ 2
  88. ci <- rowMeans(kt, na.rm = TRUE)
  89. ax <- ax + ci[1] + ci[2] * (xbar - x)
  90. kt[1, ] <- kt[1, ] - ci[1]
  91. kt[2, ] <- kt[2, ] - ci[2]
  92. list(ax = ax, bx = bx, kt = kt, b0x = b0x, gc = gc)
  93. }
  94.  
  95.  
  96.  
  97. PLAT <- StMoMo(link = "logit", staticAgeFun = TRUE, periodAgeFun = c("1", f2), cohortAgeFun = "1", constFun = constPlat)
  98.  
  99. wxt <- genWeightMat(ages = ages.fit, years = usaMaleStMoMo$years, clip = 3)
  100.  
  101. LCfit <- fit(LC, data = usaMaleStMoMo, ages.fit = ages.fit, wxt = wxt)
  102. APCfit <- fit(APC, data = usaMaleStMoMo, ages.fit = ages.fit, wxt = wxt)
  103. CBDfit <- fit(CBD, data = usaMaleStMoMo, ages.fit = ages.fit, wxt = wxt)
  104. M7fit <- fit(M7, data = usaMaleStMoMo, ages.fit = ages.fit, wxt = wxt)
  105. M6fit <- fit(M6, data = usaMaleStMoMo, ages.fit = ages.fit, wxt = wxt)
  106. M8fit <- fit(M8, data = usaMaleStMoMo, ages.fit = ages.fit, wxt = wxt)
  107. PLATfit <- fit(PLAT, data = usaMaleStMoMo, ages.fit = ages.fit, wxt = wxt)
  108. RHfit <- fit(RH, data = usaMaleStMoMo, ages.fit = ages.fit, wxt = wxt, start.ax = LCfit$ax, start.bx = LCfit$bx, start.kt = LCfit$kt)
  109.  
  110. LCfor <- forecast(LCfit, h = 50)
  111. CBDfor <- forecast(CBDfit, h = 50)
  112. APCfor <- forecast(APCfit, h = 50, gc.order = c(1, 1, 0))
  113. RHfor <- forecast(RHfit, h = 50, gc.order = c(1, 1, 0))
  114. M7for <- forecast(M7fit, h = 50, gc.order = c(2, 0, 0))
  115. M6for <- forecast(M6fit, h = 50, gc.order = c(2, 0, 0))
  116. M8for <- forecast(M8fit, h = 50, gc.order = c(2, 0, 0))
  117. PLATfor <- forecast(PLATfit, h = 50, gc.order = c(2, 0, 0))
  118.  
  119. actual2015 = FullData$rate$male[ages.fit+1,c("2015")]
  120. LC2015 = LCfor$rates[,1]
  121. CBD2015 = CBDfor$rates[,1]
  122. APC2015 = APCfor$rates[,1]
  123. RH2015 = RHfor$rates[,1]
  124. M72015 = M7for$rates[,1]
  125. M62015 = M6for$rates[,1]
  126. M82015 = M8for$rates[,1]
  127. PLAT2015 = PLATfor$rates[,1]
  128.  
  129. aicbic = matrix(ncol = 8, nrow = 4)
  130. colnames(aicbic) = c("LC", "CBD", "APC", "RH", "M7", "PLAT", "M6", "M8")
  131. rownames(aicbic) = c("Number Parameters", "AIC", "BIC", "Average Pct Error for t+1")
  132.  
  133. aicbic[1,1]=LCfit$npar
  134. aicbic[1,2]=CBDfit$npar
  135. aicbic[1,3]=APCfit$npar
  136. aicbic[1,4]=RHfit$npar
  137. aicbic[1,5]=M7fit$npar
  138. aicbic[1,6]=PLATfit$npar
  139. aicbic[1,7]=M6fit$npar
  140. aicbic[1,8]=M8fit$npar
  141. aicbic[2,1]=AIC(LCfit)
  142. aicbic[2,2]=AIC(CBDfit)
  143. aicbic[2,3]=AIC(APCfit)
  144. aicbic[2,4]=AIC(RHfit)
  145. aicbic[2,5]=AIC(M7fit)
  146. aicbic[2,6]=AIC(PLATfit)
  147. aicbic[2,7]=AIC(M6fit)
  148. aicbic[2,8]=AIC(M8fit)
  149. aicbic[3,1]=BIC(LCfit)
  150. aicbic[3,2]=BIC(CBDfit)
  151. aicbic[3,3]=BIC(APCfit)
  152. aicbic[3,4]=BIC(RHfit)
  153. aicbic[3,5]=BIC(M7fit)
  154. aicbic[3,6]=BIC(PLATfit)
  155. aicbic[3,7]=BIC(M6fit)
  156. aicbic[3,8]=BIC(M8fit)
  157. aicbic[4,1]=mean((LC2015 - actual2015)/actual2015)*100
  158. aicbic[4,2]=mean((CBD2015 - actual2015)/actual2015)*100
  159. aicbic[4,3]=mean((APC2015 - actual2015)/actual2015)*100
  160. aicbic[4,4]=mean((RH2015 - actual2015)/actual2015)*100
  161. aicbic[4,5]=mean((M72015 - actual2015)/actual2015)*100
  162. aicbic[4,6]=mean((PLAT2015 - actual2015)/actual2015)*100
  163. aicbic[4,7]=mean((M62015 - actual2015)/actual2015)*100
  164. aicbic[4,8]=mean((M82015 - actual2015)/actual2015)*100
  165.  
  166. View(aicbic)
  167.  
  168. LCRes = residuals(LCfit)
  169. plot(LCRes, type = "colourmap", main = "Lee-Carter Residuals")
  170.  
  171. APCRes = residuals(APCfit)
  172. plot(APCRes, type = "colourmap", main = "Age-Period-Cohort Residuals")
  173.  
  174. RHRes = residuals(RHfit)
  175. plot(RHRes, type = "colourmap", main = "Renshaw and Haberman Residuals")
  176.  
  177. M7Res = residuals(M7fit)
  178. plot(M7Res, type = "colourmap", main = "M7 Residuals")
  179.  
  180. PLATRes = residuals(PLATfit)
  181. plot(PLATRes, type = "colourmap", main = "Plat Residuals")
  182.  
  183. M6Res = residuals(M6fit)
  184. plot(M6Res, type = "colourmap", main = "M6 Residuals")
  185.  
  186. M8Res = residuals(M8fit)
  187. plot(M8Res, type = "colourmap", main = "M8 Residuals")
Add Comment
Please, Sign In to add comment