Skip to contents

Compares an mcnode's what-if scenarios against a baseline scenario (default "0") using various comparison metrics. Returns an mcmodule with a new comparison node.

Usage

mc_compare(
  mcmodule,
  mc_name,
  baseline = "0",
  type = "difference",
  keys_names = NULL,
  name = NULL,
  prefix = NULL,
  suffix = "_compared",
  summary = TRUE,
  align_uncertainty = TRUE
)

Arguments

mcmodule

(mcmodule object). Module containing the node.

mc_name

(character). Name of the mcnode to compare.

baseline

(character). Baseline scenario ID to compare against. Default: "0".

type

(character). Type of comparison. One of:

  • "difference" (default): whatif - baseline (absolute change)

  • "relative_difference": (whatif - baseline) / baseline (proportional change)

  • "reduction": baseline - whatif (absolute reduction)

  • "relative_reduction": (baseline - whatif) / baseline (proportional reduction)

keys_names

(character vector, optional). Column names for grouping. If NULL, uses keys from the node. Default: NULL.

name

(character, optional). Name for the new comparison node. If NULL, auto-generated from mc_name and suffix. Default: NULL.

prefix

(character, optional). Prefix for the auto-generated node name. Default: NULL.

suffix

(character). Suffix appended to auto-generated name. Default: "_compared".

summary

(logical). If TRUE, compute summary statistics for the new node. Default: TRUE.

align_uncertainty

(logical). If TRUE, align uncertainty iterations between baseline and what-if nodes using rank correlation (Spearman). This ensures that the same uncertainty iteration in both nodes represents similar uncertainty realizations, making comparisons more meaningful when nodes have multivariate dimensions. Default: TRUE.

Value

Updated mcmodule with a new comparison node containing:

  • mcnode: Comparison values as mcnode object

  • type: "compare"

  • baseline: Baseline scenario ID

  • compare_type: Type of comparison performed

  • param: Original node name

  • inputs: Original node name

  • keys: Same keys as original node

  • summary: Summary statistics (if summary = TRUE)

Details

This function compares what-if scenarios against a baseline by:

  1. Filtering the baseline scenario (scenario_id == baseline)

  2. Filtering what-if scenarios (scenario_id != baseline)

  3. Matching them across scenarios using keys

  4. Optionally aligning uncertainty iterations using rank correlation

  5. Applying the selected comparison formula

  6. Creating a new comparison node in the mcmodule

When align_uncertainty = TRUE, the function uses mc2d::cornode() to align the uncertainty iterations between matched baseline and what-if nodes. For multivariate nodes, correlation is applied independently to each variate.

For derived nodes with pre-computed summaries (types "filter", "compare", or "agg_total"), scenario filtering and key alignment use the node's summary by default as the source data.

The baseline scenario must contain all key combinations present in what-if scenarios. If what-if scenarios are missing key combinations present in baseline, those are interpreted as having baseline values (no change).

Examples

# Create example data with baseline and what-if scenarios
example_data <- data.frame(
  origin = c("A", "B", "A", "B"),
  scenario_id = c("0", "0", "1", "1")
)

# Create mcnodes for each scenario
example_mcnode <- mc2d::mcstoc(
  runif,
  min = mc2d::mcdata(c(0.1, 0.2, 0.15, 0.25), type = "0", nvariates = 4),
  max = mc2d::mcdata(c(0.2, 0.3, 0.25, 0.35), type = "0", nvariates = 4),
  nvariates = 4
)

# Create mcmodule
example_module <- list(
  data = list(example_data = example_data),
  node_list = list(
    risk = list(
      mcnode = example_mcnode,
      data_name = "example_data",
      keys = c("origin")
    )
  )
)

# Compare what-if scenario "1" against baseline "0"
result <- mc_compare(
  example_module,
  "risk",
  baseline = "0",
  type = "relative_reduction"
)
#> .temp_baseline_risk prev dim: [1001, 1, 2], new dim: [1001, 1, 2], 0 null matches
#> .temp_whatif_risk prev dim: [1001, 1, 2], new dim: [1001, 1, 2], 0 null matches

# View comparison results
result$node_list$risk_compared$summary
#>         mc_name scenario_id origin       mean         sd        Min       2.5%
#> 1 risk_compared           1      A -0.3511649 0.06786479 -0.5175916 -0.4936840
#> 2 risk_compared           1      B -0.1928004 0.02711079 -0.2609498 -0.2564064
#>          25%        50%        75%      97.5%        Max  nsv Na's
#> 1 -0.4007533 -0.3295877 -0.2977794 -0.2594786 -0.2483867 1001    0
#> 2 -0.2060113 -0.1839441 -0.1726186 -0.1598180 -0.1550361 1001    0