-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimple_SIR.R
152 lines (118 loc) · 4.59 KB
/
simple_SIR.R
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
############################################################################################
########### Simple SIR model dynamics ##################
########### ##################
########### ##################
########### ##################
########### Written by: Mike A Irvine ##################
########### ##################
########### written: 11/04/17 ##################
############################################################################################
#If running for first time and neccessary, uncomment following lines to install packages
#install library to handle differential equations
#install.packages("deSolve")
#install.packages("ggplot2")
# resets R to fresh
rm(list = ls(all = TRUE))
# load in library for solving differential equations
library("deSolve")
# load in libraries for plotting
library("ggplot2")
library("reshape2")
library("scales")
SIR_func=function(t, x, vparameters){
S = x[1]
I = x[2]
R = x[3]
with(as.list(vparameters),{
npop = S+I+R
dS = -beta*S*I/npop +delta*R
dI = +beta*S*I/npop - gamma*I
dR = +gamma*I -delta*R
vout = c(dS,dI,dR)
list(vout)
})
}
npop = 100000
I_0 = 1
R_0 = 0
S_0 = npop-I_0-R_0
tbegin = 0
tend = 365
vt = seq(tbegin,tend,1)
gamma = 1/3 #recovery rate (days)
R0 = 2.0 #Basic reproduction number
beta = R0*gamma #infectivity: product of R0 and recovery rate.
delta = 0.
vparameters = c(gamma=gamma,beta=beta,delta=delta)
inits = c(S=S_0,I=I_0,R=R_0)
model.results = as.data.frame(lsoda(inits, vt, SIR_func, vparameters))
meltdf <- melt(model.results,id="time")
p<- ggplot(meltdf, aes(x=time ,y=value,colour=variable,group=variable)) + geom_line() +
xlab("time") + scale_y_continuous(labels = scales::comma) +
ylab("number of individuals") + theme(axis.text.y = element_text(angle=45))
show(p)
#################################################
# Second model: Include vaccination and treatment
#################################################
SIR_VT_func=function(t, x, vparameters){
#define the states of the model (susceptible, infected and recovered)
S = x[1]
I = x[2]
R = x[3]
V = x[4]
T = x[5]
with(as.list(vparameters),{
#total population size.
npop = S+I+R+V+T
#Instantaneous change in states
dS = -beta*S*I/npop +delta*R - vr*S
dI = +beta*S*I/npop - gamma*I -tr*I
dR = +gamma*I -delta*R + te*T
dV = +vr*S
dT = +tr*I - te*T
#output instananeous change in states as list.
vout = c(dS,dI,dR,dV,dT)
list(vout)
})
}
npop = 100000
I_0 = 1
R_0 = 0
V_0 = 0
T_0 = 0
S_0 = npop - I_0 - R_0 - V_0 - T_0
tbegin = 0
tend = 365
vt = seq(tbegin,tend,1)
gamma = 1/3 #recovery rate (1/days)
R0 = 2.1 #Basic reproduction number (no dimensions)
beta = R0*gamma #infectivity: product of R0 and recovery rate.
delta = 0. #waning immunity
te = 1/7 #treatment efficacy (1/days)
tr = 0.0 #rate of treatment for infected (1/days)
vr = 1/365 #rate of vaccination per person (1/days)
vparameters = c(gamma=gamma,beta=beta,delta=delta,te=te,tr=tr,vr=vr)
inits = c(S=S_0,I=I_0,R=R_0,V=V_0,T=T_0)
model.results = as.data.frame(lsoda(inits, vt, SIR_VT_func, vparameters))
meltdf <- melt(model.results,id="time")
p<- ggplot(meltdf, aes(x=time ,y=value,colour=variable,group=variable)) + geom_line() +
xlab("time") + scale_y_continuous(labels = scales::comma) +
ylab("number of individuals") + theme(axis.text.y = element_text(angle=45))
show(p)
#################################################
# Calculate critical vaccination rate ###########
#################################################
vrs <- seq(1.,365.,length=100)
vrs <- 1/vrs
tot_Is <- array(0,c(100))
for(i in 1:100){
vr = vrs[i]
vparameters = c(gamma=gamma,beta=beta,delta=delta,te=te,tr=tr,vr=vr)
model.results = as.data.frame(lsoda(inits, vt, SIR_VT_func, vparameters))
tot_Is[i] <- sum(model.results['I'])
}
vaccination.results <- data.frame(vaccination=1/vrs, infections=tot_Is)
p<- ggplot(vaccination.results, aes(x=vaccination ,y=infections)) + geom_line() +
xlab("Expected time to vaccinate (days)") + scale_y_continuous(labels = scales::comma) +
ylab("Total number of individuals infected") + theme(axis.text.y = element_text(angle=45))
show(p)