## For reproducibility, specify the number of threads.
set_num_threads(1)
## Create an 'SIR' model with 6 nodes
u0 <- data.frame(
S = 100:105,
I = 1:6,
R = rep(0, 6)
)
model <- SIR(
u0 = u0,
tspan = 1:10,
beta = 0.16,
gamma = 0.077
)
## Run the model with default recording (all data).
## For reproducibility, specify the seed.
result_full <- run(model, seed = 22)
head(trajectory(result_full))
## Define a template to record only nodes 2 and 4 at times 3 and 5
## for all compartments.
df <- data.frame(
time = c(3, 5, 3, 5),
node = c(2, 2, 4, 4),
S = TRUE, I = TRUE, R = TRUE
)
punchcard(model) <- df
result_sparse <- run(model, seed = 22)
trajectory(result_sparse)
## Record only specific compartments (e.g., S and R, but not I)
df <- data.frame(
time = c(3, 5, 3, 5),
node = c(2, 2, 4, 4),
S = TRUE, I = FALSE, R = TRUE
)
punchcard(model) <- df
result_partial <- run(model, seed = 22)
trajectory(result_partial)
## Shortcut: If only 'time' and 'node' are specified, all
## compartments are recorded.
df <- data.frame(
time = c(3, 5, 3, 5),
node = c(2, 2, 4, 4)
)
punchcard(model) <- df
## Reset to record all data (equivalent to no template)
punchcard(model) <- NULL
result_reset <- run(model, seed = 22)
head(trajectory(result_reset))
Run the code above in your browser using DataLab