From 3886a043d549473b81c50a9535b2e928abd13033 Mon Sep 17 00:00:00 2001 From: Fernando Alarid-Escudero Date: Fri, 13 Nov 2026 07:10:18 -0800 Subject: [PATCH] Carry the currency of a psa object through to its analyses - ceac(), calc_exp_loss(), calc_evpi(), calc_evppi(), calc_evsi(), and calculate_icers_psa() store psa$currency on their results. - Their plot() methods (and print.icers()) use it: currency defaults to NULL, resolved by resolve_currency() as the argument if given, else the result's currency, else '$'. Existing calls keep working. - plot.ceac() gains effect_units (the x-axis label hard-coded QALY). - EVPPI and EVSI computed from net health benefit are in health units: calc_evppi() and calc_evsi() store the outcome, and their plots are labeled 'EVPPI (QALY)' / 'EVSI (QALY)' instead of a currency. calc_evsi() defaults to outcome = 'nhb', so its default plot was mislabeled in dollars. - Tests (test_currency.R) with a euro PSA for every function, overrides, effect_units, and dollar defaults; two existing tests now build the expected object with the same currency. Outputs of all vignettes and examples change only by the new currency attribute; values, plot data, and labels are unchanged. Closes #203 --- NEWS.md | 2 + R/add_common_aes.R | 27 ++++++++++++++ R/ceac.R | 11 ++++-- R/evpi.R | 7 +++- R/evppi.R | 12 ++++-- R/evsi.R | 12 ++++-- R/exp_loss.R | 7 +++- R/icers.R | 3 +- man/plot.ceac.Rd | 8 +++- man/plot.evpi.Rd | 5 ++- man/plot.evppi.Rd | 5 ++- man/plot.evsi.Rd | 5 ++- man/plot.exp_loss.Rd | 5 ++- man/resolve_currency.Rd | 20 ++++++++++ man/voi_units.Rd | 23 ++++++++++++ tests/testthat/test_calculate_icers.R | 3 +- tests/testthat/test_currency.R | 54 +++++++++++++++++++++++++++ tests/testthat/test_psa_reference.R | 4 +- 18 files changed, 186 insertions(+), 27 deletions(-) create mode 100644 man/resolve_currency.Rd create mode 100644 man/voi_units.Rd create mode 100644 tests/testthat/test_currency.R diff --git a/NEWS.md b/NEWS.md index ca145e2..e50e381 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ ## New features +- The currency of a `psa` object now carries through to its analyses: `ceac()`, `calc_exp_loss()`, `calc_evpi()`, `calc_evppi()`, `calc_evsi()`, and `calculate_icers_psa()` keep it, and their `plot()` and `print()` methods use it unless `currency` is given. `plot.ceac()` gains `effect_units` (#203). - `summary.psa()` gains `ref_str` and `wtp`. With a reference strategy it adds the mean incremental cost and effect and the proportion of PSA samples in each quadrant of the incremental cost-effectiveness plane (`propNE`, `propSE`, `propSW`, `propNW`); with `wtp` it adds the mean NMB and, with a reference, `meanIncNMB` and the proportion of samples that are cost-effective compared with the reference (`propCostEffective`) (#209, #202). - `calculate_icers_psa()` gains `ref_str`, `wtp`, and `nhb`. With `uncertainty = TRUE` and a reference strategy, it adds 95% intervals for the differences from the reference, computed sample by sample (#209). - `plot.icers()` gains `ref_str`, which plots the incremental cost-effectiveness plane with the reference strategy at the origin, and `wtp`, which draws a willingness-to-pay line. `plot.psa()` gains `wtp` for the same line on its incremental plane. `plot.icers()` uses the currency stored by `calculate_icers()` (#209, #202). @@ -27,6 +28,7 @@ ## Bug fixes +- EVPPI and EVSI computed from net health benefit (`outcome = "nhb"`, the default of `calc_evsi()`) are in health units, and their plots are now labeled with `effect_units` (e.g., "EVSI (QALY)") instead of a currency (#203). - `plot.psa(ref_str = )` now works when there are only two strategies; it used to fail with "'dims' cannot be of length 0" (#209). - `plot.owsa(col = "bw")` now draws each strategy with its own line type; all strategies used to share one line type and could not be told apart (#176). - `metamodel()` now names invalid parameters in its error message, including when called through `calc_evppi()` and `calc_evsi()` (#177). diff --git a/R/add_common_aes.R b/R/add_common_aes.R index fb29d4f..e3467d9 100644 --- a/R/add_common_aes.R +++ b/R/add_common_aes.R @@ -251,3 +251,30 @@ number_ticks <- function(n) { pretty(limits, n + 1) } } + +#' Currency to use for labels +#' +#' @param currency currency given by the user, or \code{NULL}. +#' @param x a result object that may carry a \code{"currency"} attribute. +#' @return \code{currency} if given, otherwise the currency stored on \code{x}, otherwise \code{"$"}. +#' @keywords internal +resolve_currency <- function(currency, x) { + if (!is.null(currency)) { + return(currency) + } + if (is.null(attr(x, "currency"))) "$" else attr(x, "currency") +} + +#' Units of a value-of-information result +#' +#' EVPPI and EVSI computed from net health benefit are in health units; from net monetary +#' benefit, in currency. +#' +#' @param x an \code{evppi} or \code{evsi} object. +#' @param currency currency label. +#' @param effect_units units of effectiveness. +#' @return \code{effect_units} if \code{x} was computed with \code{outcome = "nhb"}, else \code{currency}. +#' @keywords internal +voi_units <- function(x, currency, effect_units) { + if (identical(attr(x, "outcome"), "nhb")) effect_units else currency +} diff --git a/R/ceac.R b/R/ceac.R index 38a8e92..8b83c0a 100644 --- a/R/ceac.R +++ b/R/ceac.R @@ -122,6 +122,7 @@ ceac <- function(wtp, psa) { # define classes # defining data.frame as well allows the object to use print.data.frame, for example class(ceac) <- c("ceac", "data.frame") + attr(ceac, "currency") <- psa$currency ceac } @@ -134,7 +135,9 @@ ceac <- function(wtp, psa) { #' @param frontier whether to plot acceptability frontier (TRUE) or not (FALSE) #' @param points whether to plot points (TRUE) or not (FALSE) #' @param currency string with currency used in the cost-effectiveness analysis (CEA). -#'Defaults to \code{$}, but can be any currency symbol or word (e.g., £, €, peso) +#' Defaults to the currency of the PSA the CEAC was computed from (or \code{$}), but can be any +#' currency symbol or word (e.g., £, €, peso). +#' @param effect_units units of effectiveness, used in the x-axis label. Default: QALY #' @param min_prob minimum probability to show strategy in plot. #' For example, if the min_prob is 0.05, only strategies that ever #' exceed Pr(Cost Effective) = 0.05 will be plotted. Most useful in situations @@ -157,7 +160,7 @@ ceac <- function(wtp, psa) { plot.ceac <- function(x, frontier = TRUE, points = TRUE, - currency = "$", + currency = NULL, min_prob = 0, txtsize = 12, n_x_ticks = 10, @@ -167,7 +170,9 @@ plot.ceac <- function(x, ylim = NULL, xlim = c(0, NA), col = c("access", "full", "bw"), + effect_units = "QALY", ...) { + currency <- resolve_currency(currency, x) wtp_name <- "WTP" prop_name <- "Proportion" strat_name <- "Strategy" @@ -217,7 +222,7 @@ plot.ceac <- function(x, y = !!sym(prop_name), color = !!sym(strat_name))) + geom_line() + - xlab(paste("Willingness to Pay (Thousand ", currency, " / QALY)", sep = "")) + + xlab(paste("Willingness to Pay (Thousand ", currency, " / ", effect_units, ")", sep = "")) + ylab("Pr Most Cost-Effective") if (points) { diff --git a/R/evpi.R b/R/evpi.R index 3643300..be85414 100644 --- a/R/evpi.R +++ b/R/evpi.R @@ -55,6 +55,7 @@ calc_evpi <- function(psa, wtp, pop = 1) { # declare class as both evpi (plotting) and data.frame (printing) class(df_evpi) <- c("evpi", "data.frame") + attr(df_evpi, "currency") <- psa$currency df_evpi } @@ -66,7 +67,8 @@ calc_evpi <- function(psa, wtp, pop = 1) { #' @param x object of class \code{evpi}, produced by function #' \code{\link{calc_evpi}} #' @param currency string with currency used in the cost-effectiveness analysis (CEA). -#' Default: $, but it could be any currency symbol or word (e.g., £, €, peso) +#' Default: the currency of the PSA the results were computed from (or $), but it could be any +#' currency symbol or word (e.g., £, €, peso) #' @param effect_units units of effectiveness. Default: QALY #' @inheritParams add_common_aes #' @keywords expected value of perfect information @@ -77,7 +79,7 @@ calc_evpi <- function(psa, wtp, pop = 1) { #' @export plot.evpi <- function(x, txtsize = 12, - currency = "$", + currency = NULL, effect_units = "QALY", n_y_ticks = 8, n_x_ticks = 20, @@ -87,6 +89,7 @@ plot.evpi <- function(x, ylim = NULL, ...) { WTP_thou <- EVPI <- NULL + currency <- resolve_currency(currency, x) x$WTP_thou <- x$WTP / 1000 g <- ggplot(data = x, aes(x = WTP_thou, y = EVPI)) + diff --git a/R/evppi.R b/R/evppi.R index 74b5ae6..44e95a7 100644 --- a/R/evppi.R +++ b/R/evppi.R @@ -78,6 +78,7 @@ calc_evppi <- function(psa, # define parameter values and make sure they correspond to a valid option type <- match.arg(type) outcome <- match.arg(outcome) + outcome_input <- outcome # adjust outcome type outcome <- paste0(outcome, "_loss_voi") @@ -127,6 +128,8 @@ calc_evppi <- function(psa, evppi_ls <- list(df_evppi = df_evppi, metamodel_ls = l_mms) class(evppi_ls) <- c("evppi") + attr(evppi_ls, "currency") <- psa$currency + attr(evppi_ls, "outcome") <- outcome_input evppi_ls } @@ -138,7 +141,8 @@ calc_evppi <- function(psa, #' @param x object of class \code{evppi}, produced by function #' \code{\link{calc_evppi}} #' @param currency string with currency used in the cost-effectiveness analysis (CEA). -#' Default: $, but it could be any currency symbol or word (e.g., £, €, peso) +#' Default: the currency of the PSA the results were computed from (or $), but it could be any +#' currency symbol or word (e.g., £, €, peso) #' @param effect_units units of effectiveness. Default: QALY #' @inheritParams add_common_aes #' @keywords expected value of perfect information @@ -149,7 +153,7 @@ calc_evppi <- function(psa, #' @export plot.evppi <- function(x, txtsize = 12, - currency = "$", + currency = NULL, effect_units = "QALY", n_y_ticks = 8, n_x_ticks = 20, @@ -159,13 +163,15 @@ plot.evppi <- function(x, ylim = NULL, ...) { WTP_thou <- EVPPI <- NULL + currency <- resolve_currency(currency, x) + v_units <- voi_units(x, currency, effect_units) x <- x[[1]] x$WTP_thou <- x$WTP / 1000 g <- ggplot(data = x, aes(x = WTP_thou, y = EVPPI)) + geom_line() + xlab(paste("Willingness to Pay (Thousand ", currency, "/", effect_units, ")", sep = "")) + - ylab(paste("EVPPI (", currency, ")", sep = "")) + ylab(paste("EVPPI (", v_units, ")", sep = "")) add_common_aes(g, txtsize, continuous = c("x", "y"), n_x_ticks = n_x_ticks, n_y_ticks = n_y_ticks, xbreaks = xbreaks, ybreaks = ybreaks, diff --git a/R/evsi.R b/R/evsi.R index ce0fdc1..e948448 100644 --- a/R/evsi.R +++ b/R/evsi.R @@ -32,6 +32,7 @@ calc_evsi <- function(psa, progress = TRUE) { # define parameter values and make sure they correspond to a valid option outcome <- match.arg(outcome) + outcome_input <- outcome # adjust outcome type outcome <- paste0(outcome, "_loss_voi") @@ -108,6 +109,8 @@ calc_evsi <- function(psa, } evsi_ls <- list(df_evsi = df_evsi, metamodel_ls = l_mms) class(evsi_ls) <- c("evsi") + attr(evsi_ls, "currency") <- psa$currency + attr(evsi_ls, "outcome") <- outcome_input evsi_ls } @@ -282,7 +285,8 @@ predict_matrix_tensor_smooth_ga <- function(object, #' @param x object of class \code{evsi}, produced by function #' \code{\link{calc_evsi}} #' @param currency string with currency used in the cost-effectiveness analysis (CEA). -#' Default: $, but it could be any currency symbol or word (e.g., £, €, peso) +#' Default: the currency of the PSA the results were computed from (or $), but it could be any +#' currency symbol or word (e.g., £, €, peso) #' @param effect_units units of effectiveness. Default: QALY #' @inheritParams add_common_aes #' @keywords expected value of sample information @@ -293,7 +297,7 @@ predict_matrix_tensor_smooth_ga <- function(object, #' @export plot.evsi <- function(x, txtsize = 12, - currency = "$", + currency = NULL, effect_units = "QALY", n_y_ticks = 8, n_x_ticks = 20, @@ -305,6 +309,8 @@ plot.evsi <- function(x, ...) { # Select EVSI data.frame, discarding metamodel list EVSI <- WTP_thou <- NULL + currency <- resolve_currency(currency, x) + v_units <- voi_units(x, currency, effect_units) x <- x[[1]] x$WTP_thou <- x$WTP / 1000 col <- match.arg(col) @@ -332,7 +338,7 @@ plot.evsi <- function(x, g <- g + geom_line() + - ylab(paste("EVSI (", currency, ")", sep = "")) + ylab(paste("EVSI (", v_units, ")", sep = "")) add_common_aes(g, txtsize, continuous = c("x", "y"), n_x_ticks = n_x_ticks, n_y_ticks = n_y_ticks, xbreaks = xbreaks, ybreaks = ybreaks, diff --git a/R/exp_loss.R b/R/exp_loss.R index 71f9612..c5bc826 100644 --- a/R/exp_loss.R +++ b/R/exp_loss.R @@ -87,6 +87,7 @@ calc_exp_loss <- function(psa, wtp) { df_exp_loss_melt$Strategy <- factor(df_exp_loss_melt$Strategy, levels = v_strategies, ordered = TRUE) class(df_exp_loss_melt) <- c("exp_loss", "data.frame") + attr(df_exp_loss_melt, "currency") <- psa$currency df_exp_loss_melt } @@ -96,7 +97,8 @@ calc_exp_loss <- function(psa, wtp) { #' @param x object of class \code{exp_loss}, produced by function #' \code{\link{calc_exp_loss}} #' @param currency string with currency used in the cost-effectiveness analysis (CEA). -#' Default: $, but it could be any currency symbol or word (e.g., £, €, peso) +#' Default: the currency of the PSA the results were computed from (or $), but it could be any +#' currency symbol or word (e.g., £, €, peso) #' @param effect_units units of effectiveness. Default: QALY #' @param log_y take the base 10 log of the y axis #' @param frontier indicate the frontier (also the expected value of perfect information). @@ -117,7 +119,7 @@ plot.exp_loss <- function(x, points = TRUE, lsize = 1, txtsize = 12, - currency = "$", + currency = NULL, effect_units = "QALY", n_y_ticks = 8, n_x_ticks = 20, @@ -128,6 +130,7 @@ plot.exp_loss <- function(x, col = c("access", "full", "bw"), ...) { On_Frontier <- NULL + currency <- resolve_currency(currency, x) wtp_name <- "WTP_thou" loss_name <- "Expected_Loss" strat_name <- "Strategy" diff --git a/R/icers.R b/R/icers.R index 98db64c..901f295 100644 --- a/R/icers.R +++ b/R/icers.R @@ -373,7 +373,8 @@ calculate_icers_psa <- function(psa, uncertainty = FALSE, ref_str = NULL, wtp = df_icers <- calculate_icers(cost = df_psa_sum$meanCost, effect = df_psa_sum$meanEffect, strategies = df_psa_sum$Strategy, - ref_str = ref_str, wtp = wtp, nhb = nhb) + ref_str = ref_str, wtp = wtp, nhb = nhb, + currency = psa$currency) if (uncertainty == TRUE) { # the joins and column selection below drop the icers class and attributes; keep them diff --git a/man/plot.ceac.Rd b/man/plot.ceac.Rd index ee09f84..ff0e269 100644 --- a/man/plot.ceac.Rd +++ b/man/plot.ceac.Rd @@ -8,7 +8,7 @@ x, frontier = TRUE, points = TRUE, - currency = "$", + currency = NULL, min_prob = 0, txtsize = 12, n_x_ticks = 10, @@ -18,6 +18,7 @@ ylim = NULL, xlim = c(0, NA), col = c("access", "full", "bw"), + effect_units = "QALY", ... ) } @@ -29,7 +30,8 @@ \item{points}{whether to plot points (TRUE) or not (FALSE)} \item{currency}{string with currency used in the cost-effectiveness analysis (CEA). -Defaults to \code{$}, but can be any currency symbol or word (e.g., £, €, peso)} +Defaults to the currency of the PSA the CEAC was computed from (or \code{$}), but can be any +currency symbol or word (e.g., £, €, peso).} \item{min_prob}{minimum probability to show strategy in plot. For example, if the min_prob is 0.05, only strategies that ever @@ -54,6 +56,8 @@ will override \code{n_y_ticks} if provided.} \item{col}{either none, full color, accessible colors, or black and white} +\item{effect_units}{units of effectiveness, used in the x-axis label. Default: QALY} + \item{...}{further arguments to plot. This is not used by \code{dampack} but required for generic consistency.} } diff --git a/man/plot.evpi.Rd b/man/plot.evpi.Rd index bb878a6..1978b0f 100644 --- a/man/plot.evpi.Rd +++ b/man/plot.evpi.Rd @@ -7,7 +7,7 @@ \method{plot}{evpi}( x, txtsize = 12, - currency = "$", + currency = NULL, effect_units = "QALY", n_y_ticks = 8, n_x_ticks = 20, @@ -25,7 +25,8 @@ \item{txtsize}{base text size} \item{currency}{string with currency used in the cost-effectiveness analysis (CEA). -Default: $, but it could be any currency symbol or word (e.g., £, €, peso)} +Default: the currency of the PSA the results were computed from (or $), but it could be any +currency symbol or word (e.g., £, €, peso)} \item{effect_units}{units of effectiveness. Default: QALY} diff --git a/man/plot.evppi.Rd b/man/plot.evppi.Rd index 0174fd5..519e9df 100644 --- a/man/plot.evppi.Rd +++ b/man/plot.evppi.Rd @@ -7,7 +7,7 @@ \method{plot}{evppi}( x, txtsize = 12, - currency = "$", + currency = NULL, effect_units = "QALY", n_y_ticks = 8, n_x_ticks = 20, @@ -25,7 +25,8 @@ \item{txtsize}{base text size} \item{currency}{string with currency used in the cost-effectiveness analysis (CEA). -Default: $, but it could be any currency symbol or word (e.g., £, €, peso)} +Default: the currency of the PSA the results were computed from (or $), but it could be any +currency symbol or word (e.g., £, €, peso)} \item{effect_units}{units of effectiveness. Default: QALY} diff --git a/man/plot.evsi.Rd b/man/plot.evsi.Rd index fdc8186..0c9e858 100644 --- a/man/plot.evsi.Rd +++ b/man/plot.evsi.Rd @@ -7,7 +7,7 @@ \method{plot}{evsi}( x, txtsize = 12, - currency = "$", + currency = NULL, effect_units = "QALY", n_y_ticks = 8, n_x_ticks = 20, @@ -26,7 +26,8 @@ \item{txtsize}{base text size} \item{currency}{string with currency used in the cost-effectiveness analysis (CEA). -Default: $, but it could be any currency symbol or word (e.g., £, €, peso)} +Default: the currency of the PSA the results were computed from (or $), but it could be any +currency symbol or word (e.g., £, €, peso)} \item{effect_units}{units of effectiveness. Default: QALY} diff --git a/man/plot.exp_loss.Rd b/man/plot.exp_loss.Rd index 5e4322e..0d46626 100644 --- a/man/plot.exp_loss.Rd +++ b/man/plot.exp_loss.Rd @@ -11,7 +11,7 @@ points = TRUE, lsize = 1, txtsize = 12, - currency = "$", + currency = NULL, effect_units = "QALY", n_y_ticks = 8, n_x_ticks = 20, @@ -39,7 +39,8 @@ To only plot the EVPI see \code{\link{calc_evpi}}.} \item{txtsize}{base text size} \item{currency}{string with currency used in the cost-effectiveness analysis (CEA). -Default: $, but it could be any currency symbol or word (e.g., £, €, peso)} +Default: the currency of the PSA the results were computed from (or $), but it could be any +currency symbol or word (e.g., £, €, peso)} \item{effect_units}{units of effectiveness. Default: QALY} diff --git a/man/resolve_currency.Rd b/man/resolve_currency.Rd new file mode 100644 index 0000000..5782d21 --- /dev/null +++ b/man/resolve_currency.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/add_common_aes.R +\name{resolve_currency} +\alias{resolve_currency} +\title{Currency to use for labels} +\usage{ +resolve_currency(currency, x) +} +\arguments{ +\item{currency}{currency given by the user, or \code{NULL}.} + +\item{x}{a result object that may carry a \code{"currency"} attribute.} +} +\value{ +\code{currency} if given, otherwise the currency stored on \code{x}, otherwise \code{"$"}. +} +\description{ +Currency to use for labels +} +\keyword{internal} diff --git a/man/voi_units.Rd b/man/voi_units.Rd new file mode 100644 index 0000000..c865f77 --- /dev/null +++ b/man/voi_units.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/add_common_aes.R +\name{voi_units} +\alias{voi_units} +\title{Units of a value-of-information result} +\usage{ +voi_units(x, currency, effect_units) +} +\arguments{ +\item{x}{an \code{evppi} or \code{evsi} object.} + +\item{currency}{currency label.} + +\item{effect_units}{units of effectiveness.} +} +\value{ +\code{effect_units} if \code{x} was computed with \code{outcome = "nhb"}, else \code{currency}. +} +\description{ +EVPPI and EVSI computed from net health benefit are in health units; from net monetary +benefit, in currency. +} +\keyword{internal} diff --git a/tests/testthat/test_calculate_icers.R b/tests/testthat/test_calculate_icers.R index fe9e6df..3aa5e94 100644 --- a/tests/testthat/test_calculate_icers.R +++ b/tests/testthat/test_calculate_icers.R @@ -91,7 +91,8 @@ test_that("psa icers returns icers df corresponding to means", { psa_sum <- summary(psa_big) exp_icers <- calculate_icers(cost = psa_sum$meanCost, effect = psa_sum$meanEffect, - strategies = psa_sum$Strategy) + strategies = psa_sum$Strategy, + currency = psa_big$currency) expect_equal(psa_icers, exp_icers) }) diff --git a/tests/testthat/test_currency.R b/tests/testthat/test_currency.R new file mode 100644 index 0000000..65cf20f --- /dev/null +++ b/tests/testthat/test_currency.R @@ -0,0 +1,54 @@ +context("the currency of a psa object carries through to results, plots, and tables (#203)") + +cancer_psa_data <- dampack::cancer_psa_data +psa_eur <- make_psa_obj(cancer_psa_data$cost, cancer_psa_data$effectiveness, + cancer_psa_data$parameters, cancer_psa_data$strategies, + currency = "€") +psa_usd <- make_psa_obj(cancer_psa_data$cost, cancer_psa_data$effectiveness, + cancer_psa_data$parameters, cancer_psa_data$strategies) +v_wtp <- seq(10000, 100000, by = 30000) +eur <- "€" + +test_that("ceac() and calc_exp_loss() keep the currency and use it in their plots", { + df_ceac <- ceac(v_wtp, psa_eur) + expect_identical(attr(df_ceac, "currency"), eur) + expect_match(plot(df_ceac)$labels$x, eur, fixed = TRUE) + expect_match(plot(df_ceac, currency = "£")$labels$x, "£", fixed = TRUE) + expect_match(plot(df_ceac, effect_units = "LY")$labels$x, "/ LY)", fixed = TRUE) + + df_elc <- calc_exp_loss(psa_eur, v_wtp) + expect_identical(attr(df_elc, "currency"), eur) + expect_equal(plot(df_elc)$labels$y, paste0("Expected Loss (", eur, ")")) +}) + +test_that("EVPI, EVPPI, and EVSI plots use the currency or the health units of the outcome", { + df_evpi <- calc_evpi(psa_eur, v_wtp) + expect_identical(attr(df_evpi, "currency"), eur) + expect_equal(plot(df_evpi)$labels$y, paste0("EVPI (", eur, ")")) + + evppi_nmb <- calc_evppi(psa_eur, v_wtp, params = "pFailChemo", outcome = "nmb", progress = FALSE) + expect_equal(plot(evppi_nmb)$labels$y, paste0("EVPPI (", eur, ")")) + evppi_nhb <- calc_evppi(psa_eur, v_wtp, params = "pFailChemo", outcome = "nhb", progress = FALSE) + expect_equal(plot(evppi_nhb)$labels$y, "EVPPI (QALY)") + + # calc_evsi() defaults to outcome = "nhb", whose EVSI is in health units, not currency + evsi_nhb <- calc_evsi(psa_eur, v_wtp, params = "pFailChemo", n = 100, n0 = 10, progress = FALSE) + expect_equal(plot(evsi_nhb)$labels$y, "EVSI (QALY)") + evsi_nmb <- calc_evsi(psa_eur, v_wtp, params = "pFailChemo", n = 100, n0 = 10, + outcome = "nmb", progress = FALSE) + expect_equal(plot(evsi_nmb)$labels$y, paste0("EVSI (", eur, ")")) +}) + +test_that("calculate_icers_psa() keeps the currency for print and plot", { + x <- calculate_icers_psa(psa_eur) + expect_identical(attr(x, "currency"), eur) + expect_true(any(grepl(paste0("Costs (", eur, ")"), capture.output(print(x)), fixed = TRUE))) + expect_equal(plot(x)$labels$y, paste0("Cost (", eur, ")")) + x_unc <- calculate_icers_psa(psa_eur, uncertainty = TRUE) + expect_identical(attr(x_unc, "currency"), eur) +}) + +test_that("a dollar psa object still gives dollar labels", { + expect_match(plot(ceac(v_wtp, psa_usd))$labels$x, "$", fixed = TRUE) + expect_equal(plot(calc_evpi(psa_usd, v_wtp))$labels$y, "EVPI ($)") +}) diff --git a/tests/testthat/test_psa_reference.R b/tests/testthat/test_psa_reference.R index 79e0f2b..1df2caf 100644 --- a/tests/testthat/test_psa_reference.R +++ b/tests/testthat/test_psa_reference.R @@ -46,7 +46,7 @@ psa_big <- make_psa_obj(cancer_psa_data$cost, cancer_psa_data$effectiveness, test_that("calculate_icers_psa() passes ref_str, wtp, and nhb to calculate_icers()", { s <- summary(psa_big) expected <- calculate_icers(s$meanCost, s$meanEffect, s$Strategy, - ref_str = "Surgery", wtp = 50000, nhb = TRUE) + ref_str = "Surgery", wtp = 50000, nhb = TRUE, currency = psa_big$currency) obtained <- calculate_icers_psa(psa_big, ref_str = "Surgery", wtp = 50000, nhb = TRUE) expect_equal(obtained, expected) }) @@ -65,7 +65,7 @@ test_that("calculate_icers_psa(uncertainty = TRUE) adds intervals for the refere expect_true(is.na(r_ref$Lower_95_Inc_Cost_ref)) # the frontier-based columns and their existing intervals are unchanged x0 <- calculate_icers_psa(psa_big, uncertainty = TRUE) - expect_equal(as.data.frame(x)[, names(x0)], as.data.frame(x0)) + expect_equal(as.data.frame(x)[, names(x0)], as.data.frame(x0)[, names(x0)]) }) test_that("print.icers() keeps reference intervals out of the sequential view", {