From aeb14cdc46b216eccef184eb44a67a743a331315 Mon Sep 17 00:00:00 2001 From: Joshua Lambert Date: Fri, 18 Aug 2023 14:17:52 +0100 Subject: [PATCH 1/5] fix clean_epidist_params methods to handle unparameterised sets, fixes #161 --- R/epidist_utils.R | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/R/epidist_utils.R b/R/epidist_utils.R index 7b730703e..45850c700 100644 --- a/R/epidist_utils.R +++ b/R/epidist_utils.R @@ -546,6 +546,12 @@ clean_epidist_params <- function(prob_dist_params, ...) { #' @keywords internal clean_epidist_params.gamma <- function(prob_dist_params) { + # if unparameterised return named vector of NAs + if (isTRUE(is.na(prob_dist_params))) { + prob_dist_params <- c(shape = NA, scale = NA) + return(prob_dist_params) + } + # if shape and rate are provided convert to shape and scale if (all(c("shape", "rate") %in% names(prob_dist_params))) { prob_dist_params[["rate"]] <- 1 / prob_dist_params[["rate"]] @@ -580,6 +586,12 @@ clean_epidist_params.gamma <- function(prob_dist_params) { #' @keywords internal clean_epidist_params.lnorm <- function(prob_dist_params) { + # if unparameterised return named vector of NAs + if (isTRUE(is.na(prob_dist_params))) { + prob_dist_params <- c(meanlog = NA, sdlog = NA) + return(prob_dist_params) + } + # if mu and sigma are provided convert to meanlog and sdlog if (all(c("mu", "sigma") %in% names(prob_dist_params))) { @@ -616,6 +628,12 @@ clean_epidist_params.lnorm <- function(prob_dist_params) { #' @keywords internal clean_epidist_params.weibull <- function(prob_dist_params) { + # if unparameterised return named vector of NAs + if (isTRUE(is.na(prob_dist_params))) { + prob_dist_params <- c(shape = NA, scale = NA) + return(prob_dist_params) + } + if (all(c("shape", "scale") %in% names(prob_dist_params))) { # remove class attribute from prob_dist_params prob_dist_params <- unclass(prob_dist_params) @@ -638,6 +656,12 @@ clean_epidist_params.weibull <- function(prob_dist_params) { #' @keywords internal clean_epidist_params.nbinom <- function(prob_dist_params) { + # if unparameterised return named vector of NAs + if (isTRUE(is.na(prob_dist_params))) { + prob_dist_params <- c(mean = NA, dispersion = NA) + return(prob_dist_params) + } + if (all(c("n", "p") %in% names(prob_dist_params))) { # convert prob to mean @@ -682,6 +706,12 @@ clean_epidist_params.nbinom <- function(prob_dist_params) { #' @keywords internal clean_epidist_params.geom <- function(prob_dist_params) { + # if unparameterised return named NA + if (isTRUE(is.na(prob_dist_params))) { + prob_dist_params <- c(prob = NA) + return(prob_dist_params) + } + # if mean is provided convert to prob if ("mean" %in% names(prob_dist_params)) { prob_dist_params[["mean"]] <- 1 / prob_dist_params[["mean"]] @@ -732,6 +762,12 @@ clean_epidist_params.geom <- function(prob_dist_params) { #' @keywords internal clean_epidist_params.pois <- function(prob_dist_params) { + # if unparameterised return named NA + if (isTRUE(is.na(prob_dist_params))) { + prob_dist_params <- c(mean = NA) + return(prob_dist_params) + } + if (names(prob_dist_params) %in% c("mean", "l")) { names(prob_dist_params) <- "mean" From 8f50a9216d256686b27426a440f1a551389227f7 Mon Sep 17 00:00:00 2001 From: Joshua Lambert Date: Fri, 18 Aug 2023 14:18:11 +0100 Subject: [PATCH 2/5] added tests for clean_epidist_params bug fix, relates #161 --- tests/testthat/test-epidist_utils.R | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/testthat/test-epidist_utils.R b/tests/testthat/test-epidist_utils.R index ad77bee72..2edde4615 100644 --- a/tests/testthat/test-epidist_utils.R +++ b/tests/testthat/test-epidist_utils.R @@ -92,6 +92,33 @@ test_that("clean_epidist_params works for default method", { ) }) +test_that("clean_epidist_params works for unparameterised (NA), (#161)", { + params <- NA + class(params) <- "gamma" + res <- clean_epidist_params(prob_dist_params = params) + expect_identical(res, c(shape = NA, scale = NA)) + + class(params) <- "lnorm" + res <- clean_epidist_params(prob_dist_params = params) + expect_identical(res, c(meanlog = NA, sdlog = NA)) + + class(params) <- "weibull" + res <- clean_epidist_params(prob_dist_params = params) + expect_identical(res, c(shape = NA, scale = NA)) + + class(params) <- "nbinom" + res <- clean_epidist_params(prob_dist_params = params) + expect_identical(res, c(mean = NA, dispersion = NA)) + + class(params) <- "geom" + res <- clean_epidist_params(prob_dist_params = params) + expect_identical(res, c(prob = NA)) + + class(params) <- "pois" + res <- clean_epidist_params(prob_dist_params = params) + expect_identical(res, c(mean = NA)) +}) + test_that("create_epidist_region works as expected", { region <- create_epidist_region( continent = "Europe", From 38f6cf9d60560ad5c2ce966d35252404b942800c Mon Sep 17 00:00:00 2001 From: Joshua Lambert Date: Fri, 18 Aug 2023 15:00:31 +0100 Subject: [PATCH 3/5] use numeric NAs in clean_epidist_params --- R/epidist_utils.R | 12 ++++++------ tests/testthat/test-epidist_utils.R | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/R/epidist_utils.R b/R/epidist_utils.R index 45850c700..b60f6e114 100644 --- a/R/epidist_utils.R +++ b/R/epidist_utils.R @@ -548,7 +548,7 @@ clean_epidist_params.gamma <- function(prob_dist_params) { # if unparameterised return named vector of NAs if (isTRUE(is.na(prob_dist_params))) { - prob_dist_params <- c(shape = NA, scale = NA) + prob_dist_params <- c(shape = NA_real_, scale = NA_real_) return(prob_dist_params) } @@ -588,7 +588,7 @@ clean_epidist_params.lnorm <- function(prob_dist_params) { # if unparameterised return named vector of NAs if (isTRUE(is.na(prob_dist_params))) { - prob_dist_params <- c(meanlog = NA, sdlog = NA) + prob_dist_params <- c(meanlog = NA_real_, sdlog = NA_real_) return(prob_dist_params) } @@ -630,7 +630,7 @@ clean_epidist_params.weibull <- function(prob_dist_params) { # if unparameterised return named vector of NAs if (isTRUE(is.na(prob_dist_params))) { - prob_dist_params <- c(shape = NA, scale = NA) + prob_dist_params <- c(shape = NA_real_, scale = NA_real_) return(prob_dist_params) } @@ -658,7 +658,7 @@ clean_epidist_params.nbinom <- function(prob_dist_params) { # if unparameterised return named vector of NAs if (isTRUE(is.na(prob_dist_params))) { - prob_dist_params <- c(mean = NA, dispersion = NA) + prob_dist_params <- c(mean = NA_real_, dispersion = NA_real_) return(prob_dist_params) } @@ -708,7 +708,7 @@ clean_epidist_params.geom <- function(prob_dist_params) { # if unparameterised return named NA if (isTRUE(is.na(prob_dist_params))) { - prob_dist_params <- c(prob = NA) + prob_dist_params <- c(prob = NA_real_) return(prob_dist_params) } @@ -764,7 +764,7 @@ clean_epidist_params.pois <- function(prob_dist_params) { # if unparameterised return named NA if (isTRUE(is.na(prob_dist_params))) { - prob_dist_params <- c(mean = NA) + prob_dist_params <- c(mean = NA_real_) return(prob_dist_params) } diff --git a/tests/testthat/test-epidist_utils.R b/tests/testthat/test-epidist_utils.R index 2edde4615..6a5addbb8 100644 --- a/tests/testthat/test-epidist_utils.R +++ b/tests/testthat/test-epidist_utils.R @@ -96,27 +96,27 @@ test_that("clean_epidist_params works for unparameterised (NA), (#161)", { params <- NA class(params) <- "gamma" res <- clean_epidist_params(prob_dist_params = params) - expect_identical(res, c(shape = NA, scale = NA)) + expect_identical(res, c(shape = NA_real_, scale = NA_real_)) class(params) <- "lnorm" res <- clean_epidist_params(prob_dist_params = params) - expect_identical(res, c(meanlog = NA, sdlog = NA)) + expect_identical(res, c(meanlog = NA_real_, sdlog = NA_real_)) class(params) <- "weibull" res <- clean_epidist_params(prob_dist_params = params) - expect_identical(res, c(shape = NA, scale = NA)) + expect_identical(res, c(shape = NA_real_, scale = NA_real_)) class(params) <- "nbinom" res <- clean_epidist_params(prob_dist_params = params) - expect_identical(res, c(mean = NA, dispersion = NA)) + expect_identical(res, c(mean = NA_real_, dispersion = NA_real_)) class(params) <- "geom" res <- clean_epidist_params(prob_dist_params = params) - expect_identical(res, c(prob = NA)) + expect_identical(res, c(prob = NA_real_)) class(params) <- "pois" res <- clean_epidist_params(prob_dist_params = params) - expect_identical(res, c(mean = NA)) + expect_identical(res, c(mean = NA_real_)) }) test_that("create_epidist_region works as expected", { From 93517f56af84875b7a9adad05702e50332839ee3 Mon Sep 17 00:00:00 2001 From: Joshua Lambert Date: Fri, 18 Aug 2023 15:04:56 +0100 Subject: [PATCH 4/5] updated .lintr from packagetemplate due to unstable indentation_linter --- .lintr | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.lintr b/.lintr index 35d11eee8..7cb8ac888 100644 --- a/.lintr +++ b/.lintr @@ -5,6 +5,12 @@ linters: linters_with_tags( implicit_integer_linter = NULL, extraction_operator_linter = NULL, todo_comment_linter = NULL, - function_argument_linter = NULL + function_argument_linter = NULL, + indentation_linter = NULL, # unstable as of lintr 3.1.0 + # Use minimum R declared in DESCRIPTION or fall back to current R version. + # Install etdev package from https://github.com/epiverse-trace/etdev + backport_linter(if (length(x <- etdev::extract_min_r_version())) x else getRversion()) ) - +exclusions: list( + "tests/testthat.R" = list(unused_import_linter = Inf) + ) \ No newline at end of file From 418fbcd72866722c15bc65af0e3b8c744cf871c7 Mon Sep 17 00:00:00 2001 From: Joshua Lambert Date: Fri, 18 Aug 2023 15:08:00 +0100 Subject: [PATCH 5/5] added newline at end of .lintr --- .lintr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.lintr b/.lintr index 7cb8ac888..e2ca0f34a 100644 --- a/.lintr +++ b/.lintr @@ -13,4 +13,4 @@ linters: linters_with_tags( ) exclusions: list( "tests/testthat.R" = list(unused_import_linter = Inf) - ) \ No newline at end of file + )