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_nameandsuffix. 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:
Filtering the baseline scenario (scenario_id == baseline)
Filtering what-if scenarios (scenario_id != baseline)
Matching them across scenarios using keys
Optionally aligning uncertainty iterations using rank correlation
Applying the selected comparison formula
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.3387549 0.07373690 -0.5240659 -0.4927919
#> 2 risk_compared 1 B -0.2142003 0.02481278 -0.2546155 -0.2500816
#> 25% 50% 75% 97.5% Max nsv Na's
#> 1 -0.3954793 -0.3324984 -0.2699970 -0.2406071 -0.2319814 1001 0
#> 2 -0.2359205 -0.2186249 -0.1914842 -0.1683800 -0.1600135 1001 0
