**YGC » R**, and kindly contributed to R-bloggers)

The SIR model divides the population to three compartments: **S**usceptible, **I**nfected and **R**ecovered. If the disease dynamic fits the SIR model, then the flow of individuals is one direction from the susceptible group to infected group and then to the recovered group. All individuals are assumed to be identical in terms of their susceptibility to infection, infectiousness if infected and mixing behaviour associated with disease transmission.

We defined:

(S_t) = the number of susceptible individuals at time t

(I_t) = the number of infected individuals at time t

(R_t) = the number of recovered individuals at time t

Suppose on average every infected individual will contact (gamma) person, and (kappa) percent of these (gamma) person will be infected. Then on average there are (beta = gamma times kappa) person will be infected an infected individual.

So with infected number (I_t) , they will infected (beta I_t) individuals. Since not all people are susceptible, this number should multiple to the percentage of susceptible individuals. Therefore, (I_t) infected individuals will infect (beta frac{S_t}{N} I_t) individuals.

Another parameter (alpha) describes the percentage of infected individuals to recover in a time period. That is on average, it takes (1/alpha) periods for an infected person to recover.

^{?}View Code RSPLUS

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
require(reshape2) require(ggplot2) require(gridExtra) SIR <- function(N, S0, I0, beta, alpha, t) { S <- numeric(t) I <- numeric(t) S[1] <- S0 I[1] <- I0 for (i in 2:t) { S[i] <- S[i-1] - beta*S[i-1]/N*I[i-1] I[i] <- I[i-1] + beta*S[i-1]/N*I[i-1] - alpha * I[i-1] if (I[i] < 1 || S[i] < 1) break } df <- data.frame(time=1:t, S=S, I=I, R=N-S-I) df <- df[S>1&I>1,] df$AR <- (df$I+df$R)/N nr <- nrow(df) rate <- df$I[2:nr]/df$I[1:(nr-1)] df$rate <- c(rate[1], rate) return(df) } plotSIR <- function(df) { nr <- nrow(df) p1 <- ggplot(df, aes(time, I))+geom_line() p1 <- p1+ggtitle("No. of Infections") p1 <- p1+geom_point(x=df[nr, "time"], y=df[nr, "I"], color="red", size=3) p2 <- ggplot(df, aes(time, AR))+geom_line()+xlab("") p2 <- p2 + ggtitle("Attack rate increases") p2 <- p2 + geom_point(x=df[nr, "time"], y=df[nr, "AR"], color="red", size=3) p3 <- ggplot(df, aes(time, S))+geom_line() p3 <- p3 +ggtitle("Depletion of susceptibles") p3 <- p3+ylim(0, max(df$S)) + geom_point(x=df[nr, "time"], y=df[nr, "S"], color="red", size=3) p4 <- ggplot(df, aes(time, rate))+geom_line() p4 <- p4 + ggtitle("R")+ ylim(range(df$rate)+c(-.2, .2)) + geom_hline(yintercept=1, color="steelblue", linetype="dashed") p4 <- p4+geom_point(x=df[nr, "time"], y=df[nr, "rate"], color="red", size=3) grid.arrange(p1,p2,p3,p4, ncol=1) invisible(list(p1=p1, p2=p2, p3=p3, p4=p4)) } df <- SIR(1e6, 1e6-1, 1, 0.3, 0.1, 300) plotSIR(df) |

#### Related Posts

**leave a comment**for the author, please follow the link and comment on their blog:

**YGC » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...