From 1d531bbb83d527d01498cf16aeb7ea09369f43fc Mon Sep 17 00:00:00 2001 From: Fernando Alarid-Escudero Date: Fri, 13 Nov 2026 07:24:08 -0800 Subject: [PATCH 1/2] Move the test suite to testthat edition 3 - Config/testthat/edition: 3 in DESCRIPTION. - Remove the 18 context() calls and convert the 41 expect_is() calls to expect_s3_class(); both are deprecated in edition 3. - Rename tests/testthat/util_models_detfun.R to helper-models.R, which testthat loads automatically, instead of source()-ing it. - test_elc.R: check.names (an all.equal() argument) becomes ignore_attr, and the reference calculation breaks max.col() ties with 'first', as the package does since #167. All 263 expectations pass with no warnings. --- DESCRIPTION | 1 + .../{util_models_detfun.R => helper-models.R} | 0 tests/testthat/test_add_common_aes.R | 2 -- tests/testthat/test_calculate_icers.R | 1 - tests/testthat/test_ceac.R | 9 ++++----- tests/testthat/test_data.R | 4 +--- tests/testthat/test_dsa_oneway.R | 1 - tests/testthat/test_dsa_twoway.R | 7 +++---- tests/testthat/test_elc.R | 11 +++++------ tests/testthat/test_evpi.R | 11 +++++------ tests/testthat/test_evppi.R | 1 - tests/testthat/test_evsi.R | 1 - tests/testthat/test_gen_psa_samp.R | 1 - tests/testthat/test_metamod.R | 13 ++++++------- tests/testthat/test_owsa.R | 17 ++++++++--------- tests/testthat/test_psa.R | 3 +-- tests/testthat/test_run_dsa.R | 12 +++++------- tests/testthat/test_run_psa.R | 1 - tests/testthat/test_strategy_names.R | 2 -- tests/testthat/test_twsa.R | 11 +++++------ 20 files changed, 44 insertions(+), 65 deletions(-) rename tests/testthat/{util_models_detfun.R => helper-models.R} (100%) diff --git a/DESCRIPTION b/DESCRIPTION index e663f54..989dd10 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -70,4 +70,5 @@ LazyData: true BuildResaveData: no RoxygenNote: 7.3.2 VignetteBuilder: knitr +Config/testthat/edition: 3 Config/Needs/website: pkgdown diff --git a/tests/testthat/util_models_detfun.R b/tests/testthat/helper-models.R similarity index 100% rename from tests/testthat/util_models_detfun.R rename to tests/testthat/helper-models.R diff --git a/tests/testthat/test_add_common_aes.R b/tests/testthat/test_add_common_aes.R index dbf4454..9cf2bde 100644 --- a/tests/testthat/test_add_common_aes.R +++ b/tests/testthat/test_add_common_aes.R @@ -1,5 +1,3 @@ -context("add_common_aes") - # The accessible palette is chosen from the number of groups in the first layer when no # strategy names (v_str) are given. That used ggplot_build(p)@data, which exists only in # ggplot2 >= 4.0 and failed on older versions with "no applicable method for `@`" (#198). diff --git a/tests/testthat/test_calculate_icers.R b/tests/testthat/test_calculate_icers.R index fe9e6df..418b8bc 100644 --- a/tests/testthat/test_calculate_icers.R +++ b/tests/testthat/test_calculate_icers.R @@ -1,4 +1,3 @@ -context("calculate_icers") library(dampack) data("hund_strat") diff --git a/tests/testthat/test_ceac.R b/tests/testthat/test_ceac.R index 28c474e..3a03f6d 100644 --- a/tests/testthat/test_ceac.R +++ b/tests/testthat/test_ceac.R @@ -1,4 +1,3 @@ -context("ceac") library(dampack) # test the class @@ -86,16 +85,16 @@ test_that("summary.ceac summarizes a real psa whose frontier switches many times test_that("plot.ceac produces ggplot object", { ceac_obj <- ceac(wtp, psa_obj) gf <- plot(ceac_obj, frontier = TRUE) - expect_is(gf, "ggplot") + expect_s3_class(gf, "ggplot") gnof <- plot(ceac_obj, frontier = FALSE) - expect_is(gnof, "ggplot") + expect_s3_class(gnof, "ggplot") custom_breaks <- plot(ceac_obj, xbreaks = seq(0, 140)) - expect_is(custom_breaks, "ggplot") + expect_s3_class(custom_breaks, "ggplot") # black and white - expect_is(plot(ceac_obj, col = "bw"), "ggplot") + expect_s3_class(plot(ceac_obj, col = "bw"), "ggplot") }) # max.col() treats values within a relative tolerance of 1e-5 as ties and, by default, breaks diff --git a/tests/testthat/test_data.R b/tests/testthat/test_data.R index e7f4125..b6a675d 100644 --- a/tests/testthat/test_data.R +++ b/tests/testthat/test_data.R @@ -1,5 +1,3 @@ -context("bundled datasets") - test_that("cancer_psa_data has the documented structure", { data("cancer_psa_data") expect_named(cancer_psa_data, c("strategies", "cost", "effectiveness", "parameters")) @@ -21,7 +19,7 @@ test_that("superseded example_psa still loads and matches cancer_psa_data", { test_that("superseded example_psa_obj still loads as a psa object", { data("cancer_psa_data") data("example_psa_obj") - expect_is(example_psa_obj, "psa") + expect_s3_class(example_psa_obj, "psa") expect_equal(example_psa_obj$strategies, cancer_psa_data$strategies) expect_equal(unname(as.matrix(example_psa_obj$cost)), unname(as.matrix(cancer_psa_data$cost))) diff --git a/tests/testthat/test_dsa_oneway.R b/tests/testthat/test_dsa_oneway.R index 3878070..4f96e96 100644 --- a/tests/testthat/test_dsa_oneway.R +++ b/tests/testthat/test_dsa_oneway.R @@ -1,4 +1,3 @@ -context("dsa oneway") library(dampack) # creation of example data diff --git a/tests/testthat/test_dsa_twoway.R b/tests/testthat/test_dsa_twoway.R index c5902a3..4ec1758 100644 --- a/tests/testthat/test_dsa_twoway.R +++ b/tests/testthat/test_dsa_twoway.R @@ -1,4 +1,3 @@ -context("dsa twoway") library(dampack) # create of example data @@ -15,8 +14,8 @@ strategies <- c("s1", "s2") # class creation test_that("dsa class creation", { dsa <- create_dsa_twoway(params, effect, strategies, cost) - expect_is(dsa, "dsa_twoway") - expect_is(dsa, "sa") + expect_s3_class(dsa, "dsa_twoway") + expect_s3_class(dsa, "sa") }) # members within class @@ -43,6 +42,6 @@ test_that("dsa prints", { test_that("twsa() on a dsa_twoway object works with the default outcome", { dsa <- create_dsa_twoway(params, effect, strategies, cost) tw <- twsa(dsa) - expect_is(tw, "twsa") + expect_s3_class(tw, "twsa") expect_equal(tw$outcome_val[tw$strategy == "s1"], effect$s1) }) diff --git a/tests/testthat/test_elc.R b/tests/testthat/test_elc.R index d4dc827..0b8289e 100644 --- a/tests/testthat/test_elc.R +++ b/tests/testthat/test_elc.R @@ -1,4 +1,3 @@ -context("elc") library(dampack) @@ -20,8 +19,8 @@ elc_obj <- calc_exp_loss(psa = psa_obj, # classes test_that("return object has classes evpi and data.frame", { - expect_is(elc_obj, "exp_loss") - expect_is(elc_obj, "data.frame") + expect_s3_class(elc_obj, "exp_loss") + expect_s3_class(elc_obj, "data.frame") }) @@ -34,11 +33,11 @@ test_that("return object has correct column names", { # calculation test_that("expected loss is what we'd expect", { nmb <- v_wtp[1] * cancer_psa_data$effectiveness - cancer_psa_data$cost - max_str <- max.col(nmb) + max_str <- max.col(nmb, ties.method = "first") loss <- nmb[cbind(1:(psa_obj$n_sim), max_str)] - nmb exp_loss <- colMeans(loss) obs_loss <- unlist(c(elc_obj[1:3, 3])) - expect_equal(exp_loss, obs_loss, check.names = FALSE) + expect_equal(exp_loss, obs_loss, ignore_attr = TRUE) }) # methods @@ -47,7 +46,7 @@ test_that("expected loss is what we'd expect", { test_that("plot.exp_loss returns ggplot class", { p <- plot(elc_obj, title = "Title", txtsize = 16, effect_units = "QALY", currency = "Dollars ($%$%)") - expect_is(p, "ggplot") + expect_s3_class(p, "ggplot") }) test_that("plot.exp_loss runs with several combinations of arguments", { diff --git a/tests/testthat/test_evpi.R b/tests/testthat/test_evpi.R index cd93a32..5bebeab 100644 --- a/tests/testthat/test_evpi.R +++ b/tests/testthat/test_evpi.R @@ -1,4 +1,3 @@ -context("evpi") library(dampack) @@ -21,8 +20,8 @@ evpi_obj <- calc_evpi(psa = psa_obj, # classes test_that("return object has classes evpi and data.frame", { - expect_is(evpi_obj, "evpi") - expect_is(evpi_obj, "data.frame") + expect_s3_class(evpi_obj, "evpi") + expect_s3_class(evpi_obj, "data.frame") }) # structure @@ -43,8 +42,8 @@ test_that("evpi is what we'd expect", { # zero-length input: seq_len() loop must not misbehave when n == 0 test_that("calc_evpi returns an empty evpi object for zero-length wtp", { empty_evpi <- calc_evpi(psa = psa_obj, wtp = numeric(0)) - expect_is(empty_evpi, "evpi") - expect_is(empty_evpi, "data.frame") + expect_s3_class(empty_evpi, "evpi") + expect_s3_class(empty_evpi, "data.frame") expect_equal(nrow(empty_evpi), 0) expect_equal(names(empty_evpi), c("WTP", "EVPI")) }) @@ -56,5 +55,5 @@ test_that("plot.evpi has ggplot class", { p <- plot(evpi_obj, txtsize = 16, effect_units = "QALY", currency = "Dollars ($)", xbreaks = seq(0, 200, by = 10), ylim = c(0, 100000)) - expect_is(p, "ggplot") + expect_s3_class(p, "ggplot") }) diff --git a/tests/testthat/test_evppi.R b/tests/testthat/test_evppi.R index 767d52f..d3d7828 100644 --- a/tests/testthat/test_evppi.R +++ b/tests/testthat/test_evppi.R @@ -1,4 +1,3 @@ -context("evppi") library(dampack) diff --git a/tests/testthat/test_evsi.R b/tests/testthat/test_evsi.R index d2d8351..3856ead 100644 --- a/tests/testthat/test_evsi.R +++ b/tests/testthat/test_evsi.R @@ -1,4 +1,3 @@ -context("evsi") library(dampack) # test return object diff --git a/tests/testthat/test_gen_psa_samp.R b/tests/testthat/test_gen_psa_samp.R index d35cb3e..d8d49da 100644 --- a/tests/testthat/test_gen_psa_samp.R +++ b/tests/testthat/test_gen_psa_samp.R @@ -1,4 +1,3 @@ -context("gen_psa_samp") library(dampack) diff --git a/tests/testthat/test_metamod.R b/tests/testthat/test_metamod.R index 82dd97e..51a0734 100644 --- a/tests/testthat/test_metamod.R +++ b/tests/testthat/test_metamod.R @@ -1,4 +1,3 @@ -context("metamodel") library(dampack) # test the class @@ -17,7 +16,7 @@ psa_big <- make_psa_obj(cancer_psa_data$cost, cancer_psa_data$effectiveness, test_that("metamodel object has correct classes", { mm <- metamodel(psa = psa_big, param = "pFailChemo", outcome = "cost") - expect_is(mm, "metamodel") + expect_s3_class(mm, "metamodel") }) # methods @@ -26,7 +25,7 @@ test_that("metamodel with one outcome", { # metamodel ## linear mm_lin <- metamodel(psa = psa_big, params = "pFailChemo", outcome = "cost", strategies = "Chemo") - expect_is(mm_lin, "metamodel") + expect_s3_class(mm_lin, "metamodel") # predictions pred_lin <- predict(mm_lin) @@ -36,7 +35,7 @@ test_that("metamodel with one outcome", { ## poly mm_poly <- metamodel(psa = psa_big, params = "pFailChemo", outcome = "cost", strategies = "Chemo", type = "poly") - expect_is(mm_poly, "metamodel") + expect_s3_class(mm_poly, "metamodel") # predictions pred_poly <- predict(mm_poly) @@ -46,7 +45,7 @@ test_that("metamodel with one outcome", { ## GAM mm_gam <- metamodel(psa = psa_big, params = "pFailChemo", outcome = "cost", strategies = "Chemo", type = "gam") - expect_is(mm_gam, "metamodel") + expect_s3_class(mm_gam, "metamodel") # predictions pred_gam <- predict(mm_gam) @@ -57,7 +56,7 @@ test_that("metamodel with one outcome", { test_that("prediction with several outcomes", { # metamodel mm <- metamodel(psa = psa_big, outcome = "eff") - expect_is(mm, "metamodel") + expect_s3_class(mm, "metamodel") # number of linear models expect_equal(length(mm$mods), 8) @@ -124,7 +123,7 @@ test_that("two-way metamodel", { # metamodel mm <- metamodel(analysis = "twoway", params = c("pFailChemo", "pFailRadio"), psa = psa_big, outcome = "eff") - expect_is(mm, "metamodel") + expect_s3_class(mm, "metamodel") }) test_that("invalid metamodel parameters are reported", { diff --git a/tests/testthat/test_owsa.R b/tests/testthat/test_owsa.R index f765c26..2eb261d 100644 --- a/tests/testthat/test_owsa.R +++ b/tests/testthat/test_owsa.R @@ -1,4 +1,3 @@ -context("owsa") library(dampack) # test the class @@ -17,8 +16,8 @@ psa_big <- make_psa_obj(cancer_psa_data$cost, cancer_psa_data$effectiveness, test_that("metamod object has correct classes", { o <- owsa(psa_big, outcome = "eff", wtp = 100000) - expect_is(o, "owsa") - expect_is(o, "data.frame") + expect_s3_class(o, "owsa") + expect_s3_class(o, "data.frame") }) # methods @@ -26,7 +25,7 @@ test_that("metamod object has correct classes", { test_that("plot.owsa returns a ggplot object", { o <- owsa(psa_big, outcome = "nhb", wtp = 100000) g <- plot(o) - expect_is(g, "ggplot") + expect_s3_class(g, "ggplot") }) # test basecase argument for plot.owsa @@ -58,7 +57,7 @@ test_that("plot.owsa basecase argument", { basecase = list(cRadio = 10000, cSurg = 30000, muDieCancer = 0.2, pDieSurg = .1, pFailChemo = .45, pFailRadio = .5, pFailSurg = .050)) - expect_is(p, "ggplot") + expect_s3_class(p, "ggplot") # test error when wrong parameter is supplied expect_error(plot(o, @@ -72,13 +71,13 @@ test_that("owsa_tornado", { # returning plot p <- owsa_tornado(o) - expect_is(p, "ggplot") + expect_s3_class(p, "ggplot") # some parameters p2 <- owsa_tornado(o, txtsize = 15, min_rel_diff = 0.05, col = "bw", ylim = c(7e5, 2e6)) - expect_is(p2, "ggplot") + expect_s3_class(p2, "ggplot") # returning data d <- owsa_tornado(o, return = "data") @@ -94,11 +93,11 @@ test_that("owsa_opt_strat", { # returning plot p <- owsa_opt_strat(o) - expect_is(p, "ggplot") + expect_s3_class(p, "ggplot") # returning data d <- owsa_opt_strat(o, return = "data") - expect_is(d, "data.frame") + expect_s3_class(d, "data.frame") # pFailChemo should have 1 switch between .4 and .5 pfc <- d[d$parameter == "pFailChemo", ] diff --git a/tests/testthat/test_psa.R b/tests/testthat/test_psa.R index 4480f68..f627bbe 100644 --- a/tests/testthat/test_psa.R +++ b/tests/testthat/test_psa.R @@ -1,4 +1,3 @@ -context("psa") library(dampack) # test the class @@ -69,7 +68,7 @@ test_that("plot.psa runs", { test_that("Plot returns ggplot object", { p <- plot(psa_big) - expect_is(p, "ggplot") + expect_s3_class(p, "ggplot") }) # print diff --git a/tests/testthat/test_run_dsa.R b/tests/testthat/test_run_dsa.R index bbde0f1..8788d7e 100644 --- a/tests/testthat/test_run_dsa.R +++ b/tests/testthat/test_run_dsa.R @@ -1,7 +1,5 @@ -context("run_dsa") library(dampack) -source("util_models_detfun.R") state_name <- c("S", "IS", "IA", "T") cycle <- 100 @@ -59,8 +57,8 @@ test_that("check output of run_owsa_det", { strategies = NULL, state_name = state_name, cycle = cycle, progress = FALSE) - expect_is(o, "owsa") - expect_is(o, "data.frame") + expect_s3_class(o, "owsa") + expect_s3_class(o, "data.frame") params_range2 <- params_range[-2, ] o <- run_owsa_det(params_range2, params_basecase, nsamp = 10, @@ -98,7 +96,7 @@ test_that("check output from run_owsa_det with other dampack functions", { state_name = state_name, cycle = cycle, progress = FALSE) g <- plot(o) - expect_is(g, "ggplot") + expect_s3_class(g, "ggplot") expect_silent(g) }) @@ -211,8 +209,8 @@ test_that("check output from run_twsa_det", { strategies = strategy, state_name = state_name, cycle = cycle, progress = FALSE) - expect_is(tw, "twsa") - expect_is(tw, "data.frame") + expect_s3_class(tw, "twsa") + expect_s3_class(tw, "data.frame") expect_equal(min(tw[, params_range[1, 1]]), params_range[1, ]$min) expect_equal(max(tw[, params_range[1, 1]]), params_range[1, ]$max) diff --git a/tests/testthat/test_run_psa.R b/tests/testthat/test_run_psa.R index aaed20d..1f5c618 100644 --- a/tests/testthat/test_run_psa.R +++ b/tests/testthat/test_run_psa.R @@ -1,4 +1,3 @@ -context("run_psa") library(dampack) test_func <- function(params, extra_param) { diff --git a/tests/testthat/test_strategy_names.R b/tests/testthat/test_strategy_names.R index e00b608..9949c4b 100644 --- a/tests/testthat/test_strategy_names.R +++ b/tests/testthat/test_strategy_names.R @@ -1,5 +1,3 @@ -context("strategy names are kept as typed (#208)") - # Strategy names that are not syntactically valid R names: spaces, parentheses, # a leading digit, and a non-ASCII character. v_names <- c("Standard of care", "New drug (A)", "2-dose regimen ñ") diff --git a/tests/testthat/test_twsa.R b/tests/testthat/test_twsa.R index b6812e0..d095cdc 100644 --- a/tests/testthat/test_twsa.R +++ b/tests/testthat/test_twsa.R @@ -1,4 +1,3 @@ -context("twsa") library(dampack) # test the class @@ -17,8 +16,8 @@ psa_big <- make_psa_obj(cancer_psa_data$cost, cancer_psa_data$effectiveness, test_that("twsa object has correct classes", { t <- twsa(psa_big, "pFailChemo", "muDieCancer", outcome = "eff") - expect_is(t, "twsa") - expect_is(t, "data.frame") + expect_s3_class(t, "twsa") + expect_s3_class(t, "data.frame") }) test_that("parameters we expect to vary are varying", { @@ -34,7 +33,7 @@ test_that("parameters we expect to vary are varying", { test_that("plot.twsa returns a ggplot object", { t <- twsa(psa_big, "pFailChemo", "muDieCancer", outcome = "eff") g <- plot(t) - expect_is(g, "ggplot") + expect_s3_class(g, "ggplot") }) @@ -43,12 +42,12 @@ test_that("plot.twsa basecase argument", { t <- twsa(psa_big, "pFailChemo", "muDieCancer", outcome = "eff") g <- plot(t, basecase = list(pFailChemo = 0.37, muDieCancer = 0.09)) - expect_is(g, "ggplot") + expect_s3_class(g, "ggplot") t2 <- twsa(psa_big, "pFailChemo", "muDieCancer", outcome = "eff") g2 <- plot(t, basecase = list(muDieCancer = 0.09, pFailChemo = 0.37)) - expect_is(g2, "ggplot") + expect_s3_class(g2, "ggplot") # test error when wrong parameter is supplied expect_error(plot(t, From 64e871427384fa5cdf2b664f84d5b6b9aacd1475 Mon Sep 17 00:00:00 2001 From: Fernando Alarid-Escudero Date: Fri, 13 Nov 2026 07:33:34 -0800 Subject: [PATCH 2/2] Raise test coverage to 93% and add numerical validation Numerical validation: - beta_params(), gamma_params() (scale and rate), lnorm_params(), and dirichlet_params() reproduce the requested mean and spread exactly. - gen_psa_samp(): every distribution and parameterization (normal, both log-normal forms, truncated normal, both beta and gamma forms, all three Dirichlet forms, bootstrap, triangle, constant) gives samples with the expected moments (n = 20,000, fixed seed). Branch coverage (each test checks what the option changes): - test_plot_options.R: plot.psa() center/ellipse/col/ref options, plot.ceac() frontier/points/min_prob, the three plot.evsi() layouts, plot.evppi(), owsa_tornado() data/filter/two-strategy comparison. - summary.metamodel() (one-way lm and gam, two-way, multiway error) and print.metamodel(). - run_psa(): params_basecase, strategies taken from FUN, FUN output checks, progress output. Coverage (covr): 79.97% -> 93.08%. --- tests/testthat/test_gen_psa_samp.R | 94 ++++++++++++++++++++++++++++++ tests/testthat/test_metamod.R | 26 +++++++++ tests/testthat/test_plot_options.R | 83 ++++++++++++++++++++++++++ tests/testthat/test_run_psa.R | 35 +++++++++++ 4 files changed, 238 insertions(+) create mode 100644 tests/testthat/test_plot_options.R diff --git a/tests/testthat/test_gen_psa_samp.R b/tests/testthat/test_gen_psa_samp.R index d8d49da..5e6cb84 100644 --- a/tests/testthat/test_gen_psa_samp.R +++ b/tests/testthat/test_gen_psa_samp.R @@ -51,3 +51,97 @@ test_that("arguments of different lengths give an informative error", { dists_params = list(c(1, 1), c(1, 1))), "must all have the same length") }) + +# ---- numerical validation (#215) ------------------------------------------------------------ +# method-of-moments helpers reproduce the requested mean and spread exactly + +test_that("beta_params() matches the requested mean and standard deviation", { + l_p <- beta_params(mean = 0.3, sigma = 0.05) + a <- l_p$alpha + b <- l_p$beta + expect_equal(a / (a + b), 0.3) + expect_equal(sqrt(a * b / ((a + b)^2 * (a + b + 1))), 0.05) +}) + +test_that("gamma_params() matches the requested mean and standard deviation", { + l_scale <- gamma_params(mu = 10, sigma = 2) + expect_equal(l_scale$shape * l_scale$scale, 10) + expect_equal(sqrt(l_scale$shape) * l_scale$scale, 2) + l_rate <- gamma_params(mu = 10, sigma = 2, scale = FALSE) + expect_equal(l_rate$shape / l_rate$rate, 10) + expect_equal(sqrt(l_rate$shape) / l_rate$rate, 2) +}) + +test_that("lnorm_params() matches the requested mean and variance", { + l_p <- lnorm_params(m = 2, v = 0.5) + expect_equal(exp(l_p$mu + l_p$sigma^2 / 2), 2) + expect_equal((exp(l_p$sigma^2) - 1) * exp(2 * l_p$mu + l_p$sigma^2), 0.5) + expect_error(lnorm_params(m = 0, v = 1), "'m' needs to be greater than 0") +}) + +test_that("dirichlet_params() matches the requested proportions and spread", { + v_p <- c(0.2, 0.3, 0.5) + v_alpha <- dirichlet_params(p.mean = v_p, sigma = c(0.05, 0.06, 0.07)) + expect_equal(v_alpha / sum(v_alpha), v_p) + # the spread is set from the first component: Var = p (1 - p) / (alpha0 + 1) + expect_equal(sqrt(v_p[1] * (1 - v_p[1]) / (sum(v_alpha) + 1)), 0.05) + expect_error(dirichlet_params(p.mean = v_p, sigma = c(0.05, 0.06)), "Length of mean different") +}) + +# every distribution and parameterization of gen_psa_samp() gives samples with the +# expected moments (large nsamp, fixed seed, tolerances of a few standard errors) + +test_that("gen_psa_samp() samples every distribution with the right moments", { + set.seed(215) + n <- 20000 + samp <- gen_psa_samp( + params = c("norm", "lnorm_ms", "lnorm_log", "tnorm", "beta_ms", "beta_ab", + "gamma_ms", "gamma_ss", "boot", "tri", "const"), + dists = c("normal", "log-normal", "log-normal", "truncated-normal", "beta", "beta", + "gamma", "gamma", "bootstrap", "triangle", "constant"), + parameterization_types = c("mean, sd", "mean, sd", "meanlog, sdlog", "mean, sd, ll, ul", + "mean, sd", "a, b", "mean, sd", "shape, scale", + "value, weight", "ll, ul, mode", "val"), + dists_params = list(c(5, 1), c(2, 0.5), c(0, 0.25), c(0.5, 0.2, 0.3, 0.9), + c(0.3, 0.05), c(2, 8), c(10, 2), c(4, 0.5), + data.frame(value = c(1, 2, 4), weight = c(1, 1, 2)), + c(0, 1, 0.25), 7), + nsamp = n + ) + expect_equal(nrow(samp), n) + expect_equal(mean(samp$norm), 5, tolerance = 0.01) + expect_equal(sd(samp$norm), 1, tolerance = 0.02) + expect_equal(mean(samp$lnorm_ms), 2, tolerance = 0.01) + expect_equal(sd(samp$lnorm_ms), 0.5, tolerance = 0.03) + expect_equal(mean(samp$lnorm_log), exp(0.25^2 / 2), tolerance = 0.01) + expect_true(all(samp$tnorm >= 0.3 & samp$tnorm <= 0.9)) + expect_equal(mean(samp$beta_ms), 0.3, tolerance = 0.01) + expect_equal(sd(samp$beta_ms), 0.05, tolerance = 0.03) + expect_equal(mean(samp$beta_ab), 0.2, tolerance = 0.01) + expect_equal(mean(samp$gamma_ms), 10, tolerance = 0.01) + expect_equal(sd(samp$gamma_ms), 2, tolerance = 0.03) + expect_equal(mean(samp$gamma_ss), 2, tolerance = 0.01) + # bootstrap means of resamples drawn with probabilities proportional to the weights + expect_equal(mean(samp$boot), (1 * 1 + 2 * 1 + 4 * 2) / 4, tolerance = 0.01) + expect_equal(mean(samp$tri), (0 + 1 + 0.25) / 3, tolerance = 0.01) + expect_true(all(samp$const == 7)) +}) + +test_that("gen_psa_samp() Dirichlet parameterizations give proportions with the right means", { + set.seed(2150) + n <- 20000 + samp <- gen_psa_samp( + params = c("d_msd", "d_n", "d_alpha"), + dists = c("dirichlet", "dirichlet", "dirichlet"), + parameterization_types = c("value, mean_prop, sd", "value, n", "value, alpha"), + dists_params = list(data.frame(value = c("a1", "a2", "a3"), mean_prop = c(0.2, 0.3, 0.5), + sd = c(0.05, 0.06, 0.07)), + data.frame(value = c("b1", "b2"), n = c(30, 70)), + data.frame(value = c("c1", "c2", "c3"), alpha = c(2, 3, 5))), + nsamp = n + ) + expect_equal(unname(rowSums(samp[, c("a1", "a2", "a3")])), rep(1, n)) + expect_equal(unname(colMeans(samp[, c("a1", "a2", "a3")])), c(0.2, 0.3, 0.5), tolerance = 0.01) + expect_equal(unname(colMeans(samp[, c("b1", "b2")])), c(0.3, 0.7), tolerance = 0.01) + expect_equal(unname(colMeans(samp[, c("c1", "c2", "c3")])), c(0.2, 0.3, 0.5), tolerance = 0.01) +}) diff --git a/tests/testthat/test_metamod.R b/tests/testthat/test_metamod.R index 51a0734..541b128 100644 --- a/tests/testthat/test_metamod.R +++ b/tests/testthat/test_metamod.R @@ -145,3 +145,29 @@ test_that("invalid parameters are reported by EVPPI and EVSI", { "the following parameters are not valid: not_a_parameter" ) }) + +# print() and summary() methods (#215) +test_that("summary.metamodel() gives one R-squared per parameter and strategy", { + mm_lm <- metamodel("oneway", psa_big, params = c("pFailChemo", "cSurg"), outcome = "nmb", wtp = 1e5) + s <- summary(mm_lm) + expect_equal(nrow(s), 2 * length(psa_big$strategies)) + expect_setequal(s$param, c("pFailChemo", "cSurg")) + expect_true(all(s$rsquared >= 0 & s$rsquared <= 1)) + mm_gam <- metamodel("oneway", psa_big, params = "pFailChemo", outcome = "nmb", wtp = 1e5, type = "gam") + expect_true(all(summary(mm_gam)$rsquared <= 1)) + mm_tw <- metamodel("twoway", psa_big, params = c("pFailChemo", "pFailRadio"), outcome = "nmb", wtp = 1e5) + s_tw <- summary(mm_tw) + expect_equal(nrow(s_tw), length(psa_big$strategies)) + expect_true(all(c("param1", "param2", "rsquared") %in% names(s_tw))) + mm_mw <- metamodel("multiway", psa_big, params = c("pFailChemo", "pFailRadio"), outcome = "nmb", + wtp = 1e5, type = "gam") + expect_error(summary(mm_mw), "not available for multiway") +}) + +test_that("print.metamodel() describes the analysis", { + mm <- metamodel("oneway", psa_big, params = "pFailChemo", outcome = "nmb", wtp = 1e5) + out <- capture.output(print(mm)) + expect_true(any(grepl("metamodel object", out))) + expect_true(any(grepl("pFailChemo", out))) + expect_true(any(grepl("100000|1e\\+05", out))) +}) diff --git a/tests/testthat/test_plot_options.R b/tests/testthat/test_plot_options.R new file mode 100644 index 0000000..d46c083 --- /dev/null +++ b/tests/testthat/test_plot_options.R @@ -0,0 +1,83 @@ +# Plot options that were not exercised by the other tests (#215). Each test checks what the +# option changes in the plot, not only that the plot is created. + +cancer_psa_data <- dampack::cancer_psa_data +psa_big <- make_psa_obj(cancer_psa_data$cost, cancer_psa_data$effectiveness, + cancer_psa_data$parameters, cancer_psa_data$strategies) +v_wtp <- seq(10000, 150000, by = 20000) + +geoms <- function(p) unname(vapply(p$layers, function(l) class(l$geom)[1], character(1))) + +test_that("plot.psa(): center and ellipse layers can be turned off", { + expect_equal(sum(geoms(plot(psa_big)) == "GeomPath"), 1) + p <- plot(psa_big, center = FALSE, ellipse = FALSE) + expect_equal(geoms(p), "GeomPoint") + expect_s3_class(ggplot2::ggplot_build(plot(psa_big, col = "bw")), "ggplot_built") + expect_s3_class(ggplot2::ggplot_build(plot(psa_big, col = "full", xlim = c(0, 20))), "ggplot_built") +}) + +test_that("plot.psa(ref_str = ): reference axes and caption are optional", { + p <- plot(psa_big, ref_str = "Chemo") + expect_true(all(c("GeomHline", "GeomVline") %in% geoms(p))) + expect_match(p$labels$caption, "Chemo", fixed = TRUE) + p2 <- plot(psa_big, ref_str = "Chemo", ref_axis = FALSE, ref_caption = FALSE, + center = FALSE, ellipse = FALSE) + expect_false(any(c("GeomHline", "GeomVline") %in% geoms(p2))) + expect_null(p2$labels$caption) + expect_setequal(as.character(unique(ggplot2::layer_data(p2, 1)$group)), c("1", "2")) +}) + +test_that("plot.ceac(): frontier, points, and min_prob", { + df_ceac <- ceac(v_wtp, psa_big) + expect_true("GeomPoint" %in% geoms(plot(df_ceac))) + p_plain <- plot(df_ceac, frontier = FALSE, points = FALSE) + expect_false("GeomPoint" %in% geoms(p_plain)) + # min_prob drops strategies whose probability of being cost-effective never reaches it + v_max <- tapply(df_ceac$Proportion, df_ceac$Strategy, max) + cutoff <- sort(v_max)[2] - 1e-9 + expect_output(p_min <- plot(df_ceac, min_prob = cutoff), "filtered out") + kept <- as.character(unique(ggplot2::ggplot_build(p_min)$plot$data$Strategy)) + expect_false(names(which.min(v_max)) %in% kept) + for (col in c("full", "bw")) { + expect_s3_class(ggplot2::ggplot_build(plot(df_ceac, col = col)), "ggplot_built") + } +}) + +test_that("plot.evsi(): the layout follows the WTP values and sample sizes", { + set.seed(215) + evsi_n <- calc_evsi(psa_big, wtp = 50000, params = "pFailChemo", n = c(10, 100, 1000), + n0 = 10, progress = FALSE) + p_n <- plot(evsi_n) + expect_equal(rlang::as_label(p_n$mapping$x), "n") + + evsi_wtp <- calc_evsi(psa_big, wtp = v_wtp, params = "pFailChemo", n = 100, n0 = 10, + progress = FALSE) + p_wtp <- plot(evsi_wtp) + expect_equal(rlang::as_label(p_wtp$mapping$x), "WTP_thou") + + evsi_both <- calc_evsi(psa_big, wtp = c(50000, 100000), params = "pFailChemo", + n = c(10, 100), n0 = 10, progress = FALSE) + p_both <- plot(evsi_both) + expect_equal(rlang::as_label(p_both$mapping$x), "n") + expect_equal(rlang::as_label(p_both$mapping$colour), "WTP_thou") +}) + +test_that("plot.evppi() plots EVPPI against willingness to pay", { + evppi <- calc_evppi(psa_big, wtp = v_wtp, params = "pFailChemo", progress = FALSE) + d <- ggplot2::layer_data(plot(evppi), 1) + expect_equal(nrow(d), length(v_wtp)) + expect_equal(sort(d$x), v_wtp / 1000) +}) + +test_that("owsa_tornado(): data, relative-difference filter, and strategy comparison", { + o <- owsa(psa_big, params = c("pFailChemo", "pFailRadio", "cSurg"), outcome = "nmb", wtp = 100000) + df_t <- owsa_tornado(o, return = "data") + expect_true(all(c("parameter", "abs_diff", "rel_diff") %in% names(df_t))) + df_filtered <- owsa_tornado(o, return = "data", min_rel_diff = max(df_t$rel_diff) - 1e-9) + expect_lt(nrow(df_filtered), nrow(df_t)) + # with two strategies, the tornado shows the difference between them + p_diff <- owsa_tornado(o, select_str = c("Chemo", "Surgery"), outcome_name = "NMB") + expect_match(p_diff$labels$y, "NMB", fixed = TRUE) + expect_error(owsa_tornado(o, select_str = c("Chemo", "Radio", "Surgery")), "max of 2 strategies") + expect_s3_class(ggplot2::ggplot_build(owsa_tornado(o, col = "bw")), "ggplot_built") +}) diff --git a/tests/testthat/test_run_psa.R b/tests/testthat/test_run_psa.R index 1f5c618..ea4e5b6 100644 --- a/tests/testthat/test_run_psa.R +++ b/tests/testthat/test_run_psa.R @@ -75,3 +75,38 @@ test_that("run_psa() reports a FUN that fails on the PSA parameters", { expect_error(run_psa(psa_hand, FUN = bad_fun, strategies = c("A", "B"), progress = FALSE), "FUN is not well defined.*this model cannot run") }) + +# remaining run_psa() paths (#215) +test_that("run_psa() fills in parameters from params_basecase", { + basecase <- list(c_a = 10, c_b = 20, e_b = 1.5) + psa_part <- data.frame(c_b = c(25, 30)) + out <- run_psa(psa_part, params_basecase = basecase, FUN = simple_fun, + outcomes = "cost", strategies = c("A", "B"), progress = FALSE) + # c_a comes from the base case; c_b from the PSA sample + expect_equal(out$cost$other_outcome$A, c(10, 10)) + expect_equal(out$cost$other_outcome$B, c(25, 30)) +}) + +test_that("run_psa() names strategies from FUN when strategies is NULL", { + psa_hand <- data.frame(c_a = 1, c_b = 2, e_b = 3) + out <- run_psa(psa_hand, FUN = simple_fun, outcomes = "cost", progress = FALSE) + expect_identical(out$cost$strategies, c("st_A", "st_B")) +}) + +test_that("run_psa() checks FUN's output and the requested outcomes", { + psa_hand <- data.frame(c_a = 1, c_b = 2, e_b = 3) + not_df <- function(params) c(1, 2) + expect_error(run_psa(psa_hand, FUN = not_df, strategies = c("A", "B"), progress = FALSE), + "should return a data.frame") + expect_error(run_psa(psa_hand, FUN = simple_fun, strategies = c("A", "B", "C"), progress = FALSE), + "number of strategies") + expect_error(run_psa(psa_hand, FUN = simple_fun, outcomes = "qaly", strategies = c("A", "B"), + progress = FALSE), + "not in FUN outcomes") +}) + +test_that("run_psa() reports progress when asked", { + psa_hand <- data.frame(c_a = 1:10, c_b = 1:10, e_b = 1:10) + expect_output(run_psa(psa_hand, FUN = simple_fun, outcomes = "cost", strategies = c("A", "B"), + progress = TRUE), "100 % done") +})