Skip to content

Commit

Permalink
starter model updates
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencarlislewalker committed Oct 11, 2024
1 parent b05ee99 commit c751202
Show file tree
Hide file tree
Showing 18 changed files with 1,904 additions and 2 deletions.
116 changes: 116 additions & 0 deletions inst/starter_models/awareness/README.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
collapse = TRUE,
warning = FALSE,
message = FALSE,
comment = "#>",
fig.path = "./figures/"
)


## ----packages, message=FALSE, warning=FALSE-----------------------------------
library(ggplot2)
library(dplyr)
library(tidyr)
library(macpan2)


## ----model_spec---------------------------------------------------------------
specs = mp_tmb_library(
"starter_models"
, "awareness"
, package = "macpan2"
, alternative_specs = TRUE
)
print(names(specs))


## ----diagram, echo = FALSE, fig.height = 2, fig.width = 5---------------------
system.file("utils", "box-drawing.R", package = "macpan2") |> source()
layout = mp_layout_paths(specs$awareness_model)
plot_flow_diagram(layout)


## ----diagram-delayed-death, echo = FALSE, fig.height = 2, fig.width = 5-------
system.file("utils", "box-drawing.R", package = "macpan2") |> source()
layout = mp_layout_paths(specs$delayed_death_awareness_model)
plot_flow_diagram(layout)


## ----echo = FALSE-------------------------------------------------------------
plot_traj = function(traj) {
p = (traj
|> mutate(matrix = factor(matrix, levels = outputs))
|> ggplot()
+ geom_line(aes(time, value))
+ facet_wrap(~matrix
, scales = "free_y"
, ncol = 1
, dir = "v"
)
+ scale_y_continuous(limits = c(0, NA), expand = c(0, NA))
+ scale_x_continuous(
minor_breaks = \(x) {
y = seq(from = x[1], to = x[2], by = 50L)
y[1:(length(y) - 1)]
}
, expand = c(0, 0)
)
+ ylab("")
+ xlab("")
+ theme_bw()
)
return(p)
}


## ----behavioural_cycles-------------------------------------------------------
set.seed(8L)
days = 800
outputs = c(
"infection", "death"
, "S", "importation"
)
sim = (specs$longer_memory_awareness_model
|> mp_euler()
|> mp_simulator(days, outputs)
)
traj = (sim
|> mp_trajectory(include_initial = TRUE)
)
plot_traj(traj)


## ----importation--------------------------------------------------------------
set.seed(8L)
days = 800
outputs = c(
"infection", "death"
, "S", "importation"
)
sim = (specs$importation_awareness_model
|> mp_euler()
|> mp_simulator(days, outputs)
)
traj = (sim
|> mp_trajectory(include_initial = TRUE)
)
plot_traj(traj)


## ----importation_and_process_error--------------------------------------------
set.seed(8L)
days = 800
outputs = c(
"infection", "death"
, "S", "importation"
)
sim = (specs$importation_awareness_model
|> mp_euler_multinomial()
|> mp_simulator(days, outputs)
)
traj = (sim
|> mp_trajectory(include_initial = TRUE)
)
plot_traj(traj)

58 changes: 58 additions & 0 deletions inst/starter_models/hiv/README.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "./figures/"
)


## ----packages, warning=FALSE, message=FALSE-----------------------------------
library(macpan2)
library(ggplot2)
library(dplyr)


## ----model_lib----------------------------------------------------------------
spec = mp_tmb_library(
"starter_models"
, "hiv"
, package = "macpan2"
)


## ----diagram, echo = FALSE, fig.height = 2.5, fig.width = 8.5-----------------
system.file("utils", "box-drawing.R", package = "macpan2") |> source()
layout = mp_layout_grid(spec
, east = "^(infection|progression|death)"
, south = "^(protection)"
, north = "^(unprotection)"
, loops = "^(unprotection)"
, x_gap = 0.3
, y_gap = 0.3
, north_south_sep = 0.15
)
(layout
|> plot_flow_diagram(show_flow_rates = TRUE)
|> draw_outflows(layout, show_labels = TRUE, lab = "rate")
|> draw_inflows(layout, show_labels = TRUE, lab = "rate")
)


## ----simulations--------------------------------------------------------------
outputs = c(sprintf("I%s", 1:4), sprintf("A%s", 1:4))
sim = (spec
|> mp_tmb_update(default = list(lambda0 = 0.36, n = 0.2))
|> mp_rk4()
|> mp_simulator(time_steps = 50L, outputs)
)
(sim
|> mp_trajectory()
|> mutate(matrix = sub("^A([1-4])$", "Infectious and treated, stage \\1", matrix))
|> mutate(matrix = sub("^I([1-4])$", "Infectious and untreated, stage \\1", matrix))
|> ggplot()
+ geom_line(aes(time, value))
+ facet_wrap(~ matrix, ncol = 2, scales = 'free', dir = "v")
+ scale_y_continuous(limits = c(0, NA), expand = c(0, 0))
+ theme_bw()
)

