-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSIR x SIR - InteractingEpidemics.R
1071 lines (881 loc) · 45.1 KB
/
SIR x SIR - InteractingEpidemics.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
##################################################################################
############################### SETUP ###############################
##################################################################################
library(igraph)
library(animation)
library(xtable)
# Set the working directory
setwd("[Insert your working directory here]")
# Import the dataset (.csv)
dat=read.csv(file.choose(),header=TRUE)
el=as.matrix(dat)
el[,1]=as.character(el[,1])
el[,2]=as.character(el[,2])
n=graph.edgelist(el,directed=FALSE)
# Create an edgelist and set the names to FALsE
edgelist = as_edgelist(n, names = FALSE)
# Set n as the number of vertices
N = vcount(n)
##################################################################################
############################### SET THE PARAMETERS ###############################
##################################################################################
# Set the number of initially infected individuals
init_inf_A = 1
init_inf_B = 1
# Set the number of initially recovered individuals
init_rec_A = 0
init_rec_B = 0
# Set the normal infection rates alpha
# In this case, the normal transmission rate is equal for both diseases
# alpha_A = Pony Stealer
alpha_A = 0.13
# alpha_B = Angler
alpha_B = 0.13
# Set the increase of the infection rate for disease A and B
# Using the increase of the infection rate enables to choose whether the model is used for
# symetrically increasing transmission risks or for an unilateral increase
# In this case, the transmission of disease B is increased,
# if the other individual is currently infected with disease A
# The transmission of disease A, however, is not increased,
# if the other individual is currently infected with disease B
beta_A = 0
beta_B = 0.49
# Set the recovery rates gamma
# In this case, the recovery rate is equal for both diseases
gamma_A = 0.14
gamma_B = 0.14
# Set the length of the simulation
simlength = 70
# Set how often the simulation is repeated
simnumber = 500
# Set whether the network is ment to be plotted
plot.spread = FALSE
# Set whether the infected individuals can recover from timestep 0 to timestep 1
recovery.wait_A = TRUE
recovery.wait_B = TRUE
# Create 6 lists in order to save the susceptible, infected and recovered data
# for both diseases A an B for each simulation round
infdata_sum_A = list()
infdata_sum_B = list()
susdata_sum_A = list()
susdata_sum_B = list()
recdata_sum_A = list()
recdata_sum_B = list()
# Create 2 lists in order to save the incidence of both diseases A and B
# for each simulation round
incidence_sum_A = list()
incidence_sum_B = list()
complete_infected_sum_A = list()
complete_infected_sum_B = list()
peaktime_A_sum = list()
peaktime_B_sum = list()
peaknumber_A_sum = list()
peaknumber_B_sum = list()
incidence_peaktime_A_sum = list()
incidence_peaktime_B_sum = list()
incidence_peaknumber_A_sum = list()
incidence_peaknumber_B_sum = list()
no_new_infection_A_sum = list()
no_new_infection_B_sum = list()
complete_infected_sum_A_100 = list()
complete_infected_sum_A_500 = list()
complete_infected_sum_A_1000 = list()
complete_infected_sum_B_100 = list()
complete_infected_sum_B_500 = list()
complete_infected_sum_B_1000 = list()
##################################################################################
############################### SIMULATION ###############################
##################################################################################
################### PARAMETERS FOR EACH SIMULATION ROUND ##################
for (a in 1:simnumber){
print(a)
# Create a logical vector for infection data for both diseases A and B
infected_A = logical(N)
infected_B = logical(N)
# Create a logical vecotr for recovered data for both diseases A and B
recovered_A = logical(N)
recovered_B = logical(N)
# Set, which vertices are initially infected - in this case randomly
patientzero_A = sample(N,init_inf_A)
patientzero_B = sample(N,init_inf_B)
# Set, which vertices are initally recovered - in this case randomly
# In this specific case there are no initially infected individuals
recoverzero_A = sample(N, init_rec_A)
recoverzero_B = sample(N, init_rec_B)
# Set the infected vertices to TRUE
infected_A[patientzero_A] = TRUE
infected_B[patientzero_B] = TRUE
# Set the recovered vertices to NA
infected_A[recoverzero_A] = NA
infected_B[recoverzero_B] = NA
# Create a logical vector in order to store those vertices, which are infected with both A and B
bothinfected = logical(N)
# Create a logical vector in order to store those vertices, which are recovered from both A and B
bothrecovered = logical(N)
# Create a logical vector in order to store those vertices, which are infected with A and recovered from B
infA_recB = logical(N)
# Create a logical vector in order to store those vertices, which are infected with B and recovered from A
infB_recA = logical(N)
# Since the recovered data is stored as NA, create a logical vector for recovered data for
# both diseases A and B using the is.na function
recovered_A = is.na(infected_A)
recovered_B = is.na(infected_B)
# Set the number of susceptibles as N minus the sum of infected individuals minus the sum of recovered vertices
susceptible_A = N - sum(infected_A %in% TRUE, na.rm = TRUE) - sum(recovered_A == TRUE)
susceptible_B = N - sum(infected_B %in% TRUE, na.rm = TRUE) - sum(recovered_B == TRUE)
# For loop in order to find those vertices, which are infected with both A and B
# Set those vertices which are dually infected to TRUE
for (b in 1:length(infected_A)) {
if(infected_A[[b]] %in% TRUE && infected_B[[b]] %in% TRUE){
bothinfected[[b]] = TRUE
}
}
# For loop in order to find those vertices, which are infected with A and recovered from B
# Only used if there are initially recovered vertices
# Set the respective vertices to TRUE
if(length(recoverzero_B) > 0){
for (f in 1:length(infected_A)){
if(infected_A[[f]] %in% TRUE && recovered_B[[f]] == TRUE){
infA_recB[[f]] = TRUE
}
}
}
# For loop in order to find those vertices, which are infected with B and recovered from A
# Only used if there are initially recovered vertices
# Set the respective vertices to TRUE
if(length(recoverzero_A) > 0){
for (h in 1:length(infected_B)){
if(infected_B[[h]] %in% TRUE && recovered_A[[h]] == TRUE){
infB_recA[[h]] = TRUE
}
}
}
# for loop in order to find those vertices, which are recovered from both A and B
# Only used if there are initially recovered vertices
# Set the respective vertices to TRUE
# Important: If a vertex is recovered from both diseases set the respective vertex in infA_recB and infB_rec A
# to FALSE!
if(length(recoverzero_A) > 0){
for (d in 1:length(recovered_A)) {
if(recovered_A[[d]] == TRUE && recovered_B[[d]] == TRUE){
bothrecovered[[d]] = TRUE
infA_recB[[t]] = FALSE
infB_recA[[t]] = FALSE
}
}
}
# Create 6 lists in order to save the susceptible, infected and recovered data
# for both diseases A and B for each timestep of the simulation
infdata_A = list(sum(infected_A %in% TRUE, na.rm = TRUE))
infdata_B = list(sum(infected_B %in% TRUE, na.rm = TRUE))
susdata_A = list(susceptible_A)
susdata_B = list(susceptible_B)
recdata_A = list(sum(recovered_A))
recdata_B = list(sum(recovered_B))
# Create 2 lists in order to save the incidence of both diseases A and B
# for each timestep of the simulation
incidence_A = list()
incidence_B = list()
complete_infected_A_100 = 0
complete_infected_A_500 = 0
complete_infected_A_1000 = 0
complete_infected_B_100 = 0
complete_infected_B_500 = 0
complete_infected_B_1000 = 0
no_new_infection_A = 0
no_new_infection_B = 0
# If the network is plotted
# Set the layout of the graph - in this case kamada.kawai
# Define the colors for the vertices corresponding to their state
# Name the plot sim000 - important if a gif is created later on
# Important: Set dev.off() after creating the plot
if (plot.spread) {
network.x = graph.edgelist(edgelist, directed=FALSE)
fixlayout = layout.kamada.kawai(network.x)
node.colour = rep("SkyBlue2",N)
node.colour[patientzero_A] = "red"
node.colour[patientzero_B] = "orange"
node.colour[bothinfected] = "yellow"
node.colour[recoverzero_A] = "forestgreen"
node.colour[recoverzero_B] = "grey"
node.colour[bothrecovered] = "white"
node.colour[infA_recB] = "hotpink"
node.colour[infB_recA] = "chocolate4"
png(paste("sim", 000, ".png", sep = "."))
plot(network.x,layout=fixlayout, main="Time = 0", vertex.color=node.colour)
#dev.off()
}
# Required padding for the name of the automatically saved screenshot of the network
pad_int = function(t,scale){
out_string = paste(10*scale + t,sep='')
out_string = substr(out_string,2,nchar(out_string))
return(out_string)
}
################### FIND THE RESPECTIVE EDGES ###################
for (k in 1:simlength) {
# Create 4 lists in order to sort the edges according to their transmission risk
highrisk.edges_A = list()
highrisk.edges_B = list()
normal.edges_A = list()
normal.edges_B = list()
# Set counter variables in order to use the lists as defined above
l = 1 # Normal risk A
m = 1 # Normal risk B
n = 1 # High risk A
o = 1 # High risk B
# Depending on recovery.wait the vertices recover from disease A
# Therefore, first those vertices which are infected with disease A are extracted
# Then, those vertices, which recover are selected with probability gamma
# The recovered vertices are set to NA
if (recovery.wait_A == FALSE || k > 1){
infected.vertices_A = which(infected_A %in% TRUE)
recover_A = rbinom(sum(infected_A %in% TRUE, na.rm = TRUE),1,gamma_A)
recover.vertices_A = infected.vertices_A[recover_A == 1]
infected_A[recover.vertices_A] = NA
}
# The same is repeated for disease B
if (recovery.wait_B == FALSE || k > 1){
infected.vertices_B = which(infected_B %in% TRUE)
recover_B = rbinom(sum(infected_B %in% TRUE, na.rm = TRUE),1,gamma_B)
recover.vertices_B = infected.vertices_B[recover_B == 1]
infected_B[recover.vertices_B] = NA
}
# After recovery, the logical vector for the recovered vertices is updated
recovered_A = is.na(infected_A)
recovered_B = is.na(infected_B)
# Iterate through the edgelist in order to sort the vertices according to their state of disease
# and to sort the edges respectively
# Therefore one assumes two vertices (left, right) which are connected by an edge
# Then, all possibilites of transmission are represented
for(i in 1:nrow(edgelist)){
# Find those "left" vertices, which are ONLY INFECTED WITH DISEASE A
if(infected_A[edgelist[i,1]] %in% TRUE && infected_B[edgelist[i,1]] %in% FALSE){
# Find those "right" vertices, which are ONLY INFECTED WITH DISEASE B
if(infected_A[edgelist[i,2]] %in% FALSE && infected_B[edgelist[i,2]] %in% TRUE){
# High risk A
# High risk B
highrisk.edges_A[[n]] = i
highrisk.edges_B[[o]] = i
n = n + 1
o = o + 1
next
# Find those "right" vertices, which are INFECTED WITH BOTH DISEASE A AND B
} else if(infected_A[edgelist[i,2]] %in% TRUE && infected_B[edgelist[i,2]] %in% TRUE) {
# High risk B
highrisk.edges_B[[o]] = i
o = o + 1
next
# Find those "right" vertices, which are INFECTED WITH DISEASE B AND RECOVERED FROM DISEASE A
} else if(infected_B[edgelist[i,2]] %in% TRUE && recovered_A[edgelist[i,2]] == TRUE) {
# High risk B
highrisk.edges_B[[o]] = i
o = o + 1
next
# Find those "right" vertices, which are NOT INFECTED AT ALL OR
# Find those "right" vertices, which are ONLY RECOVERED FROM DISEASE B
} else if((infected_A[edgelist[i,2]] %in% FALSE && infected_B[edgelist[i,2]] %in% FALSE)||
(infected_A[edgelist[i,2]] %in% FALSE && recovered_B[edgelist[i,2]] == TRUE)) {
# Normal risk A
normal.edges_A[[l]] = i
l = l + 1
next
}
next
# Find those "left" vertices, which are INFECTED WITH BOTH DISEASES A AND B
}else if(infected_A[edgelist[i,1]] %in% TRUE && infected_B[edgelist[i,1]] %in% TRUE){
# Find those "right" vertices, which are ONLY INFECTED WITH DISEASE B
if(infected_A[edgelist[i,2]] %in% FALSE && infected_B[edgelist[i,2]] %in% TRUE){
# High risk A
highrisk.edges_A[[n]] = i
n = n + 1
next
# Find those "right" edges, which are ONLY INFECTED WITH DISEASE A
} else if(infected_A[edgelist[i,2]] %in% TRUE && infected_B[edgelist[i,2]] %in% FALSE){
# High risk B
highrisk.edges_B[[o]] = i
o = o + 1
next
# Find those "right" edges, which are NOT INFECTED AT ALL
} else if(infected_A[edgelist[i,2]] %in% FALSE && infected_B[edgelist[i,2]] %in% FALSE){
# Normal risk A
# Normal risk B
normal.edges_A[[l]] = i
normal.edges_B[[m]] = i
l = l + 1
m = m + 1
next
# Find those "right" edges, which are ONLY RECOVERED FROM DISEASE B
} else if(infected_A[edgelist[i,2]] %in% FALSE && recovered_B[edgelist[i,2]] == TRUE){
# Normal risk A
normal.edges_A[[l]] = i
l = l + 1
next
# Find those "right" edges, which are ONLY RECOVERED FROM DISEASE A
} else if(infected_B[edgelist[i,2]] %in% FALSE && recovered_A[edgelist[i,2]] == TRUE){
# Normal risk B
normal.edges_B[[m]] = i
m = m + 1
next
}
next
# Find those "left" vertices, which are INFECTED WITH DISEASE A and RECOVERED FROM DISEASE B
}else if(infected_A[edgelist[i,1]] %in% TRUE && recovered_B[edgelist[i,1]] == TRUE){
# Find those "right" vertices, which are NOT INFECTED AT ALL OR
# Find those "right" vertices, which are ONLY RECOVERED FROM DISEASE B
if((infected_A[edgelist[i,2]] %in% FALSE && infected_B[edgelist[i,2]] %in% FALSE)||
(infected_A[edgelist[i,2]] %in% FALSE && recovered_B[edgelist[i,2]] == TRUE)){
# Normal risk A
normal.edges_A[[l]] = i
l = l + 1
next
# Find those "right" vertices, which are ONLY INFECTED WITH DISEASE B
} else if(infected_A[edgelist[i,2]] %in% FALSE && infected_B[edgelist[i,2]] %in% TRUE){
# High risk A
highrisk.edges_A[[n]] = i
n = n + 1
next
}
next
# Find those "left" vertices, which are ONLY INFECTED WITH DISEASE B
}else if(infected_A[edgelist[i,1]] %in% FALSE && infected_B[edgelist[i,1]] %in% TRUE){
# Find those "right" vertices, which are ONLY INFECTED WITH DISEASE A
if(infected_A[edgelist[i,2]] %in% TRUE && infected_B[edgelist[i,2]] %in% FALSE){
# High risk A
# High risk B
highrisk.edges_A[[n]] = i
highrisk.edges_B[[o]] = i
n = n + 1
o = o + 1
next
# Find those "right" vertices, which are ONLY infected with disease A and RECOVERED from B
} else if(infected_A[edgelist[i,2]] %in% TRUE && recovered_B[edgelist[i,2]] == TRUE){
# High risk A
highrisk.edges_A[[n]] = i
n = n + 1
next
# Find those "right" vertices, which are INFECTED WITH BOTH DISEASE A AND B
} else if(infected_A[edgelist[i,2]] %in% TRUE && infected_B[edgelist[i,2]] %in% TRUE){
# High risk A
highrisk.edges_A[[n]] = i
n = n + 1
next
# Find those "right" vertices, which are NOT INFECTED AT ALL OR
# Find those "right" vertices, which are ONLY RECOVERED FROM DISEASE A
}else if((infected_A[edgelist[i,2]] %in% FALSE && infected_B[edgelist[i,2]] %in% FALSE)||
(infected_B[edgelist[i,2]] %in% FALSE && recovered_A[edgelist[i,2]] == TRUE)){
# Normal risk B
normal.edges_B[[m]] = i
m = m + 1
next
}
next
# Find those "left" vertices, which are INFECTED WITH DISEASE B AND RECOVERED FROM DISEASE A
}else if(infected_B[edgelist[i,1]] %in% TRUE && recovered_A[edgelist[i,1]] == TRUE){
# Find those "right" vertices, which are NOT INFECTED AT ALL OR
# Find those "right" vertices, which are RECOVERED FROM DISEASE A
if((infected_A[edgelist[i,2]] %in% FALSE && infected_B[edgelist[i,2]] %in% FALSE)||
(infected_B[edgelist[i,2]] %in% FALSE && recovered_A[edgelist[i,2]] == TRUE)){
# Normal risk B
normal.edges_B[[m]] = i
m = m + 1
next
# Find those "right" vertices, which are ONLY INFECTED WITH DISEASE A
}else if(infected_A[edgelist[i,2]] %in% TRUE && infected_B[edgelist[i,2]] %in% FALSE){
# High risk B
highrisk.edges_B[[o]] = i
o = o + 1
next
}
next
# Find those "left" vertices, which are NOT INFECTED AT ALL
}else if(infected_A[edgelist[i,1]] %in% FALSE && infected_B[edgelist[i,1]] %in% FALSE){
# Find those "right" vertices, which are ONLY INFECTED WITH DISEASE A OR
# Find those "right" vertices, which are INFECTED WITH DISEASE A AND RECOVERED FROM DISEASE B
if((infected_A[edgelist[i,2]] %in% TRUE && infected_B[edgelist[i,2]] %in% FALSE)||
(infected_A[edgelist[i,2]] %in% TRUE && recovered_B[edgelist[i,2]] == TRUE)){
# Normal risk A
normal.edges_A[[l]] = i
l = l + 1
next
# Find those "right" vertices, which are ONLY INFECTED WITH DISEASE B OR
# Find those "right" vertices, which are INFECTED WITH DISEASE B AND RECOVERED FROM DISEASE A
}else if((infected_A[edgelist[i,2]] %in% FALSE && infected_B[edgelist[i,2]] %in% TRUE)||
(infected_B[edgelist[i,2]] %in% TRUE && recovered_A[edgelist[i,2]] == TRUE)){
# Normal risk B
normal.edges_B[[m]] = i
m = m + 1
next
# Find those "right" vertices, which are infected with BOTH DISEASE A AND B
}else if(infected_A[edgelist[i,2]] %in% TRUE && infected_B[edgelist[i,2]] %in% TRUE){
# Normal risk A
# Normal risk B
normal.edges_A[[l]] = i
normal.edges_B[[m]] = i
l = l + 1
m = m + 1
next
}
next
}
# Find those "left" vertices, which are ONLY RECOVERED FROM DISEASE B
if(recovered_B[edgelist[i,1]] == TRUE && infected_A[edgelist[i,1]] %in% FALSE){
# Find those "right" vertices, which are ONLY INFECTED WITH DISEASE A OR
# Find those "right" vertices, which are INFECTED WITH DISEASE A AND RECOVERED FROM DISEASE B OR
# Find those "right" vertices, which are INFECTED WITH BOTH DISEASES A AND B
if((infected_A[edgelist[i,2]] %in% TRUE && infected_B[edgelist[i,2]] %in% FALSE)||
(infected_A[edgelist[i,2]] %in% TRUE && recovered_B[edgelist[i,2]] == TRUE)||
(infected_A[edgelist[i,2]] %in% TRUE && infected_B[edgelist[i,2]] %in% TRUE)){
# Normal risk A
normal.edges_A[[l]] = i
l = l + 1
next
}
next
}
# Find those "left" vertices, which are ONLY RECOVERED FROM DISEASE A
if(recovered_A[edgelist[i,1]] == TRUE && infected_B[edgelist[i,1]] %in% FALSE){
# Find those "right" vertices, which are ONLY INFECTED WITH DISEASE B OR
# Find those "right" vertices, which are INFECTED WITH DISEASE B RECOVERED FROM DISEASE A OR
# Find those "right" vertices, which are INFECTED WITH BOTH DISEASES A AND B
if((infected_A[edgelist[i,2]] %in% FALSE && infected_B[edgelist[i,2]] %in% TRUE)||
(infected_B[edgelist[i,2]] %in% TRUE && recovered_A[edgelist[i,2]] == TRUE)||
(infected_A[edgelist[i,2]] %in% TRUE && infected_B[edgelist[i,2]] %in% TRUE)){
# Normal risk B
normal.edges_B[[m]] = i
m = m + 1
next
}
next
}
}
################### TRANSMISSION OF THE DISEASES ###################
# After sorting the edges, unlist the respectivew lists
highrisk.edges_A = unlist(highrisk.edges_A, recursive = TRUE, use.names = TRUE)
highrisk.edges_B = unlist(highrisk.edges_B, recursive = TRUE, use.names = TRUE)
normal.edges_A = unlist(normal.edges_A, recursive = TRUE, use.names = TRUE)
normal.edges_B = unlist(normal.edges_B, recursive = TRUE, use.names = TRUE)
# Use the rbinom function to determine the edges upon which the disease is transmitted
# There one differentiates between those edges, which have normal probability and those edges, which
# have an increase probability
transmit_normal_A = rbinom(length(normal.edges_A),1,alpha_A)
transmit_normal_B = rbinom(length(normal.edges_B),1,alpha_B)
transmit_high_A = rbinom(length(highrisk.edges_A),1,(alpha_A+beta_A))
transmit_high_B = rbinom(length(highrisk.edges_B),1,(alpha_B+beta_B))
# Then those edges upon which the diseases are transmitted are selected
transmitter.edges_normal_A = normal.edges_A[transmit_normal_A == 1]
transmitter.edges_normal_B = normal.edges_B[transmit_normal_B == 1]
transmitter.edges_high_A = highrisk.edges_A[transmit_high_A == 1]
transmitter.edges_high_B = highrisk.edges_B[transmit_high_B == 1]
# Based on the selected edges the corresponding vertices are selected
vertices.transmitter.edges_normal_A = unique(as.vector(edgelist[transmitter.edges_normal_A,1:2]))
vertices.transmitter.edges_normal_B = unique(as.vector(edgelist[transmitter.edges_normal_B,1:2]))
vertices.transmitter.edges_high_A = unique(as.vector(edgelist[transmitter.edges_high_A,1:2]))
vertices.transmitter.edges_high_B = unique(as.vector(edgelist[transmitter.edges_high_B,1:2]))
################### UPDATE VERTEX STATUS ###################
# All vertices, which are now newly infected are set to TRUE in the corresponding logical vector
infected_A[vertices.transmitter.edges_normal_A] = TRUE
infected_A[vertices.transmitter.edges_high_A] = TRUE
infected_B[vertices.transmitter.edges_normal_B] = TRUE
infected_B[vertices.transmitter.edges_high_B] = TRUE
# As in the beginning a for loop is used in order to find those vertices, which are infected with both A and B
for (p in 1:length(infected_A)) {
if(infected_A[[p]] %in% TRUE && infected_B[[p]] %in% TRUE){
bothinfected[[p]] = TRUE
}
}
# See above
for (r in 1:length(infected_A)){
if(infected_A[[r]] %in% TRUE && recovered_B[[r]] %in% TRUE){
infA_recB[[r]] = TRUE
}
}
# See above
for (s in 1:length(infected_B)){
if(infected_B[[s]] %in% TRUE && recovered_A[[s]] %in% TRUE){
infB_recA[[s]] = TRUE
}
}
#See above
for (t in 1:length(recovered_A)) {
if(recovered_A[[t]] == TRUE && recovered_B[[t]] == TRUE){
bothrecovered[[t]] = TRUE
infA_recB[[t]] = FALSE
infB_recA[[t]] = FALSE
}
}
################### SAVE THE DATA FOR EACH TIMESTEP ###################
# After each timestep the susceptible, infected and recovered data are saved in the respective lists
# For the calculation see above for more details
infdata_A[[k+1]] = sum(infected_A %in% TRUE, na.rm = TRUE)
infdata_B[[k+1]] = sum(infected_B %in% TRUE, na.rm = TRUE)
susdata_A[[k+1]] = N - sum(infected_A %in% TRUE, na.rm = TRUE) - sum(is.na(infected_A))
susdata_B[[k+1]] = N - sum(infected_B %in% TRUE, na.rm = TRUE) - sum(is.na(infected_B))
recdata_A[[k+1]] = sum(is.na(infected_A))
recdata_B[[k+1]] = sum(is.na(infected_B))
# Then, the incidence is calculated as the number of newly infected vertices per timestep
incidence_A[[k]] = susdata_A[[k]] - susdata_A[[k+1]]
incidence_B[[k]] = susdata_B[[k]] - susdata_B[[k+1]]
if(incidence_A[[k]] == 0 && no_new_infection_A == 0){
no_new_infection_A = k
}
if(incidence_B[[k]] == 0 && no_new_infection_B == 0){
no_new_infection_B = k
}
if (N - sum(infected_A %in% FALSE, na.rm = TRUE) >= 100 && complete_infected_A_100 == 0){
complete_infected_A_100 = k
}
if (N - sum(infected_A %in% FALSE, na.rm = TRUE) >= 500 && complete_infected_A_500 == 0){
complete_infected_A_500 = k
}
if (N - sum(infected_A %in% FALSE, na.rm = TRUE) >= 1000 && complete_infected_A_1000 == 0){
complete_infected_A_1000 = k
}
###############################
if (N - sum(infected_B %in% FALSE, na.rm = TRUE) >= 100 && complete_infected_B_100 == 0){
complete_infected_B_100 = k
}
if (N - sum(infected_B %in% FALSE, na.rm = TRUE) >= 500 && complete_infected_B_500 == 0){
complete_infected_B_500 = k
}
if (N - sum(infected_B %in% FALSE, na.rm = TRUE) >= 1000 && complete_infected_B_1000 == 0){
complete_infected_B_1000 = k
}
if (k == simlength){
complete_infected_A = N - sum(infected_A %in% FALSE, na.rm = TRUE)
complete_infected_B = N - sum(infected_B %in% FALSE, na.rm = TRUE)
peaklist_A = unlist(infdata_A, recursive = TRUE, use.names = TRUE)
peaktime_A = which.max(peaklist_A)
peaknumber_A = peaklist_A[[peaktime_A]]
incidence_peaklist_A = unlist(incidence_A, recursive = TRUE, use.names = TRUE)
incidence_peaktime_A = which.max(incidence_peaklist_A)
incidence_peaknumber_A = incidence_peaklist_A[[incidence_peaktime_A]]
peaklist_B = unlist(infdata_B, recursive = TRUE, use.names = TRUE)
peaktime_B = which.max(peaklist_B)
peaknumber_B = peaklist_B[[peaktime_B]]
incidence_peaklist_B = unlist(incidence_B, recursive = TRUE, use.names = TRUE)
incidence_peaktime_B = which.max(incidence_peaklist_B)
incidence_peaknumber_B = incidence_peaklist_B[[incidence_peaktime_B]]
}
################### UPDATE PLOTTING PARAMETERS ###################
# See above for more details
if (plot.spread) {
node.colour[infected_A] = "red"
node.colour[infected_B] = "orange"
node.colour[bothinfected] = "yellow"
node.colour[recovered_A] = "forestgreen"
node.colour[recovered_B] = "grey"
node.colour[bothrecovered] = "white"
node.colour[infA_recB] = "hotpink"
node.colour[infB_recA] = "chocolate4"
padded<-pad_int(k,100)
png(paste("sim",padded,".png", sep=""))
plot(network.x,layout=fixlayout, main=paste("Time =", k), vertex.color=node.colour)
}
# Remeber to set dev.off() into comments when setting plot.spred to FALSE!
#dev.off()
}
################### SAVE THE DATA FOR EACH SIMULATION ROUND ###################
complete_infected_sum_A[[a]] = complete_infected_A
complete_infected_sum_B[[a]] = complete_infected_B
peaktime_A_sum[[a]] = peaktime_A
peaktime_B_sum[[a]] = peaktime_B
peaknumber_A_sum[[a]] = peaknumber_A
peaknumber_B_sum[[a]] = peaknumber_B
incidence_peaktime_A_sum[[a]] = incidence_peaktime_A
incidence_peaktime_B_sum[[a]] = incidence_peaktime_B
incidence_peaknumber_A_sum[[a]] = incidence_peaknumber_A
incidence_peaknumber_B_sum[[a]] = incidence_peaknumber_B
no_new_infection_A_sum[[a]] = no_new_infection_A
no_new_infection_B_sum[[a]] = no_new_infection_B
complete_infected_sum_A_100[[a]] = complete_infected_A_100
complete_infected_sum_A_500[[a]] = complete_infected_A_500
complete_infected_sum_A_1000[[a]] = complete_infected_A_1000
complete_infected_sum_B_100[[a]] = complete_infected_B_100
complete_infected_sum_B_500[[a]] = complete_infected_B_500
complete_infected_sum_B_1000[[a]] = complete_infected_B_1000
# After each simulation round the lists for the susceptible, infected and recovered data are unlisted
# This step is only required if the average is ment to be calculated
infdata_A = unlist(infdata_A, recursive = TRUE, use.names = TRUE)
infdata_B = unlist(infdata_B, recursive = TRUE, use.names = TRUE)
susdata_A = unlist(susdata_A, recursive = TRUE, use.names = TRUE)
susdata_B = unlist(susdata_B, recursive = TRUE, use.names = TRUE)
recdata_A = unlist(recdata_A, recursive = TRUE, use.names = TRUE)
recdata_B = unlist(recdata_B, recursive = TRUE, use.names = TRUE)
incidence_A = unlist(incidence_A, recursive = TRUE, use.names = TRUE)
incidence_B = unlist(incidence_B, recursive = TRUE, use.names = TRUE)
# The complete data for this simulation round is denn saved in the respective lists
infdata_sum_A[[a]] = infdata_A
infdata_sum_B[[a]] = infdata_B
susdata_sum_A[[a]] = susdata_A
susdata_sum_B[[a]] = susdata_B
recdata_sum_A[[a]] = recdata_A
recdata_sum_B[[a]] = recdata_B
incidence_sum_A[[a]] = incidence_A
incidence_sum_B[[a]] = incidence_B
}
################### CONVERT ALL DATA INTO USABLE FORMAT ###################
# After all simulations are done, create matrices for the data
# Infection A
infdata_A_table = as.data.frame(do.call(rbind,infdata_sum_A))
colnames(infdata_A_table) = paste("Time", 0:simlength)
rownames(infdata_A_table) = paste("Sim Inf A", 1:simnumber)
infdata_A_table = as.matrix(infdata_A_table)
write.csv(infdata_A_table, "PA_7_1_random_Infection_A.csv")
# Infection B
infdata_B_table = as.data.frame(do.call(rbind,infdata_sum_B))
colnames(infdata_B_table) = paste("Time", 0:simlength)
rownames(infdata_B_table) = paste("Sim Inf B", 1:simnumber)
infdata_B_table = as.matrix(infdata_B_table)
write.csv(infdata_B_table, "PA_7_1_random_Infection_B.csv")
# Susceptible A
susdata_A_table = as.data.frame(do.call(rbind,susdata_sum_A))
colnames(susdata_A_table) = paste("Time", 0:simlength)
rownames(susdata_A_table) = paste("Sim Sus A", 1:simnumber)
susdata_A_table = as.matrix(susdata_A_table)
write.csv(susdata_A_table, "PA_7_1_random_Susceptible_A.csv")
# Susceptible B
susdata_B_table = as.data.frame(do.call(rbind,susdata_sum_B))
colnames(susdata_B_table) = paste("Time", 0:simlength)
rownames(susdata_B_table) = paste("Sim Sus B", 1:simnumber)
susdata_B_table = as.matrix(susdata_B_table)
write.csv(susdata_B_table, "PA_7_1_random_Susceptible_B.csv")
# Recovered A
recdata_A_table = as.data.frame(do.call(rbind,recdata_sum_A))
colnames(recdata_A_table) = paste("Time", 0:simlength)
rownames(recdata_A_table) = paste("Sim Rec A", 1:simnumber)
recdata_A_table = as.matrix(recdata_A_table)
write.csv(recdata_A_table, "PA_7_1_random_Recovered_A.csv")
# Recovered B
recdata_B_table = as.data.frame(do.call(rbind,recdata_sum_B))
colnames(recdata_B_table) = paste("Time", 0:simlength)
rownames(recdata_B_table) = paste("Sim Rec B", 1:simnumber)
recdata_B_table = as.matrix(recdata_B_table)
write.csv(recdata_A_table, "PA_7_1_random_Recovered_B.csv")
# The same procedure must also be repeated for the incidence data
# Incidence A
incidence_A_table = as.data.frame(do.call(rbind,incidence_sum_A))
colnames(incidence_A_table) = paste("Time", 1:simlength)
rownames(incidence_A_table) = paste("Sim Inc A", 1:simnumber)
incidence_A_table = as.matrix(incidence_A_table)
write.csv(incidence_A_table, "PA_7_1_random_Incidence_A.csv")
# Incidence B
incidence_B_table = as.data.frame(do.call(rbind, incidence_sum_B))
colnames(incidence_B_table) = paste("Time", 1:simlength)
rownames(incidence_B_table) = paste("Sim Inc B", 1:simnumber)
incidence_B_table = as.matrix(incidence_B_table)
write.csv(incidence_B_table, "PA_7_1_random_Incidence_B.csv")
################### CALCULATE AVERAGE OF DATA & FORMAT DATA ###################
# To caculate the averages, calculate the column sums of the respective matrices and divide them by the number
# of simulations
avg_infdata_sum_A = colSums(infdata_A_table, na.rm = FALSE, dims = 1L)/simnumber
avg_infdata_sum_B = colSums(infdata_B_table, na.rm = FALSE, dims = 1L)/simnumber
avg_susdata_sum_A = colSums(susdata_A_table, na.rm = FALSE, dims = 1L)/simnumber
avg_susdata_sum_B = colSums(susdata_B_table, na.rm = FALSE, dims = 1L)/simnumber
avg_recdata_sum_A = colSums(recdata_A_table, na.rm = FALSE, dims = 1L)/simnumber
avg_recdata_sum_B = colSums(recdata_B_table, na.rm = FALSE, dims = 1L)/simnumber
avg_incidence_sum_A = colSums(incidence_A_table, na.rm = FALSE, dims = 1L)/simnumber
avg_incidence_sum_B = colSums(incidence_B_table, na.rm = FALSE, dims = 1L)/simnumber
# Combine all average data together for plotting purposes
avg_combined = rbind(avg_infdata_sum_A, avg_infdata_sum_B, avg_susdata_sum_A, avg_susdata_sum_B, avg_recdata_sum_A, avg_recdata_sum_B)
# Combine all data together for plotting purposes
combined = rbind(infdata_A_table, infdata_B_table, susdata_A_table, susdata_B_table, recdata_A_table, recdata_B_table)
# Combine all average incidence data together for plotting purposes
avg_incidence_combined = rbind(avg_incidence_sum_A, avg_incidence_sum_B)
# Combine all incidence data together for plotting purposes
incidence_combined = rbind(incidence_A_table, incidence_B_table)
# For coloring purposes, save the row names of avg_combined
avg.plot.rownames = row.names(avg_combined)
# For coloring purposes, save the row names of combined
plot.rownames = row.names(combined)
# For coloring purposes, save the row names of the combined incidence
inc.plot.rownames = row.names(incidence_combined)
avg.inc.plot.rownames = row.names(avg_incidence_combined)
# Find all row names for the respective data
# This works best using the grep function and then using the wild card *
# Because we use ifelse, we do not need to define all, but only all minus 1
inf_A_row = plot.rownames[grep("Sim Inf A *", plot.rownames)]
inf_B_row = plot.rownames[grep("Sim Inf B *", plot.rownames)]
sus_A_row = plot.rownames[grep("Sim Sus A *", plot.rownames)]
sus_B_row = plot.rownames[grep("Sim Sus B *", plot.rownames)]
rec_A_row = plot.rownames[grep("Sim Rec A* ", plot.rownames)]
# Repeat this procedure also for the incidence
inc_A_row = inc.plot.rownames[grep("Sim Inc A *", inc.plot.rownames)]
################### PREVALENCE - ALL ###################
# Plot all data - combined
setEPS()
postscript("PA_7_1_random_Prevalence.eps")
matplot(t(combined), type="l", lty = 1, col = ifelse(plot.rownames %in% inf_A_row, "red",
ifelse(plot.rownames %in% inf_B_row, "orange",
ifelse(plot.rownames %in% sus_A_row, "blue",
ifelse(plot.rownames %in% sus_B_row, "deepskyblue",
ifelse(plot.rownames %in% rec_A_row, "forestgreen", "green"))))),
xlab = "Time", ylab = "Number of Individuals")
title(main = "Prevalence")
legend("right", inset = 0.03, cex = 0.6, legend=c("Infected A", "Infected B", "Susceptible A", "Susceptible B", "Recovered A", "Recovered B"),
col=c("red","orange","blue","deepskyblue", "forestgreen", "green"), lty = 1:1)
dev.off()
################### PREVALENCE - AVERAGE ###################
# Plot the average data - avg_combined
setEPS()
postscript("PA_7_1_random_Prevalence_Average.eps")
matplot(t(avg_combined), type="l", lty = 1, lwd = 3, col = ifelse(avg.plot.rownames == "avg_infdata_sum_A", "red",
ifelse(avg.plot.rownames == "avg_infdata_sum_B", "orange",
ifelse(avg.plot.rownames == "avg_susdata_sum_A", "blue",
ifelse(avg.plot.rownames == "avg_susdata_sum_B", "deepskyblue",
ifelse(avg.plot.rownames == "avg_recdata_sum_A", "forestgreen", "green"))))),
xlab = "Time", ylab = "Number of Individuals")
title(main = paste("Average Prevalence over", simnumber, "Simulations"))
legend("right", inset = 0.03, cex = 0.6, legend=c("Infected A", "Infected B", "Susceptible A", "Susceptible B", "Recovered A", "Recovered B"),
col=c("red","orange","blue","deepskyblue", "forestgreen", "green"), lty = 1:1)
dev.off()
################### INCIDENCE - ALL ###################
# Plot the incidence
setEPS()
postscript("PA_7_1_random_Incidence.eps")
matplot(t(incidence_combined), type="l", lty = 1, col = ifelse(inc.plot.rownames %in% inc_A_row, "darkorchid", "deeppink"), xlab = "Time", ylab = "Number of Individuals")
title(main = "Incidence: New Cases per Day")
legend("topright", inset = .10, legend=c("Disease A", "Disease B"), col=c("darkorchid", "deeppink"), lty = 1)
dev.off()
################### INCIDENCE - AVERAGE ###################
# Plot the incidence
setEPS()
postscript("PA_7_1_random_Incidence_Average.eps")
matplot(t(avg_incidence_combined), type="l", lty = 1, lwd = 3,
col = ifelse(avg.inc.plot.rownames == "avg_incidence_sum_A", "darkorchid", "deeppink"),
xlab = "Time", ylab = "Number of newly infected Individuals")
title(main = paste("Average Incidence over", simnumber, "Simulations"))
legend("topright", inset = .10, legend=c("Disease A", "Disease B"), col=c("darkorchid", "deeppink"), lty = 1, lwd = 3)
dev.off()
################### TOTAL NUMBER OF INFECTED INDIVIDUALs - AVERAGE ###################
# Disease A
complete_A_avg = Reduce("+", complete_infected_sum_A)/simnumber
# Disease B
complete_B_avg = Reduce("+", complete_infected_sum_B)/simnumber
# Combination of both
combined_complete_avg = c(complete_A_avg, complete_B_avg)
# Barplot
setEPS()
postscript("PA_7_1_random_Average_Number.eps")
bp = barplot(combined_complete_avg, xaxs = "r" , ylim = c(0,1300), space = c(1.1,1.1), width = 0.4, xlim = c(0,2),
names.arg = c("Malware A","Malware B"), horiz = FALSE,
main = paste("Total number of infected individuals \n during the Epidemic Outbreak \n (Average Number over", simnumber, "Simulations)"),
cex.main = 0.98, col=c("red","darkorange"))
text(bp, 1200, labels = round(combined_complete_avg, digits=0))
dev.off()
################### TOTAL NUMBER OF INFECTED INDIVIDUALs - PERCENTAGE ###################
# Disease A
complete_A_per = complete_A_avg/N*100
complete_A_per = round(complete_A_per, digits = 2)
# Disease B
complete_B_per = complete_B_avg/N*100
complete_B_per = round(complete_B_per, digits = 2)
# Combination of both
combined_complete_per = c(complete_A_per, complete_B_per)
# Barplot
setEPS()
postscript("PA_7_1_random_Percentage.eps")
bp_p = barplot(combined_complete_per, xaxs = "r" , ylim = c(0,100), space = c(1.1,1.1), width = 0.4, xlim = c(0,2),
names.arg = c("Malware A","Malware B"), horiz = FALSE,
main = paste("Percentage of infected individuals \n during the Epidemic Outbreak"),
col=c("red","darkorange"))
text(bp_p, 90, labels = combined_complete_per)
dev.off()
################### PEAK TIME ###################
# Disease A
complete_peaktime_A = Reduce("+", peaktime_A_sum)/simnumber
# Disease B
complete_peaktime_B = Reduce("+", peaktime_B_sum)/simnumber
# Combination of both
combined_peaktime = c(complete_peaktime_A, complete_peaktime_B)
# Barplot
setEPS()
postscript("PA_7_1_random_Peaktime.eps")
bp_p = barplot(combined_peaktime, xaxs = "r" , xlim = c(0,10), space = c(0.2,1.1), width = 0.2,
names.arg = c("Malware A","Malware B"), horiz = TRUE,
main = ("Peak"),
col=c("red","darkorange"))
text(9, bp_p, labels = round(combined_peaktime, digits=1))
dev.off()
################### PEAK NUMBER ###################
# Disease A
complete_peaknumber_A = Reduce("+", peaknumber_A_sum)/simnumber
# Disease B
complete_peaknumber_B = Reduce("+", peaknumber_B_sum)/simnumber
# Combination of both
combined_peaknumber = c(complete_peaknumber_A, complete_peaknumber_B)
# Barplot
setEPS()
postscript("PA_7_1_random_Peaknumber.eps")
bp_p = barplot(combined_peaknumber, xaxs = "r" , xlim = c(0,1300), space = c(0.2,1.1), width = 0.2,
names.arg = c("Malware A","Malware B"), horiz = TRUE,
main = ("Peak Number of Infected Vertices"),
col=c("red","darkorange"))
text(1100, bp_p, labels = round(combined_peaknumber, digits=0))
dev.off()
################### INCIDENCE PEAK TIME ###################
# Disease A
complete_incidence_peaktime_A = Reduce("+", incidence_peaktime_A_sum)/simnumber
# Disease B
complete_incidence_peaktime_B = Reduce("+", incidence_peaktime_B_sum)/simnumber
# Combination of both
combined_incidence_peaktime = c(complete_incidence_peaktime_A, complete_incidence_peaktime_B)
# Barplot
setEPS()
postscript("PA_7_1_random_Peaktime_Incidence.eps")
bp_p = barplot(combined_incidence_peaktime, xaxs = "r" , xlim = c(0,6.5), space = c(0.2,1.1), width = 0.2,