chrissoria Claude commited on
Commit
6c8d007
·
0 Parent(s):

Initial commit: Three-party SIR model Shiny app

Browse files

- Main app.Rmd with interactive disease model simulation
- R/sir_model.R with three-group SIR model functions
- R/plot_utils.R with plotting helper functions
- Removed hardcoded API key for security

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (3) hide show
  1. R/plot_utils.R +28 -0
  2. R/sir_model.R +991 -0
  3. app.Rmd +0 -0
R/plot_utils.R ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ library(cowplot)
2
+ library(ggplot2)
3
+
4
+ #' Get legend from a ggplot object
5
+ #'
6
+ #' @param plot ggplot object
7
+ #' @param legend Optional legend identifier
8
+ #' @return Legend grob
9
+ get_legend_35 <- function(plot, legend = NULL) {
10
+ legends <- get_plot_component(plot, "guide-box", return_all = TRUE)
11
+
12
+ not_empty <- !vapply(legends, function(x) inherits(x, "zeroGrob"), logical(1))
13
+ legends <- legends[not_empty]
14
+
15
+ if (!is.null(legend)) {
16
+ pattern <- paste0("guide-box-", legend)
17
+ idx <- grep(pattern, names(legends))
18
+ if (length(idx) > 0) {
19
+ return(legends[[idx[1]]])
20
+ }
21
+ }
22
+
23
+ if (length(legends) > 0) {
24
+ return(legends[[1]])
25
+ } else {
26
+ return(NULL)
27
+ }
28
+ }
R/sir_model.R ADDED
@@ -0,0 +1,991 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ library(deSolve)
3
+ library(tidyverse)
4
+ library(reshape2)
5
+
6
+ # two group SIR model (a,b and c); each compartment is split into "Protected (P)" and "Unprotected (U)"
7
+ # groups have their own average contact rates
8
+ # contacts rates for groups can vary over time in response to number of deaths
9
+ # the way this is setup currently, it is simplest to equate protective behavior with mask usage
10
+ # both groups have a background rate of adopting protective behavior which can differ between groups
11
+ # protective behavior is protective of both transmission and infection
12
+ # fraction of the population in each group can vary
13
+ # homophily (degree to which members of a group mix within group versus outside their group) can vary;
14
+
15
+ ## IN THE THREE GROUP MODEL, I am thinking of
16
+ ## a -> democrats
17
+ ## b -> republicans
18
+ ## c -> independents
19
+ ##
20
+
21
+
22
+ ## given:
23
+ ## dbar_a - avg degree of group a
24
+ ## dbar_b - avg degree of group b
25
+ ## dbar_c - avg degree of group c
26
+ ## N_a - size of group a
27
+ ## N_b - size of group b
28
+ ## N_c - size of group c
29
+ ## h_a - homophily parameter for group a
30
+ ## h_b - homophily parameter for group b
31
+ ##
32
+ ## note that h_a, h_b corresponds more or less to
33
+ ## what Currarini et al call beta_a
34
+ ## the share of group a's contacts that are with other members
35
+ ## of group a are given by m_a^(1/beta_a)
36
+ ## h_a is in [1, \infty) and the bigger h_a gets, the
37
+ ## more homophily there is
38
+ ##
39
+ ## this implies that if we have a target value of h_a, then the value
40
+ ## of Currarini et al's beta_a implied by that is
41
+ ## h_a = m_a^(1/beta_a)
42
+ ## <=> beta_a = log(m_a) / log(h_a)
43
+ ##
44
+ ## homophily in group c is completely determined by
45
+ ## h_a, and h_b, so it cannot be varied independently
46
+
47
+
48
+ #for three groups contact matrix
49
+ avg_contact_matrix_3gp <- function(dbar_a, dbar_b, dbar_c,
50
+ N_a, N_b, N_c,
51
+ beta_a, beta_b) {
52
+ #h_a, h_b) {
53
+
54
+ # fraction of total degree in group a and group b
55
+ m_a <- (N_a * dbar_a) / ((N_a * dbar_a) + (N_b * dbar_b) + (N_c * dbar_c))
56
+ m_b <- (N_b * dbar_b) / ((N_a * dbar_a) + (N_b * dbar_b) + (N_c * dbar_c))
57
+ m_c <- (N_c * dbar_c) / ((N_a * dbar_a) + (N_b * dbar_b) + (N_c * dbar_c))
58
+
59
+ # this is the value of currarini et al's beta parameter
60
+ # implied by h_a
61
+ # we don't do anything with this directly, but it might be useful for
62
+ # debugging / substantive analysis down the line
63
+ #implied_beta_a <- log(m_a) / log(h_a)
64
+ #implied_beta_b <- log(m_b) / log(h_b)
65
+
66
+ q_aa <- m_a^(1/beta_a)
67
+ q_bb <- m_b^(1/beta_b)
68
+
69
+ #q_aa <- h_a
70
+ #q_bb <- h_b
71
+
72
+
73
+ ## for a's contacts w/ other groups, assume they are distributed
74
+ ## in proportion to the other groups' share of edges
75
+ ## (NB: this ignores homophily w/in other groups; could potentially improve this)
76
+ ## TODO - SHOULD REVISIT THIS
77
+ q_ab <- (1-q_aa) * (m_b / (m_b + m_c))
78
+ q_ac <- (1-q_aa) * (m_c / (m_b + m_c))
79
+
80
+ # implied by q_ab and q_ac
81
+ q_ba <- q_ab*(dbar_a*N_a)/(N_b*dbar_b)
82
+ # and then q_bc has to make them add up to 1
83
+ q_bc <- (1 - (q_ba + q_bb))
84
+
85
+ # implied by q_ac
86
+ q_ca <- q_ac*dbar_a*N_a/(N_c*dbar_c)
87
+
88
+ # ... and then I think q_cb will be implied by that
89
+ q_cb <- q_bc * (dbar_b*N_b)/(dbar_c*N_c)
90
+
91
+ # q_cc is residual
92
+ q_cc <- 1 - (q_ca + q_cb)
93
+
94
+ # implied beta_c
95
+ beta_c_implied <- log(m_c) / log(q_bb)
96
+
97
+ # q matrix
98
+ q_beta <- t(matrix(c(q_aa, q_ab, q_ac,
99
+ q_ba, q_bb, q_bc,
100
+ q_ca, q_cb, q_cc),
101
+ nrow=3))
102
+ ## this matrix has the avg number of contacts between groups
103
+ ## it should be symmetric
104
+ c_mat <- q_beta
105
+ c_mat[1,] <- c_mat[1,] * dbar_a
106
+ c_mat[2,] <- c_mat[2,] * dbar_b
107
+ c_mat[3,] <- c_mat[3,] * dbar_c
108
+
109
+ return(c_mat)
110
+
111
+ }
112
+
113
+
114
+ sir_three_group_pu <- function(## parameters related to popn
115
+ N0 = 10000000, # population size
116
+ frac_a = 0.33, # fraction of population in group A
117
+ frac_b = 0.33, # fraction of population in group B
118
+ ## parameters related to contacts
119
+ cmax = NA, # initial/max average number of contacts per day (specify only if its the same for both groups, otherwise NA)
120
+ cmax_a = 8, cmax_b = 8, cmax_c=8, # initial/max average number of contacts per day by group
121
+ cmin = NA, # min average contact
122
+ cmin_a = 3, cmin_b = 3, cmin_c=3,
123
+ beta_a=1, # homophily parameter for group a (beta_a = 1 means unbiased mixing; bigger values mean homophily)
124
+ beta_b=1, # homophily parameter for group b (beta_b = 1 means unbiased mixing; bigger values mean homophily)
125
+ #h_a=.5, # proportion of group A's total contact with members of their own group
126
+ #h_b=.5, # proportion of group A's total contact with members of their own group
127
+ ##
128
+ zeta = NA, # responsiveness of contact to deaths
129
+ zeta_a = 0.01, zeta_b = 0, zeta_c = 0.005,
130
+ trans_p = 0.05, # probability of transmission given contact (or susceptibility to infection given contact)
131
+ rho=1/10,# 1 / infectious period or recovery rate
132
+ mu = NA,# probability of dying following infection
133
+ mu_a = 0.01, mu_b = 0.01, mu_c = .01, # can let it differ between groups to crudely account for difference in age composition between groups
134
+ kappa=0.9, # scaling factor for probability of transmission given contact resulting from protective behavior; kappa=1 means no protection, kappa = 0 means perfect protection
135
+ phi = NA, # waning of protective behavior
136
+ phi_a = 0, phi_b=0, phi_c=0,
137
+ I0_a=1, I0_b=1, I0_c=1, #intial infected in each group
138
+ time = 500, # time steps for simulation
139
+ pi = NA, # background rate of adopting protective behavior
140
+ pi_a = 0.05, pi_b = 0.05, pi_c = 0.05,
141
+ ell = 1, # time window for considering deaths that influence adoption of protective behavior
142
+ vacc = NA, # vaccination rate (goes from S to R)
143
+ # in Roubenoff et al. we assume about 2 million daily doses are distributed which is ~ 0.6 % of the population per day
144
+ vacc_a = 0.006, vacc_b = 0.006, vacc_c = 0.006,
145
+ vstart = 365, # start of vaccination
146
+ gamma = 1/182.5, # wanning immunity
147
+
148
+ get_params=FALSE) {
149
+ # set following parameters to be the same for both groups unless specified otherwise
150
+
151
+ if(!is.na(mu)){
152
+ mu_a<-mu
153
+ mu_b<-mu
154
+ mu_c<-mu
155
+ }
156
+
157
+ if(!is.na(zeta)){
158
+ zeta_a<-zeta
159
+ zeta_b<-zeta
160
+ zeta_c<-zeta
161
+ }
162
+
163
+ if(!is.na(pi)){
164
+ pi_a<-pi
165
+ pi_b<-pi
166
+ pi_c<-pi
167
+ }
168
+
169
+ if(!is.na(phi)){
170
+ phi_a<-phi
171
+ phi_b<-phi
172
+ phi_c<-phi
173
+ }
174
+
175
+
176
+ if(!is.na(vacc)){
177
+ vacc_a<-vacc
178
+ vacc_b<-vacc
179
+ vacc_c<-vacc
180
+ }
181
+
182
+ if(!is.na(cmin)){
183
+ cmin_a<-cmin
184
+ cmin_b<-cmin
185
+ cmin_c<-cmin
186
+ }
187
+
188
+
189
+ if(!is.na(cmax)){
190
+ cmax_a<-cmax
191
+ cmax_b<-cmax
192
+ cmax_c<-cmax
193
+ }
194
+
195
+ N0_a = N0 * frac_a
196
+ N0_b = N0 * frac_b
197
+ N0_c = N0 * (1 - frac_a - frac_b)
198
+
199
+
200
+
201
+ params<-c("trans_p" = trans_p, # probability of transmission given contact
202
+ "beta_a"= beta_a, # homophily parameter for group a (beta_a = 1 means unbiased mixing; beta_a > 1 means homophily)
203
+ "beta_b"= beta_b, # homophily parameter for group b (beta_b = 1 means unbiased mixing; beta_b > 1 means homophily)
204
+ #"h_a"= h_a, # # proportion of group A's total contact with members of their own group (bounded by population size and total contacts in group B)
205
+ #"h_b"= h_b, # # proportion of group A's total contact with members of their own group (bounded by population size and total contacts in group B)
206
+ "cmax_a" = cmax_a, "cmax_b" = cmax_b, "cmax_c" = cmax_c, # upper and lower bounds for contacts
207
+ "zeta_a" = zeta_a, "zeta_b" = zeta_b, "zeta_c" = zeta_c, # responsiveness of contact to deaths
208
+ "pi_a" = pi_a, "pi_b" = pi_b, "pi_c" = pi_c, # background rate of adopting protective behavior
209
+ "kappa" = kappa, # scaling factor for probability of transmission given contact resulting from protective behavior; kappa=1 means no protection, kappa = 0 means perfect protection
210
+ "phi_a"=phi_a, "phi_b" = phi_b, "phi_c" = phi_c, # waning rate of protective behavior
211
+ "rho"=rho, # 1 / infectious period
212
+ "mu_a"=mu_a, "mu_b" = mu_b, "mu_c" = mu_c, # probability of dying following infection
213
+ "time"=time, # time steps in simulation
214
+ "I0_a"=I0_a, "I0_b"=I0_b, "I0_c"=I0_c, # starting number infected in each group
215
+ "N0_a" = N0_a, "N0_b"= N0_b, "N0_c"=N0_c, # population size in each group at time zero
216
+ "ell"=ell, # time window for considering deaths that influence adoption of protective behavior
217
+ "vacc_a"=vacc_a, "vacc_b"=vacc_b, "vacc_c"=vacc_c, # vaccination rate
218
+ "vstart" = vstart, # start of vaccination
219
+ "gamma" = gamma #waning immunity
220
+ ) # responsiveness to proportion of protected individuals for adopting protective behavior
221
+
222
+ state<-c(
223
+
224
+ #group a
225
+ ca <- cmax_a,
226
+ SUa<-(N0_a - I0_a),
227
+ SPa<-0,
228
+ IUa<-I0_a,
229
+ #IUaa<-0,
230
+ #IUab <- 0,
231
+ #IUac <- 0,
232
+ IPa<-0,
233
+ RUa<-0,
234
+ RPa<-0,
235
+ DUa<-0,
236
+ DPa<-0,
237
+ Casesa <- I0_a,
238
+
239
+ #group b
240
+ cb <- cmax_b,
241
+ SUb<-N0_b - I0_b,
242
+ SPb<-0,
243
+ IUb<-I0_b,
244
+ IPb<-0,
245
+ RUb<-0,
246
+ RPb<-0,
247
+ DUb<-0,
248
+ DPb<-0,
249
+ Casesb <- I0_b,
250
+
251
+ #group c
252
+ cc <- cmax_c,
253
+ SUc<-N0_c - I0_c,
254
+ SPc<-0,
255
+ IUc<-I0_c,
256
+ IPc<-0,
257
+ RUc<-0,
258
+ RPc<-0,
259
+ DUc<-0,
260
+ DPc<-0,
261
+ Casesc <- I0_c
262
+ )
263
+
264
+ names(state)<- c("ca","SUa", "SPa", "IUa", "IPa", "RUa", "RPa", "DUa", "DPa", "Casesa",
265
+ #"IUaa", "IUab", "IUac",
266
+ "cb","SUb", "SPb", "IUb", "IPb", "RUb", "RPb", "DUb", "DPb", "Casesb",
267
+ "cc","SUc", "SPc", "IUc", "IPc", "RUc", "RPc", "DUc", "DPc", "Casesc")
268
+
269
+ sir_up<-function(t, state, parameter){
270
+
271
+ with(as.list(c(parameter, state)), {
272
+
273
+
274
+ if(t<ell){
275
+ lag_ell<-rep(0, length(state))
276
+ }
277
+ else{
278
+ lag_ell<-lagvalue(t-ell) #provide access to past (lagged) values of all state variables; below we make sure we only look at D
279
+ }
280
+
281
+
282
+ if(t<vstart){
283
+ vacc_a <- 0
284
+ vacc_b <- 0
285
+ vacc_c <- 0
286
+ }
287
+ else{
288
+ vacc_a <- vacc_a
289
+ vacc_b <- vacc_b
290
+ vacc_c <- vacc_c
291
+ }
292
+
293
+ # for the transitions there are seven processes that are currently modeled:
294
+ # 1: transmission process (infection, recovery/death)
295
+ # 2: changes in contact rate for group A in response to deaths
296
+ # 3: adoption of protective behavior
297
+ # 5: waning of protective behavior
298
+ # 6: vaccination
299
+ # 7: waning of immunity
300
+
301
+
302
+
303
+ # recalculate population size (will change due to deaths)
304
+ N_a = SUa + IUa + RUa + SPa + IPa + RPa
305
+ N_b = SUb + IUb + RUb + SPb + IPb + RPb
306
+ N_c = SUc + IUc + RUc + SPc + IPc + RPc
307
+
308
+
309
+
310
+ # contact matrix
311
+ c_mat <- avg_contact_matrix_3gp(dbar_a = ca,
312
+ dbar_b = cb,
313
+ dbar_c = cc,
314
+ N_a = N_a,
315
+ N_b = N_b,
316
+ N_c = N_c,
317
+ beta_a = beta_a,
318
+ beta_b = beta_b)
319
+ #h_a = h_a,
320
+ #h_b = h_b)
321
+
322
+ ## TODO - should we throw an error if any implied contacts are negative?
323
+
324
+
325
+ ################################
326
+ ## transitions for group a
327
+ ################################
328
+
329
+ # Force of infection
330
+ lambda_a = trans_p*(c_mat[1,1]*(IUa/N_a + kappa*IPa/N_a) + c_mat[1,2]*(IUb/N_b + kappa*IPb/N_b) + c_mat[1,3]*(IUc/N_c + kappa*IPc/N_c))
331
+ #below are force of infection caused by specific groups
332
+ lambda_aa = trans_p*(c_mat[1,1]*(IUa/N_a + kappa*IPa/N_a))
333
+ lambda_ab = trans_p*(c_mat[1,2]*(IUb/N_b + kappa*IPb/N_b))
334
+ lambda_ac = trans_p*(c_mat[1,3]*(IUc/N_c + kappa*IPc/N_c))
335
+
336
+ # change in contacts in response to deaths occuring over a time window defined by ell (constrained to maximum/minimum observed values)
337
+
338
+ deaths_t <- rho*mu_a*IUa + rho*mu_a*IPa + rho*mu_b*IUb + rho*mu_b*IPb + rho*mu_c*IUc + rho*mu_c*IPc
339
+
340
+
341
+ deaths_tminusell <- rho*mu_a*(lag_ell[which(names(state) == "IUa")]) +
342
+ rho*mu_a*(lag_ell[which(names(state) == "IPa")]) +
343
+ rho*mu_b*(lag_ell[which(names(state) == "IUb")]) +
344
+ rho*mu_b*(lag_ell[which(names(state) == "IPb")]) +
345
+ rho*mu_c*(lag_ell[which(names(state) == "IUc")]) +
346
+ rho*mu_c*(lag_ell[which(names(state) == "IPc")])
347
+
348
+ cum_deaths_ell <- DUa - lag_ell[which(names(state) == "DUa")] +
349
+ DPa - lag_ell[which(names(state) == "DPa")] +
350
+ DUb - lag_ell[which(names(state) == "DUb")] +
351
+ DPb - lag_ell[which(names(state) == "DPb")] +
352
+ DUc - lag_ell[which(names(state) == "DUc")] +
353
+ DPc - lag_ell[which(names(state) == "DPc")]
354
+
355
+ dca <- (-zeta_a)*(cmax_a - cmin_a)*(deaths_t - deaths_tminusell)*exp(-zeta_a*(cum_deaths_ell))
356
+
357
+ dSUa <- -SUa*lambda_a - #transmission process
358
+ pi_a * SUa + # adoption of protective behavior
359
+ phi_a * SPa + #waning of protective behavior
360
+ gamma * RUa - #waning immunity
361
+ vacc_a * SUa
362
+
363
+ dSPa <- -SPa*kappa*lambda_a +
364
+ pi_a * SUa -
365
+ phi_a * SPa +
366
+ gamma * RPa -
367
+ vacc_a * SPa
368
+
369
+ dIUa <- SUa*lambda_a -
370
+ pi_a*IUa +
371
+ phi_a * IPa -
372
+ (rho) * IUa
373
+
374
+ #dIUaa <- SUa*lambda_aa - pi_a*IUa + phi_a*IPa - (rho)*IUa
375
+
376
+ #dIUab <- SUa*lambda_ab - pi_a*IUa + phi_a*IPa - (rho)*IUa
377
+
378
+ #dIUac <- SUa*lambda_ab - pi_a*IUa + phi_a*IPa - (rho)*IUa
379
+
380
+ dIPa <- SPa*kappa*lambda_a +
381
+ pi_a * IUa -
382
+ phi_a * IPa -
383
+ (rho) * IPa
384
+
385
+ dRUa <- rho * (1-mu_a) * IUa -
386
+ pi_a * RUa +
387
+ phi_a * RPa -
388
+ gamma * RUa +
389
+ vacc_a * SUa
390
+
391
+ dRPa <- rho * (1-mu_a) * IPa +
392
+ pi_a * RUa -
393
+ phi_a * RPa -
394
+ gamma * RPa +
395
+ vacc_a * SPa
396
+
397
+ dDUa <- rho*mu_a*IUa
398
+
399
+ dDPa <- rho*mu_a*IPa
400
+
401
+ dCasesa <- SUa*lambda_a + SPa*kappa*lambda_a
402
+
403
+ ################################
404
+ ## transitions for group b
405
+ ################################
406
+
407
+ # Force of infection
408
+ lambda_b = trans_p*(c_mat[2,1]*(IUa/N_a + kappa*IPa/N_a) + c_mat[2,2]*(IUb/N_b + kappa*IPb/N_b) + c_mat[2,3]*(IUc/N_c + kappa*IPc/N_c) )
409
+
410
+
411
+ # change in contacts in response to deaths (constrained to maximum/minimum observed values)
412
+ dcb <- (-zeta_b)*(cmax_b - cmin_b)*(deaths_t - deaths_tminusell)*exp(-zeta_b*(cum_deaths_ell))
413
+
414
+
415
+ dSUb <- -SUb*lambda_b -
416
+ pi_b * SUb +
417
+ phi_b * SPb +
418
+ gamma * RUb -
419
+ vacc_b * SUb
420
+
421
+ dSPb <- -SPb*kappa*lambda_b +
422
+ pi_b * SUb -
423
+ phi_b * SPb +
424
+ gamma * RPb -
425
+ vacc_b * SPb
426
+
427
+ dIUb <- SUb*lambda_b -
428
+ pi_b * IUb +
429
+ phi_b * IPb -
430
+ (rho) * IUb
431
+
432
+ dIPb <- SPb*kappa*lambda_b +
433
+ pi_b * IUb -
434
+ phi_b * IPb -
435
+ (rho) * IPb
436
+
437
+ dRUb <- (1-mu_b) * rho * IUb -
438
+ pi_b * RUb +
439
+ phi_b * RPb -
440
+ gamma * RUb +
441
+ vacc_b * SUb
442
+
443
+ dRPb <- (1-mu_b) * rho * IPb +
444
+ pi_b * RUb -
445
+ phi_b * RPb -
446
+ gamma * RPb +
447
+ vacc_b * SPb
448
+
449
+ dDUb <- rho*mu_b*IUb
450
+
451
+ dDPb <- rho*mu_b*IPb
452
+
453
+ dCasesb <- SUb*lambda_b + SPb*kappa*lambda_b
454
+
455
+ ################################
456
+ ## transitions for group c
457
+ ################################
458
+
459
+ # Force of infection
460
+ lambda_c = trans_p*(c_mat[3,1]*(IUa/N_a + kappa*IPa/N_a) + c_mat[3,2]*(IUb/N_b + kappa*IPb/N_b) + c_mat[3,3]*(IUc/N_c + kappa*IPc/N_c) )
461
+
462
+
463
+ # change in contacts in response to deaths (constrained to maximum/minimum observed values)
464
+ dcc <- (-zeta_c)*(cmax_c - cmin_c)*(deaths_t - deaths_tminusell)*exp(-zeta_c*(cum_deaths_ell))
465
+
466
+
467
+ dSUc <- -SUc*lambda_c -
468
+ pi_c * SUc +
469
+ phi_c * SPc +
470
+ gamma * RUc -
471
+ vacc_c * SUc
472
+
473
+ dSPc <- -SPc*kappa*lambda_c +
474
+ pi_c * SUc -
475
+ phi_c * SPc +
476
+ gamma * RPc -
477
+ vacc_c * SPc
478
+
479
+ dIUc <- SUc*lambda_c -
480
+ pi_c * IUc +
481
+ phi_c * IPc -
482
+ (rho) * IUc
483
+
484
+ dIPc <- SPc*kappa*lambda_c +
485
+ pi_c * IUc -
486
+ phi_c * IPc -
487
+ (rho) * IPc
488
+
489
+ dRUc <- (1-mu_c) * rho * IUc -
490
+ pi_c * RUc +
491
+ phi_c * RPc -
492
+ gamma * RUc +
493
+ vacc_c * SUc
494
+
495
+ dRPc <- (1-mu_c) * rho * IPc +
496
+ pi_c * RUc -
497
+ phi_c * RPc -
498
+ gamma * RPc +
499
+ vacc_c * SPc
500
+
501
+ dDUc <- rho*mu_c*IUc
502
+
503
+ dDPc <- rho*mu_c*IPc
504
+
505
+ dCasesc <- SUc*lambda_c + SPc*kappa*lambda_c
506
+
507
+
508
+
509
+ return(list(c(dca, dSUa, dSPa, dIUa, dIPa, dRUa, dRPa, dDUa, dDPa, dCasesa,
510
+ #dIUaa, dIUab, dIUac,
511
+ dcb, dSUb, dSPb, dIUb, dIPb, dRUb, dRPb, dDUb, dDPb, dCasesb,
512
+ dcc, dSUc, dSPc, dIUc, dIPc, dRUc, dRPc, dDUc, dDPc, dCasesc
513
+ )))
514
+ })
515
+ }
516
+
517
+ times<-seq(from=0, to=time, by=1)
518
+
519
+ as.data.frame(dede(state, times, sir_up, params))->sim
520
+
521
+ if(get_params){
522
+ return(list(sim=sim, params=params))
523
+ }
524
+
525
+ else{
526
+ return(sim)
527
+ }
528
+
529
+
530
+
531
+ }
532
+
533
+ function_in_string <- "You are in expert in these functions being run in a shiny app:
534
+
535
+ avg_contact_matrix_3gp <- function(dbar_a, dbar_b, dbar_c,
536
+ N_a, N_b, N_c,
537
+ beta_a, beta_b) {
538
+ #h_a, h_b) {
539
+
540
+ # fraction of total degree in group a and group b
541
+ m_a <- (N_a * dbar_a) / ((N_a * dbar_a) + (N_b * dbar_b) + (N_c * dbar_c))
542
+ m_b <- (N_b * dbar_b) / ((N_a * dbar_a) + (N_b * dbar_b) + (N_c * dbar_c))
543
+ m_c <- (N_c * dbar_c) / ((N_a * dbar_a) + (N_b * dbar_b) + (N_c * dbar_c))
544
+
545
+ q_aa <- m_a^(1/beta_a)
546
+ q_bb <- m_b^(1/beta_b)
547
+
548
+ #q_aa <- h_a
549
+ #q_bb <- h_b
550
+
551
+
552
+ ## for a's contacts w/ other groups, assume they are distributed
553
+ ## in proportion to the other groups' share of edges
554
+
555
+ q_ab <- (1-q_aa) * (m_b / (m_b + m_c))
556
+ q_ac <- (1-q_aa) * (m_c / (m_b + m_c))
557
+
558
+ # implied by q_ab and q_ac
559
+ q_ba <- q_ab*(dbar_a*N_a)/(N_b*dbar_b)
560
+ # and then q_bc has to make them add up to 1
561
+ q_bc <- (1 - (q_ba + q_bb))
562
+
563
+ q_ca <- q_ac*dbar_a*N_a/(N_c*dbar_c)
564
+
565
+ q_cb <- q_bc * (dbar_b*N_b)/(dbar_c*N_c)
566
+
567
+ # q_cc is residual
568
+ q_cc <- 1 - (q_ca + q_cb)
569
+
570
+ # implied beta_c
571
+ beta_c_implied <- log(m_c) / log(q_bb)
572
+
573
+ # q matrix
574
+ q_beta <- t(matrix(c(q_aa, q_ab, q_ac,
575
+ q_ba, q_bb, q_bc,
576
+ q_ca, q_cb, q_cc),
577
+ nrow=3))
578
+ ## this matrix has the avg number of contacts between groups
579
+
580
+ c_mat <- q_beta
581
+ c_mat[1,] <- c_mat[1,] * dbar_a
582
+ c_mat[2,] <- c_mat[2,] * dbar_b
583
+ c_mat[3,] <- c_mat[3,] * dbar_c
584
+
585
+ return(c_mat)
586
+
587
+ }
588
+
589
+
590
+ sir_three_group_pu <- function(## parameters related to popn
591
+ N0 = 10000000, # population size
592
+ frac_a = 0.33, # fraction of population in group A
593
+ frac_b = 0.33, # fraction of population in group B
594
+ ## parameters related to contacts
595
+ cmax = NA, # initial/max average number of contacts per day (specify only if its the same for both groups, otherwise NA)
596
+ cmax_a = 8, cmax_b = 8, cmax_c=8, # initial/max average number of contacts per day by group
597
+ cmin = NA, # min average contact
598
+ cmin_a = 3, cmin_b = 3, cmin_c=3,
599
+ beta_a=1, # homophily parameter for group a (beta_a = 1 means unbiased mixing; bigger values mean homophily)
600
+ beta_b=1, # homophily parameter for group b (beta_b = 1 means unbiased mixing; bigger values mean homophily)
601
+ #h_a=.5, # proportion of group A's total contact with members of their own group
602
+ #h_b=.5, # proportion of group A's total contact with members of their own group
603
+ ##
604
+ zeta = NA, # responsiveness of contact to deaths
605
+ zeta_a = 0.01, zeta_b = 0, zeta_c = 0.005,
606
+ trans_p = 0.05, # probability of transmission given contact (or susceptibility to infection given contact)
607
+ rho=1/10,# 1 / infectious period or recovery rate
608
+ mu = NA,# probability of dying following infection
609
+ mu_a = 0.01, mu_b = 0.01, mu_c = .01, # can let it differ between groups to crudely account for difference in age composition between groups
610
+ kappa=0.9, # scaling factor for probability of transmission given contact resulting from protective behavior; kappa=1 means no protection, kappa = 0 means perfect protection
611
+ phi = NA, # waning of protective behavior
612
+ phi_a = 0, phi_b=0, phi_c=0,
613
+ I0_a=1, I0_b=1, I0_c=1, #intial infected in each group
614
+ time = 500, # time steps for simulation
615
+ pi = NA, # background rate of adopting protective behavior
616
+ pi_a = 0.05, pi_b = 0.05, pi_c = 0.05,
617
+ ell = 1, # time window for considering deaths that influence adoption of protective behavior
618
+ vacc = NA, # vaccination rate (goes from S to R)
619
+ # in Roubenoff et al. we assume about 2 million daily doses are distributed which is ~ 0.6 % of the population per day
620
+ vacc_a = 0.006, vacc_b = 0.006, vacc_c = 0.006,
621
+ vstart = 365, # start of vaccination
622
+ gamma = 1/182.5, # wanning immunity
623
+
624
+ get_params=FALSE) {
625
+ # set following parameters to be the same for both groups unless specified otherwise
626
+
627
+ if(!is.na(mu)){
628
+ mu_a<-mu
629
+ mu_b<-mu
630
+ mu_c<-mu
631
+ }
632
+
633
+ if(!is.na(zeta)){
634
+ zeta_a<-zeta
635
+ zeta_b<-zeta
636
+ zeta_c<-zeta
637
+ }
638
+
639
+ if(!is.na(pi)){
640
+ pi_a<-pi
641
+ pi_b<-pi
642
+ pi_c<-pi
643
+ }
644
+
645
+ if(!is.na(phi)){
646
+ phi_a<-phi
647
+ phi_b<-phi
648
+ phi_c<-phi
649
+ }
650
+
651
+
652
+ if(!is.na(vacc)){
653
+ vacc_a<-vacc
654
+ vacc_b<-vacc
655
+ vacc_c<-vacc
656
+ }
657
+
658
+ if(!is.na(cmin)){
659
+ cmin_a<-cmin
660
+ cmin_b<-cmin
661
+ cmin_c<-cmin
662
+ }
663
+
664
+
665
+ if(!is.na(cmax)){
666
+ cmax_a<-cmax
667
+ cmax_b<-cmax
668
+ cmax_c<-cmax
669
+ }
670
+
671
+ N0_a = N0 * frac_a
672
+ N0_b = N0 * frac_b
673
+ N0_c = N0 * (1 - frac_a - frac_b)
674
+
675
+
676
+
677
+ params<-c(‘trans_p’ = trans_p, # probability of transmission given contact
678
+ ‘beta_a’= beta_a, # homophily parameter for group a (beta_a = 1 means unbiased mixing; beta_a > 1 means homophily)
679
+ ‘beta_b’= beta_b, # homophily parameter for group b (beta_b = 1 means unbiased mixing; beta_b > 1 means homophily)
680
+ #’h_a’= h_a, # # proportion of group A's total contact with members of their own group (bounded by population size and total contacts in group B)
681
+ #’h_b’= h_b, # # proportion of group A's total contact with members of their own group (bounded by population size and total contacts in group B)
682
+ ‘cmax_a’ = cmax_a, ‘cmax_b’ = cmax_b, ‘cmax_c’ = cmax_c, # upper and lower bounds for contacts
683
+ ‘zeta_a’ = zeta_a, ‘zeta_b’ = zeta_b, ‘zeta_c’ = zeta_c, # responsiveness of contact to deaths
684
+ ‘pi_a’ = pi_a, ‘pi_b’ = pi_b, ‘pi_c’ = pi_c, # background rate of adopting protective behavior
685
+ ‘kappa’ = kappa, # scaling factor for probability of transmission given contact resulting from protective behavior; kappa=1 means no protection, kappa = 0 means perfect protection
686
+ ‘phi_a’=phi_a, ‘phi_b’ = phi_b, ‘phi_c’ = phi_c, # waning rate of protective behavior
687
+ ‘rho’=rho, # 1 / infectious period
688
+ ‘mu_a’=mu_a, ‘mu_b’ = mu_b, ‘mu_c’ = mu_c, # probability of dying following infection
689
+ ‘time’=time, # time steps in simulation
690
+ ‘I0_a’=I0_a, ‘I0_b’=I0_b, ‘I0_c’=I0_c, # starting number infected in each group
691
+ ‘N0_a’ = N0_a, ‘N0_b’= N0_b, ‘N0_c’=N0_c, # population size in each group at time zero
692
+ ‘ell’=ell, # time window for considering deaths that influence adoption of protective behavior
693
+ ‘vacc_a’=vacc_a, ‘vacc_b’=vacc_b, ‘vacc_c’=vacc_c, # vaccination rate
694
+ ‘vstart’ = vstart, # start of vaccination
695
+ ‘gamma’ = gamma #waning immunity
696
+ ) # responsiveness to proportion of protected individuals for adopting protective behavior
697
+
698
+ state<-c(
699
+
700
+ #group a
701
+ ca <- cmax_a,
702
+ SUa<-(N0_a - I0_a),
703
+ SPa<-0,
704
+ IUa<-I0_a,
705
+ IPa<-0,
706
+ RUa<-0,
707
+ RPa<-0,
708
+ DUa<-0,
709
+ DPa<-0,
710
+
711
+ #group b
712
+ cb <- cmax_b,
713
+ SUb<-N0_b - I0_b,
714
+ SPb<-0,
715
+ IUb<-I0_b,
716
+ IPb<-0,
717
+ RUb<-0,
718
+ RPb<-0,
719
+ DUb<-0,
720
+ DPb<-0,
721
+
722
+ #group c
723
+ cc <- cmax_c,
724
+ SUc<-N0_c - I0_c,
725
+ SPc<-0,
726
+ IUc<-I0_c,
727
+ IPc<-0,
728
+ RUc<-0,
729
+ RPc<-0,
730
+ DUc<-0,
731
+ DPc<-0
732
+ )
733
+
734
+ names(state)<- c(‘ca’,’SUa’, ‘SPa’, ‘IUa’, ‘IPa’, ‘RUa’, ‘RPa’, ‘DUa’, ‘DPa’,
735
+ ‘cb’,’SUb’, ‘SPb’, ‘IUb’, ‘IPb’, ‘RUb’, ‘RPb’, ‘DUb’, ‘DPb’,
736
+ ‘cc’,’SUc’, ‘SPc’, ‘IUc’, ‘IPc’, ‘RUc’, ‘RPc’, ‘DUc’, ‘DPc’)
737
+
738
+ sir_up<-function(t, state, parameter){
739
+
740
+ with(as.list(c(parameter, state)), {
741
+
742
+
743
+ if(t<ell){
744
+ lag_ell<-rep(0, length(state))
745
+ }
746
+ else{
747
+ lag_ell<-lagvalue(t-ell) #provide access to past (lagged) values of all state variables; below we make sure we only look at D
748
+ }
749
+
750
+
751
+ if(t<vstart){
752
+ vacc_a <- 0
753
+ vacc_b <- 0
754
+ vacc_c <- 0
755
+ }
756
+ else{
757
+ vacc_a <- vacc_a
758
+ vacc_b <- vacc_b
759
+ vacc_c <- vacc_c
760
+ }
761
+
762
+ # for the transitions there are seven processes that are currently modeled:
763
+ # 1: transmission process (infection, recovery/death)
764
+ # 2: changes in contact rate for group A in response to deaths
765
+ # 3: adoption of protective behavior
766
+ # 5: waning of protective behavior
767
+ # 6: vaccination
768
+ # 7: waning of immunity
769
+
770
+
771
+
772
+ # recalculate population size (will change due to deaths)
773
+ N_a = SUa + IUa + RUa + SPa + IPa + RPa
774
+ N_b = SUb + IUb + RUb + SPb + IPb + RPb
775
+ N_c = SUc + IUc + RUc + SPc + IPc + RPc
776
+
777
+
778
+
779
+ # contact matrix
780
+ c_mat <- avg_contact_matrix_3gp(dbar_a = ca,
781
+ dbar_b = cb,
782
+ dbar_c = cc,
783
+ N_a = N_a,
784
+ N_b = N_b,
785
+ N_c = N_c,
786
+ beta_a = beta_a,
787
+ beta_b = beta_b)
788
+ #h_a = h_a,
789
+ #h_b = h_b)
790
+
791
+ ## TODO - should we throw an error if any implied contacts are negative?
792
+
793
+
794
+ ################################
795
+ ## transitions for group a
796
+ ################################
797
+
798
+ # Force of infection
799
+ lambda_a = trans_p*(c_mat[1,1]*(IUa/N_a + kappa*IPa/N_a) + c_mat[1,2]*(IUb/N_b + kappa*IPb/N_b) + c_mat[1,3]*(IUc/N_c + kappa*IPc/N_c))
800
+
801
+
802
+ # change in contacts in response to deaths occuring over a time window defined by ell (constrained to maximum/minimum observed values)
803
+
804
+ deaths_t <- rho*mu_a*IUa + rho*mu_a*IPa + rho*mu_b*IUb + rho*mu_b*IPb + rho*mu_c*IUc + rho*mu_c*IPc
805
+
806
+
807
+ deaths_tminusell <- rho*mu_a*(lag_ell[which(names(state) == ‘IUa’)]) +
808
+ rho*mu_a*(lag_ell[which(names(state) == ‘IPa’)]) +
809
+ rho*mu_b*(lag_ell[which(names(state) == ‘IUb’)]) +
810
+ rho*mu_b*(lag_ell[which(names(state) == ‘IPb’)]) +
811
+ rho*mu_c*(lag_ell[which(names(state) == ‘IUc’)]) +
812
+ rho*mu_c*(lag_ell[which(names(state) == ‘IPc’)])
813
+
814
+ cum_deaths_ell <- DUa - lag_ell[which(names(state) == ‘DUa’)] +
815
+ DPa - lag_ell[which(names(state) == ‘DPa’)] +
816
+ DUb - lag_ell[which(names(state) == ‘DUb’)] +
817
+ DPb - lag_ell[which(names(state) == ‘DPb’)] +
818
+ DUc - lag_ell[which(names(state) == ‘DUc’)] +
819
+ DPc - lag_ell[which(names(state) == ‘DPc’)]
820
+
821
+ dca <- (-zeta_a)*(cmax_a - cmin_a)*(deaths_t - deaths_tminusell)*exp(-zeta_a*(cum_deaths_ell))
822
+
823
+ dSUa <- -SUa*lambda_a - #transmission process
824
+ pi_a * SUa + # adoption of protective behavior
825
+ phi_a * SPa + #waning of protective behavior
826
+ gamma * RUa - #waning immunity
827
+ vacc_a * SUa
828
+
829
+ dSPa <- -SPa*kappa*lambda_a +
830
+ pi_a * SUa -
831
+ phi_a * SPa +
832
+ gamma * RPa -
833
+ vacc_a * SPa
834
+
835
+ dIUa <- SUa*lambda_a -
836
+ pi_a*IUa +
837
+ phi_a * IPa -
838
+ (rho) * IUa
839
+
840
+ dIPa <- SPa*kappa*lambda_a +
841
+ pi_a * IUa -
842
+ phi_a * IPa -
843
+ (rho) * IPa
844
+
845
+ dRUa <- rho * (1-mu_a) * IUa -
846
+ pi_a * RUa +
847
+ phi_a * RPa -
848
+ gamma * RUa +
849
+ vacc_a * SUa
850
+
851
+ dRPa <- rho * (1-mu_a) * IPa +
852
+ pi_a * RUa -
853
+ phi_a * RPa -
854
+ gamma * RPa +
855
+ vacc_a * SPa
856
+
857
+ dDUa <- rho*mu_a*IUa
858
+
859
+ dDPa <- rho*mu_a*IPa
860
+
861
+
862
+ ################################
863
+ ## transitions for group b
864
+ ################################
865
+
866
+ # Force of infection
867
+ lambda_b = trans_p*(c_mat[2,1]*(IUa/N_a + kappa*IPa/N_a) + c_mat[2,2]*(IUb/N_b + kappa*IPb/N_b) + c_mat[2,3]*(IUc/N_c + kappa*IPc/N_c) )
868
+
869
+
870
+ # change in contacts in response to deaths (constrained to maximum/minimum observed values)
871
+ dcb <- (-zeta_b)*(cmax_b - cmin_b)*(deaths_t - deaths_tminusell)*exp(-zeta_b*(cum_deaths_ell))
872
+
873
+
874
+ dSUb <- -SUb*lambda_b -
875
+ pi_b * SUb +
876
+ phi_b * SPb +
877
+ gamma * RUb -
878
+ vacc_b * SUb
879
+
880
+ dSPb <- -SPb*kappa*lambda_b +
881
+ pi_b * SUb -
882
+ phi_b * SPb +
883
+ gamma * RPb -
884
+ vacc_b * SPb
885
+
886
+ dIUb <- SUb*lambda_b -
887
+ pi_b * IUb +
888
+ phi_b * IPb -
889
+ (rho) * IUb
890
+
891
+ dIPb <- SPb*kappa*lambda_b +
892
+ pi_b * IUb -
893
+ phi_b * IPb -
894
+ (rho) * IPb
895
+
896
+ dRUb <- (1-mu_b) * rho * IUb -
897
+ pi_b * RUb +
898
+ phi_b * RPb -
899
+ gamma * RUb +
900
+ vacc_b * SUb
901
+
902
+ dRPb <- (1-mu_b) * rho * IPb +
903
+ pi_b * RUb -
904
+ phi_b * RPb -
905
+ gamma * RPb +
906
+ vacc_b * SPb
907
+
908
+ dDUb <- rho*mu_b*IUb
909
+
910
+ dDPb <- rho*mu_b*IPb
911
+
912
+
913
+ ################################
914
+ ## transitions for group c
915
+ ################################
916
+
917
+ # Force of infection
918
+ lambda_c = trans_p*(c_mat[3,1]*(IUa/N_a + kappa*IPa/N_a) + c_mat[3,2]*(IUb/N_b + kappa*IPb/N_b) + c_mat[3,3]*(IUc/N_c + kappa*IPc/N_c) )
919
+
920
+
921
+ # change in contacts in response to deaths (constrained to maximum/minimum observed values)
922
+ dcc <- (-zeta_c)*(cmax_c - cmin_c)*(deaths_t - deaths_tminusell)*exp(-zeta_c*(cum_deaths_ell))
923
+
924
+
925
+ dSUc <- -SUc*lambda_c -
926
+ pi_c * SUc +
927
+ phi_c * SPc +
928
+ gamma * RUc -
929
+ vacc_c * SUc
930
+
931
+ dSPc <- -SPc*kappa*lambda_c +
932
+ pi_c * SUc -
933
+ phi_c * SPc +
934
+ gamma * RPc -
935
+ vacc_c * SPc
936
+
937
+ dIUc <- SUc*lambda_c -
938
+ pi_c * IUc +
939
+ phi_c * IPc -
940
+ (rho) * IUc
941
+
942
+ dIPc <- SPc*kappa*lambda_c +
943
+ pi_c * IUc -
944
+ phi_c * IPc -
945
+ (rho) * IPc
946
+
947
+ dRUc <- (1-mu_c) * rho * IUc -
948
+ pi_c * RUc +
949
+ phi_c * RPc -
950
+ gamma * RUc +
951
+ vacc_c * SUc
952
+
953
+ dRPc <- (1-mu_c) * rho * IPc +
954
+ pi_c * RUc -
955
+ phi_c * RPc -
956
+ gamma * RPc +
957
+ vacc_c * SPc
958
+
959
+ dDUc <- rho*mu_c*IUc
960
+
961
+ dDPc <- rho*mu_c*IPc
962
+
963
+
964
+
965
+
966
+ return(list(c(dca, dSUa, dSPa, dIUa, dIPa, dRUa, dRPa, dDUa, dDPa,
967
+ dcb, dSUb, dSPb, dIUb, dIPb, dRUb, dRPb, dDUb, dDPb,
968
+ dcc, dSUc, dSPc, dIUc, dIPc, dRUc, dRPc, dDUc, dDPc
969
+ )))
970
+ })
971
+ }
972
+
973
+ times<-seq(from=0, to=time, by=1)
974
+
975
+ as.data.frame(dede(state, times, sir_up, params))->sim
976
+
977
+ if(get_params){
978
+ return(list(sim=sim, params=params))
979
+ }
980
+
981
+ else{
982
+ return(sim)
983
+ }
984
+
985
+
986
+
987
+ }
988
+ "
989
+
990
+ function_in_string <- gsub("\\s+", " ", function_in_string)
991
+ function_in_string <- trimws(function_in_string)
app.Rmd ADDED
The diff for this file is too large to render. See raw diff