auxiliary/modelfiles/DSAIDE/simulators/simulate_Complex_ID_Control_stochastic.R

#' Complex ID Control
#' 
#' @description A compartmental model with several different compartments:  Susceptibles (S), Infected and Pre-symptomatic (P), Infected and Asymptomatic (A), Infected and Symptomatic (I), Recovered and Immune (R) and Dead (D). Also modeled is an environmental pathogen stage (E), and susceptible (Sv) and infected (Iv) vectors.
#' 
#' @details Susceptible individuals (S) can become infected by pre-symptomatic (P), asymptomatic (A) or symptomatic (I) hosts. The rates at which infections from the different types of infected individuals (P, A and I) occur are governed by 3 parameters, _b~P~_, _b~A~_, and _b~I~_. Susceptible individuals (S) can also become infected by contact with the environment or infected vectors, at rates _b~E~_ and _b~v~_. Susceptible vectors (S~v~) can become infected by contact with symptomatic hosts at rate _b~h~_. All infected hosts first enter the presymptomatic stage. They remain there for some time (determined by rate _g~P~_, the inverse of which is the average time spent in the presymptomatic stage). A fraction _f_ of presymptomatic hosts move into the asymptomatic category, and the rest become symptomatic infected hosts. Asymptomatic infected hosts recover after some time (specified by the rate _g~A~_). Similarly, the rate _g~I~_ determines the duration the symptomatic hosts stay in the symptomatic state. For symptomatic hosts, two outcomes are possible. Either recovery or death. The parameter _d_ determines the fraction of hosts that die due to disease. Recovered individuals are initially immune to reinfection. They can lose their immunity at rate _w_ and return to the susceptible compartment. Symptomatic and asymptomatic hosts shed pathogen into the environment at rates p~A~ and p~I~. The pathogen in the environment decays at rate _c_. New susceptible hosts and vectors enter the system (are born) at rates _n~h~_ and _n~v~_. Mortality (death unrelated to disease) for hosts and vectors occurs at rates _m~h~_ and _m~v~_.
#' 
#' This code was generated by the modelbuilder R package.  
#' The model is implemented as a set of stochastic equations using the adaptivetau package. 
 
#' The following R packages need to be loaded for the function to work: adpativetau 
#' 
#' @param S : starting value for Susceptible : numeric
#' @param P : starting value for Presymptomatic : numeric
#' @param A : starting value for Asymptomatic : numeric
#' @param I : starting value for Symptomatic : numeric
#' @param R : starting value for Recovered : numeric
#' @param D : starting value for Dead : numeric
#' @param E : starting value for Pathogen in Environment : numeric
#' @param Sv : starting value for Susceptible Vector : numeric
#' @param Iv : starting value for Infected Vector : numeric
#' @param nH : Birth rate of susceptible hosts : numeric
#' @param mH : Death rate of hosts : numeric
#' @param nV : Birth rate of susceptible vectors : numeric
#' @param mV : Death rate of vectors : numeric
#' @param bP : rate of transmission from pre-symptomatic hosts : numeric
#' @param bA : rate of transmission from asymptomatic hosts : numeric
#' @param bI : rate of transmission from symptomatic hosts : numeric
#' @param bE : rate of transmission from environment : numeric
#' @param bV : rate of transmission from vectors to hosts : numeric
#' @param bH : rate of transmission to vectors from hosts : numeric
#' @param gP : rate of movement out of P compartment : numeric
#' @param gA : rate of movement out of A compartment : numeric
#' @param gI : rate of movement out of I compartment : numeric
#' @param pI : rate of pathogen shedding into environment by symptomatic hosts : numeric
#' @param pA : rate of pathogen shedding into environment by asymptomatic hosts : numeric
#' @param c : rate of pathogen decay in environment : numeric
#' @param f : fraction of presymptomatic that move to asymptomatic : numeric
#' @param d : fraction of symptomatic infected hosts that die due to disease : numeric
#' @param w : rate of immunity waning : numeric
#' @param tfinal : Final time of simulation : numeric
#' @param rngseed : set random number seed for reproducibility : numeric
#' @return The function returns the output as a list. 
#' The time-series from the simulation is returned as a dataframe saved as list element \code{ts}. 
#' The \code{ts} dataframe has one column per compartment/variable. The first column is time.   
#' @examples  
#' # To run the simulation with default parameters:  
#' result <- simulate_Complex_ID_Control_stochastic() 
#' # To choose values other than the standard one, specify them like this:  
#' result <- simulate_Complex_ID_Control_stochastic(S = 2000,P = 2,A = 2,I = 2,R = 0,D = 0,E = 2,Sv = 2000,Iv = 2) 
#' # You can display or further process the result, like this:  
#' plot(result$ts[,'time'],result$ts[,'S'],xlab='Time',ylab='Numbers',type='l') 
#' print(paste('Max number of S: ',max(result$ts[,'S']))) 
#' @section Warning: This function does not perform any error checking. So if you try to do something nonsensical (e.g. have negative values for parameters), the code will likely abort with an error message.
#' @section Model Author: Andreas Handel, Alexis Vittengl
#' @section Model creation date: 2020-09-24
#' @section Code Author: generated by the \code{modelbuilder} R package 
#' @section Code creation date: 2021-07-19
#' @export 
 
