-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0-CreateProject.Rmd
238 lines (181 loc) · 7.73 KB
/
0-CreateProject.Rmd
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# Create HBV model project
# ========================
Authors: Richard T. Gray, Neil Bretana
This Rmarkdown script is used to create a HBV modelling project specified
by the user. The project specifications entered by the user are hardcoded
in the "Project specifications" chunk.
```{r setup}
# Clear workspace
rm(list = ls())
library("readr")
library("dplyr")
library("ggplot2")
# Set working directory to source file directory first
basePath <- getwd()
```
## User inputs
The following chunk is where the user enters the project specifications
```{r Project specifications}
# Hard coded user specified project details
project_name <- "test_project"
project_directory <- file.path(basePath, "projects")
# Start of the first year
start_year <- 1951
#start of the final year represents end of year before
end_year <- 2050
# Simulation timestep
timestep <- 0.1
# Populations modelled
full_population_names <- c("Infants aged 0 to 4",
"Children aged 5 to 14",
"Adults aged 15 to 44",
"Adults older tha 45")
population_names <- c("age0to4",
"age5to14",
"age15to44",
"age45")
```
## Project creation
The following specifications are fixed for all models and are required
for the model to run (i.e. they need to be changed if the model changes)
```{r Model specifications}
# Fixed compartmental names
full_state_names <- c("susceptible",
"acute",
"chronic",
"cleared",
"immune")
# Short compartmental names
state_names <- c("s", "a", "ch", "cl", "i")
```
```{r Parameter specifications}
# Note parameters that are for specifying the initial population sizes
# so they can be stored separately
initialPopsNames <- c("init_pop", "pop_prop_", "sus_prop_", "acute_prop_",
"chronic_prop_", "clear_prop_", "immune_prop_")
# The following constant parameters are generated for each population
constNamesPops <- c("sus_prop_", "acute_prop_", "chronic_prop_",
"clear_prop_", "immune_prop_",
"pop_prop_", "prog_chron_",
"clr_rate_", "ac_mort_rate_",
"chr_mort_rate_", "vac_eff_",
"vac_prop_")
# The following constant parameters are the same for all populations or
# population agnostic.
constNames <- c("init_pop", "mig_series", "dis_rate", "ac_res_rate",
"fol_mult", "vacc_tog", "chronic_beta")
# Note pop_prop and init_pop are only used for setting up the initial
# population.
# Following parameters all vary over time
timeVaryNames <- c("years", "notifications", "births")
# TODO - make these population varying as well like the constants - 0 to
# npops
timeVaryPops <- c("bgmort0to4", "bgmort5to14", "bgmort15to44", "bgmort45",
"mig0to4chronic", "mig0to4cleared", "mig0to4sus",
"mig5to14chronic", "mig5to14cleared", "mig5to14sus",
"mig15to44chronic", "mig15to44cleared", "mig15to44sus",
"mig45chronic", "mig45cleared", "mig45sus")
```
The following chunk creates the files used for inputing parameters based
on the project specifications. Once these are created the user needs to
enter input values and ranges for the model to run
```{r Create directories and csv files}
# Create project directory
projectFolder <- file.path(project_directory, project_name)
dir.create(projectFolder, showWarnings = FALSE)
# Create other directories
dir.create(file.path(projectFolder, "results"), showWarnings = FALSE)
dir.create(file.path(projectFolder, "docs"), showWarnings = FALSE)
dir.create(file.path(projectFolder, "data"), showWarnings = FALSE)
# Create parameter files -------------------------------------------------
npops <- length(population_names)
nyears <- end_year - start_year + 1
numConstants <- npops * length(constNamesPops) + length(constNames)
# Create constants file
constants <- data_frame(parameter = character(numConstants),
value = 0,
lower = 1,
upper = 1)
for (param in 1:length(constNamesPops)) {
popParams <- paste0(constNamesPops[param], as.character(0:(npops-1)))
index <- (param - 1) * npops + 1
constants$parameter[index:(index+npops-1)] <- popParams
}
constants$parameter[(index + npops):numConstants] <- constNames
# Separate initial population parameters from other parameters
fullInitialNames <- character()
for (param in 1:length(initialPopsNames)){
if (initialPopsNames[param] == "init_pop") {
fullInitialNames <- c(fullInitialNames, initialPopsNames[param])
} else {
initialParams <- paste0(initialPopsNames[param],
as.character(0:(npops-1)))
fullInitialNames <- c(fullInitialNames, initialParams)
}
}
initialPops <- constants %>%
filter(parameter %in% fullInitialNames)
constants <- constants %>%
filter(!(parameter %in% fullInitialNames))
# Write constants to file
write_csv(initialPops, file.path(projectFolder,
"initial_populations.csv"))
write_csv(constants, file.path(projectFolder,
"parameters_constants.csv"))
# Create time varying paramater file
# TODO: change when time varying population paramaters are included
timeVarying <- as.data.frame(matrix(0, nrow = nyears,
ncol = length(timeVaryNames) + length(timeVaryPops)))
colnames(timeVarying) <- c(timeVaryNames, timeVaryPops)
timeVarying$years <- start_year:end_year
write_csv(timeVarying, file.path(projectFolder,
"parameters_time_varying.csv"))
# Create time varying relative factors file - for each time varying
# parameter create a start range and an upper range for the relative
# parameter values
timeFactors <- as.data.frame(matrix(1, nrow = ncol(timeVarying),
ncol = 5))
colnames(timeFactors) <- c("parameter", "start_lower", "start_upper",
"end_lower", "end_upper")
timeFactors$parameter <- colnames(timeVarying)
write_csv(timeFactors, file.path(projectFolder,
"time_varying_ranges.csv"))
# Create associated transitions and waifw_matrix files -------------------
transitions <- as.data.frame(matrix(0, nrow = npops, ncol = npops + 1))
colnames(transitions) <- c("population", population_names)
transitions$population <- population_names
waifw_matrix <- transitions
write_csv(transitions, file.path(projectFolder,
"population_transitions.csv"))
write_csv(waifw_matrix, file.path(projectFolder,
"population_waifw_matrix.csv"))
```
The following chunk creates a project R data file for storing the project
variables and inputs
```{r Create project specifications list}
# Create project specifications list
pg <- list()
pg$project_name <- project_name
pg$project_directory <- project_directory
pg$start_year <- start_year
pg$end_year <- end_year
pg$timestep <- timestep
pg$full_population_names <- full_population_names
pg$population_names <- population_names
# Additional useful specs - further specs will be appended when generating
# the parameter sets for a project
pg$npops <- npops
pg$years <- start_year:end_year
pg$nyears <- nyears
pg$full_state_names <- full_state_names
pg$state_names <- state_names
pg$nstates <- length(state_names)
# Create an inputs list
inputs <- list(initial_pops = initialPops, constants = constants,
time_varying = timeVarying,
time_varying_ranges = timeFactors,
transitions = transitions, waifw_matrix = waifw_matrix)
# Create project .rda files
save(pg, inputs, file = file.path(projectFolder,
paste0(project_name, ".rda")))
```