From 481dfa583e314703b8d538c97b1ac0ca4a4d0a7e Mon Sep 17 00:00:00 2001 From: Fernando Alarid-Escudero Date: Thu, 12 Nov 2026 14:19:14 -0800 Subject: [PATCH] Reference strategy and quadrant shares in summary.psa() and calculate_icers_psa() - summary.psa() gains ref_str and wtp (after calc_sds). With ref_str it adds meanIncCost, meanIncEffect, and the share of PSA samples in each quadrant of the incremental CE plane (propNE, propSE, propSW, propNW; NA for the reference). With wtp it adds meanNMB, and with ref_str also meanIncNMB and propCostEffective (share of samples with a higher NMB than the reference) (#209, #202). - calculate_icers_psa() gains ref_str, wtp, and nhb, passed to calculate_icers(). With uncertainty = TRUE and ref_str, it adds 95% intervals of the sample-by-sample differences from the reference (Lower_95_/Upper_95_ for Inc_Cost_ref, Inc_Effect_ref, iNMB_ref). The icers class and attributes are now kept through the uncertainty joins. - print.icers(): every *_ref column belongs to the reference view. - Docs: escape % in roxygen. An unescaped % starts an Rd comment, which had silently truncated ?calculate_icers_psa ('whether or not 95') and ?predict.metamodel. calculate_icers_psa() output for existing calls is identical to main's (objects, class, and printed tables, with and without uncertainty). Part of #209. Closes #202 --- NEWS.md | 2 + R/icers.R | 62 +++++++++++++++++++++-- R/metamodel.R | 2 +- R/psa.R | 44 ++++++++++++++++- man/add_reference_intervals.Rd | 30 +++++++++++ man/calculate_icers_psa.Rd | 27 +++++++++- man/owsa.Rd | 2 +- man/predict.metamodel.Rd | 2 +- man/summary.psa.Rd | 20 +++++++- man/twsa.Rd | 2 +- tests/testthat/test_psa_reference.R | 77 +++++++++++++++++++++++++++++ 11 files changed, 257 insertions(+), 13 deletions(-) create mode 100644 man/add_reference_intervals.Rd create mode 100644 tests/testthat/test_psa_reference.R diff --git a/NEWS.md b/NEWS.md index 534357c..39f373c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ ## New features +- `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). - `calculate_icers()` can compare every strategy with a user-selected reference strategy: with `ref_str = "Standard of care"`, it adds `Inc_Cost_ref`, `Inc_Effect_ref`, `ICER_ref`, and `Status_ref` ("Reference", "Dominant", "Dominated", "More costly, more effective", "Less costly, less effective", or "Equivalent"). The frontier-based results are unchanged, and without `ref_str` no columns are added (#209). - `calculate_icers()` gains `wtp`, which adds the net monetary benefit (`NMB`, and `iNMB_ref` with a reference strategy), and `nhb = TRUE`, which also adds the net health benefit (`NHB`, `iNHB_ref`) (#200). diff --git a/R/icers.R b/R/icers.R index 29d7459..98db64c 100644 --- a/R/icers.R +++ b/R/icers.R @@ -340,18 +340,25 @@ add_reference_and_nmb <- function(df_icers, ref_str, wtp, nhb, currency) { #' even if present in the ICER table. #' #' @param psa \code{psa} object from \code{link{make_psa_object}} -#' @param uncertainty whether or not 95% quantiles for the cost and QALY outcomes should be included +#' @param uncertainty whether or not 95\% quantiles for the cost and QALY outcomes should be included #' in the resulting ICER table. Defaults to \code{FALSE}. +#' @inheritParams calculate_icers #' #' @return A data frame and \code{icers} object of strategies and their associated #' status, cost, effect, incremental cost, incremental effect, and ICER. If \code{uncertainty} is #' set to \code{TRUE}, four additional columns are provided for the 2.5th and 97.5th quantiles for #' each strategy's cost and effect. +#' +#' With \code{ref_str}, \code{wtp}, or \code{nhb}, the reference-strategy and net-benefit columns +#' described in \code{\link{calculate_icers}} are added. With \code{uncertainty = TRUE} and +#' \code{ref_str}, the 2.5th and 97.5th quantiles of the sample-by-sample differences from the reference +#' are also given for \code{Inc_Cost_ref}, \code{Inc_Effect_ref}, and (with \code{wtp}) +#' \code{iNMB_ref}, in columns such as \code{Lower_95_Inc_Cost_ref} and \code{Upper_95_Inc_Cost_ref}. #' @seealso \code{\link{plot.icers}} #' @seealso \code{\link{calculate_icers}} #' @importFrom tidyr pivot_longer #' @export -calculate_icers_psa <- function(psa, uncertainty = FALSE) { +calculate_icers_psa <- function(psa, uncertainty = FALSE, ref_str = NULL, wtp = NULL, nhb = FALSE) { # check that psa has class 'psa' check_psa_object(psa) @@ -365,9 +372,13 @@ calculate_icers_psa <- function(psa, uncertainty = FALSE) { # Supply mean outcome values to calculate_icers df_icers <- calculate_icers(cost = df_psa_sum$meanCost, effect = df_psa_sum$meanEffect, - strategies = df_psa_sum$Strategy) + strategies = df_psa_sum$Strategy, + ref_str = ref_str, wtp = wtp, nhb = nhb) if (uncertainty == TRUE) { + # the joins and column selection below drop the icers class and attributes; keep them + l_attr <- attributes(df_icers)[c("class", "ref_str", "wtp", "currency")] + l_attr <- l_attr[!vapply(l_attr, is.null, logical(1))] # extract cost and effect data.frames from psa object df_cost <- psa$cost @@ -392,12 +403,53 @@ calculate_icers_psa <- function(psa, uncertainty = FALSE) { left_join(df_effect_bounds, by = "Strategy") %>% select(Strategy, Cost, Lower_95_Cost, Upper_95_Cost, Effect, Lower_95_Effect, Upper_95_Effect, - Inc_Cost, Inc_Effect, ICER, Status) + Inc_Cost, Inc_Effect, ICER, Status, everything()) + + if (!is.null(ref_str)) { + df_icers <- add_reference_intervals(df_icers, psa, ref_str, wtp) + } + df_icers <- as.data.frame(df_icers) + for (a in names(l_attr)) { + attr(df_icers, a) <- l_attr[[a]] + } } df_icers } +#' Add 95\% intervals for the differences from a reference strategy +#' +#' Quantiles of the sample-by-sample differences between each strategy and the reference, placed +#' after the corresponding point estimate. \code{NA} for the reference itself. +#' +#' @param df_icers an \code{icers} object with reference columns. +#' @param psa the \code{psa} object it was computed from. +#' @inheritParams calculate_icers +#' @return \code{df_icers} with the interval columns added. +#' @keywords internal +add_reference_intervals <- function(df_icers, psa, ref_str, wtp) { + m_inc_cost <- as.matrix(psa$cost) - psa$cost[[ref_str]] + m_inc_effect <- as.matrix(psa$effectiveness) - psa$effectiveness[[ref_str]] + l_inc <- list(Inc_Cost_ref = m_inc_cost, Inc_Effect_ref = m_inc_effect) + if (!is.null(wtp)) { + l_inc$iNMB_ref <- wtp * m_inc_effect - m_inc_cost + } + v_strat <- df_icers$Strategy + v_cols <- names(df_icers) + for (col in names(l_inc)) { + m_inc <- l_inc[[col]][, v_strat, drop = FALSE] + v_lower <- unname(apply(m_inc, 2, quantile, probs = 0.025, names = FALSE)) + v_upper <- unname(apply(m_inc, 2, quantile, probs = 0.975, names = FALSE)) + v_lower[v_strat == ref_str] <- NA_real_ + v_upper[v_strat == ref_str] <- NA_real_ + df_icers[[paste0("Lower_95_", col)]] <- v_lower + df_icers[[paste0("Upper_95_", col)]] <- v_upper + i <- match(col, v_cols) + v_cols <- append(v_cols, paste0(c("Lower_95_", "Upper_95_"), col), after = i) + } + df_icers[, v_cols] +} + #' compute icers for non-dominated strategies #' #' @param non_d a data frame of non-dominated strategies, with columns @@ -656,7 +708,7 @@ print.icers <- function(x, # columns that belong to each view v_seq_cols <- c("Inc_Cost", "Inc_Effect", "ICER", "Status") - v_ref_cols <- c("Inc_Cost_ref", "Inc_Effect_ref", "ICER_ref", "Status_ref", "iNMB_ref", "iNHB_ref") + v_ref_cols <- grep("_ref$", names(x), value = TRUE) v_drop <- switch(ref, sequential = v_ref_cols, reference = v_seq_cols, both = character(0)) df_table_cea <- x class(df_table_cea) <- "data.frame" diff --git a/R/metamodel.R b/R/metamodel.R index 9767350..c8491a0 100644 --- a/R/metamodel.R +++ b/R/metamodel.R @@ -303,7 +303,7 @@ summary.metamodel <- function(object, ...) { #' @param object object with class "metamodel" #' @param ranges a named list of the form c("param" = c(0, 1), ...) #' that gives the ranges for the parameter of interest. If NULL, -#' parameter values from the middle 95% of the PSA samples are used. The number of samples +#' parameter values from the middle 95\% of the PSA samples are used. The number of samples #' from this range is determined by \code{nsamp}. #' @param nsamp number of samples from ranges #' @param ... further arguments to \code{predict} (not used) diff --git a/R/psa.R b/R/psa.R index 3d64875..f2def64 100644 --- a/R/psa.R +++ b/R/psa.R @@ -104,13 +104,31 @@ check_df_and_coerce <- function(obj) { #' #' @param object the psa object #' @param calc_sds whether or not to calculate the standard deviations. Defaults to FALSE +#' @param ref_str name of a reference strategy. If given, each strategy is compared with it +#' sample by sample (see Value). Defaults to \code{NULL}. +#' @param wtp a single willingness-to-pay threshold. If given, the mean net monetary benefit is +#' added (see Value). Defaults to \code{NULL}. #' @param ... further arguments to summary (not used) #' #' @importFrom stats sd #' @return a \code{data.frame} containing the mean cost and effectiveness for each strategy and, if requested, #' the standard deviations of the cost and effectiveness for each strategy. +#' +#' With \code{ref_str}, the differences between each strategy and the reference are computed in +#' every PSA sample, and these columns are added (\code{NA} for the reference itself): +#' \code{meanIncCost} and \code{meanIncEffect}, the mean differences in cost and effect; and +#' \code{propNE}, \code{propSE}, \code{propSW}, and \code{propNW}, the proportion of samples in each +#' quadrant of the incremental cost-effectiveness plane: NE more effective and more costly, SE more +#' effective and not more costly (dominant), SW not more effective and not more costly, and NW not more +#' effective and more costly (dominated). The four proportions sum to 1. +#' +#' With \code{wtp}, \code{meanNMB} (\code{wtp * effect - cost}, averaged over samples) is added; with +#' \code{ref_str} too, also \code{meanIncNMB} and \code{propCostEffective}, the proportion of samples in +#' which the strategy has a higher NMB than the reference (it lies below the willingness-to-pay line). #' @export -summary.psa <- function(object, calc_sds = FALSE, ...) { +summary.psa <- function(object, calc_sds = FALSE, ref_str = NULL, wtp = NULL, ...) { + ref_str <- check_ref_str(ref_str, object$strategies) + check_wtp_nhb(wtp, nhb = FALSE) v_mean_cost <- colMeans(object$cost) v_mean_effect <- colMeans(object$effectiveness) @@ -125,6 +143,30 @@ summary.psa <- function(object, calc_sds = FALSE, ...) { df_sum_psa[, "sdCost"] <- v_sd_cost df_sum_psa[, "sdEffect"] <- v_sd_effect } + if (!is.null(wtp)) { + df_sum_psa[, "meanNMB"] <- unname(colMeans(wtp * object$effectiveness - object$cost)) + } + if (!is.null(ref_str)) { + # sample-by-sample differences from the reference strategy + m_inc_cost <- as.matrix(object$cost) - object$cost[[ref_str]] + m_inc_effect <- as.matrix(object$effectiveness) - object$effectiveness[[ref_str]] + v_is_ref <- v_strat == ref_str + na_ref <- function(v) { + v[v_is_ref] <- NA_real_ + unname(v) + } + df_sum_psa[, "meanIncCost"] <- na_ref(colMeans(m_inc_cost)) + df_sum_psa[, "meanIncEffect"] <- na_ref(colMeans(m_inc_effect)) + df_sum_psa[, "propNE"] <- na_ref(colMeans(m_inc_effect > 0 & m_inc_cost > 0)) + df_sum_psa[, "propSE"] <- na_ref(colMeans(m_inc_effect > 0 & m_inc_cost <= 0)) + df_sum_psa[, "propSW"] <- na_ref(colMeans(m_inc_effect <= 0 & m_inc_cost <= 0)) + df_sum_psa[, "propNW"] <- na_ref(colMeans(m_inc_effect <= 0 & m_inc_cost > 0)) + if (!is.null(wtp)) { + m_inc_nmb <- wtp * m_inc_effect - m_inc_cost + df_sum_psa[, "meanIncNMB"] <- na_ref(colMeans(m_inc_nmb)) + df_sum_psa[, "propCostEffective"] <- na_ref(colMeans(m_inc_nmb > 0)) + } + } rownames(df_sum_psa) <- seq_len(nrow(df_sum_psa)) df_sum_psa } diff --git a/man/add_reference_intervals.Rd b/man/add_reference_intervals.Rd new file mode 100644 index 0000000..45abbf9 --- /dev/null +++ b/man/add_reference_intervals.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/icers.R +\name{add_reference_intervals} +\alias{add_reference_intervals} +\title{Add 95\% intervals for the differences from a reference strategy} +\usage{ +add_reference_intervals(df_icers, psa, ref_str, wtp) +} +\arguments{ +\item{df_icers}{an \code{icers} object with reference columns.} + +\item{psa}{the \code{psa} object it was computed from.} + +\item{ref_str}{name of a reference strategy (one of \code{strategies}) to compare every +strategy against, for example \code{"Standard of care"}. The default (\code{NULL}) adds no +reference columns. The frontier-based results (\code{Status}, \code{Inc_Cost}, +\code{Inc_Effect}, \code{ICER}) never depend on the reference.} + +\item{wtp}{a single willingness-to-pay threshold (currency per unit of effect). If given, +the net monetary benefit (\code{NMB}) of each strategy is added. The default (\code{NULL}) +adds no net-benefit columns.} +} +\value{ +\code{df_icers} with the interval columns added. +} +\description{ +Quantiles of the sample-by-sample differences between each strategy and the reference, placed +after the corresponding point estimate. \code{NA} for the reference itself. +} +\keyword{internal} diff --git a/man/calculate_icers_psa.Rd b/man/calculate_icers_psa.Rd index 14b56d8..e5cfc61 100644 --- a/man/calculate_icers_psa.Rd +++ b/man/calculate_icers_psa.Rd @@ -4,19 +4,42 @@ \alias{calculate_icers_psa} \title{Calculate incremental cost-effectiveness ratios from a \code{psa} object.} \usage{ -calculate_icers_psa(psa, uncertainty = FALSE) +calculate_icers_psa( + psa, + uncertainty = FALSE, + ref_str = NULL, + wtp = NULL, + nhb = FALSE +) } \arguments{ \item{psa}{\code{psa} object from \code{link{make_psa_object}}} -\item{uncertainty}{whether or not 95% quantiles for the cost and QALY outcomes should be included +\item{uncertainty}{whether or not 95\% quantiles for the cost and QALY outcomes should be included in the resulting ICER table. Defaults to \code{FALSE}.} + +\item{ref_str}{name of a reference strategy (one of \code{strategies}) to compare every +strategy against, for example \code{"Standard of care"}. The default (\code{NULL}) adds no +reference columns. The frontier-based results (\code{Status}, \code{Inc_Cost}, +\code{Inc_Effect}, \code{ICER}) never depend on the reference.} + +\item{wtp}{a single willingness-to-pay threshold (currency per unit of effect). If given, +the net monetary benefit (\code{NMB}) of each strategy is added. The default (\code{NULL}) +adds no net-benefit columns.} + +\item{nhb}{if \code{TRUE}, also add the net health benefit (\code{NHB}). Requires \code{wtp}.} } \value{ A data frame and \code{icers} object of strategies and their associated status, cost, effect, incremental cost, incremental effect, and ICER. If \code{uncertainty} is set to \code{TRUE}, four additional columns are provided for the 2.5th and 97.5th quantiles for each strategy's cost and effect. + +With \code{ref_str}, \code{wtp}, or \code{nhb}, the reference-strategy and net-benefit columns +described in \code{\link{calculate_icers}} are added. With \code{uncertainty = TRUE} and +\code{ref_str}, the 2.5th and 97.5th quantiles of the sample-by-sample differences from the reference +are also given for \code{Inc_Cost_ref}, \code{Inc_Effect_ref}, and (with \code{wtp}) +\code{iNMB_ref}, in columns such as \code{Lower_95_Inc_Cost_ref} and \code{Upper_95_Inc_Cost_ref}. } \description{ The mean costs and QALYs for each strategy in a PSA are used diff --git a/man/owsa.Rd b/man/owsa.Rd index 422fa56..ea18fec 100644 --- a/man/owsa.Rd +++ b/man/owsa.Rd @@ -24,7 +24,7 @@ a deterministic sensitivity analysis object (\code{\link{run_owsa_det}})} \item{ranges}{a named list of the form c("param" = c(0, 1), ...) that gives the ranges for the parameter of interest. If NULL, -parameter values from the middle 95% of the PSA samples are used. The number of samples +parameter values from the middle 95\% of the PSA samples are used. The number of samples from this range is determined by \code{nsamp}.} \item{nsamp}{number of samples to take from the ranges} diff --git a/man/predict.metamodel.Rd b/man/predict.metamodel.Rd index 918d2ba..ec60a1d 100644 --- a/man/predict.metamodel.Rd +++ b/man/predict.metamodel.Rd @@ -11,7 +11,7 @@ \item{ranges}{a named list of the form c("param" = c(0, 1), ...) that gives the ranges for the parameter of interest. If NULL, -parameter values from the middle 95% of the PSA samples are used. The number of samples +parameter values from the middle 95\% of the PSA samples are used. The number of samples from this range is determined by \code{nsamp}.} \item{nsamp}{number of samples from ranges} diff --git a/man/summary.psa.Rd b/man/summary.psa.Rd index 9251f93..ca0a145 100644 --- a/man/summary.psa.Rd +++ b/man/summary.psa.Rd @@ -4,18 +4,36 @@ \alias{summary.psa} \title{summarize a psa object across all simulations} \usage{ -\method{summary}{psa}(object, calc_sds = FALSE, ...) +\method{summary}{psa}(object, calc_sds = FALSE, ref_str = NULL, wtp = NULL, ...) } \arguments{ \item{object}{the psa object} \item{calc_sds}{whether or not to calculate the standard deviations. Defaults to FALSE} +\item{ref_str}{name of a reference strategy. If given, each strategy is compared with it +sample by sample (see Value). Defaults to \code{NULL}.} + +\item{wtp}{a single willingness-to-pay threshold. If given, the mean net monetary benefit is +added (see Value). Defaults to \code{NULL}.} + \item{...}{further arguments to summary (not used)} } \value{ a \code{data.frame} containing the mean cost and effectiveness for each strategy and, if requested, the standard deviations of the cost and effectiveness for each strategy. + +With \code{ref_str}, the differences between each strategy and the reference are computed in +every PSA sample, and these columns are added (\code{NA} for the reference itself): +\code{meanIncCost} and \code{meanIncEffect}, the mean differences in cost and effect; and +\code{propNE}, \code{propSE}, \code{propSW}, and \code{propNW}, the proportion of samples in each +quadrant of the incremental cost-effectiveness plane: NE more effective and more costly, SE more +effective and not more costly (dominant), SW not more effective and not more costly, and NW not more +effective and more costly (dominated). The four proportions sum to 1. + +With \code{wtp}, \code{meanNMB} (\code{wtp * effect - cost}, averaged over samples) is added; with +\code{ref_str} too, also \code{meanIncNMB} and \code{propCostEffective}, the proportion of samples in +which the strategy has a higher NMB than the reference (it lies below the willingness-to-pay line). } \description{ summarize a psa object across all simulations diff --git a/man/twsa.Rd b/man/twsa.Rd index c167cd7..9c5f0a3 100644 --- a/man/twsa.Rd +++ b/man/twsa.Rd @@ -27,7 +27,7 @@ a deterministic sensitivity analysis object (\code{\link{run_owsa_det}})} \item{ranges}{a named list of the form c("param" = c(0, 1), ...) that gives the ranges for the parameter of interest. If NULL, -parameter values from the middle 95% of the PSA samples are used. The number of samples +parameter values from the middle 95\% of the PSA samples are used. The number of samples from this range is determined by \code{nsamp}.} \item{nsamp}{number of samples from ranges} diff --git a/tests/testthat/test_psa_reference.R b/tests/testthat/test_psa_reference.R new file mode 100644 index 0000000..79e0f2b --- /dev/null +++ b/tests/testthat/test_psa_reference.R @@ -0,0 +1,77 @@ +context("summary.psa() and calculate_icers_psa() against a reference strategy (#209, #202)") + +# Four PSA samples; the difference "New" - "Ref" falls in a different quadrant in each: +# sample inc effect inc cost quadrant +# 1 +0.2 +100 NE (more effective, more costly) +# 2 +0.2 -50 SE (more effective, less costly) +# 3 -0.1 -50 SW (less effective, less costly) +# 4 -0.1 +100 NW (less effective, more costly) +df_cost <- data.frame(Ref = c(1000, 1000, 1000, 1000), New = c(1100, 950, 950, 1100)) +df_eff <- data.frame(Ref = c(1, 1, 1, 1), New = c(1.2, 1.2, 0.9, 0.9)) +psa_q <- make_psa_obj(df_cost, df_eff, strategies = c("Ref", "New")) + +test_that("summary.psa() is unchanged without ref_str and wtp", { + expect_identical(names(summary(psa_q)), c("Strategy", "meanCost", "meanEffect")) +}) + +test_that("summary.psa(ref_str = ) gives mean differences and quadrant shares", { + s <- summary(psa_q, ref_str = "Ref") + r_new <- s[s$Strategy == "New", ] + r_ref <- s[s$Strategy == "Ref", ] + expect_equal(r_new$meanIncCost, mean(df_cost$New - df_cost$Ref)) + expect_equal(r_new$meanIncEffect, mean(df_eff$New - df_eff$Ref)) + expect_equal(unlist(r_new[, c("propNE", "propSE", "propSW", "propNW")], use.names = FALSE), + rep(0.25, 4)) + expect_true(all(is.na(r_ref[, c("meanIncCost", "meanIncEffect", "propNE", "propSE", "propSW", "propNW")]))) + expect_error(summary(psa_q, ref_str = "Nope"), "must be one of the strategies") +}) + +test_that("summary.psa(wtp = ) gives mean NMB and the share of cost-effective samples", { + s <- summary(psa_q, wtp = 1000) + expect_equal(s$meanNMB, unname(1000 * colMeans(df_eff) - colMeans(df_cost))) + expect_false(any(c("meanIncNMB", "propCostEffective") %in% names(s))) + + s_ref <- summary(psa_q, ref_str = "Ref", wtp = 1000) + inmb <- 1000 * (df_eff$New - df_eff$Ref) - (df_cost$New - df_cost$Ref) # 100, 250, -50, -200 + r_new <- s_ref[s_ref$Strategy == "New", ] + expect_equal(r_new$meanIncNMB, mean(inmb)) + expect_equal(r_new$propCostEffective, mean(inmb > 0)) + expect_equal(r_new$propCostEffective, 0.5) +}) + +data("cancer_psa_data") +psa_big <- make_psa_obj(cancer_psa_data$cost, cancer_psa_data$effectiveness, + cancer_psa_data$parameters, c("Chemotherapy", "Radiation therapy", "Surgery")) + +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) + obtained <- calculate_icers_psa(psa_big, ref_str = "Surgery", wtp = 50000, nhb = TRUE) + expect_equal(obtained, expected) +}) + +test_that("calculate_icers_psa(uncertainty = TRUE) adds intervals for the reference comparison", { + x <- calculate_icers_psa(psa_big, uncertainty = TRUE, ref_str = "Surgery", wtp = 50000) + expect_identical(attr(x, "ref_str"), "Surgery") + r <- x[x$Strategy == "Chemotherapy", ] + d_cost <- cancer_psa_data$cost[[1]] - cancer_psa_data$cost[[3]] + d_eff <- cancer_psa_data$effectiveness[[1]] - cancer_psa_data$effectiveness[[3]] + expect_equal(r$Lower_95_Inc_Cost_ref, unname(quantile(d_cost, 0.025))) + expect_equal(r$Upper_95_Inc_Cost_ref, unname(quantile(d_cost, 0.975))) + expect_equal(r$Lower_95_Inc_Effect_ref, unname(quantile(d_eff, 0.025))) + expect_equal(r$Upper_95_iNMB_ref, unname(quantile(50000 * d_eff - d_cost, 0.975))) + r_ref <- x[x$Strategy == "Surgery", ] + 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)) +}) + +test_that("print.icers() keeps reference intervals out of the sequential view", { + x <- calculate_icers_psa(psa_big, uncertainty = TRUE, ref_str = "Surgery") + out_seq <- capture.output(print(x)) + expect_false(any(grepl("Inc_Cost_ref", out_seq, fixed = TRUE))) + out_ref <- capture.output(print(x, ref = "reference")) + expect_true(any(grepl("Lower_95_Inc_Cost_ref", out_ref, fixed = TRUE))) +})