-
Notifications
You must be signed in to change notification settings - Fork 6
/
02_EPINOW2_infections.R
79 lines (62 loc) · 2.22 KB
/
02_EPINOW2_infections.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
library(tidyverse)
require(EpiNow2)
url <- "https://raw.githubusercontent.com/cmilando/RtEval/main/all_data.RDS"
all_data <- readRDS(url(url))
# ********************************
# Define input variables (replacing Shiny inputs)
case_choice <- 'daily_infections'
# Options: 'Daily Infections', 'Daily Onsets', 'Daily Reports'
rr <- which(colnames(all_data$cases) == case_choice)
# columsn are called `date` and `confirm`
incidence_df = data.frame(
date = lubridate::make_date(2020, 3, 19) + 1:nrow(all_data$cases),
confirm = as.vector(all_data$cases[, rr]))
dim(incidence_df)
colnames(incidence_df) <- c('date', 'confirm')
head(incidence_df)
tail(incidence_df)
any(is.na(incidence_df))
####
gi_pmf <- NonParametric(pmf = all_data$serial$Px)
#delay_pmf <- NonParametric(pmf = all_data$reporting_delay$Px)
# Actuall takes several minutes
## HMM THIS ALSO DOESNT LIKE AN INCOMPLETE RECORD
res_epinow <- epinow(
data = incidence_df,
generation_time = generation_time_opts(gi_pmf),
delays = delay_opts(dist = Fixed(0)),
backcalc = backcalc_opts(prior = 'infections'),
rt = rt_opts(rw = 1),
stan = stan_opts(chains = 4, cores = 4)
)
incidence_df$day = all_data$cases$day
y_extract <- rstan::extract(res_epinow$estimates$fit)$R
dim(y_extract)
# y_date = res_epinow$estimates$summarised %>% filter(variable == 'R')
# head(y_date)
# View(res_epinow$samples)
plot_data <- data.frame(
package = "EpiNow2",
date = c(all_data$cases$day, max(all_data$cases$day) + 1:7),
Rt_median = apply(y_extract, 2, quantile, probs = 0.5),
Rt_lb = apply(y_extract, 2, quantile, probs = 0.025),
Rt_ub = apply(y_extract, 2, quantile, probs = 0.975)
)
as_tibble(plot_data) %>%
ggplot() +
geom_hline(yintercept = 1, linetype = "11") +
# *******
# this is the true r(t), back-calculated
geom_line(aes(x = Day, y = Rt_calc), data = all_data$rt) +
# *******
geom_ribbon(aes(x = date, ymin = Rt_lb, ymax = Rt_ub, fill = package),
alpha = 0.25) +
geom_line(aes(x = date, y = Rt_median, color = package)) +
coord_cartesian(ylim = c(0, 5)) +
xlab("Days") +
ylab("Rt") +
theme(
axis.text = element_text(size = 10),
axis.title = element_text(size = 14)
)
saveRDS(plot_data, "plot_objects/plot_data_EpiNow2_infections.RDS")