# Tests for map_dist_parms() in R/core.R, which maps (prevalence, c-statistic) # to the parameters of the distribution of calibrated risks via mcmapper. # # Regression note: before mcmapper 0.0.12, its logit-normal mapper returned NULL # when its Nelder-Mead search did not converge. bpm_valprec() and bpm_valsamp() # then stopped with "replacement has length zero". The draw below is one where # this happened, in the tutorial's case study. # Implied mean and c-statistic of logit-normal parameters. logitnorm_mc <- function(p) { m <- logitnorm::momentsLogitnorm(p[[1]], p[[2]])[[1]] c(m = m, c = bayespmtools:::calc_cstat("logitnorm", unname(p), m = m)) } test_that("map_dist_parms() maps a draw on which mcmap() did not converge", { prev <- 0.500017426890921 cstat <- 0.767249881090165 p <- suppressMessages(bayespmtools:::map_dist_parms(prev, cstat, "logitnorm")) expect_length(p, 2) expect_equal(unname(logitnorm_mc(p)), c(prev, cstat), tolerance = 1e-4) }) test_that("map_dist_parms() retries when mcmap() returns NULL", { local_mocked_bindings( mcmap = function(...) list(value = NULL), .package = "mcmapper" ) p <- bayespmtools:::map_dist_parms(0.3, 0.75, "logitnorm") expect_length(p, 2) expect_equal(unname(logitnorm_mc(p)), c(0.3, 0.75), tolerance = 1e-4) }) test_that("map_dist_parms() gives a clear error when the mapping fails", { local_mocked_bindings( mcmap = function(...) list(value = NULL), mcmap_logitnorm = function(...) NULL, .package = "mcmapper" ) expect_error( bayespmtools:::map_dist_parms(0.3, 0.75, "logitnorm"), "Could not find logitnorm parameters" ) })