Skip to content

Commit

Permalink
Hyperlink functions and use consistent styling
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmbaazam committed Nov 14, 2023
1 parent a3ff51d commit 0834f79
Show file tree
Hide file tree
Showing 114 changed files with 701 additions and 679 deletions.
4 changes: 2 additions & 2 deletions R/adjust.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#' @description `r lifecycle::badge("stable")`
#' Maps from cases by date of infection to date of report via date of
#' onset.
#' @param infections `data.table` containing a `date` variable and a numeric
#' @param infections `<data.table>` containing a `date` variable and a numeric
#' `cases` variable.
#'
#' @param delay_defs A list of single row data.tables that each defines a
#' delay distribution (model, parameters and maximum delay for each model).
#' See `lognorm_dist_def` for an example of the structure.
#' See [lognorm_dist_def()] for an example of the structure.
#'
#' @param reporting_effect A numeric vector of length 7 that allows the scaling
#' of reported cases by the day on which they report (1 = Monday, 7 = Sunday).
Expand Down
85 changes: 43 additions & 42 deletions R/create.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ create_clean_reported_cases <- function(reported_cases, horizon,
#' This functions creates a data frame of reported cases that has been smoothed
#' using a centred partial rolling average (with a period set by
#' `smoothing_window`) and shifted back in time by some delay. It is used by
#' `estimate_infections` to generate the mean shifted prior on which the back
#' calculation method (see `backcalc_opts()`) is based.
#' [estimate_infections()] to generate the mean shifted prior on which the back
#' calculation method (see [backcalc_opts()]) is based.
#'
#' @param smoothing_window Numeric, the rolling average smoothing window
#' to apply. Must be odd in order to be defined as a centred average.
Expand All @@ -87,7 +87,7 @@ create_clean_reported_cases <- function(reported_cases, horizon,
#' @importFrom data.table copy shift frollmean fifelse .N
#' @importFrom stats lm
#' @importFrom runner mean_run
#' @return A data frame for shifted reported cases
#' @return A `<data.frame>` for shifted reported cases
#' @export
#' @author Sam Abbott
#' @examples
Expand Down Expand Up @@ -144,8 +144,8 @@ create_shifted_cases <- function(reported_cases, shift,
#' Construct the Required Future Rt assumption
#'
#' @description `r lifecycle::badge("stable")`
#' Converts the `future` argument from `rt_opts()` into arguments that can be
#' passed to `stan`.
#' Converts the `future` argument from [rt_opts()] into arguments that can be
#' passed to `{stan}`.
#'
#' @param future A character string or integer. This argument indicates how to
#' set future Rt values. Supported options are to project using the Rt model
Expand Down Expand Up @@ -184,10 +184,10 @@ create_future_rt <- function(future = "latest", delay = 0) {
#' Create Time-varying Reproduction Number Data
#'
#' @description `r lifecycle::badge("stable")`
#' Takes the output from `rt_opts()` and converts it into a list understood by
#' `stan`.
#' @param rt A list of options as generated by `rt_opts()` defining Rt
#' estimation. Defaults to `rt_opts()`. Set to `NULL` to switch to using back
#' Takes the output from [rt_opts()] and converts it into a list understood by
#' `{stan}`.
#' @param rt A list of options as generated by [rt_opts()] defining Rt
#' estimation. Defaults to [rt_opts()]. Set to `NULL` to switch to using back
#' calculation rather than generating infections using Rt.
#'
#' @param breakpoints An integer vector (binary) indicating the location of
Expand Down Expand Up @@ -256,11 +256,11 @@ create_rt_data <- function(rt = rt_opts(), breakpoints = NULL,
#' Create Back Calculation Data
#'
#' @description `r lifecycle::badge("stable")`
#' Takes the output of `backcalc_opts()` and converts it into a list understood
#' by `stan`.
#' Takes the output of [backcalc_opts()] and converts it into a list understood
#' by `{stan}`.
#'
#' @param backcalc A list of options as generated by `backcalc_opts()` to
#' define the back calculation. Defaults to `backcalc_opts()`.
#' @param backcalc A list of options as generated by [backcalc_opts()] to
#' define the back calculation. Defaults to [backcalc_opts()].
#'
#' @seealso backcalc_opts
#' @importFrom data.table fcase
Expand All @@ -284,15 +284,15 @@ create_backcalc_data <- function(backcalc = backcalc_opts()) {
#' Create Gaussian Process Data
#'
#' @description `r lifecycle::badge("stable")`
#' Takes the output of `gp_opts()` and converts it into a list understood by
#' `stan`.
#' @param gp A list of options as generated by `gp_opts()` to define the
#' Gaussian process. Defaults to `gp_opts()`.Set to NULL to disable the
#' Takes the output of [gp_opts()] and converts it into a list understood by
#' `{stan}`.
#' @param gp A list of options as generated by [gp_opts()] to define the
#' Gaussian process. Defaults to [gp_opts()]. Set to `NULL` to disable the
#' Gaussian process.
#' @param data A list containing the following numeric values:
#' `t`, `seeding_time`, `horizon`.
#' @importFrom data.table fcase
#' @seealso gp_opts
#' @seealso [gp_opts()]
#' @return A list of settings defining the Gaussian process
#' @export
#' @author Sam Abbott
Expand Down Expand Up @@ -356,13 +356,13 @@ create_gp_data <- function(gp = gp_opts(), data) {
#' Create Observation Model Settings
#'
#' @description `r lifecycle::badge("stable")`
#' Takes the output of `obs_opts()` and converts it into a list understood
#' by `stan`.
#' @param obs A list of options as generated by `obs_opts()` defining the
#' observation model. Defaults to `obs_opts()`.
#' Takes the output of [obs_opts()] and converts it into a list understood
#' by `{stan}`.
#' @param obs A list of options as generated by [obs_opts()] defining the
#' observation model. Defaults to [obs_opts()].
#' @param dates A vector of dates used to calculate the day of the week.
#' @seealso obs_opts
#' @return A list of settings ready to be passed to stan defining
#' @seealso [obs_opts()]
#' @return A list of settings ready to be passed to `{stan}` defining
#' the Observation Model
#' @export
#' @author Sam Abbott
Expand Down Expand Up @@ -408,14 +408,15 @@ create_obs_model <- function(obs = obs_opts(), dates) {
#' Create Stan Data Required for estimate_infections
#'
#' @description`r lifecycle::badge("stable")`
#' Takes the output of `stan_opts()` and converts it into a list understood by
#' `stan`. Internally calls the other `create_` family of functions to
#' construct a single list for input into stan with all data required present.
#' Takes the output of [stan_opts()] and converts it into a list understood by
#' `{stan}`. Internally calls the other `create_` family of functions to
#' construct a single list for input into `{stan}` with all data required
#' present.
#'
#' @param shifted_cases A dataframe of delay shifted cases
#' @param shifted_cases A `<data.frame>` of delay shifted cases
#'
#' @param seeding_time Integer; seeding time, usually obtained using
#' `get_seeding_time()`
#' [get_seeding_time()].
#'
#' @inheritParams create_gp_data
#' @inheritParams create_obs_model
Expand Down Expand Up @@ -497,11 +498,11 @@ create_stan_data <- function(reported_cases, seeding_time,

#' Create Initial Conditions Generating Function
#' @description `r lifecycle::badge("stable")`
#' Uses the output of `create_stan_data` to create a function which can be used
#' to sample from the prior distributions (or as close as possible) for
#' parameters. Used in order to initialise each `stan` chain within a range of
#' Uses the output of [create_stan_data()] to create a function which can be
#' used to sample from the prior distributions (or as close as possible) for
#' parameters. Used in order to initialise each `{stan}` chain within a range of
#' plausible values.
#' @param data A list of data as produced by `create_stan_data.`
#' @param data A list of data as produced by [create_stan_data()].
#' @return An initial condition generating function
#' @importFrom purrr map2_dbl
#' @importFrom truncnorm rtruncnorm
Expand Down Expand Up @@ -598,26 +599,26 @@ create_initial_conditions <- function(data) {
#' Create a List of Stan Arguments
#'
#' @description `r lifecycle::badge("stable")`
#' Generates a list of arguments as required by `rstan::sampling` or
#' `rstan::vb` by combining the required options, with data, and type of
#' Generates a list of arguments as required by [rstan::sampling()] or
#' [rstan::vb()] by combining the required options, with data, and type of
#' initialisation. Initialisation defaults to random but it is expected that
#' `create_initial_conditions` will be used.
#' [create_initial_conditions()] will be used.
#'
#' @param stan A list of stan options as generated by `stan_opts()`. Defaults
#' to `stan_opts()`. Can be used to override `data`, `init`, and `verbose`
#' @param stan A list of stan options as generated by [stan_opts()]. Defaults
#' to [stan_opts()]. Can be used to override `data`, `init`, and `verbose`
#' settings if desired.
#'
#' @param data A list of stan data as created by `create_stan_data`
#' @param data A list of `{stan}` data as created by [create_stan_data()]
#'
#' @param init Initial conditions passed to `rstan`. Defaults to "random" but
#' can also be a function (as supplied by `create_intitial_conditions`).
#' @param init Initial conditions passed to `{rstan}`. Defaults to "random" but
#' can also be a function (as supplied by [create_intitial_conditions()]).
#'
#' @param verbose Logical, defaults to `FALSE`. Should verbose progress
#' messages be returned.
#'
#' @importFrom utils modifyList
#'
#' @return A list of stan arguments
#' @return A list of `{stan}` arguments
#' @author Sam Abbott
#' @export
#' @examples
Expand Down
56 changes: 28 additions & 28 deletions R/dist.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @description `r lifecycle::badge("questioning")`
#' This function acts as a skeleton for a truncated distribution defined by
#' model type, maximum value and model parameters. It is designed to be used
#' with the output from `get_dist`.
#' with the output from [get_dist()].
#'
#' @param n Numeric vector, number of samples to take (or days for the
#' probability density).
Expand Down Expand Up @@ -177,7 +177,7 @@ dist_skel <- function(n, dist = FALSE, cum = TRUE, model,
#'
#' @description `r lifecycle::badge("stable")`
#' Fits an integer adjusted exponential, gamma or lognormal distribution using
#' `stan`.
#' `{stan}`.
#' @param values Numeric vector of values
#'
#' @param samples Numeric, number of samples to take. Must be >= 1000.
Expand All @@ -196,7 +196,7 @@ dist_skel <- function(n, dist = FALSE, cum = TRUE, model,
#' @param verbose Logical, defaults to FALSE. Should verbose progress messages
#' be printed.
#'
#' @return A `stan` fit of an interval censored distribution
#' @return A `{stan}` fit of an interval censored distribution
#' @author Sam Abbott
#' @export
#' @examples
Expand Down Expand Up @@ -286,7 +286,7 @@ dist_fit <- function(values = NULL, samples = 1000, cores = 1,
#'
#' @description `r lifecycle::badge("soft-deprecated")`
#' Generates a distribution definition when only parameter estimates
#' are available for gamma distributed parameters. See `rgamma` for
#' are available for gamma distributed parameters. See [rgamma()] for
#' distribution information.
#'
#' @param shape Numeric, shape parameter of the gamma distribution.
Expand All @@ -300,7 +300,7 @@ dist_fit <- function(values = NULL, samples = 1000, cores = 1,
#' @param samples Numeric, number of sample distributions to generate.
#'
#' @importFrom truncnorm rtruncnorm
#' @return A data.table defining the distribution as used by `dist_skel`
#' @return A `<data.table>` defining the distribution as used by [dist_skel()]
#' @export
#' @inheritParams dist_skel
#' @inheritParams lognorm_dist_def
Expand Down Expand Up @@ -370,7 +370,7 @@ gamma_dist_def <- function(shape, shape_sd,
#'
#' @description `r lifecycle::badge("soft-deprecated")`
#' Generates a distribution definition when only parameter estimates
#' are available for log normal distributed parameters. See `rlnorm` for
#' are available for log normal distributed parameters. See [rlnorm()] for
#' distribution information.
#'
#' @param mean Numeric, log mean parameter of the gamma distribution.
Expand All @@ -385,7 +385,7 @@ gamma_dist_def <- function(shape, shape_sd,
#'
#' @param to_log Logical, should parameters be logged before use.
#'
#' @return A data.table defining the distribution as used by `dist_skel`
#' @return A `<data.table>` defining the distribution as used by [dist_skel()]
#' @author Sam Abbott
#' @importFrom truncnorm rtruncnorm
#' @export
Expand Down Expand Up @@ -488,7 +488,7 @@ lognorm_dist_def <- function(mean, mean_sd,
#' @param max_value Numeric, defaults to the maximum value in the observed
#' data. Maximum delay to allow (added to output but does impact fitting).
#'
#' @return A `dist_spec` object summarising the bootstrapped distribution
#' @return A `<dist_spec>` object summarising the bootstrapped distribution
#' @author Sam Abbott
#' @importFrom purrr transpose
#' @importFrom future.apply future_lapply
Expand Down Expand Up @@ -587,16 +587,16 @@ bootstrapped_dist_fit <- function(values, dist = "lognormal",
#'
#' @description `r lifecycle::badge("maturing")`
#' Estimate a log normal delay distribution from a vector of integer delays.
#' Currently this function is a simple wrapper for `bootstrapped_dist_fit`.
#' Currently this function is a simple wrapper for [bootstrapped_dist_fit()].
#'
#' @param delays Integer vector of delays
#'
#' @param ... Arguments to pass to internal methods.
#'
#' @return A `dist_spec` summarising the bootstrapped distribution
#' @return A `<dist_spec>` summarising the bootstrapped distribution
#' @author Sam Abbott
#' @export
#' @seealso bootstrapped_dist_fit
#' @seealso [bootstrapped_dist_fit()]
#' @examples
#' \donttest{
#' delays <- rlnorm(500, log(5), 1)
Expand All @@ -614,9 +614,9 @@ estimate_delay <- function(delays, ...) {
#'
#' @description `r lifecycle::badge("soft-deprecated")`
#' Convolves cases by a PMF function. This function will soon be removed or
#' replaced with a more robust `stan` implementation.
#' replaced with a more robust `{stan}` implementation.
#'
#' @param cases A dataframe of cases (in date order) with the following
#' @param cases A `<data.frame>` of cases (in date order) with the following
#' variables: `date` and `cases`.
#'
#' @param max_value Numeric, maximum value to allow. Defaults to 120 days
Expand All @@ -639,7 +639,7 @@ estimate_delay <- function(delays, ...) {
#' @param truncate_future Logical, should cases be truncated if they occur
#' after the first date reported in the data. Defaults to `TRUE`.
#'
#' @return A `data.table` of cases by date of onset
#' @return A `<data.table>` of cases by date of onset
#' @export
#' @importFrom purrr map_dfc
#' @importFrom data.table data.table setorder
Expand Down Expand Up @@ -1054,7 +1054,7 @@ dist_spec <- function(mean, sd = 0, mean_sd = 0, sd_sd = 0,

#' Creates a delay distribution as the sum of two other delay distributions
#'
#' This is done via convolution with `stats::convolve()`. Nonparametric delays
#' This is done via convolution with [stats::convolve()]. Nonparametric delays
#' that can be combined are processed together, and their cumulative
#' distribution function is truncated at a specified tolerance level, ensuring
#' numeric stability.
Expand Down Expand Up @@ -1109,7 +1109,7 @@ dist_spec_plus <- function(e1, e2, tolerance = 0.001) {

#' Creates a delay distribution as the sum of two other delay distributions
#'
#' This is done via convolution with `stats::convolve()`. Nonparametric delays
#' This is done via convolution with [stats::convolve()]. Nonparametric delays
#' that can be combined are processed together, and their cumulative
#' distribution function is truncated at a specified tolerance level, ensuring
#' numeric stability.
Expand Down Expand Up @@ -1146,7 +1146,7 @@ dist_spec_plus <- function(e1, e2, tolerance = 0.001) {
#' distributions to [epinow()] or [estimate_infections()].
#'
#' @param ... The delay distributions (from calls to [dist_spec()]) to combine
#' @return Combined delay distributions (with class [dist_spec()]`)
#' @return Combined delay distributions (with class `<dist_spec>`)
#' @author Sebastian Funk
#' @method c dist_spec
#' @importFrom purrr transpose map
Expand Down Expand Up @@ -1174,7 +1174,7 @@ c.dist_spec <- function(...) {
##' This works out the mean of all the (parametric / nonparametric) delay
##' distributions combined in the passed [dist_spec()].
##'
##' @param x The [dist_spec()] to use
##' @param x The `<dist_spec>` to use
##' @param ... Not used
##' @return A vector of means.
##' @author Sebastian Funk
Expand Down Expand Up @@ -1226,7 +1226,7 @@ mean.dist_spec <- function(x, ...) {
#'
#' This displays the parameters of the uncertain and probability mass
#' functions of fixed delay distributions combined in the passed [dist_spec()].
#' @param x The [dist_spec()] to use
#' @param x The `<dist_spec>` to use
#' @param ... Not used
#' @return invisible
#' @author Sebastian Funk
Expand Down Expand Up @@ -1293,12 +1293,12 @@ print.dist_spec <- function(x, ...) {

#' Plot PMF and CDF for a dist_spec object
#'
#' This function takes a [dist_spec] object and plots its probability mass
#' function (PMF) and cumulative distribution function (CDF) using [ggplot2].
#' This function takes a `<dist_spec>` object and plots its probability mass
#' function (PMF) and cumulative distribution function (CDF) using `{ggplot2}`.
#' Note that currently uncertainty in distributions is not plot.
#'
#' @param x A [dist_spec] object
#' @param ... Additional arguments to pass to \code{ggplot}
#' @param x A `<dist_spec>` object
#' @param ... Additional arguments to pass to `{ggplot}`.
#' @importFrom ggplot2 aes geom_col geom_step facet_wrap vars theme_bw
#' @export
#' @author Sam Abbott
Expand Down Expand Up @@ -1380,17 +1380,17 @@ plot.dist_spec <- function(x, ...) {
return(plot)
}

##' Fix the parameters of a `dist_spec`
##' Fix the parameters of a `<dist_spec>` object
##'
##' If the given `dist_spec` has any uncertainty, it is removed and the
##' If the given `<dist_spec>` has any uncertainty, it is removed and the
##' corresponding distribution converted into a fixed one.
##' @return A `dist_spec` object without uncertainty
##' @return A `<dist_spec>` object without uncertainty
##' @author Sebastian Funk
##' @export
##' @param x A [dist_spec] object
##' @param x A `<dist_spec>` object
##' @param strategy Character; either "mean" (use the mean estimates of the
##' mean and standard deviation) or "sample" (randomly sample mean and
##' standard deviation from uncertainty given in the `dist_spec`)
##' standard deviation from uncertainty given in the `<dist_spec>`
##' @importFrom truncnorm rtruncnorm
fix_dist <- function(x, strategy = c("mean", "sample")) {
## if x is fixed already we don't have to do anything
Expand Down
Loading

0 comments on commit 0834f79

Please sign in to comment.