-
Notifications
You must be signed in to change notification settings - Fork 2
/
skeleton.Rmd
470 lines (410 loc) · 12.1 KB
/
skeleton.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
---
title: "Estimating transmissibility with population stratification"
author: Thibaut Jombart, Hugo Gruson
date: "`r Sys.Date()`"
output:
rmarkdown::html_document:
code_folding: "hide"
css: "style.css"
link-citations: no
params:
epicurve_unit:
label: "An integer or character indicating the (fixed) size of the time interval used for computing the incidence. Passed as the `interval` argument in `incidence2::incidence()`."
value: "week"
incomplete_days:
label: "Number of days to exclude from the estimation of Rt since data is likely to still be incomplete."
value: 7
r_estim_window:
label: "Number of days to include to get the latest observed value of Rt."
value: 21
use_epiparameter_database:
label: "Should the serial interval distribution be extracted directly from the epiparameter package?"
value: FALSE
disease_name:
label: "Name of the disease of interest, also used to select diseases in the epiparameter database if `use_parameter_database = TRUE`."
value: "COVID-19"
si_mean:
label: "Mean of the distribution for serial interval if not using value from epiparameter. Ignored if `use_epiparameter_database = TRUE`."
value: 4.2
si_sd:
label: "Standard deviation of the distribution for serial interval if not using value from epiparameter. Ignored if `use_epiparameter_database = TRUE`."
value: 4.9
si_dist:
label: "Choice of probability distribution for serial interval if not using value from epiparameter. Ignored if `use_epiparameter_database = TRUE`."
value: "gamma"
choices: ["beta", "binom", "cauchy", "chisq", "exp", "f", "gamma", "geom", "hyper", "lnorm", "logis", "nbinom", "norm", "pois", "smirnov", "t", "tukey", "unif", "weibull", "wilcox"]
data_file:
label: "Name of file containing case data, whether a line list or incidence data"
value: "data/covid_linelist_england.rds"
input: file
rt_estimator:
label: "Which R package to use for Rt estimation"
value: "EpiEstim"
choices: ["EpiEstim", "EpiNow2", "i2extras", "R0"]
bibliography:
- grateful-refs.bib
---
```{r lockfile, include = FALSE, message = FALSE}
renv::use(
"MASS@7.3-59",
"Matrix@1.6-3",
"R.methodsS3@1.8.2",
"R.oo@1.25.0",
"R.utils@2.12.3",
"R6@2.5.1",
"RColorBrewer@1.1-3",
"Rcpp@1.0.12",
"askpass@1.2.0",
"backports@1.4.1",
"base64enc@0.1-3",
"bit64@4.0.5",
"bit@4.0.5",
"bslib@0.6.1",
"cachem@1.0.8",
"callr@3.7.5",
"cellranger@1.1.0",
"checkmate@2.3.1",
"cli@3.6.2",
"clipr@0.8.0",
"colorspace@2.1-0",
"commonmark@1.9.1",
"cpp11@0.4.7",
"crayon@1.5.2",
"curl@5.2.1",
"data.table@1.14.8",
"digest@0.6.35",
"distcrete@1.0.3",
"distributional@0.3.2",
"dplyr@1.1.4",
"ellipsis@0.3.2",
"epiverse-trace/epiparameter@328706e", # nolint
"evaluate@0.23",
"fansi@1.0.6",
"farver@2.1.1",
"fastmap@1.1.1",
"fontawesome@0.5.2",
"forcats@1.0.0",
"foreign@0.8-86",
"fs@1.6.3",
"generics@0.1.3",
"ggplot2@3.5.0",
"glue@1.7.0",
"grateful@0.2.4",
"grates@1.1.0",
"gtable@0.3.4",
"haven@2.5.4",
"highr@0.10",
"hms@1.1.3",
"htmltools@0.5.7",
"httpuv@1.6.14",
"httr@1.4.7",
"incidence2@2.2.3",
"isoband@0.2.7",
"janitor@2.2.0",
"jquerylib@0.1.4",
"jsonlite@1.8.8",
"kableExtra@1.3.4",
"knitr@1.45",
"labeling@0.4.3",
"later@1.3.2",
"lattice@0.21-8",
"lifecycle@1.0.4",
"linelist@1.1.0",
"lubridate@1.9.3",
"magrittr@2.0.3",
"memoise@2.0.1",
"mgcv@1.9-1",
"mime@0.12",
"munsell@0.5.0",
"nlme@3.1-163",
"numDeriv@2016.8-1.1",
"openssl@2.1.1",
"pillar@1.9.0",
"pkgconfig@2.0.3",
"prettyunits@1.2.0",
"processx@3.8.4",
"progress@1.2.3",
"promises@1.2.1",
"ps@1.7.6",
"purrr@1.0.2",
"rappdirs@0.3.3",
"readr@2.1.4",
"readxl@1.4.3",
"rematch@2.0.0",
"remotes@2.4.2.1",
"renv@1.0.3",
"rio@1.0.1",
"rlang@1.1.3",
"rmarkdown@2.26",
"rstudioapi@0.15.0",
"rvest@1.0.4",
"sass@0.4.9",
"scales@1.3.0",
"selectr@0.4-2",
"shiny@1.8.0",
"snakecase@0.11.1",
"sourcetools@0.1.7-1",
"stringi@1.8.3",
"stringr@1.5.1",
"svglite@2.1.3",
"sys@3.4.2",
"systemfonts@1.0.6",
"tibble@3.2.1",
"tidyr@1.3.0",
"tidyselect@1.2.0",
"timechange@0.2.0",
"tinytex@0.50",
"epiverse-trace/tracetheme@v0.1.0", # nolint
"tzdb@0.4.0",
"utf8@1.2.4",
"vctrs@0.6.5",
"viridisLite@0.4.2",
"vroom@1.6.5",
"webshot@0.5.5",
"withr@3.0.0",
"writexl@1.4.2",
"xfun@0.42",
"xml2@1.3.6",
"xtable@1.8-4",
"yaml@2.3.8"
)
```
```{r settings, echo = FALSE}
knitr::opts_chunk$set(
fig.width = 9,
fig.height = 5,
dpi = 90,
collapse = TRUE,
message = FALSE,
warning = FALSE,
out.width = "100%"
)
```
# Outline of the report
## Estimating transmissibility from stratified population
This report provides a template for estimating transmissibility (i.e., how fast
a disease spreads) from a stratified population. It performs basic descriptive
analyses, and uses different approaches for estimating transmissibility. The key
steps of the report include:
* importing the data from an external file
* identifying key variables in the data
* producing global and stratified epidemic curves
* estimating the growth rate and doubling time from epidemic curves
* estimating the instantaneous reproduction number from epidemic curves
```{r}
knitr::include_graphics("transmissibility_pipeline.svg")
```
# Data preparation
## Loading libraries
The following code loads required packages; missing packages will be installed
automatically, but will require a working internet connection for the
installation to be successful.
```{r}
library(dplyr)
library(ggplot2)
library(forcats)
library(purrr)
library(tidyr)
library(rio)
library(linelist)
library(janitor)
library(kableExtra)
library(incidence2)
library(grateful)
library(epiparameter)
```
```{r}
custom_grey <- "#505B5B"
green_grey <- "#5E7E80"
pale_green <- "#B2D1CC"
dark_green <- "#005C5D"
dark_pink <- "#B45D75"
theme_set(tracetheme::theme_trace())
```
<!--
### System dependencies
You may need to install system dependencies to be able to generate this report:
```sh
# macOS
brew install libsodium cmake
# Linux (Debian based)
apt install libsodium-dev cmake
```
-->
## Importing the data
To illustrate the different analyses, we use real data reporting line list
(individual level) data of Covid-19 cases in England in the second half of 2020.
The data was downloaded from <global.health>:
> Xu, B., Gutierrez, B., Mekaru, S. et al. Epidemiological data from the COVID-19 outbreak, real-time case information. Sci Data 7, 106 (2020). https://doi.org/10.1038/s41597-020-0448-0
The data file is named "*covid_linelist_england.rds*" and is located in
the *data/* folder. To adapt this report to another dataset, change the name of
the file in the `data_file` parameter at the top of this document.
```{r}
data_path <- params$data_file
```
```{r}
dat_raw <- data_path %>%
import() %>%
tibble() %>%
# rio (via readxl) tends to use POSIXct for what is encoded as Date in the
# original data file.
# But POSIXct is not a good format to work with dates, as discussed in
# https://github.com/reconverse/incidence2/issues/105
mutate(across(where(\(x) inherits(x, "POSIXct")), as.Date))
```
Once imported into __R__, the dataset called `dat` includes:
* `date`: the date of admission
* `region`: the NHS region
* `org_name`: the full name of the NHS trust
* `org_code`: a short code for the NHS trust
* `n`: number of new, confirmed COVID-19 cases admitted, including inpatients
who tested positive on that day, and new admissions with a positive test
## Identifying key data
__Note__: this is not used for now, as there is no integration of linelist with
other existing tools.
Here we identify the key data needed in the analyses, including:
* the dates to be used, here, dates of hospital admission
* the strata of the population, here, coarse geographic locations (NHS regions)
* the case counts; this would not be needed if the data was a raw linelist, and
not already aggregated counts
```{r}
date_var <- "date"
group_var <- "region"
# Leave count_var as NULL if the data is really a linelist / patient-level data.
# Update count_var to a character string with the name of the column if the data
# is already aggregated.
count_var <- NULL
dat <- dat_raw %>%
make_linelist(
date_admission = date_var,
location = group_var
)
```
# Descriptive analyses
## Epidemic curves
This section creates epidemic curves ("_epicurves_"), with or without stratification.
```{r}
# convert daily incidence into weekly incidence using incidence2
dat_i <- dat_raw %>%
incidence("date",
interval = params$epicurve_unit,
counts = count_var,
groups = group_var
)
# general variables for automatic customisation of plots
n_groups <- dplyr::n_distinct(get_groups(dat_i)[[1]])
small_counts <- max(get_count_value(dat_i)) < 20
```
```{r fig.height = 5 / 3 * n_groups}
dat_i %>%
plot(alpha = 1, nrow = n_groups) +
labs(title = "Incidence of cases over time")
```
## Numbers of cases
This graph shows the total number of cases per group:
```{r }
total_cases <- dat_i %>%
select(any_of(c(group_var, "count"))) %>%
group_by(.data[[group_var]]) %>%
summarise(cases = sum(count)) %>%
mutate(group_var := fct_reorder(
.f = .data[[group_var]],
.x = cases
))
ggplot(total_cases, aes(x = cases, y = group_var)) +
geom_col(fill = green_grey) +
labs(x = "Total number of cases", y = NULL)
total_cases %>%
mutate(
percentage = sprintf("%.2f%%", cases / sum(cases) * 100)
) %>%
adorn_totals() %>%
mutate(cases = format(cases, scientific = FALSE, big.mark = " ")) %>%
set_names(toupper) %>%
kbl() %>%
kable_paper("striped", font_size = 18, full_width = FALSE)
```
# Serial interval distribution
## Explanations
The _serial interval_ ($si$) is the delay between the date of symptom onsets of primary
case and the secondary cases they have infected. Because this delay varies from
one transmission pair to another, we will characterise this variation using a
probability distribution. This distribution is a key input to methods use for
estimating the reproduction number ($R$).
Here, we assume that the mean and standard deviation of the $si$ is known, and
provided as an input by the user. We model the $si$ distribution as a
discretized Gamma.
## Results
```{r, eval = params$use_epiparameter_database}
si_epidist <- epidist_db(
disease = params$disease_name,
epi_dist = "serial_interval",
single_epidist = TRUE,
subset = is_parameterised
)
si_params <- get_parameters(si_epidist)
si_dist <- family(si_epidist)
si_mean <- si_params["mean"]
si_sd <- si_params["sd"]
```
```{r, eval = !params$use_epiparameter_database}
si_mean <- params$si_mean
si_sd <- params$si_sd
si_dist <- params$si_dist
si_epidist <- epidist(
disease = params$disease_name,
epi_dist = "serial_interval",
prob_distribution = params$si_dist,
summary_stats = create_epidist_summary_stats(mean = params$si_mean,
sd = params$si_sd)
)
```
```{r}
si <- discretise(si_epidist)
si_x <- seq(1L, to = quantile(si, 0.999), by = 1L)
```
```{r}
ggplot(
data.frame(delay = si_x, prob = si$prob_dist$d(si_x)),
aes(x = delay, y = prob)
) +
geom_col(fill = green_grey) +
labs(
title = "Serial interval distribution",
x = "Days from primary to secondary onset",
y = "Probability",
subtitle = sprintf(
"%s distribution | mean: %.1f days ; sd: %.1f days",
si_dist, si_mean, si_sd
)
)
```
# Growth rate ($r$) and reproduction number ($R$)
```{r}
last_date <- dat %>%
pull(date) %>%
max()
# version using keep_first and keep_last from i2extras
days_to_keep <- params$incomplete_days + params$r_estim_window
i_recent <- dat_raw %>%
incidence("date",
counts = count_var,
groups = group_var
) %>%
keep_last(days_to_keep) %>% # keep data for fitting
keep_first(params$r_estim_window) # remove incomplete data
```
```{r}
dat_i_day <- dat_raw %>%
incidence("date",
interval = "daily",
counts = count_var,
groups = group_var
) %>%
keep_first(n_distinct(.$date_index) - params$incomplete_days)
```
```{r, child=paste0("rmdchunks/", params$rt_estimator, ".Rmd")}
```
```{r}
cite_packages(output = "paragraph", out.dir = ".", pkgs = "Session")
```