-
Notifications
You must be signed in to change notification settings - Fork 2
/
skeleton.Rmd
499 lines (442 loc) · 12.7 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
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
---
title: "Estimating transmissibility with population stratification"
author: Thibaut Jombart, Hugo Gruson
date: "`r Sys.Date()`"
output:
rmarkdown::html_document:
code_folding: "hide"
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:
label: "Should the serial interval distribution be extracted directly from the epiparameter package?"
value: TRUE
epiparameter_pathogen:
label: "Name of the pathogen in the epiparameter database if `use_parameter = TRUE`."
value: "SARS_CoV_2_wildtype"
si_mean:
label: "Mean of the distribution for serial interval if not using value from epiparameter. Ignored if `use_epiparameter = 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 = TRUE`."
value: 4.9
si_dist:
label: "Choice of probability distribution for serial interval if not using value from epiparameter. Ignored if `use_epiparameter = 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 the count data over time"
value: "data/covid_hosp_uk_20201024.xlsx"
input: file
rt_estimator:
label: "Which R package to use for Rt estimation"
value: "i2extras"
choices: ["EpiEstim", "EpiNow2", "i2extras", "R0"]
bibliography:
- grateful-refs.bib
---
```{r, message = FALSE}
if (!require("renv")) {
install.packages("renv")
}
```
```{r lockfile, include = FALSE, message = FALSE}
renv::use(
"Pakillo/grateful@HEAD", # nolint
"R6@2.5.1",
"RColorBrewer@1.1-3",
"Rcpp@1.0.10",
"data.table@1.14.6",
"askpass@1.1",
"backports@1.4.1",
"base64enc@0.1-3",
"bit64@4.0.5",
"bit@4.0.5",
"bookdown@0.31",
"bslib@0.4.2",
"cachem@1.0.6",
"callr@3.7.3",
"cellranger@1.1.0",
"checkmate@2.1.0",
"cli@3.6.0",
"clipr@0.8.0",
"clock@0.6.1",
"colorspace@2.1-0",
"commonmark@1.8.1",
"cpp11@0.4.3",
"crayon@1.5.2",
"digest@0.6.31",
"distcrete@1.0.3",
"ellipsis@0.3.2",
"epitrix@0.2.2",
"epiverse-trace/epiparameter@fd250ce", # nolint
"epiverse-trace/episoap@HEAD", # nolint
"evaluate@0.20",
"fansi@1.0.4",
"farver@2.1.1",
"fastmap@1.1.0",
"fontawesome@0.4.0",
"forcats@0.5.2",
"fs@1.6.0",
"generics@0.1.3",
"ggplot2@3.4.0",
"glue@1.6.2",
"grates@0.3.1",
"gtable@0.3.1",
"haven@2.5.1",
"highr@0.10",
"hms@1.1.2",
"htmltools@0.5.4",
"httpuv@1.6.6",
"httr@1.4.4",
"incidence2@1.2.3",
"isoband@0.2.7",
"janitor@2.1.0",
"curl@5.0.0",
"jquerylib@0.1.4",
"jsonlite@1.8.4",
"kableExtra@1.3.4",
"knitr@1.42",
"labeling@0.4.2",
"later@1.3.0",
"lifecycle@1.0.3",
"linelist@0.0.1",
"lubridate@1.9.1",
"magrittr@2.0.3",
"memoise@2.0.1",
"mgcv@1.8-42", # https://github.com/rstudio/renv/issues/1166
"mime@0.12",
"munsell@0.5.0",
"openssl@2.0.5",
"openxlsx@4.2.5.1",
"pillar@1.8.1",
"pkgconfig@2.0.3",
"prettyunits@1.1.1",
"processx@3.8.0",
"progress@1.2.2",
"promises@1.2.0.1",
"ps@1.7.2",
"purrr@1.0.1",
"rlang@1.0.6",
"rappdirs@0.3.3",
"readr@2.1.3",
"readxl@1.4.1",
"rematch@1.0.1",
"remotes@2.4.2",
"renv@0.16.0",
"rio@0.5.29",
"rmarkdown@2.20",
"rmdformats@1.0.4",
"rstudioapi@0.14",
"rvest@1.0.3",
"sass@0.4.5",
"scales@1.2.1",
"selectr@0.4-2",
"shiny@1.7.3",
"snakecase@0.11.0",
"sodium@1.2.1",
"sourcetools@0.1.7",
"stringi@1.7.12",
"stringr@1.5.0",
"svglite@2.1.0",
"sys@3.4.1",
"systemfonts@1.0.4",
"tibble@3.1.8",
"tidyr@1.2.1",
"tidyselect@1.2.0",
"dplyr@1.0.10",
"timechange@0.2.0",
"tinytex@0.43",
"tzdb@0.3.0",
"utf8@1.2.2",
"vctrs@0.5.2",
"viridisLite@0.4.1",
"vroom@1.6.0",
"webshot@0.5.4",
"withr@2.5.0",
"xfun@0.36",
"xml2@1.3.3",
"xtable@1.8-4",
"yaml@2.3.7",
"zip@2.2.2"
)
```
```{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.png")
```
# 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(rio)
library(linelist)
library(janitor)
library(rmdformats)
library(kableExtra)
library(incidence2)
library(distcrete)
library(epitrix)
library(grateful)
library(epiparameter)
# episoap is already installed since this is where this document comes from
library(episoap)
```
```{r}
custom_grey <- "#505B5B"
green_grey <- "#5E7E80"
pale_green <- "#B2D1CC"
dark_green <- "#005C5D"
dark_pink <- "#B45D75"
theme_set(theme_episoap())
```
## Importing the data
To illustrate the different analyses, we use real data reporting daily numbers
of COVID-19 hospitalisations in England as of the 24 October 2020, broken down
to the hospital and National Health Service (NHS) region level. The data is
available online from the NHS England's
[website](https://www.england.nhs.uk/statistics/statistical-work-areas/covid-19-hospital-activity/).
The dataset analysed here is a simplified version, providing incidence of
hospital admissions by NHS trust.
The data file is named "*covid_hosp_uk_20201024.xlsx*" 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 %>%
rio::import() %>%
tibble()
```
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"
count_var <- "n"
dat <- dat_raw %>%
make_linelist(
date_admission = date_var,
location = group_var,
counts = count_var,
allow_extra = TRUE
)
```
# 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 %>%
# Temporary workaround for https://github.com/reconverse/incidence2/issues/82
mutate({{ date_var }} := as.Date(.data[[date_var]])) %>%
incidence("date",
interval = params$epicurve_unit,
counts = {{ count_var }},
groups = {{ group_var }}
)
# general variables for automatic customisation of plots
n_groups <- dplyr::n_distinct(dat_i[[group_var]])
small_counts <- max(dat_i[count_var]) < 20
# plot with regions as colors if less than 9 categories
if (n_groups <= 9L) {
dat_i %>%
plot(
fill = group_var, col_pal = muted, color = "white",
alpha = 1, angle = 45, legend = "bottom",
show_cases = small_counts
) +
guides(fill = guide_legend(title = NULL)) +
labs(title = "Incidence of cases over time")
} else {
dat_i %>%
regroup() %>%
plot(
angle = 45, color = "white",
fill = green_grey,
alpha = 1,
show_cases = small_counts
) +
labs(title = "Incidence of cases over time")
}
```
```{r fig.height = 5 / 3 * n_groups}
dat_i %>%
facet_plot(
angle = 45,
date_format = "%d %b %y",
color = "white",
fill = green_grey,
alpha = 1,
n_breaks = 6,
nrow = n_groups,
show_cases = small_counts
)
```
## Numbers of cases
This graph shows the total number of cases per group:
```{r }
total_cases <- dat %>%
select_tags(location, counts) %>%
group_by(location) %>%
summarise(cases = sum(counts)) %>%
mutate(location = fct_reorder(
.f = location,
.x = cases
))
ggplot(total_cases, aes(x = cases, y = location)) +
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}
si_epiparameter <- epiparameter::epidist(
params$epiparameter_pathogen,
"serial_interval"
)
si_params <- si_epiparameter$param
si_dist <- si_epiparameter$dist
si_mean <- si_params[[1]]
si_sd <- si_params[[2]]
```
```{r, eval = !params$use_epiparameter}
si_mean <- params$si_mean
si_sd <- params$si_sd
si_dist <- params$si_dist
si_cv <- si_sd / si_mean
si_params <- epitrix::gamma_mucv2shapescale(mu = si_mean, cv = si_cv)
```
```{r}
# do.call() because we want to pass `si_params` names as the name of arguments.
# This is important if params in si_params are not the first arguments of the
# q...() function. For example, if a gamma distribution is define by its shape
# and scale, instead of shape and rate.
si <- do.call(
distcrete,
c(
list(si_dist,
interval = 1,
w = 0
),
si_params
)
)
si_x <- seq(1L, to = si$q(0.999), by = 1L)
```
```{r}
ggplot(
data.frame(delay = si_x, prob = si$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 %>%
# Temporary workaround for https://github.com/reconverse/incidence2/issues/82
mutate({{ date_var }} := as.Date(.data[[date_var]])) %>%
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 %>%
# Temporary workaround for https://github.com/reconverse/incidence2/issues/82
mutate({{ date_var }} := as.Date(.data[[date_var]])) %>%
incidence("date",
interval = 1L,
counts = {{ count_var }},
groups = {{ group_var }}
) %>%
keep_first(n_distinct(.$date_index) - params$incomplete_days)
```
```{r, child=paste0("rmdchunks/", params$rt_estimator, ".Rmd")}
```
```{r results = 'asis'}
grateful::cite_packages(output = "paragraph")
```