-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplot_model.R
125 lines (111 loc) · 3.44 KB
/
plot_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
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
#' Plot Compartment Populations over Time for a Model Simulation
#'
#' @description Make seperate plots for each model compartment. Assumes model output is structured
#' as that produced from \code{\link[idmodelr]{solve_ode}}.
#' @param sim A tibble of model output as formated by \code{\link[idmodelr]{solve_ode}}
#' @param facet Logical, defaults to \code{TRUE}. If \code{FALSE} then the plot will not be facetted
#' otherwise it will be.
#' @param prev_sim A second tibble of model output formated as for \code{sim}. Used to compare to model runs.
#' @param model_labels A character vector of model names, defaults to \code{c("Current", "Previous")}.
#' @return A Plot of each model compartments population over time.
#' @import ggplot2
#' @import viridis
#' @importFrom tidyr gather
#' @importFrom dplyr mutate bind_rows
#' @export
#'
#' @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)
#'
#'plot_model(sim, facet = FALSE)
#'
#'plot_model(sim, facet = TRUE)
#'
#'## Compare with an updated model run
#'
#'#'## Intialise
#'R_0 = 1.3
#'beta = R_0
#'parameters <- as.matrix(c(beta = beta))
#'
#'new_sim <- solve_ode(model = SI_ode, inits, parameters, times, as.data.frame = TRUE)
#'
#'
#'plot_model(new_sim,sim, facet = FALSE)
#'
#'plot_model(new_sim, sim, facet = TRUE)
plot_model <- function(sim, prev_sim = NULL, model_labels = NULL,
facet = TRUE) {
time <- NULL; Compartment <- NULL; Model <- NULL; Population <- NULL;
## Define default lables for multiple models
if (is.null(model_labels)) {
model_labels <- c("Current", "Previous")
}
gather_columns_for_plot <- function(sim){
order <- colnames(sim)[-1]
tidy_sim <- sim %>%
gather(key = "Compartment", value = "Population", -time) %>%
mutate(Compartment = factor(Compartment, levels = order))
return(tidy_sim)
}
tidy_sim <- gather_columns_for_plot(sim)
## Add in previous model simulation if present
if (!is.null(prev_sim)) {
if ("data.frame" %in% class(prev_sim)) {
prev_sim <- gather_columns_for_plot(prev_sim)
tidy_sim <- tidy_sim %>%
mutate(Model = model_labels[1]) %>%
bind_rows(prev_sim %>%
mutate(Model = model_labels[2])) %>%
mutate(Model = factor(Model, levels = model_labels))
}else{
stop("prev_sim must be a model simulation dataframe or not be specified.")
}
}
if (!is.null(prev_sim)) {
plot <- ggplot(tidy_sim, aes(x = time, y = Population, col = Compartment, linetype = Model))
}else{
plot <- ggplot(tidy_sim, aes(x = time, y = Population, col = Compartment))
}
plot <- plot +
geom_line() +
theme_minimal() +
labs(x = "Year") +
scale_color_viridis(discrete = TRUE, end = 0.9)
if (facet) {
plot <- plot +
facet_wrap(~Compartment)
if (!is.null(prev_sim)) {
plot <- plot +
theme(legend.position = "bottom") +
guides(col = FALSE)
}else{
plot <- plot +
theme(legend.position = "none")
}
}
## Add facetting for previous simulation
if (!facet && !is.null(prev_sim)) {
plot <- plot +
theme(legend.position = "bottom") +
facet_wrap(~Model)
}
return(plot)
}