epiverse-trace / epidemics

A library of published compartmental epidemic models, and classes to represent demographic structure, non-pharmaceutical interventions, and vaccination regimes, to compose epidemic scenarios.
https://epiverse-trace.github.io/epidemics/
Other
8 stars 2 forks source link

Pass parameter time-dependence functions #92

Closed pratikunterwegs closed 11 months ago

pratikunterwegs commented 1 year ago

This issue is related to #76 and is to request that users should be able to pass time-dependence functions to epidemic_*().

These functions should be allowed to take the arguments time and x for a single parameter value, such as the transmission rate $\beta$; a pseudo-code example is shown below.

epidemic_x(
  ...,
  time_dependence = list(
    beta = function(t, x, tmax = 365) x * sinpi(t / 365)
  )
)

A simple working example of the concept is below.

mod_beta = function(time, x, tmax = 20) {
  x * scales::rescale(1 + sinpi(time / tmax), to = c(0, 1), from = c(0, 2))
}

# Define the SIR model function
sir_model <- function(time, state, parameters) {
  beta_now = parameters$fn(time, parameters$beta)

  dS <- -beta_now * state["S"] * state["I"] / parameters$N
  dI <- beta_now * state["S"] * state["I"] / parameters$N - parameters$gamma * state["I"]
  dR <- parameters$gamma * state["I"]

  return(list(c(dS, dI, dR)))
}

# Set initial conditions and parameters
initial_state <- c(S = 0.99, I = 0.01, R = 0)
parameters <- list(beta = 0.3, gamma = 0.1, N = 1, fn = mod_beta)

# Set time points for simulation
times <- seq(0, 100, by = 1)

# Solve the ODE using the ode solver from deSolve
sir_solution <- deSolve::ode(y = initial_state, times = times, func = sir_model, parms = parameters)

# Convert the solution to a data frame for plotting
solution_df <- as.data.frame(sir_solution)

library(ggplot2)

ggplot(solution_df, aes(time, I)) +
  geom_point()