From 97bd35a29f498948dacd4a96a7a7df5561e963a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20D=2E=20Uma=C3=B1a?= Date: Fri, 8 Nov 2024 20:57:47 +0100 Subject: [PATCH 1/6] Fix geometric_mean positive method --- R/utils.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/utils.R b/R/utils.R index c110e42..4eb7bb1 100644 --- a/R/utils.R +++ b/R/utils.R @@ -196,10 +196,11 @@ geometric_mean <- function(x, method = c( method <- match.arg(method) if (method == "positive") { + x_positive <- x[x>0] stopifnot("`x` includes zero or negative values, - check the geom_mean methods" = all(x > 0)) + check the geometric_mean methods" = all(x_positive > 0)) - gm <- exp(mean(log(x))) + gm <- exp(mean(log(x_positive))) } else if (method == "shifted") { x_shifted <- x + shift stopifnot("shifted `x` still includes zero or negative values, From 39d3e31d856d63c3e9f2f9b6e0bdd5882ae6cb67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20D=2E=20Uma=C3=B1a?= Date: Fri, 8 Nov 2024 21:07:55 +0100 Subject: [PATCH 2/6] Lint fix --- R/utils.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index 4eb7bb1..8734a53 100644 --- a/R/utils.R +++ b/R/utils.R @@ -196,7 +196,7 @@ geometric_mean <- function(x, method = c( method <- match.arg(method) if (method == "positive") { - x_positive <- x[x>0] + x_positive <- x[x > 0] stopifnot("`x` includes zero or negative values, check the geometric_mean methods" = all(x_positive > 0)) From fc1c5e141b2472951467fc95d7a5117334d6c52d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20D=2E=20Uma=C3=B1a?= Date: Mon, 11 Nov 2024 14:35:20 +0100 Subject: [PATCH 3/6] Update during styling --- R/demographics.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/demographics.R b/R/demographics.R index 6f7236c..4e9eddf 100644 --- a/R/demographics.R +++ b/R/demographics.R @@ -261,8 +261,10 @@ population_pyramid_plot <- function(pop_pyramid, sex = TRUE) { #' ) #' @export age_risk <- function(age, population_pyramid, sex = NULL, plot = FALSE) { - stopifnot("`age` must be a numeric vector with values greater than 0" = - ((all(age > 0)) & (is.numeric(age)))) + stopifnot( + "`age` must be a numeric vector with values greater than 0" = + ((all(age > 0)) & (is.numeric(age))) + ) if (inherits(population_pyramid, what = "list")) { population_pyramid <- population_pyramid$data } From 2f1873afaa7ba3a4f501e7d5a95a55f530132cc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20D=2E=20Uma=C3=B1a?= Date: Mon, 11 Nov 2024 14:35:45 +0100 Subject: [PATCH 4/6] Fix misleading stopifnot in geometric_mean --- R/utils.R | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/R/utils.R b/R/utils.R index 8734a53..4eaea78 100644 --- a/R/utils.R +++ b/R/utils.R @@ -183,11 +183,12 @@ incidence_rate <- function(incidence_object, level, scale = 100000) { #' geometric_mean(x, method = "optimized") #' #' @export -geometric_mean <- function(x, method = c( - "positive", "shifted", - "optimized", "weighted" - ), - shift = 1, epsilon = 1e-3) { +geometric_mean <- function( + x, method = c( + "positive", "shifted", + "optimized", "weighted" + ), + shift = 1, epsilon = 1e-3) { stopifnot( "`x`must be numeric" = (is.numeric(x)), "`shift` must be numeric" = (is.numeric(shift)), @@ -196,10 +197,13 @@ geometric_mean <- function(x, method = c( method <- match.arg(method) if (method == "positive") { + if (sum(x <= 0) > 0) { + message( + sum(x <= 0), + " zeros or negative values where ignored in the estimation" + ) + } x_positive <- x[x > 0] - stopifnot("`x` includes zero or negative values, - check the geometric_mean methods" = all(x_positive > 0)) - gm <- exp(mean(log(x_positive))) } else if (method == "shifted") { x_shifted <- x + shift @@ -296,11 +300,12 @@ geometric_mean <- function(x, method = c( #' geometric_sd(x, method = "optimized") #' #' @export -geometric_sd <- function(x, method = c( - "positive", "shifted", - "optimized", "weighted" - ), - shift = 1, delta = 1e-3) { +geometric_sd <- function( + x, method = c( + "positive", "shifted", + "optimized", "weighted" + ), + shift = 1, delta = 1e-3) { stopifnot( "`x`must be numeric" = (is.numeric(x)), "`shift` must be numeric" = (is.numeric(shift)), From e51086acbc5b1d648777ec40f952327fae1017d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20D=2E=20Uma=C3=B1a?= Date: Mon, 11 Nov 2024 14:39:38 +0100 Subject: [PATCH 5/6] Delete not expected errors --- tests/testthat/test-utils.R | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 2bc5563..0084693 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -85,14 +85,12 @@ test_that("Incidence rate calculate rates", { test_that("Geometric mean throws errors", { expect_error(geometric_mean(c(45, 20, 1000, "a"))) expect_error(geometric_mean(c(45, 20, 1000, 100), method = "test")) - expect_error(geometric_mean(c(45, 20, 1000, 100), + expect_error(geometric_mean(c(45, 20, 1000, -3), method = "shifted", shift = "2" )) expect_error(geometric_mean(c(45, 20, 1000, 100), epsilon = "test")) expect_error(geometric_mean(c(45, 20, 1000, -100), method = "shifted")) - expect_error(geometric_mean(c(45, 20, 1000, -100), epsilon = "positive")) - expect_error(geometric_mean(c(45, 20, 1000, -100), method = "positive")) }) test_that("Geometric mean works as expected", { From 901b2afcaf2b2a9cf1f6877d65d4313e98259994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20D=2E=20Uma=C3=B1a?= Date: Tue, 12 Nov 2024 11:27:22 +0100 Subject: [PATCH 6/6] Fix logical aggregation --- R/utils.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index 4eaea78..39685df 100644 --- a/R/utils.R +++ b/R/utils.R @@ -197,7 +197,7 @@ geometric_mean <- function( method <- match.arg(method) if (method == "positive") { - if (sum(x <= 0) > 0) { + if (any(x) <= 0) { message( sum(x <= 0), " zeros or negative values where ignored in the estimation"