Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating transmissibility template #144

Merged
merged 75 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
bc98e15
Removing text about hard-coded gamma distribution
CarmenTamayo Jan 9, 2024
73eab39
Changing "dat" to actual name of file "dat_raw"
CarmenTamayo Jan 9, 2024
8b9c2ab
Update description of report parameters "incomplete_days" and "r_esti…
CarmenTamayo Jan 30, 2024
822a8b3
Updating description of parameter "data_file"
CarmenTamayo Jan 30, 2024
2b85920
Updating description of report as seen when rendered
CarmenTamayo Jan 30, 2024
a993ec3
Moved description of code about loading libraries inside correspondin…
CarmenTamayo Jan 30, 2024
b49501c
Simplify and add flexibility to generation time definition
Bisaloo Jan 29, 2024
44e472f
Moving code description about changing the data_file parameter inside…
CarmenTamayo Jan 30, 2024
87a46dd
Replacing description of data with template to be filled by users
CarmenTamayo Jan 30, 2024
18092b2
Changing description of NHS data to template to be filled in by users
CarmenTamayo Jan 30, 2024
8f90373
Adding visualisation of dataset heading
CarmenTamayo Jan 30, 2024
38180e5
Changing count_var object to column name to fix error when estimating rt
CarmenTamayo Jan 30, 2024
86065c6
Updating EpiNow2 generation time parameter
CarmenTamayo Feb 9, 2024
c69fcac
Fixing lintr indentation
CarmenTamayo Feb 23, 2024
0aa288a
Fixing lintr indentation
CarmenTamayo Feb 23, 2024
730e413
Updating use of density function with epiparameter from $d to density()
CarmenTamayo Feb 23, 2024
c4130d4
Fixing typo (duplicated "days")
CarmenTamayo Apr 9, 2024
2e1f1d9
Replacing "group" by "region" to match variables in the dataset
CarmenTamayo Apr 9, 2024
91f62d7
Updating si parameter description for user to choose between manually…
CarmenTamayo Apr 9, 2024
3563cf3
Updating description of time period included in table with Rt estimat…
CarmenTamayo Apr 9, 2024
07e9dc8
Adding description to the code chunk used to generate table with Rt e…
CarmenTamayo Apr 9, 2024
9ad75ba
Keeping only columns needed by EpiNow2 to avoid errors
CarmenTamayo Apr 9, 2024
2d6d453
Ensure the date column's class is correct to avoid errors with region…
CarmenTamayo Apr 9, 2024
0d9f554
Plotting si allowing negative values (relevant mainly only for normal…
CarmenTamayo Apr 12, 2024
b68ba48
Updating use of incidence2 from "daily" to 1L
CarmenTamayo Apr 12, 2024
0d59ef7
Reverting description of grouping variable to "group" so users can gr…
CarmenTamayo Apr 23, 2024
f6e246d
Adding code to specify that both epicurves are now included, and ment…
CarmenTamayo Apr 23, 2024
cf2405a
Adding code to plot global epicurve (as it appeared in episoap vignette)
CarmenTamayo Apr 23, 2024
99cd925
Adding incidence2 to list of loaded packages
CarmenTamayo Apr 23, 2024
8813852
Removing redundant comma
CarmenTamayo Apr 23, 2024
dbdd3a5
Adding info about epicurves with stratification by group_var
CarmenTamayo Apr 24, 2024
f80b2ac
Specifying that the data_file parameter should be a path and not just…
CarmenTamayo Apr 24, 2024
3071222
Adding specific details on how to select column for count_var so it's…
CarmenTamayo Apr 24, 2024
b8769d4
Adding plot for epicurve with total cases (as it appears on episoap v…
CarmenTamayo Apr 24, 2024
504ac07
Adding info about stratified epicurves and removing unnecessary labs …
CarmenTamayo Apr 24, 2024
f81bd57
Reverting to saying "group" instead of "region" since users might wan…
CarmenTamayo Apr 24, 2024
e293637
Adding info about supported file types
CarmenTamayo Apr 24, 2024
fe7624b
Removing code chunks from rendering that are not relevant for reader
CarmenTamayo Apr 26, 2024
6999bb5
Adding brief explanation for package user of what the code in each ch…
CarmenTamayo Apr 26, 2024
5eac066
Removing "group_var" from table for reader, since the column "region"…
CarmenTamayo Apr 26, 2024
5cafd66
Adding explanation for reader about transmissibility by group results…
CarmenTamayo Apr 26, 2024
356c2ff
Removing "count_variable" from output table for reader as it's a colu…
CarmenTamayo Apr 26, 2024
a6ab15d
Adding small explanation about what each code chunk is doing for pack…
CarmenTamayo Apr 26, 2024
250f472
Adding Explanation section to EpiEstim to match i2extras chunk
CarmenTamayo Apr 26, 2024
09a8058
Adding myself as author of template
CarmenTamayo Apr 26, 2024
538d173
Adding explanation for EpiNow2 to match i2extras
CarmenTamayo Apr 26, 2024
da13d0b
Adding explanations for the code for pkg users in EpiNow2 chunk
CarmenTamayo Apr 26, 2024
b7beb18
Removing empty code chunk
CarmenTamayo Apr 26, 2024
ffca06d
Adding description of results for the transmissibility by group secti…
CarmenTamayo Apr 26, 2024
cf29e92
Updating incorrect description of table in global tx section of EpiNo…
CarmenTamayo Apr 26, 2024
6b53b33
Adding code to automatically print whether the si has been provided b…
CarmenTamayo Apr 26, 2024
56757bc
Removing data preparation section as it didn't contain any text and t…
CarmenTamayo Apr 26, 2024
597dc59
Modifying Identifying key data section so that variables are only des…
CarmenTamayo Apr 26, 2024
39467e9
Improve description of results in "Number of cases" section
CarmenTamayo Apr 26, 2024
e666ae9
Replacing text with object that's rendered automatically to indicate …
CarmenTamayo Apr 26, 2024
1c27da7
Changing description in "Importing the data" to address the reader
CarmenTamayo Apr 26, 2024
dd14007
Removing text describing the data since this is now done at section "…
CarmenTamayo Apr 26, 2024
fd556f3
Fixing typo in EpiEstim chunk
CarmenTamayo Apr 26, 2024
b841278
Removing note about {linelist} not being used
CarmenTamayo Apr 30, 2024
63fc590
Removing chunk where object "last_date" was created as it is not used…
CarmenTamayo Apr 30, 2024
c089fc0
Updating EpiEstim explanation to mention effective Rt and clarify whi…
CarmenTamayo Apr 30, 2024
44e9111
Updating Epinow2 explanation to mention effective Rt and clarify whic…
CarmenTamayo Apr 30, 2024
a64a2b8
Adding missing column with grouping variable to table displaying grow…
CarmenTamayo Apr 30, 2024
4a901bc
Adding brief description of what each code section is doing in i2extr…
CarmenTamayo Apr 30, 2024
0d55b0d
Adding info about grouping variable used to the descriptions of plots…
CarmenTamayo Apr 30, 2024
cd15819
Adding Explanation section to R0 chunk
CarmenTamayo Apr 30, 2024
bd20b49
Adding code explanations for user to R0 chunk
CarmenTamayo Apr 30, 2024
f803057
Adding row.names=FALSE when generating table since these were being p…
CarmenTamayo Apr 30, 2024
161faaf
Updating description of Rt estimates per group with latest dates avai…
CarmenTamayo Apr 30, 2024
db84501
Adding brief description of transmissibility by group results in R0 c…
CarmenTamayo Apr 30, 2024
3778eaa
Update inst/rmarkdown/templates/transmissibility/skeleton/skeleton.Rmd
CarmenTamayo May 2, 2024
9f80644
Using kbl for better output of data heade
CarmenTamayo May 2, 2024
367ccc3
Removing include=FALSE to make all rmd chunks visible to reader again
CarmenTamayo May 2, 2024
65dbb11
Increasing fig.height for Rt estimates by group_var in RO chunk
CarmenTamayo May 2, 2024
c2dcaf9
Update inst/rmarkdown/templates/transmissibility/skeleton/rmdchunks/E…
CarmenTamayo May 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ library(EpiEstim)

### Explanations

The package *EpiEstim* is used to estimate the effective, time-varying reproduction number, $Rt$,
using a branching process method that requires daily incidence data and an estimate of the disease's serial interval. Through this method, $Rt$ is estimated over weekly sliding windows, i.e., successive overlapping 7-day periods. $Rt$ estimates can be biased by reporting delays. For this reason, this report excludes the last `r params$incomplete_days` days included in the incidence data as incomplete, and retains a total of `r params$r_estim_window` days to obtain the most recent estimate of the time varying reproduction number.


$Rt$ is defined as the average number of secondary cases that an infected individual would infect if
conditions remained as they were at time _t_. It represents the transmission potential of a disease at a specified timepoint. Observing changes in $Rt$ provides valuable information about variations in transmissibility during an epidemic, and can be used to assess the effectiveness of control interventions aimed at reducing virus transmission.
For more information on *Rt* and its interpretation, see [this paper by the Royal Society](https://royalsociety.org/-/media/policy/projects/set-c/set-covid-19-R-estimates.pdf).


### Results

```{r rt-wrappers-prep}
Expand Down Expand Up @@ -69,6 +78,8 @@ associated 95% credibility intervals
* a table summarising these results for the last `r params$r_estim_window` days

```{r rt-estim-global}
# This code uses the {EpiEstim} function `estimate_R` to generate an estimate of
# the global Rt over time
res_epiestim_global <- dat_i_day %>%
regroup() %>%
get_count_value() %>%
Expand All @@ -77,7 +88,7 @@ res_epiestim_global <- dat_i_day %>%
```

```{r rt-plot-global}
# Graph of all values over time
# Plot to visualise the global Rt over time
ggplot(res_epiestim_global, aes(x = end)) +
geom_ribbon(aes(ymin = lower, ymax = upper), fill = pale_green) +
geom_line(aes(y = median), color = dark_green) +
Expand All @@ -91,7 +102,7 @@ ggplot(res_epiestim_global, aes(x = end)) +
```

```{r rt-table-global}
# Table
# This code generates a table with Rt values on the last params$r_estim_window
res_epiestim_global %>%
tail(params$r_estim_window) %>%
mutate(
Expand All @@ -116,22 +127,31 @@ res_epiestim_global %>%

#### Transmissibility by group

These analyses present Rt results for the stratified incidence. Results include:

* a graph of the estimates of $R_t$ of the observed data over time per group, with
associated 95% credibility intervals

* a plot with the latest $R_t$ estimate per group, with associated 95% credibility intervals

* a table with the latest $R_t$ estimates per group, with associated 95% credibility intervals

```{r rt-estim-group}
# Get results by group, keeping only the last 14 days of data
# This code estimates Rt by group_var, keeping only the last 14 days of data
res_epiestim_group <- dat_i_day %>%
nest(data = c(get_date_index_name(.), get_count_value_name(.))) %>%
mutate(
res_epiestim = map(data, ~ wrap_res(
estimate_R(.x$count, config = ee_config),
dat_i_day)
)
dat_i_day
))
) %>%
unnest(res_epiestim) %>%
dplyr::select(-data)
```

```{r rt-plot-group, fig.height = 5 / 3 * n_groups}
# Graph of all values over time
# Plot of Rt estimates for each group_var over time
ggplot(res_epiestim_group, aes(x = end)) +
geom_ribbon(aes(ymin = lower, ymax = upper), fill = pale_green) +
geom_line(aes(y = median), color = dark_green) +
Expand All @@ -146,25 +166,26 @@ ggplot(res_epiestim_group, aes(x = end)) +
```

```{r rt-plot-estimates-group}
# Plot of latest estimates
# Plot of latest estimates of Rt for each group_var
res_epiestim_group %>%
filter(end == max(end)) %>%
ggplot(aes(y = .data[[group_var]]), fill = custom_grey) +
geom_point(aes(x = median), color = dark_green) +
geom_errorbar(aes(xmin = lower, xmax = upper), color = dark_green) +
geom_vline(xintercept = 1, color = dark_pink) +
labs(
title = "Latest estimates of Rt",
subtitle = sprintf(
"As of %s",
format(max(get_dates(dat_i_day)), "%d %B %Y")
),
y = "",
x = "Instantaneous Reproduction Number (Rt)"
)
geom_point(aes(x = median), color = dark_green) +
geom_errorbar(aes(xmin = lower, xmax = upper), color = dark_green) +
geom_vline(xintercept = 1, color = dark_pink) +
labs(
title = "Latest estimates of Rt",
subtitle = sprintf(
"As of %s",
format(max(get_dates(dat_i_day)), "%d %B %Y")
),
y = "",
x = "Instantaneous Reproduction Number (Rt)"
)
```

```{r rt-table-group}
# This code generates a table with the latest Rt estimates for each group_var
res_epiestim_group %>%
filter(end == max(end)) %>%
mutate(
Expand All @@ -176,7 +197,7 @@ res_epiestim_group %>%
upper
)
) %>%
dplyr::select(-c(sd, lower, upper)) %>%
dplyr::select(-c(sd, lower, upper, count_variable)) %>%
rename(
"mean $R$" = mean,
"median $R$" = median
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,20 @@ library(EpiNow2)

### Explanations

The package *EpiNow2* is used to estimate the effective, time-varying reproduction number, $Rt$, using a Bayesian inference method that requires data on observed infections, and an estimate of the generation time, which is approximated using the disease's serial interval distribution. $Rt$ estimates can be biased by reporting delays. For this reason, this report excludes the last `r params$incomplete_days` days included in the incidence data as incomplete,
and retains a total of `r params$r_estim_window` days to obtain the most recent estimate of the time varying reproduction number.

$Rt$ is defined as the average number of secondary cases that an infected individual would infect if
conditions remained as they were at time _t_. It represents the transmission potential of a disease at a specified timepoint. Observing changes in $Rt$ provides valuable information about variations in transmissibility during an epidemic, and can be used to assess the effectiveness of control interventions aimed at reducing virus transmission.
For more information on *Rt* and its interpretation, see [this paper by the Royal Society](https://royalsociety.org/-/media/policy/projects/set-c/set-covid-19-R-estimates.pdf).

### Results

```{r rt-wrappers-prep}
# Approximate serial interval with gamma distribution since this is what EpiNow2
# will use for generation_time
si_gamma <- epiparameter::extract_param(
type = "range",
values = c(median(si$prob_dist$r(1e3)),
min(si$prob_dist$r(1e3)),
max(si$prob_dist$r(1e3))),
distribution = params$si_dist,
samples = 1e3
)
si_gamma <- epiparameter::convert_params_to_summary_stats(
distribution = "gamma", shape = si_gamma[[1]], scale = si_gamma[[2]])

generation_time <- list(
mean = si_gamma$mean,
mean_sd = 0,
sd = si_gamma$sd,
sd_sd = 0,
max = max(si_x)
# This code takes the serial interval distribution of params$disease_name and
# uses it to approximate the disease's generation time
generation_time <- generation_time_opts(
dist_spec(pmf = density(si, at = si_x))
)
```

Expand All @@ -43,16 +35,19 @@ stratification. Results include:
* a graph of the estimates of $R_t$ of the observed data over time, with
associated 95% credibility intervals

* a table summarising these results for the last `r params$r_estim_window` days
* a table summarising $Rt$ values for the last `r params$r_estim_window` days of available data

```{r rt-estim-global}
# This code uses {EpiNow2}'s function `epinow()` to estimate Rt over time
# from daily incidence data
# Warning, this step will take 2 cores of your computer and may be running
# for a long time!
res_epinow2_global <- dat_i_day %>%
regroup() %>%
dplyr::rename(
confirm = .data[[count_var]],
date = date_index
dplyr::mutate(
confirm = count,
date = as.Date(date_index),
.keep = "unused"
) %>%
epinow(
generation_time = generation_time,
Expand All @@ -67,6 +62,7 @@ res_epinow2_global <- dat_i_day %>%
```

```{r rt-plot-global}
# Plot of Rt values over time for global incidence data (without stratification)
plot(res_epinow2_global, "R") +
scale_x_date(breaks = scales::pretty_breaks(n = 8), date_label = "%d %b %Y") +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) +
Expand All @@ -78,6 +74,8 @@ plot(res_epinow2_global, "R") +
```

```{r rt-table-global}
# This code generates a table that contains Rt estimates for the last
# params$r_estim_window
res_epinow2_global$estimates$summarised %>%
dplyr::filter(variable == "R") %>%
tail(params$r_estim_window) %>%
Expand All @@ -98,15 +96,27 @@ res_epinow2_global$estimates$summarised %>%

#### Transmissibility by group

These analyses present results for stratified incidence, i.e., by `r group_var`. Results include:

* a graph of the estimates of $R_t$ of the observed data over time, with associated 95% credibility intervals, stratified by `r group_var`

* a table with $Rt$ estimates for each `r group_var`, excluding the most recent `r params$incomplete_days` days from the available data

```{r rt-estim-group, message = FALSE}
# This code uses {EpiNow2}'s function `epinow()` to estimate Rt over time
# from daily incidence data, stratified by group_var
# Warning, this step will take 2 cores of your computer and may be running
# for a long time!
res_epinow2_group <- dat_i_day %>%
select("date_index", "region", "count") %>%
dplyr::rename(
confirm = .data[[count_var]],
confirm = count,
date = date_index,
region = .data[[group_var]]
) %>%
dplyr::mutate(date = as.Date(date)) %>%
regional_epinow(
generation_time = generation_time,
generation_time = generation_time_opts(generation_time),
stan = stan_opts(samples = 1e3, chains = 2),
rt = rt_opts(gp_on = "R0"),
horizon = 0,
Expand All @@ -118,14 +128,12 @@ res_epinow2_group <- dat_i_day %>%
```

```{r rt-plot-group}
# Plot of Rt estimates over time, stratified by group_var
res_epinow2_group$summary$plots$R
```

```{r rt-plot-estimates-group}
# Missing for now
```

```{r rt-table-group}
# This code generates a table with an overall estimate of Rt per group_var
res_epinow2_group$summary$summarised_results$data %>%
dplyr::filter(metric == "Effective reproduction no.") %>%
dplyr::select(region, mean, median, lower_95, upper_95) %>%
Expand Down
69 changes: 48 additions & 21 deletions inst/rmarkdown/templates/transmissibility/skeleton/rmdchunks/R0.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ library(R0)

### Explanations

The package *R0* is used to estimate the effective, time-varying reproduction number, $Rt$, implementing a time-dependent method, described by [Wallinga and Teunis](https://academic.oup.com/aje/article/160/6/509/79472?login=false). This method requires incidence data and an estimate of the generation time, which here is approximated using the disease's serial interval distribution.
$Rt$ estimates can be biased by reporting delays. For this reason, this report excludes the last `r params$incomplete_days` days included in the incidence data as incomplete.

$Rt$ is defined as the average number of secondary cases that an infected individual would infect if
conditions remained as they were at time _t_. It represents the transmission potential of a disease at a specified timepoint. Observing changes in $Rt$ provides valuable information about variations in transmissibility during an epidemic, and can be used to assess the effectiveness of control interventions aimed at reducing virus transmission.
For more information on *Rt* and its interpretation, see [this paper by the Royal Society](https://royalsociety.org/-/media/policy/projects/set-c/set-covid-19-R-estimates.pdf).

### Results

```{r rt-wrappers-prep}
# This code uses the serial interval to approximate the generation time
mGT <- generation.time("empirical", si$prob_dist$d(si_x))

# Helper function to get R0 outputs in a tidy format, compatible with pipelines
Expand Down Expand Up @@ -41,19 +49,23 @@ associated 95% credibility intervals
* a table summarising these results for the last `r params$r_estim_window` days

```{r rt-estim-global}
# This code uses the {R0} function `estimate.R()` to generate Rt estimates over
# time using daily incidence data (dat_i_day)
res_r0_global <- dat_i_day %>%
regroup() %>%
mutate(r0_quantiles(estimate.R(
get_count_value(.),
mGT,
date_index,
begin = 1L, end = length(date_index),
nsim = 1e4, method = "TD"
)$estimates$TD)
get_count_value(.),
mGT,
date_index,
begin = 1L, end = length(date_index),
nsim = 1e4, method = "TD"
)$estimates$TD)
)
```

```{r rt-plot-global}
# Plot of global Rt estimates over time for daily incidence data included in
# dat_i_day
res_r0_global %>%
na.omit() %>%
ggplot(aes(x = date_index, y = median, ymin = lower, ymax = upper)) +
Expand All @@ -69,6 +81,8 @@ res_r0_global %>%
```

```{r rt-table-global}
# This code generates a table with Rt estimates for the last
# `r params$r_estim_window` days included in dat_i_day
res_r0_global %>%
tail(params$r_estim_window) %>%
mutate(
Expand All @@ -85,13 +99,24 @@ res_r0_global %>%
"median $R$" = median
) %>%
set_names(toupper) %>%
kbl() %>%
kbl(row.names = FALSE) %>%
kable_paper("striped", font_size = 18, full_width = FALSE)
```

#### Transmissibility by group

These analyses present results for incidence stratified by `r group_var`. Results in this section include:

* a graph of the estimates of $R_t$ of the observed data over time for each `r group_var`, with
associated 95% credibility intervals

* a plot with the latest $R_t$ results by `r group_var`

* a table with the latest $R_t$ estimates by `r group_var`

```{r rt-estim-group}
# This code uses the {R0} function `estimate_R()` to obtain Rt estimates by
# group_var over time
res_r0_group <- dat_i_day %>%
regroup(groups = get_group_names(.)) %>%
arrange(date_index) %>%
Expand All @@ -107,7 +132,8 @@ res_r0_group <- dat_i_day %>%
)
```

```{r rt-plot-group}
```{r rt-plot-group, fig.height=15}
# Plot of Rt estimates over time by group_var
res_r0_group %>%
na.omit() %>%
ggplot(aes(x = date_index, y = median, ymin = lower, ymax = upper)) +
Expand All @@ -124,26 +150,27 @@ res_r0_group %>%
```

```{r rt-plot-estimates-group}
# Plot of latest estimates
# Plot of latest estimates of Rt by group_var
res_r0_group %>%
na.omit() %>%
filter(date_index == max(date_index)) %>%
ggplot(aes(y = .data[[group_var]]), fill = custom_grey) +
geom_point(aes(x = median), color = dark_green) +
geom_errorbar(aes(xmin = lower, xmax = upper), color = dark_green) +
geom_vline(xintercept = 1, color = dark_pink) +
labs(
title = "Latest estimates of Rt",
subtitle = sprintf(
"As of %s",
format(max(get_dates(dat_i_day)), "%d %B %Y")
),
y = "",
x = "Instantaneous Reproduction Number (Rt)"
)
geom_point(aes(x = median), color = dark_green) +
geom_errorbar(aes(xmin = lower, xmax = upper), color = dark_green) +
geom_vline(xintercept = 1, color = dark_pink) +
labs(
title = "Latest estimates of Rt",
subtitle = sprintf(
"As of %s",
format(max(get_dates(dat_i_day)), "%d %B %Y")
),
y = "",
x = "Instantaneous Reproduction Number (Rt)"
)
```

```{r rt-table-group}
# This code generates a table with latest estimates of Rt by group_var
res_r0_group %>%
na.omit() %>%
filter(date_index == max(date_index)) %>%
Expand Down
Loading
Loading