##
## A Simple Vaccine Intervention
##
## SISMID: Network Modeling for Epidemics
## Source page: mod4-Vax.qmd
##
## Run top to bottom.
##

# EpiModel provides the network estimation, simulation, and plotting tools
# used throughout this script.
library(EpiModel)

# Set tighter plot margins so the epidemic plots display cleanly.
par(mar = c(3, 3, 1, 1), mgp = c(2, 1, 0))

# The EpiModel built-in network models include parameters for a simple
# intervention that lowers the probability of infection given contact between
# a susceptible and an infected person. This can stand in for a vaccine that
# reduces susceptibility or a condom provision program. This simple built-in
# approach has three features:
#
#   1. The intervention applies to everyone in the population. There is no
#      heterogeneity in who receives it.
#   2. It has an efficacy, inter.eff, that gives the relative reduction in the
#      infection probability set by inf.prob.
#   3. It starts at a defined time step, inter.start.
#
# Research-level models implement more complex interventions with more
# parameters or greater demographic structure (covered in NME-II).


# ---- Network Model Estimation ----

# We start with a very simple temporal ERGM. The network has 100 nodes, the
# formation model has only an edges term, and the target of 50 edges sets the
# mean degree to 1. Partnerships dissolve at a rate consistent with a mean
# duration of 20 time steps.
nw <- network_initialize(n = 100)
formation <- ~edges
target.stats <- 50
coef.diss <- dissolution_coefs(dissolution = ~offset(edges), duration = 20)
est <- netest(nw, formation, target.stats, coef.diss, verbose = FALSE)


# ---- SI Model with a Strong Intervention ----

# Simulate an SI disease in a closed population in which the intervention
# (for example, a vaccine) has very strong efficacy for the entire population.
# inter.eff = 0.96 reduces the per-contact infection probability by 96%, and
# inter.start = 25 turns the intervention on at week 25.
param <- param.net(inf.prob = 0.5, inter.eff = 0.96, inter.start = 25)

# Seed the epidemic with 5 infected nodes.
init <- init.net(i.num = 5)

# Run 10 stochastic simulations over 100 time steps. ncores spreads the
# simulations across CPU cores to run them in parallel; lower it (or drop it)
# if your machine has fewer cores available.
control <- control.net(type = "SI", nsteps = 100, nsims = 10, ncores = 5)
sim <- netsim(est, param, init, control)

# Plot prevalence over time. The intervention at week 25 sharply slows the
# rise in infections.
plot(sim)


# ---- SIS Model with a Moderate Intervention ----

# Simulate an SIS disease in which the intervention is somewhat less effective,
# closer to the functional effectiveness of condoms. Here inter.eff = 0.8 and
# the intervention starts later, at week 100. rec.rate adds recovery back to
# the susceptible state, which the SIS model requires.
param <- param.net(inf.prob = 0.5, inter.eff = 0.8, inter.start = 100,
                   rec.rate = 0.07)

# Seed the epidemic with 10 infected nodes.
init <- init.net(i.num = 10)

# Run 10 simulations over 250 time steps so the longer-acting intervention has
# time to take effect. See the note on ncores above.
control <- control.net(type = "SIS", nsteps = 250, nsims = 10, ncores = 5)
sim <- netsim(est, param, init, control)

# Plot prevalence over time. The intervention at week 100 reduces but does not
# eliminate ongoing transmission.
plot(sim)
