Skip to content

Commit

Permalink
update {epiparameter} usage from breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwlambert committed Sep 10, 2024
1 parent b404781 commit 258943b
Show file tree
Hide file tree
Showing 28 changed files with 238 additions and 236 deletions.
8 changes: 4 additions & 4 deletions R/sim_contacts.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#'
#' # to simulate more realistic contact tracing data load epiparameters from
#' # {epiparameter}
#' contact_distribution <- epiparameter::epidist(
#' contact_distribution <- epiparameter::epiparameter(
#' disease = "COVID-19",
#' epi_dist = "contact distribution",
#' prob_distribution = "pois",
#' prob_distribution_params = c(mean = 2)
#' )
#'
#' infectious_period <- epiparameter::epidist(
#' infectious_period <- epiparameter::epiparameter(
#' disease = "COVID-19",
#' epi_dist = "infectious period",
#' prob_distribution = "gamma",
Expand All @@ -52,8 +52,8 @@ sim_contacts <- function(contact_distribution = function(x) stats::dpois(x = x,
config = create_config()) {
# check and convert distribution to func if needed before .check_sim_input()
stopifnot(
"Input delay distributions need to be either functions or <epidist>" =
inherits(infectious_period, c("function", "epidist"))
"Input delay distributions need to be either functions or <epiparameter>" =
inherits(infectious_period, c("function", "epiparameter"))
)
contact_distribution <- as.function(
contact_distribution, func_type = "density"
Expand Down
44 changes: 22 additions & 22 deletions R/sim_linelist.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#' * `proportion`: a column with the proportion of the population that are in
#' that age group. Proportions must sum to one.
#'
#' @param contact_distribution A `function` or an `<epidist>` object to generate
#' the number of contacts per infection.
#' @param contact_distribution A `function` or an `<epiparameter>` object to
#' generate the number of contacts per infection.
#'
#' The function can be defined or anonymous. The function must have a single
#' argument in the form of an `integer` vector with elements representing the
Expand All @@ -33,13 +33,13 @@
#' of observing zero contacts, the second element is the probability of
#' observing one contact, etc.
#'
#' An `<epidist>` can be provided. This will be converted into a probability
#' mass function internally.
#' An `<epiparameter>` can be provided. This will be converted into a
#' probability mass function internally.
#'
#' The default is an anonymous function with a Poisson probability mass function
#' ([dpois()]) with a mean (\eqn{\lambda}) of 2 contacts per infection.
#'
#' @param infectious_period A `function` or an `<epidist>` object for the
#' @param infectious_period A `function` or an `<epiparameter>` object for the
#' infectious period. This defines the duration from becoming infectious to
#' no longer infectious. In the simulation, individuals are assumed to
#' become infectious immediately after being infected (the latency period is
Expand All @@ -52,42 +52,42 @@
#' The function must have a single argument, the number of random infectious
#' periods to generate.
#'
#' An `<epidist>` can be provided. This will be converted into random number
#' generator internally.
#' An `<epiparameter>` can be provided. This will be converted into random
#' number generator internally.
#'
#' The default is an anonymous function with a lognormal distribution random
#' number generator ([rlnorm()]) with `meanlog = 2` and `sdlog = 0.5`.
#'
#' @param prob_infection A single `numeric` for the probability of a secondary
#' contact being infected by an infected primary contact.
#'
#' @param onset_to_hosp A `function` or an `<epidist>` object for the
#' @param onset_to_hosp A `function` or an `<epiparameter>` object for the
#' onset-to-hospitalisation delay distribution. `onset_to_hosp` can also be
#' set to `NULL` to not simulate hospitalisation (admission) dates.
#'
#' The function can be defined or anonymous. The function must return a vector
#' of `numeric`s for the length of the onset-to-hospitalisation delay. The
#' function must have a single argument.
#'
#' An `<epidist>` can be provided. This will be converted into a random number
#' generator internally.
#' An `<epiparameter>` can be provided. This will be converted into a random
#' number generator internally.
#'
#' The default is an anonymous function with a lognormal distribution random
#' number generator ([rlnorm()]) with `meanlog = 1.5` and `sdlog = 0.5`.
#'
#' If `onset_to_hosp` is set to `NULL` then `hosp_risk` and `hosp_death_risk`
#' will be automatically set to `NULL` if not manually specified.
#'
#' @param onset_to_death A `function` or an `<epidist>` object for the
#' @param onset_to_death A `function` or an `<epiparameter>` object for the
#' onset-to-death delay distribution. `onset_to_death` can also be set to
#' `NULL` to not simulate dates for individuals that died.
#'
#' The function can be defined or anonymous. The function must return a vector
#' of `numeric`s for the length of the onset-to-death delay. The function must
#' have a single argument.
#'
#' An `<epidist>` can be provided. This will be converted into a random number
#' generator internally.
#' An `<epiparameter>` can be provided. This will be converted into a random
#' number generator internally.
#'
#' The default is an anonymous function with a lognormal distribution random
#' number generator ([rlnorm()]) with `meanlog = 2.5` and `sdlog = 0.5`.
Expand All @@ -96,16 +96,16 @@
#' `hosp_death_risk` will be automatically set to `NULL` if not manually
#' specified.
#'
#' @param onset_to_recovery A `function` or an `<epidist>` object for the
#' @param onset_to_recovery A `function` or an `<epiparameter>` object for the
#' onset-to-recovery delay distribution. `onset_to_recovery` can also be `NULL`
#' to not simulate dates for individuals that recovered.
#'
#' The function can be defined or anonymous. The function must return a vector
#' of `numeric`s for the length of the onset-to-recovery delay. The function
#' must have a single argument.
#'
#' An `<epidist>` can be provided. This will be converted into a random number
#' generator internally.
#' An `<epiparameter>` can be provided. This will be converted into a random
#' number generator internally.
#'
#' The default is `NULL` so by default cases that recover get an `NA` in the
#' `$date_outcome` line list column.
Expand Down Expand Up @@ -180,32 +180,32 @@
#'
#' # to simulate a more realistic line list load epiparameters from
#' # {epiparameter}
#' contact_distribution <- epiparameter::epidist(
#' contact_distribution <- epiparameter::epiparameter(
#' disease = "COVID-19",
#' epi_dist = "contact distribution",
#' prob_distribution = "pois",
#' prob_distribution_params = c(mean = 2)
#' )
#'
#' infectious_period <- epiparameter::epidist(
#' infectious_period <- epiparameter::epiparameter(
#' disease = "COVID-19",
#' epi_dist = "infectious period",
#' prob_distribution = "gamma",
#' prob_distribution_params = c(shape = 1, scale = 1)
#' )
#'
#' # get onset to hospital admission from {epiparameter} database
#' onset_to_hosp <- epiparameter::epidist_db(
#' onset_to_hosp <- epiparameter::epiparameter_db(
#' disease = "COVID-19",
#' epi_dist = "onset to hospitalisation",
#' single_epidist = TRUE
#' single_epiparameter = TRUE
#' )
#'
#' # get onset to death from {epiparameter} database
#' onset_to_death <- epiparameter::epidist_db(
#' onset_to_death <- epiparameter::epiparameter_db(
#' disease = "COVID-19",
#' epi_dist = "onset to death",
#' single_epidist = TRUE
#' single_epiparameter = TRUE
#' )
#' # example with single hospitalisation risk for entire population
#' linelist <- sim_linelist(
Expand Down
12 changes: 6 additions & 6 deletions R/sim_outbreak.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@
#'
#' # to simulate a more realistic outbreak load epiparameters from
#' # {epiparameter}
#' contact_distribution <- epiparameter::epidist(
#' contact_distribution <- epiparameter::epiparameter(
#' disease = "COVID-19",
#' epi_dist = "contact distribution",
#' prob_distribution = "pois",
#' prob_distribution_params = c(mean = 2)
#' )
#'
#' infectious_period <- epiparameter::epidist(
#' infectious_period <- epiparameter::epiparameter(
#' disease = "COVID-19",
#' epi_dist = "infectious period",
#' prob_distribution = "gamma",
#' prob_distribution_params = c(shape = 1, scale = 1)
#' )
#'
#' # get onset to hospital admission from {epiparameter} database
#' onset_to_hosp <- epiparameter::epidist_db(
#' onset_to_hosp <- epiparameter::epiparameter_db(
#' disease = "COVID-19",
#' epi_dist = "onset to hospitalisation",
#' single_epidist = TRUE
#' single_epiparameter = TRUE
#' )
#'
#' # get onset to death from {epiparameter} database
#' onset_to_death <- epiparameter::epidist_db(
#' onset_to_death <- epiparameter::epiparameter_db(
#' disease = "COVID-19",
#' epi_dist = "onset to death",
#' single_epidist = TRUE
#' single_epiparameter = TRUE
#' )
#'
#' outbreak <- sim_outbreak(
Expand Down
2 changes: 1 addition & 1 deletion R/simulist-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ NULL
# nocov start
# fix for R CMD check from https://github.com/hadley/r-pkgs/issues/828
ignore_unused_imports <- function() {
epiparameter::epidist
epiparameter::epiparameter
}
# nocov end
23 changes: 12 additions & 11 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
out
}

#' Convert `<epidist>` or `NULL` to function
#' Convert `<epiparameter>` or `NULL` to function
#'
#' @description
#' An extension to [as.function()], particularly the \pkg{epiparameter}
Expand All @@ -132,25 +132,26 @@
#' If a function is already passed to `as_function` it will be returned
#' unchanged.
#'
#' There is also input checking to error if input is not an `<epidist>`,
#' There is also input checking to error if input is not an `<epiparameter>`,
#' `function` ([closure]), or for onset-to-event distributions `NULL`.
#'
#' @param x A named list containing either `<epidist>`, `function` or `NULL`.
#' Named list elements are: `"contact_distribution"`, `"infectious_period"`,
#' `"onset_to_hosp"`, `"onset_to_death"`, `"onset_to_recovery".`
#' @param x A named list containing either `<epiparameter>`, `function` or
#' `NULL`. Named list elements are: `"contact_distribution"`,
#' `"infectious_period"`, `"onset_to_hosp"`, `"onset_to_death"`,
#' `"onset_to_recovery".`
#'
#' @return A list of `function`s.
#' @keywords internal
as_function <- function(x) {
stopifnot(
"Input delay distributions need to be either functions or <epidist>" =
inherits(x$contact_distribution, c("function", "epidist")) &&
inherits(x$infectious_period, c("function", "epidist")),
"Input delay distributions need to be either functions or <epiparameter>" =
inherits(x$contact_distribution, c("function", "epiparameter")) &&
inherits(x$infectious_period, c("function", "epiparameter")),
"onset_to_hosp, onset_to_death and onset_to_recovery need to be a function,
<epidist> or NULL" =
(inherits(x$onset_to_hosp, c("function", "epidist")) ||
<epiparameter> or NULL" =
(inherits(x$onset_to_hosp, c("function", "epiparameter")) ||
is.null(x$onset_to_hosp)) &&
(inherits(x$onset_to_death, c("function", "epidist")) ||
(inherits(x$onset_to_death, c("function", "epiparameter")) ||
is.null(x$onset_to_death))
)
contact_distribution <- as.function(
Expand Down
12 changes: 6 additions & 6 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,33 @@ library(epiparameter)

```{r create-epidists}
# create COVID-19 contact distribution
contact_distribution <- epiparameter::epidist(
contact_distribution <- epiparameter::epiparameter(
disease = "COVID-19",
epi_dist = "contact distribution",
prob_distribution = "pois",
prob_distribution_params = c(mean = 2)
)
# create COVID-19 infectious period
infectious_period <- epiparameter::epidist(
infectious_period <- epiparameter::epiparameter(
disease = "COVID-19",
epi_dist = "infectious period",
prob_distribution = "gamma",
prob_distribution_params = c(shape = 1, scale = 1)
)
# get onset to hospital admission from {epiparameter} database
onset_to_hosp <- epiparameter::epidist_db(
onset_to_hosp <- epiparameter::epiparameter_db(
disease = "COVID-19",
epi_dist = "onset to hospitalisation",
single_epidist = TRUE
single_epiparameter = TRUE
)
# get onset to death from {epiparameter} database
onset_to_death <- epiparameter::epidist_db(
onset_to_death <- epiparameter::epiparameter_db(
disease = "COVID-19",
epi_dist = "onset to death",
single_epidist = TRUE
single_epiparameter = TRUE
)
```

Expand Down
11 changes: 6 additions & 5 deletions man/as_function.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions man/dot-add_cols.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 258943b

Please sign in to comment.