simulate_Complex_ID_Control_stochastic <- function(S = 1000, P = 1, A = 1, I = 1, R = 0, D = 0, E = 1, Sv = 1000, Iv = 1, nH = 0.01, mH = 0.001, nV = 0.01, mV = 0.001, bP = 0.02, bA = 0.02, bI = 0.02, bE = 0.02, bV = 0.02, bH = 0.02, gP = 0.7, gA = 0.1, gI = 0.1, pI = 0.04, pA = 0.03, c = 0.7, f = 0.08, d = 0.01, w = 0.003, tfinal = 100, rngseed = 123) 
{ 
  #Block of ODE equations for adaptivetau 
  Complex_ID_Control_fct <- function(y, parms, t) 
  {
    with(as.list(c(y,parms)),   
     { 
       #specify each rate/transition/reaction that can happen in the system 
     rates = c((1-d)*gI*I, (1-f)*gP*P, bH*I*Sv, d*gI*I, f*gP*P, gA*A, S*bA*A, S*bE*E, S*bI*I, S*bP*P, S*bV*Iv, w*R, c*E, mH*A, mH*I, mH*P, mH*R, mH*S, mV*Iv, mV*Sv, nH, nV, pA*A, pI*I)
     return(rates) 
      }
	 	)   
  } # end function specifying rates used by adaptive tau 

   #specify for each reaction/rate/transition how the different variables change 
  #needs to be in exactly the same order as the rates listed in the rate function 
  transitions = list(c(I = -1,R = +1), 
 	 	 				c(I = +1,P = -1), 
 	 	 				c(Iv = +1,Sv = -1), 
 	 	 				c(D = +1,I = -1), 
 	 	 				c(A = +1,P = -1), 
 	 	 				c(R = +1,A = -1), 
 	 	 				c(P = +1,S = -1), 
 	 	 				c(P = +1,S = -1), 
 	 	 				c(P = +1,S = -1), 
 	 	 				c(P = +1,S = -1), 
 	 	 				c(P = +1,S = -1), 
 	 	 				c(R = -1,S = +1), 
 	 	 				c(E = -1), 
 	 	 				c(A = -1), 
 	 	 				c(I = -1), 
 	 	 				c(P = -1), 
 	 	 				c(R = -1), 
 	 	 				c(S = -1), 
 	 	 				c(Iv = -1), 
 	 	 				c(Sv = -1), 
 	 	 				c(S = +1), 
 	 	 				c(Sv = +1), 
 	 	 				c(E = +1), 
 	 	 				c(E = +1)) 
 
  ############################## 
  #Main function code block 
  ############################## 
  set.seed(rngseed) #set random number seed for reproducibility 
  #Creating named vectors 
  varvec = c(S = S, P = P, A = A, I = I, R = R, D = D, E = E, Sv = Sv, Iv = Iv) 
  parvec = c(nH = nH, mH = mH, nV = nV, mV = mV, bP = bP, bA = bA, bI = bI, bE = bE, bV = bV, bH = bH, gP = gP, gA = gA, gI = gI, pI = pI, pA = pA, c = c, f = f, d = d, w = w) 
  #Running the model 
  simout = adaptivetau::ssa.adaptivetau(init.values = varvec, transitions = transitions,
                  	 	 	 rateFunc = Complex_ID_Control_fct, params = parvec, tf = tfinal) 
  #Setting up empty list and returning result as data frame called ts 
  result <- list() 
  result$ts <- as.data.frame(simout) 
  return(result) 
 }  
 
ahgroup/modelbuilder documentation built on April 14, 2024, 2:29 p.m.