-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow.R
153 lines (127 loc) · 4.58 KB
/
workflow.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
## header
# library(lowerGPreff)
# library(GPreff)
linelist_file <- "../sitassess-data/processed_linelist_20231102.RDS"
linelist_all_dates <- readRDS(linelist_file)
study_seq <- seq(from = as.Date('2021-06-01'), to = as.Date('2021-12-31'), 'days')
notif_dat <- linelist_all_dates |>
dplyr::filter(test_type == 'PCR',
date_confirmation %in% study_seq,
import_status == 'local') |>
dplyr::select(c(date_confirmation, state)) |>
dplyr::rename(date = date_confirmation,
jurisdiction = state) |>
dplyr::group_by(date, jurisdiction) |>
dplyr::summarise(count = dplyr::n()) |>
tidyr::pivot_wider(id_cols = date,
names_from = jurisdiction,
values_from = count,
values_fn = sum,
values_fill = 0) |>
fill_date_gaps() |>
tidyr::pivot_longer(!date,
names_to = 'jurisdiction',
values_to = 'count')
# jurisdictions
jurisdictions <- unique(notif_dat$jurisdiction)
n_jurisdictions <- length(jurisdictions)
## delay data
notif_delay_file <- 'data/ECDF_delay_constant_PCR.rds'
notif_delay_ecdf <- readRDS(notif_delay_file)
## days of infection timeseries
infection_days <- seq(from = as.Date('2021-05-03'),
to = as.Date('2022-01-29'), 'days')
n_days_infection <- length(infection_days)
## proportions
car <- GPreff::expand_constant_value(
dates = infection_days,
jurisdictions = jurisdictions,
constant_val = 0.75,
col_name = 'prop')
## incubation period
incubation_period_distribution <- lowerGPreff::make_cdf(
option = "Delta")
## formatted delay distribution
notif_delay_dist <- GPreff::expand_constant_value(
dates = infection_days,
jurisdictions = jurisdictions,
constant_val = list(notif_delay_ecdf),
col_name = 'delay_fxn')
notif_full_delay_dist <- lowerGPreff::extend_delay_data(
notif_delay_dist,
incubation_period_distribution)
## generation interval distribution
gi_distribution_data <- readr::read_csv(
file = "data/nishiura_samples.csv",
col_types = readr::cols(param1 = readr::col_double(),
param2 = readr::col_double()))
generation_interval_distribution <- lowerGPreff::make_generation_interval_density(
gi_distribution_data)
## optional day-of-week effect model
dow_model <- lowerGPreff::create_dow_priors(
n_jurisdictions)
## infection timeseries model
infection_model_objects <- lowerGPreff::create_infection_timeseries(
n_days_infection,
n_jurisdictions,
effect_type = 'growth_rate')
## observation models
notif_observation_model_objects <- lowerGPreff::create_observation_model(
infection_timeseries = infection_model_objects$infection_timeseries,
delay_distribution = notif_full_delay_dist,
proportion_observed = car,
count_data = notif_dat,
dow_model = dow_model,
data_id = 'notif')
## Reff
reff_model_objects <- lowerGPreff::estimate_reff(
infection_timeseries = infection_model_objects$infection_timeseries,
generation_interval_mass_fxns = generation_interval_distribution)
## greta model fit
m <- greta::model(infection_model_objects$infection_timeseries,
reff_model_objects$reff)
plot(m)
fit <- GPreff::fit_model(
model = m,
n_chains = 4,
max_convergence_tries = 1,
warmup = 1000,
n_samples = 1000,
n_extra_samples = 1000)
# long is classic output, then map to infection traj.
infections_out <- GPreff::generate_long_estimates(
infection_model_objects$infection_timeseries,
fit,
infection_days,
jurisdictions)
reff_out <- GPreff::generate_long_estimates(
reff_model_objects$reff,
fit,
infection_days,
jurisdictions)
infection_traj <- GPreff::build_trajectories(
param = infection_model_objects$infection_timeseries,
infection_days,
fit,
nsim = 1000,
jurisdictions)
GPreff::plot_reff_interval_curves(
'../sitassess-data/outputs/reff2.png',
reff_out,
dates = infection_days,
start_date = min(study_seq),
end_date = max(study_seq),
jurisdictions = jurisdictions)
infection_sims <- greta::calculate(infection_model_objects$infection_timeseries,
values = fit,
nsim = 1000)
GPreff::plot_timeseries_sims(
'../sitassess-data/outputs/infection_timeseries2.png',
infection_sims[[1]],
type = "infection",
dates = infection_days,
start_date = study_seq[1],
end_date = study_seq[length(study_seq)],
states = jurisdictions,
dim_sim = "2",
infection_nowcast = FALSE)