Skip to content

Instantly share code, notes, and snippets.

@N8python
Created July 7, 2020 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save N8python/73c760595c137f3eab85c10b33d121de to your computer and use it in GitHub Desktop.
Save N8python/73c760595c137f3eab85c10b33d121de to your computer and use it in GitHub Desktop.
SEIRS Model - A SEIRS Model with support for vital dynamics, mortality, and seasonal variance in r0.
library(deSolve)
library(reshape2)
library(ggplot2)
initial_state_values <- c(S = 0.999, E = 0, I = 0.001, R = 0, M = 0) # S = Susceptible, E = Exposed, I = Infected, R = Recovered, M = Dead
parameters <- c(
beta = 0.07142857142 * 3, # Contact Rate
theta = 0.2, # 1 / (Length of Latent Period)
gamma = 0.07142857142, # 1 / (Length of Infection)
ro = 0.00130718954, # 1 / (Length of Immunity)
nu = 0.00003281917, # Birth Rate
mu = 0.00003281917, # Death Rate
alpha = 0.035 # Mortality Rate
)
startTime <- 0 # Time when the disease starts - 0 is winter, 180 is summer, 360 is winter, etc.
r0_reduction <- 0.3 # Percent by which the r0 goes down during the summer - 0.3 means a 30% reduction at the peak of summer.
days_simulated <- 7 * 52 * 10 # Set this variable to how many days you want to simulate. (by default, 10 years are simulated.)
times <- seq(from = 0, to = days_simulated, by = 1) #
beta_t <- function(time) { # Function to Calculate Seasonal Variance of r0
return((r0_reduction / 2)*sin(2*pi/365*(time + 91.25))+(1-r0_reduction / 2))
}
cohort_model <- function(time, state, parameters) { # Differential Equations
with (as.list(c(state, parameters)), {
dS <- nu - beta * beta_t(time + startTime) * S * I + ro * R - mu * S
dE <- beta * beta_t(time + startTime) * S * I - theta * E - mu * E
dI <- theta * E - gamma * I - (mu + alpha * gamma) * I
dR <- gamma * I - ro * R - mu * R
dM <- alpha * gamma * I
return(list(c(dS, dE, dI, dR, dM)))
})
}
output <- as.data.frame(ode(y = initial_state_values, times = times, func = cohort_model, parms = parameters))
output_long <- melt(as.data.frame(output), id = "time")
ggplot(data = output_long, aes(x = time, y = value, colour = variable, group = variable)) +
geom_line() +
xlab("Time (days)") + # X Axis Label
ylab("Number of people") + # Y Axis Label
labs(title = "SEIRS Model", colour = "Compartments") # Graph Title & Legend Title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment