-
Notifications
You must be signed in to change notification settings - Fork 3
/
tool.R
479 lines (397 loc) · 12 KB
/
tool.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#' Tools in epiworld
#'
#' Tools are functions that affect how agents react to the virus. They can be
#' used to simulate the effects of vaccination, isolation, and social
#' distancing.
#'
#' @param model Model
#' @param name Name of the tool
#' @param susceptibility_reduction Numeric. Proportion it reduces susceptibility.
#' @param transmission_reduction Numeric. Proportion it reduces transmission.
#' @param recovery_enhancer Numeric. Proportion it improves recovery.
#' @param death_reduction Numeric. Proportion it reduces probability of death.e
#' @param tool_pos Positive integer. Index of the tool's position in the model.
#' @examples
#' # Simple model
#' model_sirconn <- ModelSIRCONN(
#' name = "COVID-19",
#' n = 10000,
#' prevalence = 0.01,
#' contact_rate = 5,
#' transmission_rate = 0.4,
#' recovery_rate = 0.95
#' )
#'
#' # Running and printing
#' run(model_sirconn, ndays = 100, seed = 1912)
#' plot(model_sirconn)
#'
#' epitool <- tool(
#' name = "Vaccine",
#' susceptibility_reduction = .9,
#' transmission_reduction = .5,
#' recovery_enhancer = .5,
#' death_reduction = .9
#' )
#'
#' epitool
#'
#' set_name_tool(epitool, 'Pfizer') # Assigning name to the tool
#' get_name_tool(epitool) # Returning the name of the tool
#' add_tool(model_sirconn, epitool, .5)
#' run(model_sirconn, ndays = 100, seed = 1912)
#' model_sirconn
#' plot(model_sirconn)
#'
#' # To declare a certain number of individuals with the tool
#' rm_tool(model_sirconn, 0) # Removing epitool from the model
#' add_tool_n(model_sirconn, epitool, 5500)
#' run(model_sirconn, ndays = 100, seed = 1912)
#'
#' # Adjusting probabilities due to tool
#' set_susceptibility_reduction(epitool, 0.1) # Susceptibility reduction
#' set_transmission_reduction(epitool, 0.2) # Transmission reduction
#' set_recovery_enhancer(epitool, 0.15) # Probability increase of recovery
#' set_death_reduction(epitool, 0.05) # Probability reduction of death
#' run(model_sirconn, ndays = 100, seed = 1912) # Run model to view changes
#'
#' @export
#' @returns
#' - The `tool` function creates a tool of class [epiworld_tool].
#' @aliases epiworld_tool
tool <- function(
name,
susceptibility_reduction,
transmission_reduction,
recovery_enhancer,
death_reduction
) {
structure(
tool_cpp(
name,
susceptibility_reduction,
transmission_reduction,
recovery_enhancer,
death_reduction
),
class = "epiworld_tool"
)
}
#' @export
print.epiworld_tool <- function(x, ...) {
invisible(print_tool_cpp(x))
}
stopifnot_tool <- function(tool) {
if (!inherits(tool, "epiworld_tool")) {
stop(
"The -tool- object must be of class \"epiworld_tool\". ",
"The object passed to the function is of class(es): ",
paste(class(tool), collapse = ", ")
)
}
}
stopifnot_tfun <- function(tfun) {
if (!inherits(tfun, "epiworld_tool_fun")) {
stop(
"The -tfun- object must be of class \"epiworld_tool_fun\". ",
"The object passed to the function is of class(es): ",
paste(class(tfun), collapse = ", ")
)
}
}
#' @export
#' @details
#' The name of the `epiworld_tool` object can be manipulated with the functions
#' [set_name_tool()] and [get_name_tool()].
#' @returns
#' - The `set_name_tool` function assigns a name to the tool of class
#' [epiworld_tool] and returns the tool.
#' @rdname tool
set_name_tool <- function(tool, name) {
stopifnot_tool(tool)
invisible(set_name_tool_cpp(tool, name))
}
#' @returns
#' - The `get_name_tool` function returns the name of the tool of class
#' [epiworld_tool].
#' @rdname tool
#' @export
get_name_tool <- function(tool) {
stopifnot_tool(tool)
get_name_tool_cpp(tool)
}
#' @export
#' @param tool An object of class `epiworld_tool`
#' @param proportion In the case of `add_tool`, a proportion, otherwise, an integer.
#' @details
#' The `add_tool` function adds the specified tool to the model of class
#' [epiworld_model] with specified proportion.
#' @rdname tool
add_tool <- function(model, tool, proportion) UseMethod("add_tool")
#' @export
add_tool.epiworld_model <- function(model, tool, proportion) {
stopifnot_tool(tool)
add_tool_cpp(model, tool, as.double(proportion))
invisible(model)
}
#' @export
#' @rdname tool
#' @returns
#' - The `add_tool_n` function adds the specified tool to the model of class
#' [epiworld_model] with specified count n.
#' @param n A positive integer. Number of agents to initially have the tool.
add_tool_n <- function(model, tool, n) UseMethod("add_tool_n")
#' @export
add_tool_n.epiworld_model <- function(model, tool, n) {
stopifnot_tool(tool)
add_tool_n_cpp(model, tool, as.integer(n))
invisible(model)
}
#' @export
#' @returns
#' - The `rm_tool` function removes the specified tool from a model.
#' @rdname tool
rm_tool <- function(model, tool_pos) {
stopifnot_model(model)
invisible(rm_tool_cpp(model, as.integer(tool_pos)))
}
# Tool functions ---------------------------------------------------------------
#' @export
#' @param vars Integer vector. Indices (starting from 0) of the positions of the
#' variables used to compute the logit probability.
#' @param coefs Numeric vector. Of the same length of `vars`, is a vector of
#' coefficients associated to the logit probability.
#' @rdname tool
#' @examples
#'
#' # Using the logit function --------------
#' sir <- ModelSIR(
#' name = "COVID-19", prevalence = 0.01,
#' transmission_rate = 0.9, recovery_rate = 0.1
#' )
#'
#' # Adding a small world population
#' agents_smallworld(
#' sir,
#' n = 10000,
#' k = 5,
#' d = FALSE,
#' p = .01
#' )
#'
#' # Creating a tool
#' mask_wearing <- tool(
#' name = "Mask",
#' susceptibility_reduction = 0.0,
#' transmission_reduction = 0.3, # Only transmission
#' recovery_enhancer = 0.0,
#' death_reduction = 0.0
#' )
#'
#' add_tool(sir, mask_wearing, .5)
#'
#' run(sir, ndays = 50, seed = 11)
#' hist_0 <- get_hist_total(sir)
#'
#' # And adding features
#' dat <- cbind(
#' female = sample.int(2, 10000, replace = TRUE) - 1,
#' x = rnorm(10000)
#' )
#'
#' set_agents_data(sir, dat)
#'
#' # Creating the logit function
#' tfun <- tool_fun_logit(
#' vars = c(0L, 1L),
#' coefs = c(-1, 1),
#' model = sir
#' )
#'
#' # The infection prob is lower
#' hist(plogis(dat %*% rbind(.5,1)))
#'
#' tfun # printing
#'
#'
#' set_susceptibility_reduction_fun(
#' tool = get_tool(sir, 0),
#' model = sir,
#' tfun = tfun
#' )
#'
#' run(sir, ndays = 50, seed = 11)
#' hist_1 <- get_hist_total(sir)
#'
#' op <- par(mfrow = c(1, 2))
#' plot(hist_0); abline(v = 30)
#' plot(hist_1); abline(v = 30)
#' par(op)
#'
#'
tool_fun_logit <- function(vars, coefs, model) {
stopifnot_model(model)
structure(
tool_fun_logit_cpp(as.integer(vars), as.double(coefs), model),
class = "epiworld_tool_fun",
builder = "tool_fun_logit",
vars = vars,
coefs = coefs,
model = model
)
}
#' @export
print.epiworld_tool_fun <- function(x, ...) {
cat("An epiworld_tool_function object.\n")
cat("(model: ", get_name(attr(x, "model")), ")\n", sep = "")
cat("This function was built using -tool_fun_logit()-. and it features ")
cat("the following coefficients:\n")
cat(
paste(sprintf(
" % 2i: %5.2f",
attr(x, "vars"),
attr(x, "coefs")
), collapse = "\n"
), "\n"
)
invisible(x)
}
# Susceptibility reduction -----------------------------------------------------
#' @export
#' @param prob Numeric scalar. A probability (between zero and one).
#' @returns
#' - The `set_susceptibility_reduction` function assigns a probability reduction
#' to the specified tool of class [epiworld_tool].
#' @rdname tool
set_susceptibility_reduction <- function(tool, prob) {
stopifnot_tool(tool)
set_susceptibility_reduction_cpp(tool, as.double(prob))
}
#' @export
#' @param param Character scalar. Name of the parameter featured in `model` that
#' will be added to the tool (see details).
#' @details
#' In the case of `set_susceptibility_reduction_ptr`, `set_transmission_reduction_ptr`,
#' `set_recovery_enhancer`, and
#' `set_death_reduction_ptr`, the corresponding parameters are passed as a pointer to
#' the tool. The implication of using pointers is that the values will be
#' read directly from the `model` object, so changes will be reflected.
#'
#' @rdname tool
set_susceptibility_reduction_ptr <- function(tool, model, param) {
stopifnot_tool(tool)
stopifnot_model(model)
invisible(set_susceptibility_reduction_ptr_cpp(tool, model, param))
}
#' @export
#' @param tfun An object of class `epiworld_tool_fun`.
#' @rdname tool
set_susceptibility_reduction_fun <- function(tool, model, tfun) {
stopifnot_tool(tool)
stopifnot_model(model)
stopifnot_tfun(tfun)
invisible(set_susceptibility_reduction_fun_cpp(tool, model, tfun))
}
# Transmission reduction -------------------------------------------------------
#' @export
#' @returns
#' - The `set_transmission_reduction` function assigns a probability reduction
#' to the specified tool of class [epiworld_tool].
#' @rdname tool
set_transmission_reduction <- function(tool, prob) {
stopifnot_tool(tool)
set_transmission_reduction_cpp(tool, as.double(prob))
}
#' @export
#' @rdname tool
set_transmission_reduction_ptr <- function(tool, model, param) {
stopifnot_tool(tool)
stopifnot_model(model)
set_transmission_reduction_ptr_cpp(tool, model, param)
}
#' @export
#' @rdname tool
set_transmission_reduction_fun <- function(tool, model, tfun) {
stopifnot_tool(tool)
stopifnot_model(model)
stopifnot_tfun(tfun)
set_transmission_reduction_fun_cpp(tool, model, tfun)
}
# Recovery enhancer ------------------------------------------------------------
#' @export
#' @returns
#' - The `set_recovery_enhancer` function assigns a probability increase
#' to the specified tool of class [epiworld_tool].
#' @rdname tool
set_recovery_enhancer <- function(tool, prob) {
stopifnot_tool(tool)
set_recovery_enhancer_cpp(tool, as.double(prob))
}
#' @export
#' @rdname tool
set_recovery_enhancer_ptr <- function(tool, model, param) {
stopifnot_tool(tool)
stopifnot_model(model)
set_recovery_enhancer_ptr_cpp(tool, model, param)
}
#' @export
#' @rdname tool
set_recovery_enhancer_fun <- function(tool, model, tfun) {
stopifnot_tool(tool)
stopifnot_model(model)
stopifnot_tfun(tfun)
set_recovery_enhancer_fun_cpp(tool, model, tfun)
}
# Death reduction --------------------------------------------------------------
#' @export
#' @returns
#' - The `set_death_reduction` function assigns a probability decrease
#' to the specified tool of class [epiworld_tool].
#' @rdname tool
set_death_reduction <- function(tool, prob) {
stopifnot_tool(tool)
set_death_reduction_cpp(tool, as.double(prob))
}
#' @export
#' @rdname tool
set_death_reduction_ptr <- function(tool, model, param) {
stopifnot_tool(tool)
stopifnot_model(model)
set_death_reduction_ptr_cpp(tool, model, param)
}
#' @export
#' @rdname tool
set_death_reduction_fun <- function(tool, model, tfun) {
stopifnot_tool(tool)
stopifnot_model(model)
stopifnot_tfun(tfun)
set_death_reduction_fun_cpp(tool, model, tfun)
}
#' @export
#' @rdname agents_smallworld
#' @returns
#' - `get_agents_tools` returns a list of class `epiworld_agents_tools`
#' with `epiworld_tools` (list of lists).
get_agents_tools <- function(model) {
stopifnot_model(model)
res <- lapply(
get_agents_tools_cpp(model),
`class<-`,
"epiworld_tools"
)
structure(res, class = c("epiworld_agents_tools", class(res)))
}
#' @export
#' @rdname tool
#' @param max_print Numeric scalar. Maximum number of tools to print.
#' @param ... Currently ignored.
#' @param x An object of class `epiworld_agents_tools`.
print.epiworld_agents_tools <- function(x, max_print = 10, ...) {
for (i in 1:min(max_print, length(x))) {
print_agent_tools_cpp(x[[i]])
}
if (length(x) > max_print) {
cat(sprintf("Showing first %s of %s tools.\n", max_print, length(x)))
}
invisible(x)
}