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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
62 changes: 57 additions & 5 deletions R/icers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion R/metamodel.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 43 additions & 1 deletion R/psa.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
30 changes: 30 additions & 0 deletions man/add_reference_intervals.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions man/calculate_icers_psa.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/owsa.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/predict.metamodel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion man/summary.psa.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/twsa.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading