auxiliary/modelfiles/other/simulators/simulate_Influenza_Antibody_Model_discrete.R

#' Influenza Antibody Model
#' 
#' @description Influenza Antibody Model
#' 
#' @details Model based on Zarnitsyna et al 2016 PLoS Pathogens
#' 
#' This code was generated by the modelbuilder R package.  
#' The model is implemented as a set of discrete time equations using a for loop. 
 
#' The following R packages need to be loaded for the function to work: none 
#' 
#' @param Hf : starting value for free antigen : numeric
#' @param Hb : starting value for bound antigen : numeric
#' @param B : starting value for B cells : numeric
#' @param A : starting value for antibodies : numeric
#' @param k : binding rate : numeric
#' @param df : free antigen decay rate : numeric
#' @param db : bound antigen decay rate : numeric
#' @param s : B cell generation rate : numeric
#' @param del : bound antibody strength : numeric
#' @param p : saturation level : numeric
#' @param a : FIM impact  : numeric
#' @param g : antibody production rate : numeric
#' @param dA : antibody decay rate : numeric
#' @param tstart : Start time of simulation : numeric
#' @param tfinal : Final time of simulation : numeric
#' @param dt : Time step : 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_Influenza_Antibody_Model_discrete() 
#' # To choose values other than the standard one, specify them like this:  
#' result <- simulate_Influenza_Antibody_Model_discrete(Hf = 20000,Hb = 0,B = 2,A = 2) 
#' # You can display or further process the result, like this:  
#' plot(result$ts[,'time'],result$ts[,'Hf'],xlab='Time',ylab='Numbers',type='l') 
#' print(paste('Max number of Hf: ',max(result$ts[,'Hf']))) 
#' @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
#' @section Model creation date: 2023-05-08
#' @section Code Author: generated by the \code{modelbuilder} R package 
#' @section Code creation date: 2023-05-08
#' @export 
 
simulate_Influenza_Antibody_Model_discrete <- function(Hf = 10000, Hb = 0, B = 1, A = 1, k = 0.01, df = 0.5, db = 0.5, s = 1, del = 0, p = 0, a = 0, g = 0.1, dA = 0.1, tstart = 0, tfinal = 100, dt = 0.1) 
{ 
  #Function that encodes simulation loop 
  Influenza_Antibody_Model_fct <- function(vars, pars, times) 
  {
    with( as.list(c(vars,pars)), {  
      ts = data.frame(cbind(times, matrix(0,nrow=length(times),ncol=length(vars)))) 
      colnames(ts) = c('time','Hf','Hb','B','A') 
      ct=1 #a counter to index array 
      for (t in times) 
      {
        ts[ct,] = c(t,Hf,Hb,B,A) 
        Hfp = Hf + dt*(-k*A*Hf -df*Hf) 
        Hbp = Hb + dt*(+k*A*Hf -db*Hb) 
        Bp = B + dt*(+s*B*(Hf+del*Hb)/(p+Hf+del*Hb)*(1/(1+a*Hb))) 
        Ap = A + dt*(+g*B -k*A*Hf -dA*A) 
        Hf = Hfp 
        Hb = Hbp 
        B = Bp 
        A = Ap 
        ct = ct + 1 
      } #finish loop 
      return(ts) 
    }) #close with statement 
 } #end function encoding loop 
 
  ############################## 
  #Main function code block 
  ############################## 
  #Creating named vectors 
  varvec = c(Hf = Hf, Hb = Hb, B = B, A = A) 
  parvec = c(k = k, df = df, db = db, s = s, del = del, p = p, a = a, g = g, dA = dA) 
  timevec = seq(tstart, tfinal,by = dt) 
  #Running the model 
  simout <- Influenza_Antibody_Model_fct(vars = varvec, pars = parvec, times = timevec) 
  #Setting up empty list and returning result as data frame called ts 
  result <- list() 
  result$ts <- simout 
  return(result) 
} 
ahgroup/modelbuilder documentation built on April 14, 2024, 2:29 p.m.