From 45486addda5a53ef5f5756e84d173ac5e4185c79 Mon Sep 17 00:00:00 2001 From: Giovanni Charles Date: Tue, 9 Jan 2024 10:10:45 +0000 Subject: [PATCH] Check that booster_spacing is monotonically increasing --- R/pev_parameters.R | 15 +++++++++++++++ tests/testthat/test-pev-epi.R | 20 ++++++++++++++++++++ tests/testthat/test-pev.R | 20 ++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/R/pev_parameters.R b/R/pev_parameters.R index 73947b29..7edcb39c 100644 --- a/R/pev_parameters.R +++ b/R/pev_parameters.R @@ -98,6 +98,13 @@ set_pev_epi <- function( stop("coverages and timesteps must align") } + # Check that booster_spacing are monotonically increasing + if (length(booster_spacing) > 1) { + if (!all(diff(booster_spacing) > 0)) { + stop('booster_spacing must be monotonically increasing') + } + } + # Check that seasonal booster parameters make sense stopifnot(min_wait >= 0) stopifnot(age >= 0) @@ -178,6 +185,14 @@ set_mass_pev <- function( if (length(min_ages) != length(max_ages)) { stop('min and max ages do not align') } + + # Check that booster_spacing are monotonically increasing + if (length(booster_spacing) > 1) { + if (!all(diff(booster_spacing) > 0)) { + stop('booster_spacing must be monotonically increasing') + } + } + stopifnot((length(booster_coverage)) == 0 || all(booster_coverage >= 0 & booster_coverage <= 1)) if (!all(c(ncol(booster_coverage), length(booster_profile)) == length(booster_spacing))) { stop('booster_spacing, booster_coverage and booster_profile do not align') diff --git a/tests/testthat/test-pev-epi.R b/tests/testthat/test-pev-epi.R index 56db32e7..6555700c 100644 --- a/tests/testthat/test-pev-epi.R +++ b/tests/testthat/test-pev-epi.R @@ -70,6 +70,26 @@ test_that('set_pev_epi checks booster coverage matrix shape', { ) }) +test_that('set_pev_epi checks that booster_spacing are increasing', { + parameters <- get_parameters() + expect_error( + parameters <- set_pev_epi( + parameters, + profile = rtss_profile, + coverages = c(0.1, 0.8), + timesteps = c(10, 100), + min_wait = 0, + age = 5 * 30, + booster_spacing = c(5, 5) * 30, + booster_coverage = matrix(c(.9, .8), nrow=2, ncol=1), + booster_profile = list(rtss_booster_profile, rtss_booster_profile) + ), + 'booster_spacing must be monotonically increasing', + fixed = TRUE + ) +}) + + test_that('pev epi targets correct age and respects min_wait', { timestep <- 5*365 parameters <- get_parameters(list(human_population = 5)) diff --git a/tests/testthat/test-pev.R b/tests/testthat/test-pev.R index 11a9030b..88b95b35 100644 --- a/tests/testthat/test-pev.R +++ b/tests/testthat/test-pev.R @@ -74,6 +74,26 @@ test_that('set_mass_pev checks booster coverage matrix shape', { ) }) +test_that('set_mass_pev checks booster_spacing is increasing', { + parameters <- get_parameters() + expect_error( + parameters <- set_mass_pev( + parameters, + profile = rtss_profile, + coverages = c(0.1), + timesteps = c(10), + min_wait = 0, + min_ages = 5 * 30, + max_ages = 17 * 30, + booster_spacing = c(5, 5) * 30, + booster_coverage = matrix(c(.9, .8), nrow=2, ncol=1), + booster_profile = list(rtss_booster_profile, rtss_booster_profile) + ), + 'booster_spacing must be monotonically increasing', + fixed = TRUE + ) +}) + test_that('Mass vaccination fails pre-emptively for unaligned booster parameters', { parameters <- get_parameters() expect_error(