-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredict_Past_Models.R
More file actions
77 lines (62 loc) · 2.51 KB
/
Predict_Past_Models.R
File metadata and controls
77 lines (62 loc) · 2.51 KB
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
library(here)
library(tidyverse)
library(lubridate)
library(tidybayes)
fitted_dates <- here("Past Models", "Models") %>%
list.files %>%
str_match("_(2020-[0-9]{2}-[0-9]{2})\\.rds") %>%
.[, 2] %>%
na.omit %>%
ymd
for (i in seq_along(fitted_dates)) {
if (file.exists(here("Past Models", "Predictions", str_c("Predictions_", fitted_dates[i], ".csv")))) {
next
}
writeLines(str_c("\n",
"Predicting for model from: ",
fitted_dates[i],
"\n"))
m <- here("Past Models", "Models", str_c("Model_", fitted_dates[i], ".rds")) %>%
read_rds
d <- here("Past Models", "Data", str_c("COVID_Data_", fitted_dates[i], ".csv")) %>%
read_csv
res <- spread_draws(m,
alpha[location_id],
beta[location_id],
S[location_id],
phi[location_id],
nu[location_id],
n = 1000) %>%
ungroup %>%
select(-.chain, .iteration, .draw) %>%
group_by(location_id) %>%
mutate(iter = row_number()) %>%
ungroup %>%
inner_join(
d %>%
group_by(location, location_id, population) %>%
summarise(start_date = min(date),
start_cases = min(total_cases)),
by = "location_id"
) %>%
expand_grid(date = seq.Date(ymd("2020-02-01"), ymd("2020-09-01"), by = 1)) %>%
filter(date > start_date) %>%
mutate(days = as.numeric(date - start_date)) %>%
select(-start_date) %>%
mutate(z = beta * (days - alpha),
f = S - S / (1 + nu * exp(z))^(1/nu),
dfdt = beta * (S - f) * (1 - ((S - f) / S)^nu) / nu,
new_cases = rnbinom(n(), mu = dfdt * population, size = phi)) %>%
group_by(location, iter) %>%
mutate(total_cases = as.numeric(cumsum(new_cases * (days > 0))) + start_cases) %>%
ungroup %>%
pivot_longer(c(new_cases, total_cases), names_to = "variable", values_to = "value") %>%
group_by(location, location_id, date, variable) %>%
summarise(median = median(value),
conf.low = quantile(value, .025),
conf.high = quantile(value, .975))
res %>%
write_csv(here("Past Models", "Predictions", str_c("Predictions_", fitted_dates[i], ".csv")))
rm(res)
gc()
}