R/sim_SI.R

Defines functions sim_SI

Documented in sim_SI

#' sim_SI
#'
#' sim_SI simulates the spread of an infectious disease on a social network
#' using the Susceptible-Infected compartmental model
#'
#' @param network_el an edgelist file generated by igraph
#' @param beta a value from 0 to 1 indicating per contract probability of
#'   pathogen transmission
#' @param intxn_per_day the per capita interaction rate
#' @param days the maximum number of days for which your simulation will run
#'
#' @return vector with 3 elements, 1: number of days simulation went for, 2:
#'   final number of susceptible individuals, 3: final numbre of infected
#'   individuals
#' @export
#'
#' @examples
sim_SI <- function(network_el, beta, intxn_per_day, days) {

  n = network_el[1,4]
  e = network_el[1,5]
  cdata <- network_el[,1:2]

  infection_status <- c(rep(1,n))
  index_infected <- sample(1:n, 1)
  infection_status[index_infected] = 2

  day_counter <- 0
  while(day_counter <= days) {

    int_counter <- 0
    while(int_counter <= intxn_per_day*n) {

      selected_edge <- sample(1:e,1)

      if (sum(infection_status[cdata[selected_edge,1:2]]) == 3) {
        if (beta >= runif(1,0,1)) {
          infection_status[cdata[selected_edge,1:2]] = 2
        }
      }

      int_counter <- sum(int_counter,1)
    }

    day_counter = day_counter+1
    if (sum(infection_status%%2) == 0) break
  }
  return(c(day_counter-1,sum(infection_status == 1),sum(infection_status == 2)))
}
collinmmccabe/enss documentation built on May 5, 2024, 6:23 a.m.