From ccdc19729b2fec77e571bae7cfae5fa0d509210a Mon Sep 17 00:00:00 2001 From: Sebastian Funk Date: Thu, 2 May 2024 22:37:47 +0100 Subject: [PATCH] rename argument silent -> strict (#648) * rename argument silent -> strict * example requires non-strictness * re-render docs * fix typo Co-authored-by: James Azam --------- Co-authored-by: James Azam --- R/create.R | 2 +- R/dist_spec.R | 19 ++++++++++--------- man/discretise.Rd | 13 +++++++------ tests/testthat/test-dist_spec.R | 3 ++- tests/testthat/test-epinow.R | 2 +- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/R/create.R b/R/create.R index 8f4bbfd31..55be17600 100644 --- a/R/create.R +++ b/R/create.R @@ -728,7 +728,7 @@ create_stan_args <- function(stan = stan_opts(), create_stan_delays <- function(..., time_points = 1L) { delays <- list(...) ## discretise - delays <- map(list(...), discretise) + delays <- map(delays, discretise, strict = FALSE) ## convolve where appropriate delays <- map(delays, collapse) ## apply tolerance diff --git a/R/dist_spec.R b/R/dist_spec.R index 8e8ae7362..281fb5a08 100644 --- a/R/dist_spec.R +++ b/R/dist_spec.R @@ -406,9 +406,10 @@ max.dist_spec <- function(x, ...) { #' (i.e. those with finite support and constant parameters). #' @title Discretise a #' @param x A `` -#' @param silent Logical; if `TRUE` then any distribution that can't be -#' discretised will be returned as is. If `FALSE` then an error will be -#' thrown. +#' @param strict Logical; If `TRUE` (default) an error will be thrown if a +#' distribution cannot be discretised (e.g., because no finite maximum has been +#' specified or parameters are uncertain). If `FALSE` then any distribution +#' that cannot be discretised will be returned as is. #' @return A `` where all distributions with constant parameters are #' nonparametric. #' @export @@ -420,14 +421,14 @@ max.dist_spec <- function(x, ...) { #' dist2 <- LogNormal(mean = Normal(3, 0.5), sd = Normal(2, 0.5), max = 20) #' #' # The maxf the sum of two distributions -#' discretise(dist1 + dist2) -discretise <- function(x, silent = TRUE) { +#' discretise(dist1 + dist2, strict = FALSE) +discretise <- function(x, strict = TRUE) { if (!is(x, "dist_spec")) { stop("Can only discretise a .") } ## check max max_x <- max(x) - if (any(is.infinite(max_x)) && !silent) { + if (any(is.infinite(max_x)) && strict) { stop("Cannot discretise a distribution with infinite support.") } ## discretise @@ -445,12 +446,12 @@ discretise <- function(x, silent = TRUE) { )) z$distribution <- "nonparametric" return(z) - } else if (silent) { - return(y) - } else { + } else if (strict) { stop( "Cannot discretise a distribution with uncertain parameters." ) + } else { + return(y) } } }) diff --git a/man/discretise.Rd b/man/discretise.Rd index f6163d29a..a24848ebd 100644 --- a/man/discretise.Rd +++ b/man/discretise.Rd @@ -5,16 +5,17 @@ \alias{discretize} \title{Discretise a } \usage{ -discretise(x, silent = TRUE) +discretise(x, strict = TRUE) -discretize(x, silent = TRUE) +discretize(x, strict = TRUE) } \arguments{ \item{x}{A \verb{}} -\item{silent}{Logical; if \code{TRUE} then any distribution that can't be -discretised will be returned as is. If \code{FALSE} then an error will be -thrown.} +\item{strict}{Logical; If \code{TRUE} (default) an error will be thrown if a +distribution cannot be discretised (e.g., because no finite maximum has been +specified or parameters are uncertain). If if \code{FALSE} then any distribution +that cannot be discretised will be returned as is.} } \value{ A \verb{} where all distributions with constant parameters are @@ -35,5 +36,5 @@ dist1 <- Gamma(mean = 5, sd = 1, max = 20) dist2 <- LogNormal(mean = Normal(3, 0.5), sd = Normal(2, 0.5), max = 20) # The maxf the sum of two distributions -discretise(dist1 + dist2) +discretise(dist1 + dist2, strict = FALSE) } diff --git a/tests/testthat/test-dist_spec.R b/tests/testthat/test-dist_spec.R index 8c028a825..992e746a2 100644 --- a/tests/testthat/test-dist_spec.R +++ b/tests/testthat/test-dist_spec.R @@ -14,7 +14,8 @@ test_that("dist_spec returns correct output for fixed lognormal distribution", { test_that("dist_spec returns correct output for uncertain gamma distribution", { result <- discretise( - Gamma(shape = Normal(3, 0.5), rate = Normal(2, 0.5), max = 19) + Gamma(shape = Normal(3, 0.5), rate = Normal(2, 0.5), max = 19), + strict = FALSE ) expect_equal(result[[1]]$parameters$shape[[1]]$parameters$mean, 3) expect_equal(result[[1]]$parameters$shape[[1]]$parameters$sd, 0.5) diff --git a/tests/testthat/test-epinow.R b/tests/testthat/test-epinow.R index 876fdcbba..50264ef18 100644 --- a/tests/testthat/test-epinow.R +++ b/tests/testthat/test-epinow.R @@ -28,7 +28,7 @@ test_that("epinow produces expected output when run with default settings", { control = list(adapt_delta = 0.8) ), logs = NULL, verbose = FALSE - ) + ) ))) expect_equal(names(out), expected_out)