From de1e374e397eea1969795d1e7f950f14af3e6d92 Mon Sep 17 00:00:00 2001 From: Sebastian Funk Date: Mon, 30 Sep 2024 17:21:51 +0100 Subject: [PATCH] use `tail` for check (#802) * use `tail` for check * add news item * add regression test --- NEWS.md | 2 +- R/checks.R | 2 +- tests/testthat/test-delays.R | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index b1fdee1c9..1f0f1c64d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -23,7 +23,7 @@ A release that introduces model improvements to the Gaussian Process models, alo - Switch to broadcasting from random walks and added unit tests. By @seabbs in #747 and reviewed by @jamesmbaazam. - Optimised convolution code to take into account the relative length of the vectors being convolved. See #745 by @seabbs and reviewed by @jamesmbaazam. - Switch to broadcasting the day of the week effect. By @seabbs in #746 and reviewed by @jamesmbaazam. -- A warning is now thrown if nonparametric PMFs passed to delay options have consecutive tail values that are below a certain low threshold as these lead to loss in speed with little gain in accuracy. By @jamesmbaazam in #752 and reviewed by @seabbs. +- A warning is now thrown if nonparametric PMFs passed to delay options have consecutive tail values that are below a certain low threshold as these lead to loss in speed with little gain in accuracy. By @jamesmbaazam in #752 and reviewed by @seabbs, with a subsequent bug fix in #802. - `dist_fit()` can now accept any number of `samples` without throwing a warning when `samples` < 1000 in #751 by @jamesmbaazam and reviewed by @seabbs and @sbfnk. ## Package changes diff --git a/R/checks.R b/R/checks.R index ab730773e..2ad4136aa 100644 --- a/R/checks.R +++ b/R/checks.R @@ -132,7 +132,7 @@ check_stan_delay <- function(dist) { #' @return Called for its side effects. #' @keywords internal check_sparse_pmf_tail <- function(pmf, span = 5, tol = 1e-6) { - if (all(pmf[(length(pmf) - span + 1):length(pmf)] < tol)) { + if (all(tail(pmf, span) < tol)) { cli_warn( c( "!" = "The PMF tail has {col_blue(span)} consecutive value{?s} smaller diff --git a/tests/testthat/test-delays.R b/tests/testthat/test-delays.R index afd4c9e61..e2f056b3b 100644 --- a/tests/testthat/test-delays.R +++ b/tests/testthat/test-delays.R @@ -63,6 +63,13 @@ test_that("delay parameters can be specified in different ways", { ), digits = 2), n = -2), c(0.08, 0.33, 0.36, 0.16, 0.05, 0.02, 0.01, 0.00, 0.00, 1.00) ) + expect_equal( + test_stan_delays( + delays = delay_opts(NonParametric(pmf = c(0.1, 0.6, 0.3))), + params = delay_params + ), + c(0.0, 1.0, 0.1, 0.6, 0.3, 1.0) + ) }) test_that("truncation parameters can be specified in different ways", {