-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2-CalibrateHBVmodel.R
427 lines (346 loc) · 17.3 KB
/
2-CalibrateHBVmodel.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
## Function used fo calibrate the HBV model for a inputed project
# This function is used to load updated model paramters and rerun the HBV
# model for the best estimate parameters. It's primary purpose is to
# assist with the calibration of the model to available data. It is a
# combination of the 1-SetupModel.Rmd and 2-RunHBVmodel.Rmd scripts.
#
# 1-SetupModel.Rmd needs to be run before using this function to ensure all
# the project specifications are entered and after the calibrations is
# complete to save the parmeters into the project.
#
# Calibration process.....
#
# Authors: Richard T. Gray, Neil Bretana
CalibrateHIVmodel <- function(project, resource = FALSE) {
# Function for calibrating a HBV model project
#
# Args:
# project: list containing project specifications
# resource: Set to true to source main libraries and functions.
# Should only need to do this once.
# Returns:
# Generates a plot for visual inspection.
#
# Initilization --------------------------------------------------------
graphics.off() # Close current plot
# Need to set wd to source file location
basePath <- getwd()
project_directory <- file.path(basePath, "projects")
if (resource) {
Rcode <- file.path(basePath, "code")
# Load useful libraries
source(file.path(Rcode, "LoadLibrary.R"))
source(file.path(Rcode, "DataLibraries.R"))
source(file.path(Rcode, "PlotOptions.R"))
source(file.path(Rcode, "PlotFunctions.R"))
LoadLibrary(cowplot) # Note masks ggsave
# Source model files
source(file.path(Rcode, "HBVmodel.R"))
}
#Load current project specs and inputs ---------------------------------
projectFolder <- file.path(project_directory, project)
load(file.path(projectFolder, paste0(project, ".rda")))
# Read in input files (which have been updated by the user) -------------
initialPops <- read_csv(file.path(projectFolder,
"initial_populations.csv"))
constants <- read_csv(file.path(projectFolder,
"parameters_constants.csv"))
timeVarying <- read_csv(file.path(projectFolder,
"parameters_time_varying.csv"))
timeFactors <- read_csv(file.path(projectFolder,
"time_varying_ranges.csv"))
transitions <- read_csv(file.path(projectFolder,
"population_transitions.csv"))
waifw_matrix <- read_csv(file.path(projectFolder,
"population_waifw_matrix.csv"))
# Convert transistions and waifw_matrix into matrices
transitions <- as.matrix(transitions[, 2:(pg$npops + 1)])
rownames(transitions) <- colnames(transitions)
waifw_matrix <- as.matrix(waifw_matrix[, 2:(pg$npops + 1)])
rownames(waifw_matrix) <- colnames(waifw_matrix)
# Update best estimate parameter set ------------------------------------
bestEstimateYears <- timeVarying
# Reshape constants so they can be appended to timevarying
constantsSpread <- constants %>%
select(parameter, value)
parameters <- constantsSpread$parameter
constantsSpread <- as.data.frame(t(constantsSpread[,-1]))
colnames(constantsSpread) <- parameters
rownames(constantsSpread) <- NULL
# Append to time varyingCC
constantsDf <- constantsSpread[rep(1,nrow(timeVarying)),]
bestEstimateYears <- cbind(timeVarying, constantsDf)
# Expand to all points by linear extrapolation bewteen years
best_estimates <- as.data.frame(matrix(0, ncol = ncol(bestEstimateYears),
nrow = pg$npts))
colnames(best_estimates) <- colnames(bestEstimateYears)
for (var in colnames(bestEstimateYears)) {
tempValues <- bestEstimateYears[, var]
for (year in 1:(pg$nyears-1)) {
indices <- ((year-1)* 1/pg$timestep + 1): (year * 1/pg$timestep + 1)
yearValues <- seq(tempValues[year], tempValues[year+1],
length = (1 + 1/pg$timestep))
best_estimates[indices, var] <- yearValues
}
}
# Set up best estimate initial population
init_pop <- filter(initialPops, parameter == "init_pop")$value
initial_pop <- initialPops %>%
filter(parameter != "init_pop")
initialPopMat <- as.data.frame(matrix(initial_pop$value,
ncol = pg$nstates + 1,
nrow = pg$npops))
colnames(initialPopMat) <- c(pg$state_names, "pop_prop")
rownames(initialPopMat) <- pg$population_names
popProp <- init_pop * initialPopMat$pop_prop
best_initial_pop <- apply(initialPopMat[, 1:pg$nstates], 2,
function(x) x * popProp)
# Run model on updated best estimates -----------------------------------
results <- HBVmodel(pg, best_estimates, best_initial_pop, pg$pts,
transitions, waifw_matrix)
# Create calibration plot ----------------------------------------------
# Plot in a separate figure
windows(width = 35, height=30, xpos = 200) # Dimensions big enough to
# maximize
# Population sizes
totalPop <- popResults(pg, bestResults,
populations = "all", states = "all")
totalPopPlot <- indicatorPlot(totalPop, ylabel = "Population size",
range = FALSE,
xlimits = c(pg$start_year,
pg$end_year, 20)) +
ggtitle("Overall population")
# Plot of population size in every group
popSizesAge <- popResults(pg, bestResults,
states = "all") %>%
FactorPop(popLabels)
agePopPlot <- indicatorPlot(popSizesAge, ylabel = "Population size",
xlimits = c(pg$start_year,
pg$end_year, 20),
range = FALSE,
groupPlot = "population") +
ggtitle("Population by age")
# Prevalence
totalNumInfected <- popResults(pg, bestResults,
states = c("a", "ch"),
populations = "all")
ageNumInfected <- popResults(pg, bestResults,
states = c("a", "ch")) %>%
FactorPop(popLabels)
totalInfectedPlot <- indicatorPlot(totalNumInfected,
ylabel = "Total number infected",
range = FALSE,
xlimits = c(pg$start_year,
pg$end_year, 20)) +
ggtitle("Overall number infected")
popInfectedPlot <- indicatorPlot(ageNumInfected,
ylabel = "Number infected",
range = FALSE,
xlimits = c(pg$start_year,
pg$end_year, 20),
groupPlot = "population") +
ggtitle("Infected by population")
# Total prevalance
tempInfected <- popResults(pg, bestResults,
states = c("a", "ch"),
range = FALSE, populations = "all") %>%
select(-year)
tempTotal <- popResults(pg, bestResults,
populations = "all", states = "all",
range = FALSE) %>% select(-year)
tempPrev <- data.frame(year = totalPop$year,
best = (100 * tempInfected / tempTotal)) %>%
tbl_df()
totalPrevPlot <- indicatorPlot(tempPrev,
ylabel = "Overall prevalence (%)",
range = FALSE,
xlimits = c(pg$start_year,
pg$end_year, 20)) +
ggtitle("Overall prevalence")
# Pop prevalence
tempInfected <- popResults(pg, bestResults,
states = c("a", "ch"),
range = FALSE) %>% select(-year, -population)
tempTotal <- popResults(pg, bestResults,
states = "all",
range = FALSE) %>% select(-year, -population)
tempPrev <- data.frame(year = popSizesAge$year,
population = popSizesAge$population,
best = (100 * tempInfected / tempTotal)) %>%
tbl_df()
popPrevPlot <- indicatorPlot(tempPrev,
ylabel = "Population prevalence (%)",
xlimits = c(pg$start_year,
pg$end_year, 20),
range = FALSE,
groupPlot = "population") +
ggtitle("Prevalence by population")
# New infections
# Annual number of new infections
totalInfections <- indicatorResults(pg, bestResults,
"newInfections",
populations = "all",
annual = "sum")
popInfections <- indicatorResults(pg, bestResults,
"newInfections",
annual = "sum") %>%
FactorPop(popLabels)
totalNewInfectionsPlot <- indicatorPlot(totalInfections,
ylabel = "New infections",
range = FALSE,
xlimits = c(pg$start_year,
pg$end_year, 20)) +
ggtitle("Overall new infections")
popNewInfectionsPlot <- indicatorPlot(popInfections,
ylabel = "New infections",
xlimits = c(pg$start_year,
pg$end_year, 20),
range = FALSE,
groupPlot = "population") +
ggtitle("Overall new infections")
# Cumulative incidence plots
tempTotalInfections <- indicatorResults(pg, bestResults,
"newInfections",
populations = "all",
annual = "sum")
totalCumInfections <- tempTotalInfections %>%
mutate(best = cumsum(best))
totalCumInfectionsPlot <- indicatorPlot(totalCumInfections,
ylabel = "Number of infections",
range = FALSE,
xlimits = c(pg$start_year,
pg$end_year, 20)) +
ggtitle("Overall cumulative infections")
tempPopInfections <- indicatorResults(pg, bestResults,
"newInfections",
annual = "sum") %>%
FactorPop(popLabels)
popCumInfections <- tempPopInfections %>%
group_by(population) %>%
mutate(best = cumsum(best))
popCumInfectionsPlot <- indicatorPlot(popCumInfections,
ylabel = "Number of infections",
range = FALSE,
xlimits = c(pg$start_year,
pg$end_year, 20),
groupPlot = "population") +
ggtitle("Cumulative infections by population")
# Incidence
tempTotalInfections <- select(tempTotalInfections, -year)
tempPopInfections <- select(tempPopInfections, -year)
tempTotalPop <- popResults(pg, bestResults,
populations = "all", states = "all",
range = FALSE) %>% select(-year)
tempTotalPop <- tempTotalPop[MidyearIndex(nrow(tempTotalPop),
pg$timestep), ]
tempPopPop <- popResults(pg, bestResults, states = "all",
range = FALSE) %>%
select(-year) %>%
arrange(population) %>%
FactorPop(popLabels)
tempPopPop <- tempPopPop[MidyearIndex(nrow(tempPopPop),
pg$timestep), ]
savePops <- tempPopPop$population
tempPopPop <- select(tempPopPop, -population)
#Calculate incidence per 100,000
tempTotalInc <- data.frame(year = head(pg$years, -1),
best = (1e5 * tempTotalInfections /
tempTotalPop)) %>%
tbl_df()
totalInc <- tempTotalInc %>%
gather("sim", "incidence", 2:ncol(tempTotalInc)) %>%
group_by(year) %>%
summarise(min = min(incidence),
max = max(incidence)) %>%
ungroup() %>%
mutate(best = tempTotalInc$best) %>%
select(year, best, min, max)
totalIncidencePlot <- indicatorPlot(totalInc,
ylabel = "Incidence per 100,000",
range = FALSE,
xlimits = c(pg$start_year,
pg$end_year, 20)) +
ggtitle("Overall incidence")
tempPopInc <- data.frame(year = rep(head(pg$years, -1), pg$npops),
population = savePops,
best = (1e5 * select(tempPopInfections, -population) /
tempPopPop)) %>%
tbl_df()
popIncidencePlot <- indicatorPlot(tempPopInc,
ylabel = "Incidence per 100,000",
xlimits = c(pg$start_year,
pg$end_year, 20),
range = FALSE,
groupPlot = "population") +
ggtitle("Incidence by population")
# Treatment
totalTreatments <- indicatorResults(pg, bestResults,
"newTreatments",
populations = "all",
annual = "sum")
popTreatments <- indicatorResults(pg, bestResults,
"newTreatments",
annual = "sum")
totalTreatmentPlot <- indicatorPlot(totalTreatments,
ylabel = "Number initiated treatment",
range = FALSE,
xlimits = c(pg$start_year,
pg$end_year, 20)) +
ggtitle("Overall initiated treatment")
popTreatmentPlot <- indicatorPlot(popTreatments,
ylabel = "Number initiated treatment",
xlimits = c(pg$start_year,
pg$end_year, 20),
range = FALSE,
groupPlot = "population") +
scale_colour_brewer(name = "Age Group", palette = "Set1",
labels = c("Age < 4", "Age 5 to 14",
"Age 15 to 44", "Age > 45")) +
scale_fill_brewer(name = "Age Group", palette = "Set1",
labels = c("Age < 4", "Age 5 to 14",
"Age 15 to 44", "Age > 45")) +
ggtitle("Initiated treatment by population")
# HBV deaths
totalDeaths <- indicatorResults(pg, bestResults,
"newHBVdeaths",
populations = "all",
annual = "sum")
popDeaths <- indicatorResults(pg, bestResults,
"newHBVdeaths",
annual = "sum")
totalDeathsPlot <- indicatorPlot(totalDeaths,
ylabel = "Number of deaths",
range = FALSE,
xlimits = c(pg$start_year,
pg$end_year, 20)) +
ggtitle("Overall number of deaths")
popDeathsPlot <- indicatorPlot(popDeaths,
ylabel = "Number of deaths",
xlimits = c(pg$start_year,
pg$end_year, 20),
range = FALSE,
groupPlot = "population") +
scale_colour_brewer(name = "Age Group", palette = "Set1",
labels = c("Age < 4", "Age 5 to 14",
"Age 15 to 44", "Age > 45")) +
scale_fill_brewer(name = "Age Group", palette = "Set1",
labels = c("Age < 4", "Age 5 to 14",
"Age 15 to 44", "Age > 45")) +
ggtitle("Deaths by population")
# Put all plots in a grid
print(plot_grid(totalPopPlot,
agePopPlot,
totalPrevPlot,
popPrevPlot,
totalNewInfectionsPlot,
totalCumInfectionsPlot,
popNewInfectionsPlot,
popCumInfectionsPlot,
totalIncidencePlot,
popIncidencePlot,
totalTreatmentPlot,
popTreatmentPlot,
totalDeathsPlot,
popDeathsPlot,
ncol = 4, nrow = 4))
}