coef_m <- function() { m <- as.matrix(es_coefficients[, -1]) rownames(m) <- es_coefficients$landuse m } test_that("ESV totals and changes are correct", { a1 <- setNames(landuse_change$area_2005, landuse_change$landuse) a2 <- setNames(landuse_change$area_2025, landuse_change$landuse) esv <- ecosystem_service_value(a1, coef_m(), area2 = a2) expect_equal(esv$total[["period1"]], sum(a1 * coef_m())) expect_equal(sum(esv$by_landuse$value), sum(esv$by_service$value)) expect_equal(sum(esv$by_service$change), esv$total[["period2"]] - esv$total[["period1"]]) # CS equals each land use's share of ESV under proportional adjustment expect_equal(esv$sensitivity$cs, esv$by_landuse$value / sum(esv$by_landuse$value)) expect_output(print(esv), "Total ESV") }) test_that("ESV input checks", { m <- coef_m() expect_error(ecosystem_service_value(c(1, 2), m)) expect_error(ecosystem_service_value(c(mars = 1), m)) expect_error(ecosystem_service_value(c(cropland = 1), m, area2 = c(orchard = 1))) }) test_that("benefit transfer adjusts correctly", { bt <- benefit_transfer(100, study_income = 400, policy_income = 100, elasticity = 0.5, price_index_ratio = 1.2, exchange_rate = 0.5, quantity = 10, elasticity_range = c(0, 1), transfer_error = 0.1) expect_equal(bt$unit_value, 100 * 0.5 * 1.2 / 0.5) expect_equal(bt$total_value, bt$unit_value * 10) expect_equal(bt$unit_low, 100 * 0.25 * 1.2 / 0.5 * 0.9) expect_equal(bt$unit_high, 100 * 1 * 1.2 / 0.5 * 1.1) expect_equal(nrow(benefit_transfer(c(1, 2, 3), 1, 1)), 3) expect_error(benefit_transfer(1, 0, 1)) })