-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpev.R
306 lines (289 loc) · 7.86 KB
/
pev.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
#' @title EPI PEV vaccination process
#'
#' @description schedules individuals to be vaccinated according to the epi
#' strategy
#'
#' @param variables list of variables in the model
#' @param events a list of events in the model
#' @param parameters the model parameters
#' @param correlations correlation parameters
#' @noRd
create_epi_pev_process <- function(
variables,
events,
parameters,
correlations,
coverages,
timesteps
) {
function(timestep) {
timestep_index <- match_timestep(ts = timesteps, t = timestep)
if(timestep_index == 0){
return()
}
coverage <- coverages[timestep_index]
if(coverage == 0){
return()
}
to_vaccinate <- variables$birth$get_index_of(
set = timestep - parameters$pev_epi_age
)
if (parameters$pev_epi_min_wait == 0) {
target <- to_vaccinate$to_vector()
} else {
not_recently_vaccinated <- variables$pev_timestep$get_index_of(
a = max(timestep - parameters$pev_epi_min_wait, 0),
b = timestep
)$not(TRUE)
target <- to_vaccinate$and(not_recently_vaccinated)$to_vector()
}
target <- target[
sample_intervention(
target,
'pev',
coverage,
correlations
)
]
schedule_vaccination(
target,
events,
parameters,
events$pev_epi_doses
)
}
}
#' @title mass PEV listener
#'
#' @description schedules individuals to be vaccinated according to the mass
#' strategy
#'
#' @param variables list of variables in the model
#' @param events a list of events in the model
#' @param parameters the model parameters
#' @param correlations correlation parameters
#' @noRd
create_mass_pev_listener <- function(
variables,
events,
parameters,
correlations
) {
function(timestep) {
in_age_group <- individual::Bitset$new(parameters$human_population)
for (i in seq_along(parameters$mass_pev_min_ages)) {
min_birth <- timestep - parameters$mass_pev_max_ages[[i]]
max_birth <- timestep - parameters$mass_pev_min_ages[[i]]
in_age_group$or(variables$birth$get_index_of(a = min_birth, b = max_birth))
}
if (parameters$mass_pev_min_wait == 0) {
target <- in_age_group$to_vector()
} else {
not_recently_vaccinated <- variables$pev_timestep$get_index_of(
a = max(timestep - parameters$mass_pev_min_wait, 0),
b = timestep
)$not(TRUE)
target <- in_age_group$and(not_recently_vaccinated)$to_vector()
}
time_index = which(parameters$mass_pev_timesteps == timestep)
target <- target[
sample_intervention(
target,
'pev',
parameters$mass_pev_coverages[[time_index]],
correlations
)
]
schedule_vaccination(
target,
events,
parameters,
events$mass_pev_doses
)
if (time_index < length(parameters$mass_pev_timesteps)) {
events$mass_pev$schedule(
parameters$mass_pev_timesteps[[time_index + 1]] - timestep
)
}
}
}
#' @title Schedule vaccination doses and efficacy
#'
#' @param target vector of individuals to target
#' @param variables list of variables in the model
#' @param events a list of events in the model
#' @param parameters the model parameters
#' @param dose_events a list of dose events to schedule
#' @noRd
schedule_vaccination <- function(
target,
events,
parameters,
dose_events
) {
if (length(target) > 0) {
for (d in seq_along(parameters$pev_doses)) {
dose_events[[d]]$schedule(target, parameters$pev_doses[[d]])
}
}
}
#' @title pev efficacy listener
#'
#' @description creates a listener to start pev efficacy in individuals
#'
#' @param variables list of variables in the model
#' @param pev_profile_index the index of the pev profile to introduce
#' @param parameters the model parameters
#' @noRd
create_pev_efficacy_listener <- function(variables, pev_profile_index) {
function(timestep, target) {
if (target$size() > 0) {
variables$pev_timestep$queue_update(timestep, target)
variables$pev_profile$queue_update(pev_profile_index, target)
}
}
}
create_pev_booster_listener <- function(
variables,
coverage,
booster_number,
pev_profile_index,
next_booster_event,
next_booster_delay,
renderer,
strategy
) {
render_name <- paste0("n_pev_", strategy, "_booster_", booster_number)
renderer$set_default(render_name, 0)
force(next_booster_event) # because R lazy evaluation is rubbish
force(next_booster_delay)
force(coverage)
function(timestep, target) {
target <- sample_bitset(target, coverage)
variables$pev_timestep$queue_update(timestep, target)
variables$pev_profile$queue_update(pev_profile_index, target)
renderer$render(render_name, target$size(), timestep)
if (!is.null(next_booster_event)) {
next_booster_event$schedule(target, next_booster_delay)
}
}
}
calculate_pev_antibodies <- function(
t,
cs,
rho,
ds,
dl,
parameters
) {
cs * (
rho * exp(-t * log(2) / ds) + (
1 - rho
) * exp(-t * log(2) / dl)
)
}
calculate_pev_efficacy <- function(antibodies, vmax, beta, alpha) {
vmax * (
1 - (1 / (
1 + (antibodies / beta) ** alpha
))
)
}
create_dosage_renderer <- function(renderer, strategy, dose) {
output_name <- paste0('n_pev_', strategy ,'_dose_', dose)
renderer$set_default(output_name, 0)
function(t, target) renderer$render(output_name, target$size(), t)
}
attach_pev_dose_listeners <- function(
variables,
parameters,
dose_events,
booster_events,
booster_delays,
booster_coverages,
pev_profile_indices,
strategy,
renderer
) {
# set up dosing
for (d in seq_along(dose_events)) {
dose_events[[d]]$add_listener(
create_dosage_renderer(renderer, strategy, d)
)
if (d == length(dose_events)) {
dose_events[[d]]$add_listener(
create_pev_efficacy_listener(
variables,
pev_profile_indices[[1]]
)
)
if (length(booster_events) > 0) {
seasonal_boosters <- FALSE
if (!is.null(parameters$pev_epi_seasonal_boosters)) {
seasonal_boosters <- parameters$pev_epi_seasonal_boosters
}
if (seasonal_boosters) {
dose_events[[d]]$add_listener(
create_seasonal_booster_scheduler(
booster_events[[1]],
booster_delays[[1]],
parameters
)
)
} else {
dose_events[[d]]$add_listener(
individual::reschedule_listener(
booster_events[[1]],
booster_delays[[1]]
)
)
}
}
}
}
# set up boosters
for (b in seq_along(booster_events)) {
if (b == length(booster_events)) {
next_booster_event <- NULL
next_booster_delay <- NULL
} else {
next_booster_event <- booster_events[[b + 1]]
next_booster_delay <- diff(
booster_delays[c(b, b + 1)]
)
}
booster_events[[b]]$add_listener(
create_pev_booster_listener(
variables = variables,
coverage = booster_coverages[[b]],
booster_number = b,
pev_profile_index = pev_profile_indices[[b + 1]],
next_booster_event = next_booster_event,
next_booster_delay = next_booster_delay,
renderer = renderer,
strategy = strategy
)
)
}
}
create_seasonal_booster_scheduler <- function(
booster_event,
booster_delay,
parameters
) {
function(timestep, target) {
delay <- booster_delay - timestep %% 365
if (delay < 0) {
delay <- delay + 365
}
if (delay <= parameters$pev_epi_min_wait) {
delay <- delay + 365
}
booster_event$schedule(target, delay)
}
}
sample_pev_param <- function(profile_index, profile_list, param_name) {
mu <- vnapply(profile_list, function(p) p[[param_name]][[1]])
sigma <- vnapply(profile_list, function(p) p[[param_name]][[2]])
rnorm(length(profile_index), mu[profile_index], sigma[profile_index])
}