Skip to content

Instantly share code, notes, and snippets.

@N8python
Created July 10, 2020 14:08
Show Gist options
  • Select an option

  • Save N8python/474c86c5b7e844bd2f881ef312e0b038 to your computer and use it in GitHub Desktop.

Select an option

Save N8python/474c86c5b7e844bd2f881ef312e0b038 to your computer and use it in GitHub Desktop.
runs <- 1
for(run in 1:runs) {
beta <- 0.65
gamma <- 0.15
N <- 1000
S <- c(999)
I <- c(1)
R <- c(0)
for(i in 1:(7*8)){
l <- rbinom(1, round(I[length(I)]), beta)
yI <- rbinom(1, round(I[length(I)]), gamma)
sProportion <- S[length(S)] / N
S <- c(S, S[length(S)] - l * sProportion)
I <- c(I, I[length(I)] + l * sProportion - yI)
R <- c(R, R[length(R)] + yI)
}
if (run == 1) {
plot(S, type="l", col = "blue", main = "SIR Stochastic Model", xlab = "Time (In Days)", ylab = "Number of People", ylim = c(0, N))
legend("left", legend = c("S", "I", "R"), fill = c("blue", "red", "green"))
} else {
lines(S, col="blue")
}
lines(I, col="red")
lines(R, col="green")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment