Skip to content
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ LazyData: true
BuildResaveData: no
RoxygenNote: 7.3.2
VignetteBuilder: knitr
Config/testthat/edition: 3
Config/Needs/website: pkgdown
File renamed without changes.
2 changes: 0 additions & 2 deletions tests/testthat/test_add_common_aes.R
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test_calculate_icers.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("calculate_icers")
library(dampack)

data("hund_strat")
Expand Down
9 changes: 4 additions & 5 deletions tests/testthat/test_ceac.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("ceac")
library(dampack)

# test the class
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test_data.R
Original file line number Diff line number Diff line change
@@ -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"))
Expand All @@ -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)))
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test_dsa_oneway.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("dsa oneway")
library(dampack)

# creation of example data
Expand Down
7 changes: 3 additions & 4 deletions tests/testthat/test_dsa_twoway.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("dsa twoway")
library(dampack)

# create of example data
Expand All @@ -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
Expand All @@ -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)
})
11 changes: 5 additions & 6 deletions tests/testthat/test_elc.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("elc")
library(dampack)


Expand All @@ -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")
})


Expand All @@ -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
Expand All @@ -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", {
Expand Down
11 changes: 5 additions & 6 deletions tests/testthat/test_evpi.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("evpi")
library(dampack)


Expand All @@ -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
Expand All @@ -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"))
})
Expand All @@ -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")
})
1 change: 0 additions & 1 deletion tests/testthat/test_evppi.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("evppi")
library(dampack)


Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test_evsi.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("evsi")
library(dampack)

# test return object
Expand Down
95 changes: 94 additions & 1 deletion tests/testthat/test_gen_psa_samp.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("gen_psa_samp")
library(dampack)


Expand Down Expand Up @@ -52,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)
})
39 changes: 32 additions & 7 deletions tests/testthat/test_metamod.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("metamodel")
library(dampack)

# test the class
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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", {
Expand All @@ -146,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)))
})
Loading
Loading