95 changes: 95 additions & 0 deletions inst/starter_models/lotka_volterra_competition/README.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
collapse = TRUE,
warning = FALSE,
message = FALSE,
comment = "#>",
fig.path = "./figures/"
)
system.file("utils", "round-coef.R", package = "macpan2") |> source()


## ----packages, message=FALSE, warning=FALSE-----------------------------------
library(ggplot2)
library(dplyr)
library(tidyr)
library(macpan2)


## ----options------------------------------------------------------------------
options(macpan2_verbose = FALSE)


## ----model_spec---------------------------------------------------------------
spec = mp_tmb_library(
"starter_models"
, "lotka_volterra_competition"
, package = "macpan2"
)
print(spec)


## ----fake_data----------------------------------------------------------------
set.seed(1L)
# set number of time steps in simulation
time_steps = 100L
# ayx value to simulate data with (species X has a carrying capacity of 200)
true = list(ayx = 0.8/200)

# simulator object
fake_data = (spec
|> mp_tmb_insert(
phase = "during"
, at = Inf
, expressions = list(X_noisy ~ rpois(X), Y_noisy ~ rpois(Y))
, default = true
)
|> mp_simulator(
time_steps = time_steps
, outputs = c("X_noisy","Y_noisy")
)
|> mp_trajectory()
|> mutate(matrix = substr(matrix, 1L, 1L))
)

(fake_data
|> ggplot()
+ geom_line(aes(time, value, colour = matrix))
+ theme_bw()
)


## -----------------------------------------------------------------------------
cal = (spec
|> mp_tmb_calibrator(
data = fake_data
, traj = c("X", "Y")
, par = c("ayx","rx", "ry")
)
)
mp_optimize(cal)


## ----coef---------------------------------------------------------------------
mp_tmb_coef(cal, conf.int = TRUE) |> round_coef_tab()
print(true)


## ----traj_fit-----------------------------------------------------------------
comparison_data = list(
obs = fake_data
, fit = mp_trajectory_sd(cal, conf.int = TRUE)
) |> bind_rows(.id = "type")
(comparison_data
|> ggplot()
+ geom_line(aes(time, value, colour = type))
+ facet_wrap(~matrix)
+ geom_ribbon(aes(time, ymin = conf.low, ymax = conf.high)
, colour = "red"
, fill = "red"
, alpha = 0.5
, filter(comparison_data, type == "fit")
)
+ theme_bw()
)

110 changes: 110 additions & 0 deletions inst/starter_models/lotka_volterra_predator_prey/README.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
collapse = TRUE,
warning = FALSE,
message = FALSE,
comment = "#>",
fig.path = "./figures/"
)
system.file("utils", "round-coef.R", package = "macpan2") |> source()


## ----packages, message=FALSE, warning=FALSE-----------------------------------
library(ggplot2)
library(dplyr)
library(tidyr)
library(macpan2)


## ----options------------------------------------------------------------------
options(macpan2_verbose = FALSE)


## ----model_spec---------------------------------------------------------------
specs = mp_tmb_library(
"starter_models"
, "lotka_volterra_predator_prey"
, package = "macpan2"
, alternative_specs = TRUE
)


## ----simple_spec--------------------------------------------------------------
specs$holling_1


## ----logistic_spec------------------------------------------------------------
specs$holling_2


## ----func_resp_spec-----------------------------------------------------------
specs$holling_3


## ----fake_data----------------------------------------------------------------
spec = specs$holling_1
set.seed(1L)
# set number of time steps in simulation
time_steps = 100L
# delta value to simulate data with
true = list(delta = 2.5/10)

# simulator object
fake_data = (spec
|> mp_tmb_insert(
phase = "during"
, at = Inf
, expressions = list(X_noisy ~ rpois(X), Y_noisy ~ rpois(Y))
, default = true
)
|> mp_simulator(
time_steps = time_steps
, outputs = c("X_noisy","Y_noisy")
)
|> mp_trajectory()
|> mutate(matrix = substr(matrix, 1L, 1L))
)

(fake_data
|> mutate(species = ifelse(matrix == "X", "prey", "predator"))
|> ggplot()
+ geom_line(aes(time, value, colour = species))
+ theme_bw()
)


## -----------------------------------------------------------------------------
cal = (spec
|> mp_tmb_calibrator(
data = fake_data
, traj = c("X", "Y")
, par = "delta"
)
)
mp_optimize(cal)


## ----coef---------------------------------------------------------------------
coef = mp_tmb_coef(cal) |> round_coef_tab()
coef$true = true[coef$mat]
print(coef)


## ----traj_fit-----------------------------------------------------------------
comparison_data = list(
obs = fake_data
, fit = mp_trajectory_sd(cal, conf.int = TRUE)
) |> bind_rows(.id = "type") |> mutate(species = ifelse(matrix == "X", "prey", "predator"))
(comparison_data
|> ggplot()
+ geom_line(aes(time, value, colour = type))
+ facet_wrap(~species)
+ geom_ribbon(aes(time, ymin = conf.low, ymax = conf.high)
, colour = "red"
, fill = "red"
, alpha = 0.5
, filter(comparison_data, type == "fit")
)
+ theme_bw()
)

Loading

0 comments on commit c751202

Please sign in to comment.