forked from yimengyin16/Model_Main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model_RunControl_reprod_M1.R
130 lines (85 loc) · 4.29 KB
/
Model_RunControl_reprod_M1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
## Run control file for pension simulation model
## 8/2017
#*********************************************************************************************************
# Preamble ####
#*********************************************************************************************************
rm(list = ls())
gc()
library(knitr)
library(data.table)
library(gdata) # read.xls
library(plyr)
library(dplyr)
library(ggplot2)
library(magrittr)
library(tidyr) # gather, spread
library(foreach)
library(doParallel)
library(microbenchmark)
library(readxl)
library(stringr)
# library(xlsx)
# library(XLConnect) # slow but convenient because it reads ranges
# devtools::install_github("donboyd5/decrements")
# devtools::install_github("donboyd5/pp.prototypes")
library(pp.prototypes)
library(decrements) # mortality and termination for now
# Initial actives and retirees
# Load data for new prototypes before they are in the pp.prototypes package
load("Data/2015-10-07/actives.rda")
load("Data/2015-10-07/retirees.rda")
# Decrements
load("Data/2015-10-07/retrates.rda"); retrates %<>% dplyr::rename(qxr = retrate)
# load("Data/retrates_AZ.RData"); retrates <- retrate_AZ
load("Data/2015-10-07/termrates.rda"); termrates %<>% dplyr::rename(qxt = termrate) # %>% mutate(qxt = 0.5*qxt)
load("Data/2015-10-07/mortality.rda")
load("Data/winklevossdata.rdata") # disability, disability mortaity and early retirement
# Salary scale
load("Data/2015-10-07/salgrowth.rda"); salgrowth %<>% mutate(age = NULL)
source("Functions.R")
devMode <- FALSE # Enter development mode if true. Parameters and initial population will be imported from Dev_Params.R instead of the RunControl file.
#*********************************************************************************************************
# ## Calibration of decrements ####
#*********************************************************************************************************
# Calibrate term rates, mortality rates and retirement rates to approximately match workforce flows of AZ-PERS
# in 2013.
termrates %<>% mutate(qxt = 1.2 * qxt)
mortality %<>% mutate(qxm = 0.6 * qxm) %>%
mutate(qxm.r = qxm)
retrates %<>% mutate(qxr = qxr * 0.7)
#*********************************************************************************************************
# Read in Run Control files ####
#*********************************************************************************************************
folder_run <- "IO_M1_new"
filename_RunControl <-"Reprod_RunControl_M1_new.xlsx"
path_RunControl <- paste0(folder_run, "/" ,filename_RunControl)
# Import global parameters
Global_params <- read_excel(path_RunControl, sheet="GlobalParams", skip = 1)
# Import parameters for all plans
plan_params <- read_excel(path_RunControl, sheet="RunControl", skip=4) %>% filter(!is.na(runname))
plan_returns <- read_excel(path_RunControl, sheet="Returns", skip=0) %>% filter(!is.na(runname))
plan_contributions <- read_excel(path_RunControl, sheet="Contributions", skip=0) %>% filter(!is.na(runname))
#*********************************************************************************************************
# Read in Run Control files ####
#*********************************************************************************************************
## select plans
runlist <- plan_params %>% filter(include == TRUE) %>% select(runname) %>% unlist
runlist
## Run selected plans
for (runName in runlist){
suppressWarnings(rm(paramlist, Global_paramlist))
## Extract plan parameters
paramlist <- get_parmsList(plan_params, runName)
paramlist$plan_returns <- plan_returns %>% filter(runname == runName)
if(paramlist$exCon) paramlist$plan_contributions <- trans_cont(plan_contributions, runName) else
paramlist$plan_contributions <- list(0)
## Extract global parameters and coerce the number of simulation to 1 when using deterministic investment reuturns.
Global_paramlist <- Global_params %>% as.list
if ((paramlist$return_type == "simple" & paramlist$ir.sd == 0) |
(paramlist$return_type == "internal" & all(paramlist$plan_returns$ir.sd == 0))|
(paramlist$return_type == "external")){
Global_paramlist$nsim <- 1
}
## Run the model
source("Model_Master.R", echo = TRUE)
}