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

Use natural parameters when specifying uncertain distributions #893

Merged
merged 18 commits into from
Dec 17, 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

- Brought the docs on `alpha_sd` up to date with the code change from prior PR #853. By @zsusswein in #862 and reviewed by @jamesmbaazam.
- The `...` argument in `estimate_secondary()` has been removed because it was not used. By @jamesmbaazam in #894 and reviewed by @.
- All examples now use the natural parameters of distributions rather than the mean and standard deviation when specifying uncertain distributions. This is to eliminate warnings and encourage best practice. By @jamesmbaazam in #893 and reviewed by @sbfnk.

# EpiNow2 1.6.1

Expand Down
90 changes: 65 additions & 25 deletions R/dist_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,12 @@ discrete_pmf <- function(distribution =
#' )
#' dist1 + dist1
#'
#' # An uncertain gamma distribution with mean 3 and sd 2
#' # An uncertain gamma distribution with shape and rate normally distributed
#' # as Normal(3, 0.5) and Normal(2, 0.5) respectively
#' dist2 <- Gamma(
#' mean = Normal(3, 0.5), sd = Normal(2, 0.5), max = 20
#' shape = Normal(3, 0.5),
#' rate = Normal(2, 0.5),
#' max = 20
#' )
#' dist1 + dist2
`+.dist_spec` <- function(e1, e2) {
Expand Down Expand Up @@ -197,9 +200,12 @@ discrete_pmf <- function(distribution =
#' )
#' dist1 + dist1
#'
#' # An uncertain gamma distribution with mean 3 and sd 2
#' # An uncertain gamma distribution with shape and rate normally distributed
#' # as Normal(3, 0.5) and Normal(2, 0.5) respectively
#' dist2 <- Gamma(
#' mean = Normal(3, 0.5), sd = Normal(2, 0.5), max = 20
#' shape = Normal(3, 0.5),
#' rate = Normal(2, 0.5),
#' max = 20
#' )
#' c(dist1, dist2)
c.dist_spec <- function(...) {
Expand Down Expand Up @@ -257,9 +263,12 @@ c.dist_spec <- function(...) {
#' dist1 <- LogNormal(mean = 5, sd = 1, max = 20)
#' mean(dist1)
#'
#' # An uncertain gamma distribution with mean 3 and sd 2
#' # An uncertain gamma distribution with shape and rate normally distributed
#' # as Normal(3, 0.5) and Normal(2, 0.5) respectively
#' dist2 <- Gamma(
#' mean = Normal(3, 0.5), sd = Normal(2, 0.5), max = 20
#' shape = Normal(3, 0.5),
#' rate = Normal(2, 0.5),
#' max = 20
#' )
#' mean(dist2)
#'
Expand Down Expand Up @@ -393,8 +402,13 @@ sd.default <- function(x, ...) {
#' dist1 <- Gamma(mean = 5, sd = 1, max = 20)
#' max(dist1)
#'
#' # An uncertain lognormal distribution with mean 3 and sd 2
#' dist2 <- LogNormal(mean = Normal(3, 0.5), sd = Normal(2, 0.5), max = 20)
#' # An uncertain lognormal distribution with meanlog and sdlog normally
#' # distributed as Normal(3, 0.5) and Normal(2, 0.5) respectively
#' dist2 <- LogNormal(
#' meanlog = Normal(3, 0.5),
#' sdlog = Normal(2, 0.5),
#' max = 20
#' )
#' max(dist2)
#'
#' # The max the sum of two distributions
Expand Down Expand Up @@ -447,8 +461,13 @@ discretise <- function(x, ...) {
#' # A fixed gamma distribution with mean 5 and sd 1.
#' dist1 <- Gamma(mean = 5, sd = 1, max = 20)
#'
#' # An uncertain lognormal distribution with mean 3 and sd 2
#' dist2 <- LogNormal(mean = Normal(3, 0.5), sd = Normal(2, 0.5), max = 20)
#' # An uncertain lognormal distribution with meanlog and sdlog normally
#' # distributed as Normal(3, 0.5) and Normal(2, 0.5) respectively
#' dist2 <- LogNormal(
#' meanlog = Normal(3, 0.5),
#' sdlog = Normal(2, 0.5),
#' max = 20
#' )
#'
#' # The maxf the sum of two distributions
#' discretise(dist1 + dist2, strict = FALSE)
Expand Down Expand Up @@ -531,11 +550,16 @@ collapse <- function(x, ...) {
#' # A fixed gamma distribution with mean 5 and sd 1.
#' dist1 <- Gamma(mean = 5, sd = 1, max = 20)
#'
#' # An uncertain lognormal distribution with mean 3 and sd 2
#' dist2 <- LogNormal(mean = 3, sd = 2, max = 20)
#' # An uncertain lognormal distribution with meanlog and sdlog normally
#' # distributed as Normal(3, 0.5) and Normal(2, 0.5) respectively
#' dist2 <- LogNormal(
#' meanlog = Normal(3, 0.5),
#' sdlog = Normal(2, 0.5),
#' max = 20
#' )
#'
#' # The maxf the sum of two distributions
#' collapse(discretise(dist1 + dist2))
#' collapse(discretise(dist1 + dist2, strict = FALSE))
collapse.dist_spec <- function(x, ...) {
return(x)
}
Expand Down Expand Up @@ -588,9 +612,10 @@ collapse.multi_dist_spec <- function(x, ...) {
#' dist1 <- LogNormal(mean = 1.5, sd = 0.5, max = 20)
#' print(dist1)
#'
#' # An uncertain gamma distribution with mean 3 and sd 2
#' # An uncertain gamma distribution with shape and rate normally distributed
#' # as Normal(3, 0.5) and Normal(2, 0.5) respectively
#' dist2 <- Gamma(
#' mean = Normal(3, 0.5), sd = Normal(2, 0.5), max = 20
#' shape = Normal(3, 0.5), rate = Normal(2, 0.5), max = 20
#' )
#' print(dist2)
print.dist_spec <- function(x, ...) {
Expand Down Expand Up @@ -680,9 +705,12 @@ print.dist_spec <- function(x, ...) {
#' # Plot discretised distribution with 0.01 day discretisation window
#' plot(dist1, res = 0.01, cumulative = FALSE)
#'
#' # An uncertain gamma distribution with mean 3 and sd 2
#' # An uncertain gamma distribution with shape and rate normally distributed
#' # as Normal(3, 0.5) and Normal(2, 0.5) respectively
#' dist2 <- Gamma(
#' mean = Normal(3, 0.5), sd = Normal(2, 0.5), max = 20
#' shape = Normal(3, 0.5),
#' rate = Normal(2, 0.5),
#' max = 20
#' )
#' plot(dist2)
#'
Expand Down Expand Up @@ -781,9 +809,12 @@ plot.dist_spec <- function(x, samples = 50L, res = 1, cumulative = TRUE, ...) {
#' @examples
#' dist1 <- LogNormal(mean = 1.6, sd = 0.5, max = 20)
#'
#' # An uncertain gamma distribution with mean 3 and sd 2
#' # An uncertain gamma distribution with shape and rate normally distributed
#' # as Normal(3, 0.5) and Normal(2, 0.5) respectively
#' dist2 <- Gamma(
#' mean = Normal(3, 0.5), sd = Normal(2, 0.5), max = 20
#' shape = Normal(3, 0.5),
#' rate = Normal(2, 0.5),
#' max = 20
#' )
#'
#' # Multiple distributions
Expand Down Expand Up @@ -828,9 +859,12 @@ fix_parameters <- function(x, ...) {
#' @importFrom rlang arg_match
#' @method fix_parameters dist_spec
#' @examples
#' # An uncertain gamma distribution with mean 3 and sd 2
#' dist <- LogNormal(
#' meanlog = Normal(3, 0.5), sdlog = Normal(2, 0.5), max = 20
#' # An uncertain gamma distribution with shape and rate normally distributed
#' # as Normal(3, 0.5) and Normal(2, 0.5) respectively
#' dist <- Gamma(
#' shape = Normal(3, 0.5),
#' rate = Normal(2, 0.5),
#' max = 20
#' )
#'
#' fix_parameters(dist)
Expand Down Expand Up @@ -891,8 +925,13 @@ is_constrained <- function(x, ...) {
#' # A fixed gamma distribution with mean 5 and sd 1.
#' dist1 <- Gamma(mean = 5, sd = 1, max = 20)
#'
#' # An uncertain lognormal distribution with mean 3 and sd 2
#' dist2 <- LogNormal(mean = Normal(3, 0.5), sd = Normal(2, 0.5), max = 20)
#' # An uncertain lognormal distribution with meanlog and sdlog normally
#' # distributed as Normal(3, 0.5) and Normal(2, 0.5) respectively
#' dist2 <- LogNormal(
#' meanlog = Normal(3, 0.5),
#' sdlog = Normal(2, 0.5),
#' max = 20
#' )
#'
#' # both distributions are constrained and therefore so is the sum
#' is_constrained(dist1 + dist2)
Expand Down Expand Up @@ -952,7 +991,8 @@ is_constrained.multi_dist_spec <- function(x, ...) {
#' @examples
#' LogNormal(mean = 4, sd = 1)
#' LogNormal(mean = 4, sd = 1, max = 10)
#' LogNormal(mean = Normal(4, 1), sd = 1, max = 10)
#' # If specifying uncertain parameters, use the natural parameters
#' LogNormal(meanlog = Normal(1.5, 0.5), sdlog = 0.25, max = 10)
LogNormal <- function(meanlog, sdlog, mean, sd, ...) {
params <- as.list(environment())
return(new_dist_spec(params, "lognormal", ...))
Expand Down
10 changes: 7 additions & 3 deletions R/opts.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
#' # An uncertain gamma distributed generation time
#' generation_time_opts(
#' Gamma(
#' mean = Normal(mean = 3, sd = 1),
#' sd = Normal(mean = 2, sd = 0.5),
#' shape = Normal(mean = 3, sd = 1),
#' rate = Normal(mean = 2, sd = 0.5),
#' max = 14
#' )
#' )
Expand Down Expand Up @@ -186,7 +186,11 @@ secondary_opts <- function(type = c("incidence", "prevalence"), ...) {
#' delay_opts()
#'
#' # A single delay that has uncertainty
#' delay <- LogNormal(mean = Normal(1, 0.2), sd = Normal(0.5, 0.1), max = 14)
#' delay <- LogNormal(
#' meanlog = Normal(1, 0.2),
#' sdlog = Normal(0.5, 0.1),
#' max = 14
#' )
#' delay_opts(delay)
#'
#' # A single delay without uncertainty
Expand Down
3 changes: 2 additions & 1 deletion man/Distributions.Rd

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

7 changes: 5 additions & 2 deletions man/c.dist_spec.Rd

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

11 changes: 8 additions & 3 deletions man/collapse.Rd

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

6 changes: 5 additions & 1 deletion man/delay_opts.Rd

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

9 changes: 7 additions & 2 deletions man/discretise.Rd

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

7 changes: 5 additions & 2 deletions man/extract_single_dist.Rd

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

9 changes: 6 additions & 3 deletions man/fix_parameters.Rd

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

4 changes: 2 additions & 2 deletions man/generation_time_opts.Rd

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

9 changes: 7 additions & 2 deletions man/is_constrained.Rd

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

9 changes: 7 additions & 2 deletions man/max.dist_spec.Rd

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

7 changes: 5 additions & 2 deletions man/mean.dist_spec.Rd

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

7 changes: 5 additions & 2 deletions man/plot.dist_spec.Rd

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

Loading
Loading