-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsummarise_model.R
80 lines (69 loc) · 1.95 KB
/
summarise_model.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
#' Summarise a Model Simulation
#'
#' @description Provides simple summary statistics for a model produced using
#' \code{\link[idmodelr]{solve_ode}}. These include the final population
#' sizes, the time and size of the maximum epidemic peak, and the duration of
#' the epidemic.
#' @param sim A tibble of model output as produced by
#' \code{\link[idmodelr]{solve_ode}}.
#'
#' @return A tibble of summary information for a model simulation.
#' @export
#' @importFrom dplyr filter pull arrange select bind_cols slice rename rename_at funs
#' @importFrom tibble tibble
#' @import magrittr
#' @examples
#'
#' ## Intialise
#'N = 100000
#'I_0 = 1
#'S_0 = N - I_0
#'R_0 = 1.1
#'beta = R_0
#'
#' ##Time for model to run over
#'tbegin = 0
#'tend = 50
#'times <- seq(tbegin, tend, 1)
#'
#' ##Vectorise input
#'parameters <- as.matrix(c(beta = beta))
#'inits <- as.matrix(c(S = S_0, I = I_0))
#'
#'sim <- solve_ode(model = SI_ode, inits, parameters, times, as.data.frame = TRUE)
#'
#'summarise_model(sim)
summarise_model <- function(sim) {
time <- NULL; . <- NULL;
epi_peak <- sim %>%
filter(I == max(I)) %>%
arrange(time) %>%
slice(1)
epi_peak_size <- epi_peak %>%
pull(I)
epi_peak_time <- epi_peak %>%
pull(time)
epi_dur <- sim %>%
filter(I < 1) %>%
arrange(I) %>%
slice(1) %>%
pull(time)
if (length(epi_dur) == 0) {
epi_dur <- Inf
}
sum_stat <- tibble(epi_peak_time = epi_peak_time,
epi_peak_size = round(epi_peak_size, digits = 0),
epi_dur = epi_dur)
sum_stat <- sim %>%
filter(time == max(time)) %>%
round(digits = 0) %>%
select(-time) %>%
bind_cols(sum_stat)
## Format output
sum_stat <- sum_stat %>%
rename_at(.vars = colnames(.)[!grepl("epi_", colnames(.))], .funs = funs(paste0("Final size: ", .))) %>%
rename(`Epidemic peak time` = epi_peak_time,
`Epidemic peak` = epi_peak_size,
`Epidemic duration` = epi_dur)
return(sum_stat)
}