forked from Como-DTC-Collaboration/comomodels-explore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
1131 lines (977 loc) · 37.8 KB
/
server.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# The server definition of a shiny app object.
#
# packages required by comomodels
library(tidyverse) # tidyselect >1.1.1
library(deSolve)
library(ggplot2)
library(reshape2)
library(magrittr)
# packages for drawing model diagram
library(DiagrammeR)
# packages for building interactive plots
library(plotly)
# packages for building shiny app
library(shiny)
library(miniUI)
library(shinydashboard)
library(shinyjs)
# core package
library(comomodels)
server <- function(input, output, session) {
rv <- reactiveValues()
#%%%%%%%%%
# SEIRD
#%%%%%%%%%
# reactive buttons for modifying transmission params
# observeEvent(input$control.params.beta, {
# shinyjs::toggle(id="seird.params.beta")
# })
# model diagram
output$model_flowchart <- renderGrViz(
DiagrammeR::grViz("
digraph seird {
# initiate graph
graph [layout = dot, rankdir = LR]
# global node settings
node [shape = box, fontname = Helvetica, fixedsize = true,
style = filled, fillcolor = lightgray, width = 0.6]
S;E;I;R;D
node [shape = circle, fillcolor = lightblue, width = 0.3]
beta[label = <β>];
kappa[label = <κ>];
mu[label = <μ>];
gamma[label = <γ>];
# edge definitions with the node IDs
edge [arrowhead=none]
S -> beta
E -> kappa
I -> {mu gamma}
edge [arrowhead=vee]
beta -> E
kappa -> I
mu -> D
gamma -> R
}")
)
observe({
req(input$model_flowchart_click)
nodeid <- input$model_flowchart_click$id[[1]]
nodeid.int <- strsplit(nodeid, "node")[[1]][2] %>% as.integer()
nodeval <- input$model_flowchart_click$nodeValues
desc <- c("Susceptible", "Exposed", "Infected", "Recovered (Removed)", "Mortality",
"the rate at which an infected individual exposes susceptible",
"the rate of progression from exposed to infectious (reciprocal of the incubation period)",
"the rate of disease-caused mortality of the infected",
"the rate of removal (i.e. the recovery rate of the infected)")
trans.params <- c("beta", "kappa", "mu", "gamma")
output$SEIRD.param.desc <- renderText({paste0(nodeval[[length(nodeval)]], ": ", desc[nodeid.int])})
if(nodeid.int %in% seq(6, 9)){
shinyjs::toggle(id=paste0("seird.params.", trans.params[nodeid.int - 5]))
}
})
# ODE system
output$dS <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}S}{\\text{d}t} = -S \\beta I$$"))
})
output$dE <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}E}{\\text{d}t} = S \\beta I - \\kappa E$$"))
})
output$dI <- renderUI({
withMathJax(
helpText("$$\\frac{dI}{dt} = \\kappa E - (\\gamma + \\mu)I$$"))
})
output$dR <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}R}{\\text{d}t} = \\gamma I$$"))
})
output$dD <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}D}{\\text{d}t} = \\mu I$$"))
})
# model initialization
model0 <- reactive({
beta <- input$beta
kappa <- input$kappa
gamma <- input$gamma
mu <- input$mu
# Test model
# set up test data:
# initial population (in fraction)
S <- 0.99
E <- 0.00
I <- 0.01
R <- 0.00
# 1. create the instance in class SEIRD
model <- SEIRD()
# 2. set up parameters and initial population
transmission_parameters(model) <- list(beta = beta, kappa = kappa, gamma = gamma, mu = mu)
initial_conditions(model) <- list(S0=S, E0=E, I0=I, R0=R)
# 3. run simulation
return(model)
})
# ODE simulation and plot
model0.output <- reactive({
input$goButton
model <- model0()
# 4. ode simulation (period: 2000 time points, most of the time will relax)
t <- seq(0, 2000, by = 1)
output <- run(model, t)
return(output)
})
# 5.1 plot states
output$SEIRD.states <- renderPlotly({
output <- model0.output()
model.plot <- ggplot(output$states, aes(x = time, y = value)) +
geom_line(aes(color = compartment)) +
scale_color_brewer(palette = "Dark2") +
labs(x = "time (days)", y = "Population proportion") +
theme(legend.position = "bottom", legend.title = element_blank())
# theme(text = element_text(size = 20))
# ggtitle(paste0("model: R0=", model@R0)) # Show R0 on the title
model.plot <- ggplotly(model.plot)#, dynamicTicks = TRUE, originalData = FALSE)
return(model.plot)
})
outputOptions(output, "SEIRD.states", suspendWhenHidden = FALSE)
# 5.2 plot changes
output$SEIRD.changes <- renderPlotly({
output <- model0.output()
model.plot <- ggplot(output$changes, aes(x = time, y = value, fill = compartment)) +
geom_bar(stat="identity", position = position_dodge()) +
labs(x = "time (days)", y = "Population proportion per day") +
theme(legend.position = "bottom", legend.title = element_blank()) #, text = element_text(size = 20)) +
scale_fill_brewer(palette = "Dark2")
model.plot <- ggplotly(model.plot)
return(model.plot)
})
outputOptions(output, "SEIRD.changes", suspendWhenHidden = FALSE)
# R0 calculation
output$R0 <- renderText({
# input$goButton
model <- model0()
# calculate R0
paste0("R0 = ", R0(model))
})
# Download csv of simulation result
output$download.SEIRD <- downloadHandler(
#output <- model0.output()
filename = function() {
paste0("SEIRD_simulation_results", ".csv")
},
content = function(file) {
output <- model0.output()
output.merge <- rbind(output$states %>% mutate(group_type="states"), output$changes %>% mutate(group_type="changes"))
write.table(output.merge, row.names=T, col.names=T, sep=",", quote=FALSE, file=file)
}
)
#%%%%%%%%%%%
# SEIaImIsRD
#%%%%%%%%%%%
# observeEvent(input$control.params.sc.beta, {
# shinyjs::toggle(id="sc.params.beta")
# })
# model diagram
output$sc_model_flowchart <- renderGrViz(
DiagrammeR::grViz("
digraph seiaimisrd {
# initiate graph
graph [layout = neato, rankdir = LR]
# global node settings
node [shape = box, fontname = Helvetica, fixedsize = true,
style = filled, fillcolor = lightgray, width = 0.6]
S;E;Ia;Im;Is;R;D
node [shape = circle, fillcolor = lightblue, width = 0.3]
beta[label = <β>];
kappa[label = <κ>];
eta[label = <η>];
mu[label = <μ>];
gamma[label = <γ>];
omega[label = <ω>]
# edge definitions with the node IDs
# edge definitions with the node IDs
edge [arrowhead=none]
S -> beta
E -> kappa
kappa -> eta
{Ia Im Is} -> mu
{Ia Im Is} -> gamma
R -> omega
edge [arrowhead=vee]
beta -> E
eta -> {Ia Im Is}
mu -> D
gamma -> R
omega -> S
}")
)
observe({
req(input$sc_model_flowchart_click)
nodeid <- input$sc_model_flowchart_click$id[1]
nodeid.int <- strsplit(nodeid, "node")[[1]][2] %>% as.integer()
nodeval <- input$sc_model_flowchart_click$nodeValues
desc <- c("Susceptible", "Exposed", "Infected (asymptomatic)", "Infected (mild)", "Infected (severe)",
"Recovered (Removed)", "Mortality",
"the rate at which an infected individual exposes susceptible", # beta
"the rate of progression from exposed to infectious (reciprocal of the incubation period)", # kappa
"the probability of exposed individuals moving into each of the infected groups", # eta
"the rate of disease-caused mortality of the infected", # mu
"the rate of removal (i.e. the recovery rate of the infected)", # gamma
"the rate at which recovered individuals become susceptible (i.e. the loss of immunity)") # omega
trans.params <- c("beta", "kappa", "p_symptom", "mu", "gamma", "omega")
# for Ia, Im, Is, the correct (entire) nodeval is the second item of the list
output$SEIaImIsRD.param.desc <- renderText({paste0(nodeval[[length(nodeval)]], ": ", desc[nodeid.int])})
if(nodeid.int %in% seq(8, 13)){
shinyjs::toggle(id=paste0("sc.params.", trans.params[nodeid.int - 7]))
}
})
# ODE system
output$sc.dS <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}S}{\\text{d}t} = -S \\sum_i{\\beta_iI_i} + \\omega R$$"))
})
output$sc.dE <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}E}{\\text{d}t} = S \\sum_i{\\beta_iI_i} - \\kappa E$$"))
})
output$sc.dI <- renderUI({
withMathJax(
helpText("$$\\frac{dI_i}{dt} = \\eta_i \\kappa E - (\\gamma_i + \\mu_i)I_i$$"))
})
output$sc.dR <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}R}{\\text{d}t} = - \\omega R + \\sum_i{\\gamma_iI_i}$$"))
})
output$sc.dD <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}D}{\\text{d}t} = \\sum_i{\\mu_i I_i}$$"))
})
# model initialization
model0.sc <- reactive({
# params
beta <- list(asymptomatic = input$sc.beta.ia, mild = input$sc.beta.im, severe = input$sc.beta.is)
kappa <- input$sc.kappa
eta_symptom <- list(mild = input$sc.p_symptom.im, severe = input$sc.p_symptom.is)
gamma <- list(asymptomatic = input$sc.gamma.ia, mild = input$sc.gamma.im, severe = input$sc.gamma.is)
mu <- list(asymptomatic = input$sc.mu.ia, mild = input$sc.mu.im, severe = input$sc.mu.is)
# Test model
# set up test data:
# initial population (in fraction)
S <- 0.99
E <- 0.01
I_asymptomatic <- 0.00
I_mild <- 0.00
I_severe <- 0.00
R <- 0.00
D <- 0.00
# 1. create the instance in class SEIaImIsRD
model <- SEIaImIsRD()
# 2. set up parameters and initial population
transmission_parameters(model) <- list(beta = beta, kappa = kappa, p_symptom = eta_symptom, gamma = gamma, mu = mu)
initial_conditions(model) <- list(S = S, E = E, I_asymptomatic = I_asymptomatic, I_mild = I_mild, I_severe = I_severe, R = R, D = D)
return(model)
})
# ODE simulation and plot
model0.sc.output <- reactive({
model <- model0.sc()
# 4. ode simulation (period: 2000 time points, most of the time will relax)
t <- seq(0, 2000, by = 1)
output <- run(model,t)
return(output)
})
# 5.1 plot states
output$SEIaImIsRD.states <- renderPlotly({
output <- model0.sc.output()
model.plot <- ggplot(output$states, aes_string(x = "time", y = "value")) +
geom_line(aes_string(colour = "compartment")) +
scale_color_brewer(palette = "Dark2") +
theme(legend.position = "bottom", legend.title = element_blank()) +
labs(x = "time (days)", y = "fraction of the population")
# ggtitle(paste0("model: R0=", model@R0)) # Show R0 on the title
ggplotly(p=model.plot)#, dynamicTicks = TRUE, originalData = FALSE)
model.plot
})
outputOptions(output, "SEIaImIsRD.states", suspendWhenHidden = FALSE)
# 5.2 plot changes
output$SEIaImIsRD.changes <- renderPlotly({
output <- model0.sc.output()
model.plot <- ggplot(output$changes, aes_string(x = "time", y = "value", fill = "compartment")) +
geom_bar(stat="identity", position = position_dodge()) +
scale_fill_brewer(palette = "Dark2") +
theme(legend.position = "bottom", legend.title = element_blank()) +
labs(x = "time (days)", y = "fraction of the population per day")
ggplotly(p=model.plot)
model.plot
})
outputOptions(output, "SEIaImIsRD.changes", suspendWhenHidden = FALSE)
# R0 calculation
output$sc.R0 <- renderText({
model <- model0.sc()
# calculate R0
R0 <- R0(model)
paste0("R0 = ", R0)
})
#%%%%%%%%%
# SEIRDAge
#%%%%%%%%%
# reactive buttons for modifying transmission params
# observeEvent(input$control.params.age.beta, {
# shinyjs::toggle(id="age.params.beta")
# })
# list the file names of uploaded .rda files
output$age.contact.names <- renderText({
# names(input)[grepl("age.contact.file", names(input))]
input$age.contact.files[, 1]
})
# load .rda files (contact matrices population)
load.age.contact_matrices <- reactive({
if(is.null(input$age.contact.files))
return ()
else{
# load age-structured contact matrices
nfiles = nrow(input$age.contact.files)
rda = list() # store all rda file contents into a list
for (f in 1 : nfiles)
{
file <- input$age.contact.files$datapath[f]
ext <- tools::file_ext(file)
validate(need(ext == "rda", "Please upload an rda file"))
e <- new.env(parent = parent.frame())
load(file, e)
rda[[ls(e)[1]]] = get(ls(e)[1], e)
# load(file = paste0(getwd(), "/data/contact_work.rda"))
# is /data/population.rda consistent with the contact matrices??
}
return(rda)
}
})
# update input$age.country choices
observeEvent(load.age.contact_matrices(),{
if(is.null(input$age.contact.files))
updateSelectInput(session,'age.country',choices = NULL)
else{
rda <- load.age.contact_matrices()
country.list <- names(rda$contact_home)
names(country.list) <- country.list
updateSelectInput(session,'age.country',choices = country.list)
}
})
# model diagram
output$age_model_flowchart <- renderGrViz(
DiagrammeR::grViz("
digraph seirdage {
# initiate graph
graph [layout = dot, rankdir = LR]
# global node settings
node [shape = box, fontname = Helvetica, fixedsize = true,
style = filled, fillcolor = lightgray, width = 0.6]
S;E;I;R;D
node [shape = circle, fillcolor = lightblue, width = 0.3]
beta[label = <β>];
kappa[label = <κ>];
mu[label = <μ>];
gamma[label = <γ>];
# edge definitions with the node IDs
edge [arrowhead=none]
S -> beta
E -> kappa
I -> {mu gamma}
edge [arrowhead=vee]
beta -> E
kappa -> I
mu -> D
gamma -> R
}")
)
observe({
req(input$age_model_flowchart_click)
nodeid <- input$age_model_flowchart_click$id[[1]]
nodeid.int <- strsplit(nodeid, "node")[[1]][2] %>% as.integer()
nodeval <- input$age_model_flowchart_click$nodeValues
desc <- c("Susceptible", "Exposed", "Infected", "Recovered (Removed)", "Mortality",
"the rate at which an infected individual exposes susceptible",
"the rate of progression from exposed to infectious (reciprocal of the incubation period)",
"the rate of disease-caused mortality of the infected",
"the rate of removal (i.e. the recovery rate of the infected)")
trans.params <- c("beta", "kappa", "mu", "gamma")
output$SEIRDAge.param.desc <- renderText({paste0(nodeval[[length(nodeval)]], ": ", desc[nodeid.int])})
if(nodeid.int %in% seq(6, 9)){
shinyjs::toggle(id=paste0("age.params.", trans.params[nodeid.int - 5]))
}
})
# ODE system
output$age.dS <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}S_i}{\\text{d}t} = - \\beta S_i \\sum_j{C_{ij} I_j} $$"))
})
output$age.dE <- renderUI({
withMathJax(
helpText("$$\\frac{dE_i}{dt} = \\beta S_i \\sum_j{C_{i,j} I_j} - \\kappa E_i $$"))
})
output$age.dI <- renderUI({
withMathJax(
helpText("$$\\frac{dI_i}{dt} = \\kappa E_i - (\\gamma + \\mu_i) I_i $$"))
})
output$age.dR <- renderUI({
withMathJax(
helpText("$$\\frac{dR_i}{dt} = \\gamma I_i $$"))
})
output$age.dD <- renderUI({
withMathJax(
helpText("$$\\frac{dD_i}{dt} = \\mu_i I_i $$"))
})
# model initialization
model0.age <- reactive({
if(is.null(input$age.contact.files))
return()
else {
# access to contact matrices & country data
rda = load.age.contact_matrices()
contact_home <- rda$contact_home
contact_work <- rda$contact_work
contact_school <- rda$contact_school
contact_other <- rda$contact_other
population <- rda$population
country = input$age.country
# construct age groups - number equal to the nrow (ncol) of contact matrices
n_ages = length(contact_home[[country]])
ages <- c(0, seq_len(n_ages) * 80/n_ages)
age_names <- vector(length = n_ages)
for(i in seq_along(age_names)) {
age_names[i] <- paste0(ages[i], "-", ages[i + 1])
}
format_matrix <- function(contact_matrix, age_names) {
colnames(contact_matrix) <- age_names
contact_matrix$age_infectee <- age_names
contact_matrix %>%
pivot_longer(all_of(age_names)) %>%
rename(age_infector=name) %>%
mutate(age_infector=fct_relevel(age_infector, age_names)) %>%
mutate(age_infectee=fct_relevel(age_infectee, age_names))
}
c_home <- format_matrix(contact_home[[country]], age_names) %>% mutate(type="home")
c_work <- format_matrix(contact_work[[country]], age_names) %>% mutate(type="work")
c_school <- format_matrix(contact_school[[country]], age_names) %>% mutate(type="school")
c_other <- format_matrix(contact_other[[country]], age_names) %>% mutate(type="other")
c_all <- c_home %>%
bind_rows(c_work) %>%
bind_rows(c_school) %>%
bind_rows(c_other)
c_combined <- c_all %>%
group_by(age_infectee, age_infector) %>%
summarise(value=sum(value))
# !!! some countries population data are missing !!!
pops = population[population$country == country, ]$pop
pop_fraction = pops/sum(pops)
pop_fraction[n_ages] = sum(pop_fraction[n_ages:length(pop_fraction)])
pop_fraction = pop_fraction[1:n_ages] # suppose our contact matrices are subpop of 1:n_age in population
c_combined_wider <- c_combined %>%
pivot_wider(id_cols = age_infectee, names_from = age_infector,
values_from=value) %>%
ungroup() %>%
select(-age_infectee) %>%
as.matrix()
# update input$age_range.select choices
age.list <- age_names
names(age.list) <- age_names
observeEvent(input$age.contact.files,{
updateSelectInput(session,'age_range.select',choices = age.list)
})
beta <- input$age.beta
kappa <- input$age.kappa
gamma <- input$age.gamma
mu <- input$age.mu
S <- 0.99
E <- 0.00
I <- 0.01
R <- 0.00
D <- 0.00
model <- SEIRDAge(n_age_categories = n_ages,
contact_matrix = c_combined_wider,
age_ranges = as.list(age_names))
transmission_parameters(model) <- list(b=beta, k=kappa, g=gamma, mu=mu)
initial_conditions(model) <- list(S0=pop_fraction*S,
E0=rep(0, n_ages),
I0=pop_fraction*I,
R0=rep(0, n_ages),
D0=rep(0, n_ages))
return(model)
}
})
# ODE simulation and plot
## simulation result
age.model.simulation <- reactive({
if(is.null(input$age.contact.files))
return ()
else{
model <- model0.age()
t <- seq(0, 200, by = 1)
output <- run(model, times=t)
return (output)
}
})
## plot by compartment (states)
output$SEIRDAge.states.by.compartment <- renderPlotly({
if(is.null(input$age.contact.files))
return ()
else{
output <- age.model.simulation()
output <- output$states
# set up color palette - more than 8 groups, need to be set manually
n.cols <- length(unique(output$age_range))
colors <- grDevices::colorRampPalette(RColorBrewer::brewer.pal(8, "Set2"))(n.cols)
model.plot <- ggplot(output, aes(x=time, y=value, colour=age_range)) +
geom_line() +
facet_grid(vars(compartment), space = "free", scales = "free") +
scale_color_manual(values = colors) +
theme_classic()
model.plot <- ggplotly(p=model.plot, dynamicTicks = TRUE, originalData = FALSE)
return (model.plot)
}
})
## plot by compartment (changes)
output$SEIRDAge.changes.by.compartment <- renderPlotly({
if(is.null(input$age.contact.files))
return ()
else{
output <- age.model.simulation()
output <- output$changes
# set up color palette - more than 8 groups, need to be set manually
n.cols <- length(unique(output$age_range))
colors <- grDevices::colorRampPalette(RColorBrewer::brewer.pal(8, "Set2"))(n.cols)
model.plot <- ggplot(output, aes(x=time, y=value, fill=age_range)) +
geom_bar(stat="identity", position=position_dodge()) +
facet_grid(vars(compartment), space = "free", scales = "free") +
scale_color_manual(values = colors) +
theme_classic()
model.plot <- ggplotly(p=model.plot, dynamicTicks = TRUE, originalData = FALSE)
return (model.plot)
}
})
## plot by age groups (states)
output$SEIRDAge.states.by.age <- renderPlotly({
if(is.null(input$age.contact.files))
return ()
else{
output <- age.model.simulation()
output <- output$states
output <- subset(output, age_range == input$age_range.select)
model.plot <- ggplot(output, aes(x=time, y=value)) +
geom_line(aes(colour=compartment)) +
scale_color_brewer(palette = "Set2") +
theme_classic()
model.plot <- ggplotly(p=model.plot, dynamicTicks = TRUE, originalData = FALSE)
return(model.plot)
}
})
## plot by age groups (states)
output$SEIRDAge.changes.by.age <- renderPlotly({
if(is.null(input$age.contact.files))
return ()
else{
output <- age.model.simulation()
output <- output$changes
# output <- output %>% filter(compartment=="Incidence")
output <- subset(output, age_range == input$age_range.select)
model.plot <- ggplot(output, aes(x=time, y=value, fill=compartment)) +
geom_bar(stat="identity", position=position_dodge()) +
scale_color_brewer(palette = "Set2") +
theme_classic()
model.plot <- ggplotly(p=model.plot, dynamicTicks = TRUE, originalData = FALSE)
return(model.plot)
}
})
#%%%%%%%%%%%
# SEIRDV
#%%%%%%%%%%%
# model diagram
output$vac_model_flowchart <- renderGrViz(
DiagrammeR::grViz("
digraph seirdv {
# initiate graph
graph [layout = neato, rankdir = LR]
# global node settings
node [shape = box, fontname = Helvetica, fixedsize = true,
style = filled, fillcolor = lightgray, width = 0.6]
S;E;I;R;V;VR;D
node [shape = circle, fillcolor = lightblue, width = 0.3]
beta[label = <β>];
kappa[label = <κ>];
mu[label = <μ>];
gamma[label = <γ>];
deltar[label = <δ>];
deltav[label = <δ>];
deltavr[label = <δ>];
nu[label = <ν>];
# edge definitions with the node IDs
# edge definitions with the node IDs
edge [arrowhead=none]
S -> beta
S -> nu
E -> kappa
I -> gamma
I -> mu
R -> deltar
R -> nu
V -> deltav
VR -> deltavr
edge [arrowhead=vee]
beta -> E
nu -> V
nu -> VR
kappa -> I
gamma -> R
mu -> D
deltar -> S
deltav -> S
deltavr -> S
}")
)
observe({
req(input$vac_model_flowchart_click)
nodeid <- input$vac_model_flowchart_click$id[1]
nodeid.int <- strsplit(nodeid, "node")[[1]][2] %>% as.integer()
nodeval <- input$vac_model_flowchart_click$nodeValues
desc <- c("Susceptible", "Exposed", "Infected",
"Recovered (Removed)", "Vaccinated", "Vaccinated that after previously infected recovered", "Mortality",
"the rate at which an infected individual exposes susceptible", # beta
"the rate of progression from exposed to infectious (reciprocal of the incubation period)", # kappa
"the rate of disease-caused mortality of the infected", # mu
"the rate of removal (i.e. the recovery rate of the infected)", # gamma
"the rate at which recovered individuals become susceptible (i.e. the loss of immunity)", # deltar
"the rate at which vaccinated individuals who were previously infected and recovered become susceptible (i.e. the loss of immunity)", # deltavr
"the rate at which vaccinated individuals become susceptible (i.e. the loss of immunity)", # deltav
"the rate of vaccination in susceptible population") # nu
trans.params <- c("beta", "kappa", "mu", "gamma", "deltar", "deltav", "deltavr", "nu")
output$SEIRDV.param.desc <- renderText({paste0(nodeval[[length(nodeval)]], ": ", desc[nodeid.int])})
n_init <- 7
n_trans_params <- 8
if(nodeid.int %in% seq(n_init+1, n_init+n_trans_params)){
shinyjs::toggle(id=paste0("vac.params.", trans.params[nodeid.int - n_init]))
}
})
# ODE system
output$vac.dS <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}S}{\\text{d}t} = -S \\beta I - \nu \\text{Inter}(t) S + \\delta_V V + \\delta_R R + \\delta_{VR} VR$$"))
})
output$vac.dE <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}E}{\\text{d}t} = S \\beta I - \\kappa E$$"))
})
output$vac.dI <- renderUI({
withMathJax(
helpText("$$\\frac{dI}{dt} = \\kappa E - (\\gamma + \\mu)I$$"))
})
output$vac.dR <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}R}{\\text{d}t} = \\gamma I - \\delta_R R$$"))
})
output$vac.dV <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}V}{\\text{d}t} = \\nu Inter(t) S - \\delta_V V$$"))
})
output$vac.dVR <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}VR}{\\text{d}t} = \\nu Inter(t) R - \\delta_{VR} VR$$"))
})
output$vac.dD <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}D}{\\text{d}t} = \\mu I$$"))
})
# model initialization
model0.vac <- reactive({
# params
beta <- input$vac.beta
kappa <- input$vac.kappa
gamma <- input$vac.gamma
mu <- input$vac.mu
nu <- input$vac.nu
delta_R <- input$vac.delta.r
delta_V <- input$vac.delta.v
delta_VR <- input$vac.delta.vr
# Test model
# set up test data:
# initial population (in fraction)
S <- 0.99
E <- 0.00
I <- 0.01
R <- 0.00
V <- 0.00
VR <- 0.00
# 1. create the instance in class SEIRDV
model <- SEIRDV()
# 2. set up parameters and initial population
transmission_parameters(model) <- list(beta = beta, kappa = kappa, gamma = gamma, mu = mu,
nu = nu, delta_V = delta_V, delta_R = delta_R, delta_VR = delta_VR)
initial_conditions(model) <- list(S0 = S, E0 = E, I0 = I, R0 = R, V0 = V, VR0 = VR)
intervention_parameters(model) <- list(starts=c(17, 35, 42),
stops=c(25, 39, 49),
coverages=c(1, 1, 1))
return(model)
})
# ODE simulation and plot
model0.vac.output <- reactive({
model <- model0.vac()
# 4. ode simulation (period: 2000 time points, most of the time will relax)
t <- seq(0, 200, by = 1)
output <- run(model, t)
return(output)
})
# 5.1 plot states
output$SEIRDV.states <- renderPlotly({
output <- model0.vac.output()
model.plot <- ggplot(output$states, aes(x = time, y = value)) +
geom_line(aes(color = compartment)) +
scale_color_brewer(palette = "Dark2") +
labs(x = "time (days)", y = "fraction of the population") +
theme(legend.position = "bottom", legend.title = element_blank())
# theme(text = element_text(size = 20))
model.plot <- ggplotly(model.plot)#, dynamicTicks = TRUE, originalData = FALSE)
model.plot
})
outputOptions(output, "SEIRDV.states", suspendWhenHidden = FALSE)
# 5.2 plot changes
output$SEIRDV.changes <- renderPlotly({
output <- model0.vac.output()
model.plot <- ggplot(output$changes, aes(x = time, y = value, fill = compartment)) +
geom_bar(stat="identity", position = position_dodge()) +
scale_color_brewer(palette = "Dark2") +
labs(x = "time (days)", y = "fraction of the population \n per day") +
theme(legend.position = "bottom", legend.title = element_blank())#, text = element_text(size = 20))
model.plot <- ggplotly(model.plot)
model.plot
})
outputOptions(output, "SEIRDV.changes", suspendWhenHidden = FALSE)
#%%%%%%%%%
# SEIRD_RU
#%%%%%%%%%
# list the file names of uploaded .rda files
output$ru.contact.names <- renderText({
input$ru.contact.files[, 1]
})
# load .rda files (contact matrices population)
load.ru.contact_matrices <- reactive({
if(is.null(input$ru.contact.files))
return ()
else{
# load contact matrices
nfiles = nrow(input$ru.contact.files)
rda = list() # store all rda file contents into a list
for (f in 1 : nfiles)
{
file <- input$ru.contact.files$datapath[f]
ext <- tools::file_ext(file)
validate(need(ext == "rda", "Please upload an rda file"))
e <- new.env(parent = parent.frame())
load(file, e)
rda[[ls(e)[1]]] = get(ls(e)[1], e)
## ???? is /data/{percentrural_by_country,agedemographics_by_country,country.codetoname}.rda; consistent with the contact matrices ????
}
return(rda)
}
})
# update input$ru.country choices
observeEvent(load.ru.contact_matrices(),{
if(is.null(input$ru.contact.files))
updateSelectInput(session,'ru.country',choices = NULL)
else{
rda <- load.ru.contact_matrices()
country.list <- rda$country_codetoname$CountryName
names(country.list) <- country.list
updateSelectInput(session,'ru.country',choices = country.list)
}
})
# model diagram
output$ru_model_flowchart <- renderGrViz(
DiagrammeR::grViz("
digraph seird_ru {
# initiate graph
graph [layout = dot, rankdir = LR]
# global node settings
node [shape = box, fontname = Helvetica, fixedsize = true,
style = filled, fillcolor = lightgray, width = 0.6]
S;E;I;R;D
node [shape = circle, fillcolor = lightblue, width = 0.3]
beta[label = <β>];
kappa[label = <κ>];
mu[label = <μ>];
gamma[label = <γ>];
# edge definitions with the node IDs
edge [arrowhead=none]
S -> beta
E -> kappa
I -> {mu gamma}
edge [arrowhead=vee]
beta -> E
kappa -> I
mu -> D
gamma -> R
}")
)
observe({
req(input$ru_model_flowchart_click)
nodeid <- input$ru_model_flowchart_click$id[[1]]
nodeid.int <- strsplit(nodeid, "node")[[1]][2] %>% as.integer()
nodeval <- input$ru_model_flowchart_click$nodeValues
desc <- c("Susceptible", "Exposed", "Infected", "Recovered (Removed)", "Mortality",
"the rate at which an infected individual exposes susceptible",
"the rate of progression from exposed to infectious (reciprocal of the incubation period)",
"the rate of disease-caused mortality of the infected",
"the rate of removal (i.e. the recovery rate of the infected)")
trans.params <- c("beta", "kappa", "mu", "gamma")
output$SEIRD_RU.param.desc <- renderText({paste0(nodeval[[length(nodeval)]], ": ", desc[nodeid.int])})
if(nodeid.int %in% seq(6, 9)){
shinyjs::toggle(id=paste0("ru.params.", trans.params[nodeid.int - 5]))
}
})
# ODE system
output$ru.dS <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}S_i}{\\text{d}t} = - \\beta S_i \\left((\\sum_j{I_j})(\\sum_j{\\phi_j N_j}C + \\frac{I_i}{\\phi_i}N_i(1-C)\\right) $$"))
})
output$ru.dE <- renderUI({
withMathJax(
helpText("$$\\frac{\\text{d}E_i}{\\text{d}t} = \\beta S_i \\left((\\sum_j{I_j})(\\sum_j{\\phi_j N_j}C + \\frac{I_i}{\\phi_i}N_i(1-C)\\right) - \\kappa E_i$$"))
})
output$ru.dI <- renderUI({
withMathJax(
helpText("$$\\frac{dI_i}{dt} = \\kappa E_i - (\\gamma + \\mu_i) I_i $$"))
})
output$ru.dR <- renderUI({
withMathJax(
helpText("$$\\frac{dR_i}{dt} = \\gamma I_i $$"))
})
output$ru.dD <- renderUI({
withMathJax(
helpText("$$\\frac{dD_i}{dt} = \\mu_i I_i $$"))
})
# model initialization
model0.ru <- reactive({
if(is.null(input$ru.contact.files))
return()
else {
# access to contact matrices & country data
rda = load.ru.contact_matrices()
contact_all_urban <- rda$contact_all_urban
contact_all_rural <- rda$contact_all_rural
names_urban <- names(contact_all_urban)
names_rural <- names(contact_all_rural)