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

fix rt aggregate table #25

Merged
merged 8 commits into from
Mar 30, 2024
66 changes: 53 additions & 13 deletions episodes/quantify-transmissibility.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,24 @@ This tutorial illustrates the usage of `epinow()` to estimate the time-varying r
To illustrate the functions of `EpiNow2` we will use outbreak data of the start of the COVID-19 pandemic from the United Kingdom. The data are available in the R package `{incidence2}`.

```{r}
head(incidence2::covidregionaldataUK)
dplyr::as_tibble(incidence2::covidregionaldataUK)
```

To use the data, we must format the data to have two columns:

+ `date` : the date (as a date object see `?is.Date()`),
+ `confirm` : number of confirmed cases on that date.

```{r}
cases <- aggregate(
cases_new ~ date,
data = incidence2::covidregionaldataUK[, c("date", "cases_new")],
FUN = sum
)
colnames(cases) <- c("date", "confirm")
Let's use `{dplyr}` for this:

```{r, warning = FALSE, message = FALSE}
library(dplyr)

cases <- incidence2::covidregionaldataUK %>%
select(date, cases_new) %>%
group_by(date) %>%
summarise(confirm = sum(cases_new, na.rm = TRUE)) %>%
ungroup()
```


Expand Down Expand Up @@ -142,9 +145,9 @@ The number of delays and type of delay is a flexible input that depends on the d

| Data source | Delay(s) |
| ------------- |-------------|
|Time of symptom onset |Incubation period |
|Time of case report |Incubation period + time from symptom onset to case notification |
|Time of hospitalisation |Incubation period + time from symptom onset to hospitalisation |
|Time of symptom onset |Incubation period |

</center>

Expand Down Expand Up @@ -317,14 +320,47 @@ generation_time_fixed <- dist_spec(

```{r, message = FALSE, eval = TRUE}
reported_cases <- cases[1:90, ]

estimates <- epinow(
reported_cases = reported_cases,
generation_time = generation_time_opts(generation_time_fixed),
delays = delay_opts(
incubation_period_fixed + reporting_delay_fixed
),
rt = rt_opts(
prior = list(mean = rt_log_mean, sd = rt_log_sd)
)
)
```

::::::::::::::::::::::::::::::::: spoiler

### Reduce computation time

Using an appropriate number of samples and chains is crucial for ensuring convergence and obtaining reliable estimates in Bayesian computations using Stan. Inadequate sampling or insufficient chains may lead to issues such as divergent transitions, impacting the accuracy and stability of the inference process.

For the purpose of this tutorial, we can add more configuration details to get an useful output in less time. You can specify a fixed number of `samples` and `chains` to the `stan` argument using the `stan_opts()` function:

The code in the previous chunk can take around 10 minutes. We expect this chunk below to take approximately 3 minutes:

```{r,eval=FALSE}
estimates <- epinow(
# same code as previous chunk
reported_cases = reported_cases,
generation_time = generation_time_opts(generation_time_fixed),
delays = delay_opts(incubation_period_fixed + reporting_delay_fixed),
rt = rt_opts(prior = list(mean = rt_log_mean, sd = rt_log_sd))
delays = delay_opts(
incubation_period_fixed + reporting_delay_fixed
),
rt = rt_opts(
prior = list(mean = rt_log_mean, sd = rt_log_sd)
),
# [new] set a fixed number of samples and chains
stan = stan_opts(samples = 1000, chains = 3)
)
```

:::::::::::::::::::::::::::::::::

### Results

We can extract and visualise estimates of the effective reproduction number through time:
Expand Down Expand Up @@ -402,8 +438,12 @@ To find regional estimates, we use the same inputs as `epinow()` to the function
estimates_regional <- regional_epinow(
reported_cases = regional_cases,
generation_time = generation_time_opts(generation_time_fixed),
delays = delay_opts(incubation_period_fixed + reporting_delay_fixed),
rt = rt_opts(prior = list(mean = rt_log_mean, sd = rt_log_sd))
delays = delay_opts(
incubation_period_fixed + reporting_delay_fixed
),
rt = rt_opts(
prior = list(mean = rt_log_mean, sd = rt_log_sd)
)
)

estimates_regional$summary$summarised_results$table
Expand Down
Loading