The compare_scenarios() function allows you to compare
multiple analysis scenarios side-by-side. This is useful for:
The most common use case is comparing agricultural nutrient balances with and without wastewater treatment plant data.
library(manureshed)
# Run analysis without WWTP
base_scenario <- run_builtin_analysis(
scale = "huc8",
year = 2016,
nutrients = "nitrogen",
include_wwtp = FALSE
)
# Run analysis with WWTP
wwtp_scenario <- run_builtin_analysis(
scale = "huc8",
year = 2016,
nutrients = "nitrogen",
include_wwtp = TRUE
)
# Compare the two scenarios
comparison <- compare_scenarios(list(
"Agricultural Only" = base_scenario,
"Agricultural + WWTP" = wwtp_scenario
))The comparison returns three main components:
A data frame with metrics for each scenario:
Key metrics include: - n_sources: Number of nutrient
source areas - n_sinks: Number of nutrient sink areas -
n_balanced / n_within_watershed: Balanced
areas - n_excluded: Excluded areas (below cropland
threshold) - total_surplus_kg: Total nutrient surplus -
total_deficit_kg: Total nutrient deficit
Compare more than two scenarios:
# Create three different scenarios
conservative <- run_builtin_analysis(
scale = "huc8",
year = 2016,
nutrients = "nitrogen",
include_wwtp = FALSE,
cropland_threshold = 2000 # More restrictive
)
moderate <- run_builtin_analysis(
scale = "huc8",
year = 2016,
nutrients = "nitrogen",
include_wwtp = TRUE,
cropland_threshold = 1234 # Default
)
liberal <- run_builtin_analysis(
scale = "huc8",
year = 2016,
nutrients = "nitrogen",
include_wwtp = TRUE,
cropland_threshold = 500 # Less restrictive
)
# Compare all three
multi_comparison <- compare_scenarios(list(
"Conservative (No WWTP, High Threshold)" = conservative,
"Moderate (WWTP, Default Threshold)" = moderate,
"Liberal (WWTP, Low Threshold)" = liberal
))
# View results
print(multi_comparison$comparison_data)
multi_comparison$plots$bar_chartCompare the same parameters across different years:
# Analyze multiple years
year_2010 <- run_builtin_analysis(
scale = "county",
year = 2010,
nutrients = "nitrogen",
include_wwtp = TRUE
)
year_2013 <- run_builtin_analysis(
scale = "county",
year = 2013,
nutrients = "nitrogen",
include_wwtp = TRUE
)
year_2016 <- run_builtin_analysis(
scale = "county",
year = 2016,
nutrients = "nitrogen",
include_wwtp = TRUE
)
# Compare temporal trends
temporal <- compare_scenarios(list(
"2010" = year_2010,
"2013" = year_2013,
"2016" = year_2016
))
# See how classifications changed over time
temporal$plots$bar_chart
temporal$plots$percent_changeCompare the same year at different spatial scales:
county_scale <- run_builtin_analysis(
scale = "county",
year = 2016,
nutrients = "nitrogen",
include_wwtp = TRUE
)
huc8_scale <- run_builtin_analysis(
scale = "huc8",
year = 2016,
nutrients = "nitrogen",
include_wwtp = TRUE
)
huc2_scale <- run_builtin_analysis(
scale = "huc2",
year = 2016,
nutrients = "nitrogen",
include_wwtp = TRUE
)
# Compare scales
scale_comp <- compare_scenarios(list(
"County (n=~3000)" = county_scale,
"HUC8 (n=~2000)" = huc8_scale,
"HUC2 (n=18)" = huc2_scale
))
print(scale_comp$comparison_data)Save comparison plots to files:
# Create output directory
output_dir <- "scenario_comparison_results"
dir.create(output_dir, showWarnings = FALSE)
# Save all plots
save_plot(
comparison$plots$bar_chart,
file.path(output_dir, "classification_comparison.png"),
width = 12, height = 8
)
save_plot(
comparison$plots$surplus_deficit,
file.path(output_dir, "surplus_deficit_comparison.png"),
width = 12, height = 8
)
save_plot(
comparison$plots$percent_change,
file.path(output_dir, "percent_change.png"),
width = 12, height = 8
)Export comparison data to CSV:
Positive vs. negative changes:
# Extract differences
diffs <- comparison$summary$differences
# Interpret changes
if (diffs$delta_sources > 0) {
cat("Adding WWTP increased sources by", diffs$delta_sources, "units\n")
} else if (diffs$delta_sources < 0) {
cat("Adding WWTP decreased sources by", abs(diffs$delta_sources), "units\n")
}
# Percent change
cat("This represents a", round(diffs$pct_change_sources, 1), "% change\n")Classification Changes: -
delta_sources: Change in nutrient source areas -
delta_sinks: Change in nutrient sink areas
Magnitude Changes: - delta_surplus:
Change in total surplus (kg) - delta_deficit: Change in
total deficit (kg)
Negative values mean the second scenario has fewer/less than the base scenario.
Evaluate the impact of adding WWTP nutrient recovery:
# Current state (no WWTP recovery)
current <- run_builtin_analysis(
scale = "huc8",
year = 2016,
nutrients = "nitrogen",
include_wwtp = FALSE
)
# With WWTP recovery management
with_policy <- run_builtin_analysis(
scale = "huc8",
year = 2016,
nutrients = "nitrogen",
include_wwtp = TRUE
)
# Compare
policy_impact <- compare_scenarios(list(
"Current (No WWTP)" = current,
"With WWTP Recovery" = with_policy
))
# Key question: How many sink areas could benefit?
sinks_helped <- policy_impact$summary$differences$delta_sinks
cat("WWTP recovery could help", abs(sinks_helped), "deficit areas\n")Test sensitivity to cropland threshold:
thresholds <- c(500, 1000, 1234, 1500, 2000)
results <- list()
for (thresh in thresholds) {
results[[paste0("Threshold_", thresh)]] <- run_builtin_analysis(
scale = "huc8",
year = 2016,
nutrients = "nitrogen",
include_wwtp = TRUE,
cropland_threshold = thresh
)
}
# Compare all thresholds
sensitivity <- compare_scenarios(results)
# See how excluded areas change
excluded_counts <- sensitivity$comparison_data$n_excluded
names(excluded_counts) <- names(results)
print(excluded_counts)Compare different states or regions:
# Iowa
iowa <- run_state_analysis(
state = "IA",
scale = "county",
year = 2016,
nutrients = "nitrogen",
include_wwtp = TRUE
)
# Nebraska
nebraska <- run_state_analysis(
state = "NE",
scale = "county",
year = 2016,
nutrients = "nitrogen",
include_wwtp = TRUE
)
# Compare states
state_comp <- compare_scenarios(list(
"Iowa" = iowa,
"Nebraska" = nebraska
))
state_comp$plots$bar_chartCompare scenarios that differ in one key aspect for clearest interpretation:
# GOOD: Only WWTP inclusion changes
compare_scenarios(list(
"No WWTP" = run_builtin_analysis(year=2016, include_wwtp=FALSE),
"With WWTP" = run_builtin_analysis(year=2016, include_wwtp=TRUE)
))
# LESS CLEAR: Multiple things change at once
compare_scenarios(list(
"Base" = run_builtin_analysis(year=2016, scale="county", include_wwtp=FALSE),
"Alternative" = run_builtin_analysis(year=2015, scale="huc8", include_wwtp=TRUE)
))Use descriptive names that explain what differs:
Save parameters with results:
If comparing scenarios with different scales, metrics won’t be directly comparable:
vignette("getting-started") - Basic package usagevignette("dashboard-guide") - Interactive
dashboardvignette("advanced-features") - Advanced analysis
techniques