From eb2db429c3900ba85144aa94497a97844c607337 Mon Sep 17 00:00:00 2001 From: Dirk Schumacher Date: Fri, 1 Nov 2024 12:01:05 +0100 Subject: [PATCH] Fix overflow in zscore estimates --- R/prevalence-simple.R | 4 ++-- tests/testthat/test-prevalence-simple-estimates.R | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/R/prevalence-simple.R b/R/prevalence-simple.R index ee12c0e..b97b323 100644 --- a/R/prevalence-simple.R +++ b/R/prevalence-simple.R @@ -162,7 +162,7 @@ sample_size <- function(x, N, empty_data_prototype) { } sample_se <- function(x, x_mean, n, N) { - scale <- N / (N - 1) * 1 / (n * n) + scale <- N / (N - 1) x_deviation <- x - x_mean - sqrt(sum(scale * x_deviation * x_deviation)) + sqrt(scale)/n * sqrt(sum(x_deviation * x_deviation)) } diff --git a/tests/testthat/test-prevalence-simple-estimates.R b/tests/testthat/test-prevalence-simple-estimates.R index 164c220..248b2b2 100644 --- a/tests/testthat/test-prevalence-simple-estimates.R +++ b/tests/testthat/test-prevalence-simple-estimates.R @@ -30,3 +30,10 @@ test_that("survey and approximation yield are equal within tolerance", { expect_equal(res_simple[[!!col]], res_survey[[!!col]], tolerance = 0.0001) } }) + +test_that("zscore estimates should not overflow", { + x <- rnorm(100000) + expect_no_warning( + zscore_estimate(x, length(x), data.frame()) + ) +})