-
Notifications
You must be signed in to change notification settings - Fork 0
/
SEIR_run.R
315 lines (279 loc) · 15.2 KB
/
SEIR_run.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
######################################
### TWO-STEP VAX LITTLE MODEL ########
######################################
# This file contain the definition of the differential equations and the
# numerical integrator.
library(dde)
library(zeallot)
# # Reads the epidemiological parameters for COVID-19 and Brazilian demographics
# source('./functions/parms_epi.R')
# Reads the parametrizations of the vaccine and vaccination campaign, calls parms_epi now
source('./functions/parms_vaccine.R')
# Parameters of the numerical integration
SIM.DURATION.DAYS = MAX.TIME.DAYS # Days of simulation
#SIM.DURATION.DAYS = 200
TIME.VECTOR <- seq(0, SIM.DURATION.DAYS)
# Population is a named vector of tri-vectors
POP0 = c(# Never-vaccinated populations:
POP0,
# POP.S = c(0.7, 0.71, 0.71)*POP.DISTR, # Susceptible
# POP.E = c(0.02, 0.02, 0.015)*POP.DISTR, # Exposed (infected but not infectious)
# POP.A = c(0.02, 0.01, 0.015)*POP.DISTR, # Asymptomatic infectious
# POP.I = c(0.01, 0.01, 0.01)*POP.DISTR, # Mild cases infectious
# POP.H = c(0,0,0), # Severe cases/hospitalization infectious
# POP.R = c(0.25, 0.25, 0.25)*POP.DISTR, # Recovered
# POP.D = c(0,0,0), # Deaths
## Once-vaccinated populations:
# POP.Sv = c(0,0,0), # Susceptible
# POP.Ev = c(0,0,0), # Exposed (infected but not infectious)
# POP.Av = c(0,0,0), # Asymptomatic infectious
# POP.Iv = c(0,0,0), # Mild cases infectious
# POP.Hv = c(0,0,0), # Severe cases/hospitalization infectious
# POP.Rv = c(0,0,0), # Recovered
# POP.Dv = c(0,0,0), # Deaths
# ## Twice vaccinated populations:
# POP.Sw = c(0,0,0), # Susceptible
# POP.Ew = c(0,0,0), # Exposed (infected but not infectious)
# POP.Aw = c(0,0,0), # Asymptomatic infectious
# POP.Iw = c(0,0,0), # Mild cases infectious
# POP.Hw = c(0,0,0), # Severe cases/hospitalization infectious
# POP.Rw = c(0,0,0), # Recovered
# POP.Dw = c(0,0,0), # Deaths
## Total number of vaccines
VAX = VAX.INITIAL.STORAGE.NUM
)
func.factory <- function(
ASYMPTOMATIC_FRAC = ASYMPTOMATIC.FRAC, BETA_RATE = BETA.RATE,
CONT_REDUC_FRAC = CONT.REDUC.FRAC, CONTACT_M = CONTACT.M, DEATH_FRAC =
DEATH.FRAC, EXPOSURE_PERIOD_DAYS = EXPOSURE.PERIOD.DAYS,
FIRST_DOSE_REL_EFFIC = FIRST.DOSE.REL.EFFIC, MAX_TIME_DAYS = MAX.TIME.DAYS,
MAX_VAC_RATE = MAX.VAC.RATE, POP_DISTR = POP.DISTR, POP_ESTADO_REL_FRAC =
POP.ESTADO.REL.FRAC, POP_TOTAL_NUM = POP.TOTAL.NUM, REL_INFEC_PRESYMP =
REL.INFEC.PRESYMP, SECOND_VAX_LOSS_FRAC = SECOND.VAX.LOSS.FRAC,
SEVERE_CONT_REDUC_FRAC = SEVERE.CONT.REDUC.FRAC, SEVERE_PERIOD_DAYS =
SEVERE.PERIOD.DAYS, SEVERITY_FRAC = SEVERITY.FRAC, SICKNESS_PERIOD_DAYS
= SICKNESS.PERIOD.DAYS, VAX_INITIAL_STORAGE_NUM = VAX.INITIAL.STORAGE.NUM,
VAX_PRODUCTION_RATE = VAX.PRODUCTION.RATE, VAX_WINDOW_DAYS =
VAX.WINDOW.DAYS, VAX1_ASYMPTOMATIC_FRAC = VAX1.ASYMPTOMATIC.FRAC,
VAX1_BETA_RATE = VAX1.BETA.RATE, VAX1_DEATH_FRAC = VAX1.DEATH.FRAC,
VAX1_EFFIC_CLIN = VAX1.EFFIC.CLIN, VAX1_EFFIC_DEATH = VAX1.EFFIC.DEATH,
VAX1_EFFIC_SEVERE = VAX1.EFFIC.SEVERE, VAX1_SEVERITY_FRAC =
VAX1.SEVERITY.FRAC, VAX2_ASYMPTOMATIC_FRAC = VAX2.ASYMPTOMATIC.FRAC,
VAX2_BETA_RATE = VAX2.BETA.RATE, VAX2_DEATH_FRAC = VAX2.DEATH.FRAC,
VAX2_EFFIC_CLIN = VAX2.EFFIC.CLIN, VAX2_EFFIC_DEATH = VAX2.EFFIC.DEATH,
VAX2_EFFIC_SEVERE = VAX2.EFFIC.SEVERE, VAX2_SEVERITY_FRAC =
VAX2.SEVERITY.FRAC) {
# code that generates this beauty:
# source('./parms_epi.R')
# source('./parms_vaccine.R')
# objs = setdiff(ls(), lsf.str())
# paste0(lapply(objs, function(x) paste0(gsub('\\.', '_', x), " = ", x)), collapse=", ")
#
# WARNING: several of those are not actual parameters! They are only used
# in parms_* to calculate other parameters
# calculates current rate of vaccination
OPT.VAX.RATE <- opt_vax_rate(VAX_INITIAL_STORAGE_NUM, VAX_PRODUCTION_RATE,
MAX_VAC_RATE, VAX_WINDOW_DAYS,
SECOND_VAX_LOSS_FRAC, MAX_TIME_DAYS)
# interpolation for continuous time
VAX.RATE <- interpolate.VAX.RATE(OPT.VAX.RATE)
diffEqs = function(t, POP, parms) {
c(POP.S, POP.E, POP.A, POP.I, POP.H, POP.R, POP.D,
POP.Sv, POP.Ev, POP.Av, POP.Iv, POP.Hv, POP.Rv, POP.Dv,
POP.Sw, POP.Ew, POP.Aw, POP.Iw, POP.Hw, POP.Rw, POP.Dw, VAX) %<-%
split(POP, ceiling(seq_along(POP)/3))
debug = FALSE
if(debug && (any(c(POP.S, POP.E, POP.A, POP.I, POP.H, POP.R, POP.D,
POP.Sv, POP.Ev, POP.Av, POP.Iv, POP.Hv, POP.Rv, POP.Dv,
POP.Sw, POP.Ew, POP.Aw, POP.Iw, POP.Hw, POP.Rw, POP.Dw, VAX) < -1e-8) ||
! all(is.finite(c(POP.S, POP.E, POP.A, POP.I, POP.H, POP.R, POP.D,
POP.Sv, POP.Ev, POP.Av, POP.Iv, POP.Hv, POP.Rv, POP.Dv,
POP.Sw, POP.Ew, POP.Aw, POP.Iw, POP.Hw, POP.Rw, POP.Dw, VAX))))){
cat(paste("Pop. negativa!",
paste("tempo t = ", t),
"Unvaccinated: ",
paste(c(POP.S, POP.E), collapse = ', '),
paste(c(POP.A, POP.I, POP.H), collapse = ', '),
paste(c(POP.R, POP.D), collapse = ', '),
"Vaccinated first dose: ",
paste(c(POP.Sv, POP.Ev), collapse = ', '),
paste(c(POP.Av, POP.Iv, POP.Hv), collapse = ', '),
paste(c(POP.Rv, POP.Dv), collapse = ', '),
"Vaccinated second dose: ",
paste(c(POP.Sw, POP.Ew), collapse = ', '),
paste(c(POP.Aw, POP.Iw, POP.Hw), collapse = ', '),
paste(c(POP.Rw, POP.Dw), collapse = ', '),
"Vaccines: ", VAX,
sep="\n"))
stop()
}
# Some repeated factors:
# Total infectious
lambda = (BETA_RATE / POP_TOTAL_NUM) * CONTACT_M %*%
((POP.A + REL_INFEC_PRESYMP*POP.E + (1-CONT_REDUC_FRAC)*POP.I +(1-SEVERE_CONT_REDUC_FRAC)*POP.H) +
(POP.Av + REL_INFEC_PRESYMP*POP.Ev + (1-CONT_REDUC_FRAC)*POP.Iv +(1-SEVERE_CONT_REDUC_FRAC)*POP.Hv) +
(POP.Aw + REL_INFEC_PRESYMP*POP.Ew + (1-CONT_REDUC_FRAC)*POP.Iw +(1-SEVERE_CONT_REDUC_FRAC)*POP.Hw))
SAR.NORM = 1.0/(POP.S+POP.R) # Normalization of S, E, A and R
if(t <= MAX_TIME_DAYS){
V.TOTAL = VAX.RATE(t)
}
else{
V.TOTAL <- 0
}
Vt = VAX.DISTR.RATE(V.TOTAL, 1/SAR.NORM)
# delayed variables
a = t - VAX_WINDOW_DAYS # Delayed time
if (a <= 0){
Vt.a = rep(0,each=3)
POP.S.a = c(POP0['POP.S1'], POP0['POP.S2'], POP0['POP.S3'])
POP.A.a = c(POP0['POP.A1'], POP0['POP.A2'], POP0['POP.A3'])
POP.R.a = c(POP0['POP.R1'], POP0['POP.R2'], POP0['POP.R3'])
SAR.NORM.a = 1.0/(POP.S.a+POP.R.a)
} else {
POP.S.a = ylag(a, 1:3)
POP.A.a = ylag(a, 7:9)
POP.R.a = ylag(a, 16:18)
V.TOTAL.a = VAX.RATE(a)
SAR.NORM.a = 1.0/(POP.S.a+POP.R.a)
Vt.a = VAX.DISTR.RATE(V.TOTAL.a, 1/SAR.NORM.a)
}
# print(c(t,Vt,Vt.a))
# NEVER-VACCINATED POPULATIONS
# Susceptible
dS = - POP.S * lambda - # Getting infected
Vt * POP.S * SAR.NORM # Getting vaccinated
# Pre-symptomatic
dE = POP.S * lambda - # Getting infected
POP.E / EXPOSURE_PERIOD_DAYS # Becoming asymptomatic or mild case
# Assymptomatic
dA = ASYMPTOMATIC_FRAC*(1 - SEVERITY_FRAC)*POP.E / EXPOSURE_PERIOD_DAYS - # Becoming asymptomatic
POP.A / SICKNESS_PERIOD_DAYS # Recovering
#Vt * POP.A * SAR.NORM # Getting vaccinated
# Mild Infectious
dI = (1 - ASYMPTOMATIC_FRAC) * (1 - SEVERITY_FRAC) * POP.E / EXPOSURE_PERIOD_DAYS - # Becoming mild case
POP.I / SICKNESS_PERIOD_DAYS # Recovering
# Severe Case/Hospitalization
dH = SEVERITY_FRAC * POP.E / EXPOSURE_PERIOD_DAYS - # Becoming severe case
POP.H / SEVERE_PERIOD_DAYS
# Recovered
dR = POP.A / SICKNESS_PERIOD_DAYS + # Asymptomatic recovering
POP.I / SICKNESS_PERIOD_DAYS + # Mild case recovering
(1-DEATH_FRAC)*POP.H / SEVERE_PERIOD_DAYS - # Severe case recovering
Vt * POP.R * SAR.NORM # Getting vaccinated
# Dead
dD = DEATH_FRAC *POP.H / SEVERE_PERIOD_DAYS # Severe case dying
# FIRST VACCINATED POPULATIONS
# Susceptible
dSv = - POP.Sv * VAX1_BETA_RATE * lambda + # Getting infected
Vt * POP.S * SAR.NORM - # Getting vaccinated once
(1 - SECOND_VAX_LOSS_FRAC) * Vt.a * POP.S.a * SAR.NORM.a # Getting vaccinated twice
a1 <- POP.Sv * VAX1_BETA_RATE * lambda
a1 <- a1[3]
a2<- Vt * POP.S * SAR.NORM
a2 <- a2[3]
# print(c(t,POP.Sv[3],a1,a2,VAX))
# Pre-symptomatic
dEv = POP.Sv * VAX1_BETA_RATE * lambda - # Getting infected
POP.Ev / EXPOSURE_PERIOD_DAYS # Becoming asymptomatic or mild case
# Assymptomatic
dAv = VAX1_ASYMPTOMATIC_FRAC*(1 - VAX1_SEVERITY_FRAC)*POP.Ev / EXPOSURE_PERIOD_DAYS - # Becoming asymptomatic
POP.Av / SICKNESS_PERIOD_DAYS # Recovering
#Vt * POP.A * SAR.NORM - # Getting vaccinated
#(1 - SECOND_VAX_LOSS_FRAC) * Vt.a * POP.A.a * SAR.NORM.a # Getting vaccinated twice
# Mild Infectious
dIv = (1 - VAX1_ASYMPTOMATIC_FRAC) * (1 - VAX1_SEVERITY_FRAC) * POP.Ev / EXPOSURE_PERIOD_DAYS - # Becoming mild case
POP.Iv / SICKNESS_PERIOD_DAYS # Recovering
# Severe Case/Hospitalization
dHv = VAX1_SEVERITY_FRAC * POP.Ev / EXPOSURE_PERIOD_DAYS - # Becoming severe case
POP.Hv / SEVERE_PERIOD_DAYS
# Recovered
dRv = POP.Av / SICKNESS_PERIOD_DAYS + # Asymptomatic recovering
POP.Iv / SICKNESS_PERIOD_DAYS + # Mild case recovering
(1 - VAX1_DEATH_FRAC)*POP.Hv / SEVERE_PERIOD_DAYS + # Severe case recovering
Vt * POP.R * SAR.NORM - # Getting vaccinated
(1 - SECOND_VAX_LOSS_FRAC) * Vt.a * POP.R.a * SAR.NORM.a # Getting vaccinated twice
# Dead
dDv = VAX1_DEATH_FRAC *POP.Hv / SEVERE_PERIOD_DAYS # Severe case dying
# TWICE VACCINATED POPULATIONS
# Susceptible
dSw = - POP.Sw * VAX2_BETA_RATE * lambda + # Getting infected
(1 - SECOND_VAX_LOSS_FRAC) * Vt.a * POP.S.a * SAR.NORM.a # Getting vaccinated twice
# Pre-symptomatic
dEw = POP.Sw * VAX2_BETA_RATE * lambda - # Getting infected
POP.Ew / EXPOSURE_PERIOD_DAYS # Becoming assymthomatic or mild case
# Assymptomatic
dAw = VAX2_ASYMPTOMATIC_FRAC *(1 - VAX2_SEVERITY_FRAC)* POP.Ew / EXPOSURE_PERIOD_DAYS - # Becoming assymtomathic
POP.Aw / SICKNESS_PERIOD_DAYS # Recovering
#(1 - SECOND_VAX_LOSS_FRAC) * Vt.a * POP.A.a * SAR.NORM.a # Getting vaccinated twice
# Mild Infectious
dIw = (1 - VAX2_ASYMPTOMATIC_FRAC) * (1 - VAX2_SEVERITY_FRAC) * POP.Ew / EXPOSURE_PERIOD_DAYS - # Becoming mild case
POP.Iw / SICKNESS_PERIOD_DAYS # Recovering
# Severe Case/Hospitalization
dHw = VAX2_SEVERITY_FRAC * POP.Ew / EXPOSURE_PERIOD_DAYS - # Becoming severe case
POP.Hw / SEVERE_PERIOD_DAYS
# Recovered
dRw = POP.Aw / SICKNESS_PERIOD_DAYS + # Asymptomatic recovering
POP.Iw / SICKNESS_PERIOD_DAYS + # Mild case recovering
(1 - VAX2_DEATH_FRAC)*POP.Hw / SEVERE_PERIOD_DAYS + # Severe case recovering
(1 - SECOND_VAX_LOSS_FRAC) * Vt.a * POP.R.a * SAR.NORM.a # Getting vaccinated twice
# Dead
dDw = VAX2_DEATH_FRAC *POP.Hw / SEVERE_PERIOD_DAYS # Severe case dying
# VACCINE
dVAX = VAX_PRODUCTION_RATE - sum(Vt) - (1 - SECOND_VAX_LOSS_FRAC) * sum(Vt.a)
# debug = TRUE
if(debug && ! all(is.finite(c(dS, dE, dA, dI, dH, dR, dD,
dSv, dEv, dAv, dIv, dHv, dRv, dDv,
dSw, dEw, dAw, dIw, dHw, dRw, dDw, dVAX)))){
cat(paste(" *** Derivadas infinitas/NaN! ***",
paste("tempo t = ", t),
"Unvaccinated: ",
paste(c(dS, dE), collapse = ', '),
paste(c(dA, dI, dH), collapse = ', '),
paste(c(dR, dD), collapse = ', '),
"Vaccinated first dose: ",
paste(c(dSv, dEv), collapse = ', '),
paste(c(dAv, dIv, dHv), collapse = ', '),
paste(c(dRv, dDv), collapse = ', '),
"Vaccinated second dose: ",
paste(c(dSw, dEw), collapse = ', '),
paste(c(dAw, dIw, dHw), collapse = ', '),
paste(c(dRw, dDw), collapse = ', '),
"Vaccines: ", dVAX,
SAR.NORM, SAR.NORM.a,"",
sep="\n"))
cat(paste(" *** Populações: ***",
"Unvaccinated: ",
paste(c(POP.S, POP.E), collapse = ', '),
paste(c(POP.A, POP.I, POP.H), collapse = ', '),
paste(c(POP.R, POP.D), collapse = ', '),
"Vaccinated first dose: ",
paste(c(POP.Sv, POP.Ev), collapse = ', '),
paste(c(POP.Av, POP.Iv, POP.Hv), collapse = ', '),
paste(c(POP.Rv, POP.Dv), collapse = ', '),
"Vaccinated second dose: ",
paste(c(POP.Sw, POP.Ew), collapse = ', '),
paste(c(POP.Aw, POP.Iw, POP.Hw), collapse = ', '),
paste(c(POP.Rw, POP.Dw), collapse = ', '),
"Vaccines: ", VAX,
sep="\n"))
stop()
}
return(c(dS, dE, dA, dI, dH, dR, dD, dSv, dEv, dAv, dIv, dHv, dRv, dDv, dSw, dEw, dAw, dIw, dHw, dRw, dDw, dVAX))
}
return(list(OPT.VAX.RATE = OPT.VAX.RATE,
VAX.RATE = VAX.RATE,
diffEqs = diffEqs))
}
source('./functions/Reff_NGM.R')
# SIM.DURATION.DAYS = 120 # Days of simulation
# #SIM.DURATION.DAYS = 200
# TIME.VECTOR <- seq(0, SIM.DURATION.DAYS)
# REFF.0 <- reff_ngm(sum(POP0[1:21]), POP0[1:3], CONTACT.M, BETA.RATE = 1., EXPOSURE.PERIOD.DAYS,
# SICKNESS.PERIOD.DAYS, SEVERE.PERIOD.DAYS, CONT.REDUC.FRAC,
# SEVERE.CONT.REDUC.FRAC, REL.INFEC.PRESYMP, ASYMPTOMATIC.FRAC,
# SEVERITY.FRAC, DEATH.FRAC)
# REFF.0 <- Re(REFF.0$values)
# BETA.RATE <- 1 / REFF.0
# diffEqs <- func.factory(BETA_RATE = BETA.RATE, MAX_TIME_DAYS = MAX.TIME.DAYS)$diffEqs
# SOLUTION <- dopri(y = POP0, times = TIME.VECTOR, parms = c(), func = diffEqs,
# n_history = 4e6, return_history = FALSE,step_max_n = 1e6)