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

update local renv including new cleanepi package #80

Merged
merged 8 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ episodes:
- describe-cases.Rmd
- simple-analysis.Rmd
- delays-reuse.Rmd
- quantify-transmissibility.Rmd
# - quantify-transmissibility.Rmd
- delays-functions.Rmd

# Information for Learners
Expand Down
93 changes: 43 additions & 50 deletions episodes/delays-functions.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@
)
```

Now, we have an epidemiological parameter we can use in our analysis! In the chunk below we replaced one of the **summary statistics** inputs into `EpiNow2::dist_spec()`
Now, we have an epidemiological parameter we can use in our analysis! In the chunk below we replaced one of the **summary statistics** inputs into `EpiNow2::LogNormal()`

```r
generation_time <-
EpiNow2::dist_spec(
EpiNow2::LogNormal(
mean = covid_serialint$summary_stats$mean, # replaced!
sd = covid_serialint$summary_stats$sd, # replaced!
max = 20,
distribution = "gamma"
max = 20
)
```

Expand Down Expand Up @@ -113,7 +112,7 @@

If you need it, read in detail about the [R probability functions for the normal distribution](https://sakai.unc.edu/access/content/group/3d1eb92e-7848-4f55-90c3-7c72a54e7e43/public/docs/lectures/lecture13.htm#probfunc), each of its definitions and identify in which part of a distribution they are located!

![The four probability functions for the normal distribution ([Jack Weiss, 2012](https://sakai.unc.edu/access/content/group/3d1eb92e-7848-4f55-90c3-7c72a54e7e43/public/docs/lectures/lecture13.htm#probfunc))](fig/fig5a-normaldistribution.png)

Check warning on line 115 in episodes/delays-functions.Rmd

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[image missing alt-text]: fig/fig5a-normaldistribution.png

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

Expand Down Expand Up @@ -209,9 +208,9 @@

## Discretise a continuous distribution

We are getting closer to the end! `EpiNow2::dist_spec()` still needs a maximum value (`max`).
We are getting closer to the end! `EpiNow2::LogNormal()` still needs a maximum value (`max`).

One way to do this is to get the quantile value for the distribution's 99.9th percentile or `0.999` cumulative probability. For this, we need access to the set of distribution functions for our `<epidist>` object.
One way to do this is to get the quantile value for the distribution's 99th percentile or `0.99` cumulative probability. For this, we need access to the set of distribution functions for our `<epidist>` object.

We can use the set of distribution functions for a _continuous_ distribution (as above). However, these values will be _continuous_ numbers. We can **discretise** the continuous distribution stored in our `<epidist>` object to get discrete values from a continuous distribution.

Expand Down Expand Up @@ -240,11 +239,11 @@
plot(covid_serialint_discrete)
```

To finally get a `max` value, let's access the quantile value of the 99.9th percentile or `0.999` probability of the distribution with the `prob_dist$q` notation, similarly to how we access the `summary_stats` values.
To finally get a `max` value, let's access the quantile value of the 99th percentile or `0.99` probability of the distribution with the `prob_dist$q` notation, similarly to how we access the `summary_stats` values.

```{r}
covid_serialint_discrete_max <-
quantile(covid_serialint_discrete, p = 0.999)
quantile(covid_serialint_discrete, p = 0.99)
```

::::::::::::::::::::::::::::::::: challenge
Expand Down Expand Up @@ -312,7 +311,7 @@
```{r,eval=TRUE}
# create a discrete distribution visualisation
# from a maximum value from the distribution
quantile(covid_serialint_discrete, p = 0.999) %>%
quantile(covid_serialint_discrete, p = 0.99) %>%
# generate quantile values
# as a sequence for each natural number
seq(1L, to = ., by = 1L) %>%
Expand Down Expand Up @@ -346,13 +345,13 @@

## Plug-in `{epiparameter}` to `{EpiNow2}`

Now we can plug everything into the `EpiNow2::dist_spec()` function!
Now we can plug everything into the `EpiNow2::LogNormal()` function!

- the **summary statistics** `mean` and `sd` of the distribution,
- a maximum value `max`,
- the `distribution` name.

However, when using `EpiNow2::dist_spec()`, to define a **Lognormal** distribution, like the one in the `covid_serialint` object, we need to convert its summary statistics to distribution parameters named logmean and logsd. With `{epiparameter}` we can directly get the *distribution parameters* using `epiparameter::get_parameters()`:
When using `EpiNow2::LogNormal()` to define a **log normal** distribution like the one in the `covid_serialint` object we can specify the mean and sd as parameters. Alternatively, to get the "natural" parameters for a log normal distribution we can convert its summary statistics to distribution parameters named `meanlog` and `sdlog`. With `{epiparameter}` we can directly get the *distribution parameters* using `epiparameter::get_parameters()`:

```{r}
covid_serialint_parameters <-
Expand All @@ -363,11 +362,10 @@

```{r}
serial_interval_covid <-
dist_spec(
mean = covid_serialint_parameters["meanlog"],
sd = covid_serialint_parameters["sdlog"],
max = covid_serialint_discrete_max,
distribution = "lognormal"
EpiNow2::LogNormal(
meanlog = covid_serialint_parameters["meanlog"],
sdlog = covid_serialint_parameters["sdlog"],
max = covid_serialint_discrete_max
)

serial_interval_covid
Expand All @@ -378,7 +376,7 @@
```{r,message=FALSE}
epinow_estimates_cg <- epinow(
# cases
reported_cases = example_confirmed[1:60],
data = example_confirmed[1:60],
# delays
generation_time = generation_time_opts(serial_interval_covid)
)
Expand Down Expand Up @@ -449,17 +447,16 @@
covid_serialint_discrete_max <-
covid_serialint %>%
discretise() %>%
quantile(p = 0.999)
quantile(p = 0.99)

covid_serialint_parameters <-
epiparameter::get_parameters(covid_serialint)

covid_serial_interval <-
dist_spec(
mean = covid_serialint_parameters["meanlog"],
sd = covid_serialint_parameters["sdlog"],
max = covid_serialint_discrete_max,
distribution = "lognormal"
EpiNow2::LogNormal(
meanlog = covid_serialint_parameters["meanlog"],
sdlog = covid_serialint_parameters["sdlog"],
max = covid_serialint_discrete_max
)

# incubation time ---------------------------------------------------------
Expand All @@ -476,25 +473,24 @@
covid_incubation_discrete_max <-
covid_incubation %>%
discretise() %>%
quantile(p = 0.999)
quantile(p = 0.99)

covid_incubation_parameters <-
epiparameter::get_parameters(covid_incubation)

covid_incubation_time <-
dist_spec(
mean = covid_incubation_parameters["meanlog"],
sd = covid_incubation_parameters["sdlog"],
max = covid_incubation_discrete_max,
distribution = "lognormal" # do not forget this!
EpiNow2::LogNormal(
meanlog = covid_incubation_parameters["meanlog"],
sdlog = covid_incubation_parameters["sdlog"],
max = covid_incubation_discrete_max
)

# epinow ------------------------------------------------------------------

# run epinow
epinow_estimates_cgi <- epinow(
# cases
reported_cases = example_confirmed[1:60],
data = example_confirmed[1:60],
# delays
generation_time = generation_time_opts(covid_serial_interval),
delays = delay_opts(covid_incubation_time)
Expand Down Expand Up @@ -559,8 +555,8 @@

To get delay distribution using `{epiparameter}` we can use functions like:

- `epidist_db()`
- `list_distributions()`
- `epiparameter::epidist_db()`
- `epiparameter::parameter_tbl()`
- `discretise()`
- `quantile()`

Expand All @@ -582,7 +578,7 @@

# list distributions
epidist_db(disease = "ebola") %>%
list_distributions()
epiparameter::parameter_tbl()
```

```{r,message=FALSE,eval=TRUE}
Expand All @@ -600,11 +596,10 @@
ebola_serial_discrete <- discretise(ebola_serial)

serial_interval_ebola <-
dist_spec(
EpiNow2::Gamma(
mean = ebola_serial$summary_stats$mean,
sd = ebola_serial$summary_stats$sd,
max = quantile(ebola_serial_discrete, p = 0.999),
distribution = "gamma"
max = quantile(ebola_serial_discrete, p = 0.99)
)

# incubation time ---------------------------------------------------------
Expand All @@ -620,19 +615,18 @@
ebola_incubation_discrete <- discretise(ebola_incubation)

incubation_period_ebola <-
dist_spec(
EpiNow2::Gamma(
mean = ebola_incubation$summary_stats$mean,
sd = ebola_incubation$summary_stats$sd,
max = quantile(ebola_serial_discrete, p = 0.999),
distribution = "gamma"
max = quantile(ebola_serial_discrete, p = 0.99)
)

# epinow ------------------------------------------------------------------

# run epinow
epinow_estimates_egi <- epinow(
# cases
reported_cases = ebola_confirmed,
data = ebola_confirmed,
# delays
generation_time = generation_time_opts(serial_interval_ebola),
delays = delay_opts(incubation_period_ebola)
Expand All @@ -654,7 +648,7 @@

::::::::::::::::: hint

`EpiNow2::dist_spec()` also accepts Probability Mass Functions (PMF) from any distribution family. Read the reference guide on [Specify a distribution](https://epiforecasts.io/EpiNow2/reference/dist_spec.html).
`EpiNow2::NonParametric()` accepts Probability Mass Functions (PMF) from any distribution family. Read the reference guide on [Probability distributions](https://epiforecasts.io/EpiNow2/reference/Distributions.html).

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

Expand All @@ -663,8 +657,7 @@
```{r,message=FALSE,eval=TRUE}
# What parameters are available for Influenza?
epidist_db(disease = "influenza") %>%
list_distributions() %>%
as_tibble() %>%
epiparameter::parameter_tbl() %>%
count(epi_distribution)

# generation time ---------------------------------------------------------
Expand All @@ -685,7 +678,7 @@
epiparameter::discretise(influenza_generation)

influenza_generation_max <-
quantile(influenza_generation_discrete, p = 0.999)
quantile(influenza_generation_discrete, p = 0.99)

influenza_generation_pmf <-
density(
Expand All @@ -695,9 +688,9 @@

influenza_generation_pmf

# EpiNow2::dist_spec() can also accept the PMF values
# EpiNow2::NonParametric() can also accept the PMF values
generation_time_influenza <-
dist_spec(
EpiNow2::NonParametric(
pmf = influenza_generation_pmf
)

Expand All @@ -716,7 +709,7 @@
epiparameter::discretise(influenza_incubation)

influenza_incubation_max <-
quantile(influenza_incubation_discrete, p = 0.999)
quantile(influenza_incubation_discrete, p = 0.99)

influenza_incubation_pmf <-
density(
Expand All @@ -726,9 +719,9 @@

influenza_incubation_pmf

# EpiNow2::dist_spec() can also accept the PMF values
# EpiNow2::NonParametric() can also accept the PMF values
incubation_time_influenza <-
dist_spec(
EpiNow2::NonParametric(
pmf = influenza_incubation_pmf
)

Expand Down Expand Up @@ -761,7 +754,7 @@

### How to get distribution parameters from statistical distributions?

How to get the mean and standard deviation from a generation time with *only* distribution parameters but no summary statistics like `mean` or `sd` for `EpiNow2::dist_spec()`?
How to get the mean and standard deviation from a generation time with *only* distribution parameters but no summary statistics like `mean` or `sd` for `EpiNow2::Gamma()` or `EpiNow2::LogNormal()`?

Look at the `{epiparameter}` vignette on [parameter extraction and conversion](https://epiverse-trace.github.io/epiparameter/articles/extract_convert.html) and its [use cases](https://epiverse-trace.github.io/epiparameter/articles/extract_convert.html#use-cases)!

Expand Down
Loading
Loading