Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix geometric_mean positive method #154

Merged
merged 6 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions R/demographics.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
34 changes: 20 additions & 14 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -196,10 +197,14 @@ geometric_mean <- function(x, method = c(
method <- match.arg(method)

if (method == "positive") {
stopifnot("`x` includes zero or negative values,
check the geom_mean methods" = all(x > 0))

gm <- exp(mean(log(x)))
if (any(x) <= 0) {
message(
sum(x <= 0),
" zeros or negative values where ignored in the estimation"
)
}
x_positive <- x[x > 0]
gm <- exp(mean(log(x_positive)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we previously errored if there were any negative or zero value, I believe we are guaranteed to have strictly positive values at this stage, aren't we?

If that's not the case, could you add a test case that fails on main but passes on this branch please?

} else if (method == "shifted") {
x_shifted <- x + shift
stopifnot("shifted `x` still includes zero or negative values,
Expand Down Expand Up @@ -295,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)),
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
Loading