-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-epi-analysis.Rmd
2182 lines (1704 loc) · 63.4 KB
/
main-epi-analysis.Rmd
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
---
title: "Analysis of the data from the WKU game"
author: "Salihu Musa, Trusting Inekwe, Andres Colubri"
output: html_document
date: "2024-02-12"
---
# (1) "Histories1": We generated daily infections named "hist5".
```{r}
library(dplyr)
library(tidyr)
# Load the data - Need to retrieve histories.csv from the Zenodo repository:
# https://zenodo.org/records/10674401
data <- read.csv("./data/histories.csv")
# Filter only 'infection' entries and assign 1 to them
data <- data %>%
filter(type == "infection") %>%
mutate(type = 1)
# Convert 'time' to POSIXct format and create new columns for date
data <- data %>%
mutate(time = as.POSIXct(time, origin = "1970-01-01"),
date = as.Date(time))
# Group by 'date' and sum the 'type' column to count total infections per day
result <- data %>%
group_by(date) %>%
summarise(total_infection = sum(type))
print(result)
# Save the result as a CSV file
write.csv(result, "./data/hist5.csv", row.names = FALSE)
```
# (2) Time series plot of the generated data (a)
```{r}
###############################
#### This one prints total_infection and cumulative_cases separately
###############################
library(dplyr)
library(tidyr)
library(ggplot2)
# Load the data
data <- read.csv("./data/hourly_data_full.csv")
# Convert 'date' and 'hour' to POSIXct format
data$datetime <- as.POSIXct(paste(data$date, data$hour), format="%m/%d/%Y %H:%M:%S")
# Generate cumulative cases column
data <- data %>%
mutate(cumulative_case = cumsum(total_infection))
# Save the updated data as CSV
write.csv(data, "./hourly_data_with_cumulative.csv", row.names = FALSE)
# Plot time series for total infection and cumulative case
plot_total <- ggplot(data, aes(x=datetime, y=total_infection)) +
geom_line(color="black", size=1.0) +
labs(x="Date and Time", y="Infection", title="Time Series of Total Infections") +
theme_minimal()
plot_cumulative <- ggplot(data, aes(x=datetime, y=cumulative_case)) +
geom_line(color="brown", size=1.0) +
labs(x="Date and Time", y="Cumulative Case", title="Time Series of Cumulative Case") +
theme_minimal()
# Print and save each plot
print(plot_total)
print(plot_cumulative)
# Save the plots in the same folder as the data
ggsave("./plots/total_infection_plot.png", plot = plot_total, width = 8, height = 6)
ggsave("./plots/cumulative_case_plot.png", plot = plot_cumulative, width = 8, height = 6)
######################################
```
# (3) Time series plot of the generated data (a)
```{r}
###############################
#### This one prints total_infection and cumulative_cases separately
###############################
library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
# Load the data
data <- read.csv("./data/hourly_data_full.csv")
# Convert 'date' and 'hour' to POSIXct format
data$datetime <- as.POSIXct(paste(data$date, data$hour), format="%m/%d/%Y %H:%M:%S")
# Generate cumulative cases column
data <- data %>%
mutate(cumulative_case = cumsum(total_infection))
# Save the updated data as CSV
write.csv(data, "./data/hourly_data_with_cumulative.csv", row.names = FALSE)
# Plot time series for total infection and cumulative case
plot_total <- ggplot(data, aes(x=datetime, y=total_infection)) +
geom_line(color="#1b9e77", size=1.2) +
geom_point(color="#d95f02", size=2) +
labs(x="Date and Time", y="# of cases (hourly)", title="") +
theme_minimal(base_size = 15) +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1),
axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"),
panel.grid.major = element_line(color = "gray80"),
panel.grid.minor = element_line(color = "gray90")
) +
scale_x_datetime(labels = date_format("%b %d %H:%M"), breaks = date_breaks("2 days"))
plot_cumulative <- ggplot(data, aes(x=datetime, y=cumulative_case)) +
geom_line(color="#7570b3", size=1.2) +
geom_point(color="#e7298a", size=2) +
labs(x="Date and time", y="cum. # of cases", title="") +
theme_minimal(base_size = 15) +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1),
axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"),
panel.grid.major = element_line(color = "gray80"),
panel.grid.minor = element_line(color = "gray90")
) +
scale_x_datetime(labels = date_format("%b %d %H:%M"), breaks = date_breaks("2 days"))
# Print and save each plot
print(plot_total)
print(plot_cumulative)
# Save the plots in the same folder as the data
ggsave("./plots/total_infection_plot.png", plot = plot_total, width = 12, height = 8, dpi = 300)
ggsave("./plots/cumulative_case_plot.png", plot = plot_cumulative, width = 12, height = 8, dpi = 300)
######################################
```
# (4) Time series plot of the generated data (a)
# Combined Plots a&b
```{r}
###############################
## This one prints total_infection and cumulative_cases on the same figure
###############################
library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
library(patchwork)
# Load the data
data <- read.csv("./data/hourly_data_full.csv")
# Convert 'date' and 'hour' to POSIXct format
data$datetime <- as.POSIXct(paste(data$date, data$hour), format="%m/%d/%Y %H:%M:%S")
# Generate cumulative cases column
data <- data %>%
mutate(cumulative_case = cumsum(total_infection))
# Save the updated data as CSV
write.csv(data, "./data/hourly_data_with_cumulative.csv", row.names = FALSE)
# Plot time series for total infection
plot_total <- ggplot(data, aes(x=datetime, y=total_infection)) +
geom_line(color="#1b9e77", size=1.2) +
geom_point(color="#d95f02", size=2) +
labs(y="# of cases (hourly)", title="") +
theme_minimal(base_size = 15) +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
axis.text.x = element_blank(), # Remove x-axis text for the first plot
axis.ticks.x = element_blank(), # Remove x-axis ticks for the first plot
axis.title.x = element_blank(), # Remove x-axis title for the first plot
axis.title.y = element_text(face = "bold"),
panel.grid.major = element_line(color = "gray80"),
panel.grid.minor = element_line(color = "gray90")
) +
scale_x_datetime(labels = date_format("%b %d %H:%M"), breaks = date_breaks("2 days")) +
labs(tag = "(a)")
# Plot time series for cumulative cases
plot_cumulative <- ggplot(data, aes(x=datetime, y=cumulative_case)) +
geom_line(color="#7570b3", size=1.2) +
geom_point(color="#e7298a", size=2) +
labs(x="Date and time", y="cum. # of cases", title="") +
theme_minimal(base_size = 15) +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1),
axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"),
panel.grid.major = element_line(color = "gray80"),
panel.grid.minor = element_line(color = "gray90")
) +
scale_x_datetime(labels = date_format("%b %d %H:%M"), breaks = date_breaks("2 days")) +
labs(tag = "(b)")
# Combine the plots into one figure
combined_plot <- plot_total / plot_cumulative +
plot_layout(ncol = 1, heights = c(1, 1)) +
plot_annotation(
title = "",
theme = theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold")
)
)
# Print and save the combined plot
print(combined_plot)
# Save the combined plot in the same folder as the data
ggsave("./plots/infection_data_combined_plot.png", plot = combined_plot, width = 12, height = 12, dpi = 300)
############################
```
# (5) Time series plot of the generated data (b)
# Combined cases & cumulative on same graph
```{r}
###################
## This one print both total_infection and cummulative_cases on the same graph
##################
library(ggplot2)
library(dplyr)
# Load the data
data <- read.csv("./data/hourly_data_full.csv")
# Convert 'date' and 'hour' to POSIXct format
data$datetime <- as.POSIXct(paste(data$date, data$hour), format="%m/%d/%Y %H:%M:%S")
# Calculate cumulative cases
data <- data %>%
mutate(cumulative_case = cumsum(total_infection))
# Plot the time series for total infection and cumulative case
ggplot(data, aes(x=datetime)) +
geom_line(aes(y=total_infection, color="Total Infection"), size=1.0) +
geom_line(aes(y=cumulative_case, color="Cumulative Case"), size=1.0) +
labs(x="Date and Time", y="Count", title="Time Series of Total Infection and Cumulative Case") +
scale_color_manual(values=c("Total Infection"="orange", "Cumulative Case"="blue")) +
theme_minimal()
# Save the plot in the same folder as the data
ggsave("./plots/ts_plot_combined.png", plot = last_plot(), width = 8, height = 6)
```
# (6) Time series plot of generated data (c)
# (similar to plot (a) with diff scale)
```{r}
########################
## This one plots only the total_infection
######################
library(ggplot2)
# Load the data
data <- read.csv("./data/hourly_data_full.csv")
# Convert 'date' and 'hour' to POSIXct format
data$datetime <- as.POSIXct(paste(data$date, data$hour), format="%m/%d/%Y %H:%M:%S")
# Plot the time series
ggplot(data, aes(x=datetime, y=total_infection, color="T Infection")) +
geom_line(size=1.0) +
labs(x="Date and Time", y="Total Infection", title="Time Series of Total Infection") +
theme_minimal() +
theme(legend.position = "top") +
scale_x_datetime(date_breaks = "1 day", date_labels = "%m/%d") + # Increase x-axis scale
guides(color=guide_legend(title=NULL)) # Legend without mentioning color
plot(data)
# Save the plot in the same folder as the data
ggsave("./plots/ts_plot2.png", plot = last_plot(), width = 8, height = 6)
```
# (7) Time series plot of the generated data (e)
# Combined cases & cumulative on same graph
# Stacked Area Charts plot
```{r}
##################################
# Stacked Area Charts
library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
library(patchwork)
# Load the data
data <- read.csv("./data/hourly_data_with_cumulative.csv")
# Convert 'date' and 'hour' to POSIXct format
data$datetime <- as.POSIXct(paste(data$date, data$hour), format="%m/%d/%Y %H:%M:%S")
# Plot time series for total infection
plot_total <- ggplot(data, aes(x=datetime, y=total_infection)) +
geom_line(color="#1b9e77", size=1.2) +
geom_point(color="#d95f02", size=2) +
labs(y="# of cases (hourly)") +
theme_minimal(base_size = 15) +
theme(
plot.title = element_text(hjust = 0, face = "bold"),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_text(face = "bold"),
panel.grid.major = element_line(color = "gray80"),
panel.grid.minor = element_line(color = "gray90"),
plot.margin = margin(t = 10, r = 10, b = 0, l = 10)
) +
scale_x_datetime(labels = date_format("%b %d %H:%M"), breaks = date_breaks("2 days")) +
labs(tag = "(a)")
# Plot stacked area chart for cumulative cases
plot_cumulative <- ggplot(data, aes(x=datetime, y=cumulative_case, fill=cumulative_case)) +
geom_area(alpha=0.6) +
labs(x="Date and time", y="cum. # of cases") +
theme_minimal(base_size = 15) +
theme(
plot.title = element_text(hjust = 0, face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1),
axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"),
panel.grid.major = element_line(color = "gray80"),
panel.grid.minor = element_line(color = "gray90"),
plot.margin = margin(t = 0, r = 10, b = 10, l = 10),
legend.position = "none"
) +
scale_x_datetime(labels = date_format("%b %d %H:%M"), breaks = date_breaks("2 days")) +
scale_fill_gradient(low="#7570b3", high="#e7298a") +
labs(tag = "(b)")
# Combine the plots into one figure
combined_plot <- plot_total / plot_cumulative +
plot_layout(ncol = 1) +
plot_annotation(title = "", theme = theme(plot.title = element_text(hjust = 0.5)))
# Print and save the combined plot
print(combined_plot)
# Save the combined plot in the same folder as the data
ggsave("./plots/infection_data_combined_plot.png", plot = combined_plot, width = 12, height = 12, dpi = 300)
###########################################
```
# (8) both plots on same graph
```{r}
#####################################
# Load necessary libraries
library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
# Load the data
data <- read.csv("./data/hourly_data_with_cumulative.csv")
# Convert 'date' and 'hour' to POSIXct format
data$datetime <- as.POSIXct(paste(data$date, data$hour), format="%m/%d/%Y %H:%M:%S")
# Plot both total infection and cumulative cases on the same graph
combined_plot <- ggplot(data) +
geom_line(aes(x = datetime, y = total_infection, color = "Total Infections"), size = 1.2) +
geom_point(aes(x = datetime, y = total_infection, color = "Total Infections"), size = 2) +
geom_area(aes(x = datetime, y = cumulative_case, fill = "Cumulative Cases"), alpha = 0.6, color = NA) +
scale_y_continuous(
name = "# of cases (hourly)",
sec.axis = sec_axis(~., name = "cum. # of cases")
) +
scale_color_manual(name = NULL, values = c("Total Infections" = "red")) +
scale_fill_manual(name = NULL, values = c("Cumulative Cases" = "#7570b3")) +
labs(x = "Date and time", y = "# of cases (hourly)", title = "") +
theme_minimal(base_size = 15) +
theme(
plot.title = element_text(size = 20, face = "bold", hjust = 0.5),
axis.title.y.left = element_text(face = "bold", color = "red"),
axis.title.y.right = element_text(face = "bold", color = "#7570b3"),
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major = element_line(color = "gray80"),
panel.grid.minor = element_line(color = "gray90"),
legend.position = c(0.05, 0.95), # Positioning legend inside the plot
legend.justification = c(0, 1), # Adjusting the legend position
legend.title = element_blank(),
legend.text = element_text(size = 12)
) +
scale_x_datetime(labels = date_format("%b %d %H:%M"), breaks = date_breaks("2 days"))
# Print and save the combined plot
print(combined_plot)
# Save the combined plot in the same folder as the data
ggsave("./plots/infection_data_combined_plot.png", plot = combined_plot, width = 12, height = 8, dpi = 300)
########################################
```
# (9) Simulation
### Sub-Model
# hourly_data_with_cumulative.csv # daily_data_full_N1.csv
```{r}
##############################
# Clear the environment
rm(list = ls())
# Load necessary libraries
library(dplyr)
library(ggplot2)
library(stringr)
library(cowplot)
# Read the new CSV file with daily data
data <- read.csv("./data/daily_data_full_N1_ssm.csv")
# Convert 'date' column to Date format
data$date <- as.Date(data$date, format = "%m/%d/%Y")
# Get the initial number of confirmed cases
initial_confirm <- data$confirm[1]
# Define the simulation function
simulate <- function(epsilon1 = 0.5, zeta = 0.5, Ro = 3.732787, initial_confirm = 1) {
N <- 794
Sn <- 472
En <- 50
Pn <- 17
An <- 16
In <- initial_confirm # Start with the initial number of confirmed cases
R <- N * 0.1
C <- 0
C0 <- 0
P <- 0
# Parameters
xi <- 0.5
alpha <- 0.205
epsilon <- 0.1
omega <- 0.185
thetan <- 1/3
sigman <- 0.3
kappa <- 0.7
tauAn <- 10/30
tauIn <- 10/29.6
m <- 1/1.5
R0 <- Ro
dt <- 1.5 #365 / 365
result <- numeric(40)
for (i in 1:40) {
beta <- ifelse(i > 20 & i < 30, 0.75, 0)
n1 <- ceiling(((Pn + xi * An + alpha * In) / N) * (R0 * (sigman * tauIn * tauAn)) / ((alpha * kappa * sigman * tauAn) + (tauIn * xi * sigman * (1 - kappa)) + (tauIn * tauAn)) * Sn * dt * (1 - epsilon) * (1 - P / N)^zeta)
n3 <- ceiling(omega * R * dt)
n6 <- ceiling(thetan * En * dt)
n9 <- ceiling(sigman * Pn * dt)
n11 <- ceiling(sigman * (1 - kappa) * Pn * dt)
n12 <- ceiling(tauAn * An * dt)
n15 <- ceiling(sigman * kappa * Pn * dt)
n16 <- ceiling(tauIn * In * dt)
n19 <- ceiling(m * P * dt)
Sn <- Sn - n1 + n3
En <- En + n1 - n6
Pn <- Pn + n6 - n9
An <- An + n11 - n12
In <- In + n15 - n16
R <- R + n12 + n16 - n3
C <- C + n9
P <- P + (n12 + n16) * 0.2 - n19
C0 <- C0 + n1
Sn <- Sn - ceiling(Sn * beta)
En <- En - ceiling(En * beta)
Pn <- Pn - ceiling(Pn * beta)
An <- An - ceiling(An * beta)
In <- In - ceiling(In * beta)
R <- R - ceiling(R * beta)
N <- N - ceiling(N * beta)
if (i > 20) epsilon <- epsilon1 / 2
if (i > 30) epsilon <- epsilon1
result[i] <- C
}
result
}
# Define incidence values based on simulations
incid <- data.frame(
value = c(
diff(simulate(initial_confirm = initial_confirm)),
diff(simulate(0, 100, initial_confirm = initial_confirm)),
diff(simulate(0, 1000, initial_confirm = initial_confirm))
),
type = rep(
c("naive", "IP", "IP+NPI"),
each = 39
),
date = rep(seq(as.Date("2023-11-20"), length.out = 39, by = "day"), 3)
)
# Combine data with simulated data
incid <- rbind(
data.frame(value = data$confirm, type = "reported", date = data$date),
incid
)
# Adjust factor levels
incid$type <- factor(
incid$type,
levels = c("reported", "naive", "IP", "IP+NPI")
)
# Calculate breaks and labels for x-axis
start_date <- as.Date("2023-11-20")
end_date <- as.Date("2024-01-01")
breaks <- seq(start_date, end_date, by = "week")
labels <- format(breaks, "%b") # Abbreviated month names
##################################
# Plotting p.incid
p.incid <- incid %>%
ggplot(aes(x = date, y = value, colour = type)) +
geom_line(aes(linetype = type)) +
geom_point(aes(shape = type)) +
scale_y_log10(breaks = 10^(1:3), labels = 10^(1:3)) +
scale_x_date(breaks = breaks, labels = labels) + # Automatic breaks and labels
scale_color_manual(
labels = stringr::str_wrap(
c("reported", "naive", "IP", "IP + NPI"), 5
),
values = c("black", "#E69F00", "red", "#009E73") # #999999 #56B4E9
) +
scale_linetype_manual(
labels = stringr::str_wrap(
c("reported", "naive", "IP", "IP + NPI"), 5
),
values = c("dotted", "solid", "dashed", "dotted") #solid
) +
scale_shape_manual(
labels = stringr::str_wrap(
c("reported", "naive", "IP", "IP + NPI"), 5
),
values = c(16, 10, 10, 16)
) +
labs(
x = "time (days)",
y = "# of reported cases (daily)",
colour = "",
linetype = "",
shape = ""
) +
theme_minimal() +
theme(
legend.position = c(0.8, 0.8), # Adjust this to position the legend inside the plot
legend.background = element_rect(fill = "white", color = "black"),
axis.text.x = element_text(angle = 45, hjust = 1),
text = element_text(size = 12),
legend.text = element_text(size = 10)
) +
guides(shape = guide_legend(override.aes = list(shape = c(16, 10, 10, 16))))
# Print the plot before saving
print(p.incid)
# Save the plot
ggsave(
filename = "./plots/p_incid_ss3.png",
plot = p.incid,
width = 12, height = 6
)
##################################
# Plotting p.incid
p.incid <- incid %>%
ggplot(aes(x = date, y = value, colour = type)) +
geom_line(aes(linetype = type), size = 1.0) + # Thicker lines for better visibility
geom_point(aes(shape = type), size = 4) + # Larger points for better visibility
scale_y_log10(breaks = 10^(1:3), labels = scales::comma) + # Use commas for labels
scale_x_date(breaks = breaks, labels = labels) + # Automatic breaks and labels
scale_color_manual(
labels = c("Reported", "Naive", "IP", "IP + NPI"), # Simplified labels
values = c("black", "#E69F00", "red", "#009E73") # Bold colors
) +
scale_linetype_manual(
labels = c("Reported", "Naive", "IP", "IP + NPI"), # Simplified labels
values = c("dotted", "solid", "solid", "solid") # c("dotted", "solid", "dashed", "dotted")
) +
scale_shape_manual(
labels = c("Reported", "Naive", "IP", "IP + NPI"), # Simplified labels
values = c(20, NA, NA, NA) # #c(16, 17, 18, 19) #16, 10, 10, 16
) +
labs(
x = "time (days)",
y = "# of reported cases (daily)",
colour = "",
linetype = "",
shape = ""
) +
theme_minimal(base_size = 15) + # Increase base font size for better readability
theme(
legend.position = c(0.8, 0.8), # Adjust this to position the legend inside the plot
legend.background = element_rect(fill = "white", color = "black", size = 0.5, linetype = "solid"),
axis.text.x = element_text(angle = 45, hjust = 1),
text = element_text(size = 14), # Increase text size for better readability
legend.text = element_text(size = 12), # Increase legend text size for better readability
panel.grid.major = element_line(color = "grey80", linetype = "dashed"),
panel.grid.minor = element_line(color = "grey90", linetype = "dotted"),
plot.background = element_rect(fill = "white", color = NA), #aliceblue
panel.background = element_rect(fill = "lightgray", color = NA)
) +
guides(shape = guide_legend(override.aes = list(size = 4))) # Increase legend shape size
# Print the plot before saving
print(p.incid)
# Save the plot
ggsave(
filename = "./plots/p_incid_ss3.png",
plot = p.incid,
width = 12, height = 6
)
#########################
# Plotting p.ratio
p.ratio <- incid %>%
filter(type == "IP+NPI") %>%
filter(date %in% data$date) %>%
mutate(ratio = data$confirm / value) %>%
ggplot(aes(x = date, y = ratio)) +
geom_point(size = 3, shape = 21, fill = "blue", color = "darkblue", stroke = 1) +
geom_line(linetype = "dashed", color = "blue", size = 1) +
coord_cartesian(ylim = c(0, 20)) +
labs(y = "Reporting ratio", x = "") +
theme_minimal() +
theme(
panel.grid.major = element_line(colour = "gray", linetype = "dotted", size = 0.5),
panel.grid.minor = element_line(colour = "gray", linetype = "dotted", size = 0.25),
legend.position = "none",
axis.text = element_text(color = "darkblue", size = 12),
axis.title = element_text(color = "darkblue", size = 14, face = "bold"),
plot.title = element_text(color = "darkblue", size = 16, face = "bold", hjust = 0.5),
plot.background = element_rect(fill = "white", color = NA),
panel.background = element_rect(fill = "lightgray", color = "darkblue", size = 1)
) +
ggtitle("") #Reporting Ratio Over Time
# Print the ratio plot
print(p.ratio)
# Save the ratio plot as a PNG file
output_file_ratio <- "./plots/ratio_plot.png"
ggsave(output_file_ratio, p.ratio, width = 16, height = 8, units = "cm", dpi = 300)
print(paste("Ratio plot saved to", output_file_ratio))
#####################
```
# (10) Sensitivity
# hourly_data_with_cumulative.csv # daily_data_full_N1.csv
```{r}
#####################################
# rm(list=ls())
# library(dplyr)
case <- read.csv("./data/daily_data_full_N1_ssm.csv")
# Create a new column 'incid' with the same values as 'confirm'
case <- mutate(case, incid = confirm)
# Select only the 'date' and 'incid' columns
case <- select(case, date, incid)
#### Figure 5:
library(reshape2)
library(ggplot2)
# Define the simulation function
simulate <- function(epsilon1 = 0.5, zeta = 100, Ro = 3.732787, initial_confirm = 1) {
N <- 794
Sn <- 472
En <- 50
Pn <- 17
An <- 16
In <- initial_confirm # Start with the initial number of confirmed cases
R <- N * 0.1
C <- 0
C0 <- 0
D <- 0
# Parameters
xi <- 0.5
alpha <- 0.205
epsilon <- 0.1
omega <- 0.185
thetan <- 1/3
sigman <- 0.3
kappa <- 0.7
tauAn <- 10/30
tauIn <- 10/29.6
m <- 1/1.5 #0.14
R0 <- Ro
dt <- 1 #365/ 365
result <- numeric(40)
for (i in 1:40) {
beta <- ifelse(i > 20 & i < 40, 0.75, 0)
n1 <- ceiling(((Pn + xi * An + alpha * In) / N) * (R0 * (sigman * tauIn * tauAn)) / ((alpha * kappa * sigman * tauAn) + (tauIn * xi * sigman * (1 - kappa)) + (tauIn * tauAn)) * Sn * dt * (1 - epsilon) * (1 - D / N)^zeta)
n3 <- ceiling(omega * R * dt)
n6 <- ceiling(thetan * En * dt)
n9 <- ceiling(sigman * Pn * dt)
n11 <- ceiling(sigman * (1 - kappa) * Pn * dt)
n12 <- ceiling(tauAn * An * dt)
n15 <- ceiling(sigman * kappa * Pn * dt)
n16 <- ceiling(tauIn * In * dt)
n19 <- ceiling(m * D * dt)
Sn <- Sn - n1 + n3
En <- En + n1 - n6
Pn <- Pn + n6 - n9
An <- An + n11 - n12
In <- In + n15 - n16
R <- R + n12 + n16 - n3
C <- C + n9
D <- D + (n12 + n16) * 0.2 - n19
C0 <- C0 + n1
Sn <- Sn - ceiling(Sn * beta)
En <- En - ceiling(En * beta)
Pn <- Pn - ceiling(Pn * beta)
An <- An - ceiling(An * beta)
In <- In - ceiling(In * beta)
R <- R - ceiling(R * beta)
N <- N - ceiling(N * beta)
if (i > 13) epsilon <- epsilon1 / 2
if (i > 20) epsilon <- epsilon1
result[i] <- C
}
result
}
epsilons <- sapply(seq(0.3, 2.5, by = 0.5), function(a) {
simulate(epsilon1 = a, zeta = 10) %>% diff()
}) %>%
as.data.frame() %>%
cbind(date = seq(as.Date("2023-11-20"), length.out = 39, by = "day")) %>%
melt(id.var = "date")
# Generate data for different zeta values 100, 2000, by = 400
zetas <- sapply(seq(10, 500, by = 100), function(k) {
simulate(epsilon1 = 0.5, zeta = k) %>% diff()
}) %>%
as.data.frame() %>%
cbind(date = seq(as.Date("2023-11-20"), length.out = 39, by = "day")) %>%
melt(id.var = "date")
# Plot for epsilons
p.epsilons <- epsilons %>%
ggplot(aes(x = date, y = value, color = variable, linetype = variable)) +
geom_line(size = 1) +
geom_line(data = case, aes(y = incid, x = as.Date(date, format = "%Y-%m-%d")), colour = "grey", inherit.aes = FALSE, linetype = "dashed", size = 1) +
geom_point(data = case, aes(y = incid, x = as.Date(date, format = "%Y-%m-%d")), colour = "grey", inherit.aes = FALSE, size = 2) +
scale_y_log10(breaks = 10^(1:3), labels = 10^(1:3)) +
scale_linetype_manual(values = c(1, 2, 3, 4, 5)) +
scale_color_manual(values = c("black", "red", "darkgreen", "blue", "cyan")) +
coord_cartesian(ylim = c(1, 1e2)) +
theme_minimal() +
theme(
panel.grid.major = element_line(colour = "lightgrey", linetype = "dashed", size = 0.5),
panel.grid.minor = element_line(colour = "lightgrey", linetype = "dotted", size = 0.2),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14, face = "bold"),
legend.position = "none"
) +
labs(y = "Daily New Infections", x = "Date", title = "")
# Plot for zetas
p.zetas <- zetas %>%
ggplot(aes(x = date, y = value, color = variable, linetype = variable)) +
geom_line(size = 1) +
geom_line(data = case, aes(y = incid, x = as.Date(date, format = "%Y-%m-%d")), colour = "grey", inherit.aes = FALSE, linetype = "dashed", size = 1) +
geom_point(data = case, aes(y = incid, x = as.Date(date, format = "%Y-%m-%d")), colour = "grey", inherit.aes = FALSE, size = 2) +
scale_y_log10(breaks = 10^(1:3), labels = 10^(1:3)) +
scale_linetype_manual(values = c(1, 2, 3, 4, 5)) +
scale_color_manual(values = c("black", "red", "darkgreen", "blue", "cyan")) +
coord_cartesian(ylim = c(1, 1e2)) +
theme_minimal() +
theme(
panel.grid.major = element_line(colour = "lightgrey", linetype = "dashed", size = 0.5),
panel.grid.minor = element_line(colour = "lightgrey", linetype = "dotted", size = 0.2),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14, face = "bold"),
legend.position = "none"
) +
labs(y = "Daily New Infections", x = "Date", title = "")
# Combine the plots
library(cowplot)
plot_grid(p.epsilons, p.zetas, ncol = 2, align = "h",
labels = c("(a)", "(b)"),
label_x = 0.05)
#######################################
```
# (11)
```{r}
# Load necessary libraries
library(dplyr)
library(reshape2)
library(ggplot2)
library(cowplot)
# Read the data
case <- read.csv("./data/daily_data_full_N1_ssm.csv")
# Create a new column 'incid' with the same values as 'confirm'
case <- mutate(case, incid = confirm)
# Select only the 'date' and 'incid' columns
case <- select(case, date, incid)
# Define the simulation function
simulate <- function(epsilon1 = 0.5, zeta = 100, Ro = 3.732787, initial_confirm = 1) {
N <- 794
Sn <- 472
En <- 50
Pn <- 17
An <- 16
In <- initial_confirm
R <- N * 0.1
C <- 0
D <- 0
# Parameters
xi <- 0.5
alpha <- 0.205
epsilon <- 0.1
omega <- 0.185
thetan <- 1/3
sigman <- 0.3
kappa <- 0.7
tauAn <- 10/30
tauIn <- 10/29.6
m <- 1/1.5
R0 <- Ro
dt <- 1
result <- numeric(40)
for (i in 1:40) {
beta <- ifelse(i > 20 & i < 40, 0.75, 0)
n1 <- ceiling(((Pn + xi * An + alpha * In) / N) * (R0 * (sigman * tauIn * tauAn)) / ((alpha * kappa * sigman * tauAn) + (tauIn * xi * sigman * (1 - kappa)) + (tauIn * tauAn)) * Sn * dt * (1 - epsilon) * (1 - D / N)^zeta)
n3 <- ceiling(omega * R * dt)
n6 <- ceiling(thetan * En * dt)
n9 <- ceiling(sigman * Pn * dt)
n11 <- ceiling(sigman * (1 - kappa) * Pn * dt)
n12 <- ceiling(tauAn * An * dt)
n15 <- ceiling(sigman * kappa * Pn * dt)
n16 <- ceiling(tauIn * In * dt)
n19 <- ceiling(m * D * dt)
Sn <- Sn - n1 + n3
En <- En + n1 - n6
Pn <- Pn + n6 - n9
An <- An + n11 - n12
In <- In + n15 - n16
R <- R + n12 + n16 - n3
C <- C + n9