R/zchunk_L242.Bld_Inc_Elas_scenarios.R

Defines functions module_socioeconomics_L242.Bld_Inc_Elas_scenarios

Documented in module_socioeconomics_L242.Bld_Inc_Elas_scenarios

#' module_socioeconomics_L242.Bld_Inc_Elas_scenarios
#'
#' Calculates building income elasticity for each GCAM region by linear interpolation of assumption data
#'
#' @param command API command to execute
#' @param ... other optional parameters, depending on command
#' @return Depends on \code{command}: either a vector of required inputs,
#' a vector of output names, or (if \code{command} is "MAKE") all
#' the generated outputs: \code{L242.IncomeElasticity_bld_GCAM3}, \code{object}. The corresponding file in the
#' original data system was \code{L242.Bld_Inc_Elas_scenarios.R} (socioeconomics level2).
#' @details Takes per-capita GDP from SSP scenarios in each region.
#' Then calculates building income elasticity for each region by linear interpolation of assumption data.
#' @importFrom assertthat assert_that
#' @importFrom dplyr filter mutate select
#' @importFrom tidyr gather spread
#' @importFrom stats approx
#' @author RH April 2017
module_socioeconomics_L242.Bld_Inc_Elas_scenarios <- function(command, ...) {
  if(command == driver.DECLARE_INPUTS) {
    return(c(FILE = "common/GCAM_region_names",
             FILE = "socioeconomics/A42.inc_elas",
             "L102.pcgdp_thous90USD_Scen_R_Y",
             "L101.Pop_thous_GCAM3_R_Y",
             "L102.gdp_mil90usd_GCAM3_R_Y"))
  } else if(command == driver.DECLARE_OUTPUTS) {
    return(c("L242.IncomeElasticity_bld_gSSP1",
             "L242.IncomeElasticity_bld_gSSP2",
             "L242.IncomeElasticity_bld_gSSP3",
             "L242.IncomeElasticity_bld_gSSP4",
             "L242.IncomeElasticity_bld_gSSP5",
             "L242.IncomeElasticity_bld_SSP1",
             "L242.IncomeElasticity_bld_SSP2",
             "L242.IncomeElasticity_bld_SSP3",
             "L242.IncomeElasticity_bld_SSP4",
             "L242.IncomeElasticity_bld_SSP5",
             "L242.IncomeElasticity_bld_GCAM3"))
  } else if(command == driver.MAKE) {

    GCAM_region_ID <- value <- year <- pcgdp_90thousUSD <- scenario <-
        region <- energy.final.demand <- income.elasticity <- . <-
      value.x <- value.y <- NULL # silence package check.

    all_data <- list(...)[[1]]

    # Load required inputs
    GCAM_region_names <- get_data(all_data, "common/GCAM_region_names")
    A42.inc_elas <- get_data(all_data, "socioeconomics/A42.inc_elas")
    L102.pcgdp_thous90USD_Scen_R_Y <- get_data(all_data, "L102.pcgdp_thous90USD_Scen_R_Y") %>%
      ungroup() %>%
      rename(pcgdp_90thousUSD = value) %>%
      mutate(year = as.integer(year))
    L101.Pop_thous_GCAM3_R_Y <- get_data(all_data, "L101.Pop_thous_GCAM3_R_Y")
    L102.gdp_mil90usd_GCAM3_R_Y <- get_data(all_data, "L102.gdp_mil90usd_GCAM3_R_Y")

    # ===================================================
    # Linearly interpolate income elasticity for each level of per-capita GDP,
    # using the assumption data
    L242.pcgdp_thous90USD_Scen_R_Y <- L102.pcgdp_thous90USD_Scen_R_Y %>%
      left_join_error_no_match(GCAM_region_names, by = "GCAM_region_ID") %>%
      filter(year %in% MODEL_FUTURE_YEARS) %>%
      # Using approx rather than approx_fun because data is from assumption file, not in our tibble
      mutate(income.elasticity = approx(x = A42.inc_elas$pcgdp_90thousUSD, y = A42.inc_elas$inc_elas,
                                        xout = pcgdp_90thousUSD,
                                        # Rule 2 means that data outside of the interval of input
                                        # data will be assigned the cloest data extreme
                                        rule = 2)[['y']] %>% round(3),
             energy.final.demand = "building") %>%
      select(scenario, region, energy.final.demand, year, income.elasticity) %>%
      arrange(year)

    # Split by scenario and remove scenario column from each tibble

    L242.pcgdp_thous90USD_Scen_R_Y <- L242.pcgdp_thous90USD_Scen_R_Y %>%
      split(.$scenario) %>%
      lapply(function(df) {select(df, -scenario) %>%
          add_units("Unitless (% change in service demand / % change in income)") %>%
          add_comments("Uses previously calculated per-capita GDP assumptions for all SSP scenarios") %>%
          add_comments("Building income elasticity for each GCAM region generated by linear interpolation of assumption data") %>%
          add_precursors("common/GCAM_region_names", "socioeconomics/A42.inc_elas",
                         "L102.pcgdp_thous90USD_Scen_R_Y") })

    # L242.IncomeElasticity_bld_GCAM3: building sector income elasticity for GCAM 3.0 socioeconomics
    # For the GCAM 3.0 scenario, calculate the per-capita GDP
    L242.IncomeElasticity_bld_GCAM3 <- L102.gdp_mil90usd_GCAM3_R_Y %>%
      left_join_error_no_match(L101.Pop_thous_GCAM3_R_Y, by = c("GCAM_region_ID", "year")) %>%
      filter(year %in% MODEL_FUTURE_YEARS) %>%
      left_join_error_no_match(GCAM_region_names, by = "GCAM_region_ID") %>%
      transmute(region, year, pcgdp_90thousUSD = value.x / value.y) %>%
      # Using approx rather than approx_fun because data is from assumption file, not in our tibble
      mutate(income.elasticity = approx(x = A42.inc_elas$pcgdp_90thousUSD, y = A42.inc_elas$inc_elas,
                                        xout = pcgdp_90thousUSD,
                                        # Rule 2 means that data outside of the interval of input
                                        # data will be assigned the cloest data extreme
                                        rule = 2)[['y']] %>% round(3),
             energy.final.demand = "building") %>%
      select(region, energy.final.demand, year, income.elasticity)
    # ===================================================

    # Produce outputs
    L242.pcgdp_thous90USD_Scen_R_Y[["gSSP1"]] %>%
      add_title("Building Income Elasticity: gSSP1") %>%
      add_legacy_name("L242.IncomeElasticity_bld_gSSP1") ->
      L242.IncomeElasticity_bld_gSSP1

    L242.pcgdp_thous90USD_Scen_R_Y[["gSSP2"]] %>%
      add_title("Building Income Elasticity: gSSP2") %>%
      add_legacy_name("L242.IncomeElasticity_bld_gSSP2") ->
      L242.IncomeElasticity_bld_gSSP2

    L242.pcgdp_thous90USD_Scen_R_Y[["gSSP3"]] %>%
      add_title("Building Income Elasticity: gSSP3") %>%
      add_legacy_name("L242.IncomeElasticity_bld_gSSP3") ->
      L242.IncomeElasticity_bld_gSSP3

    L242.pcgdp_thous90USD_Scen_R_Y[["gSSP4"]] %>%
      add_title("Building Income Elasticity: gSSP4") %>%
      add_legacy_name("L242.IncomeElasticity_bld_gSSP4") ->
      L242.IncomeElasticity_bld_gSSP4

    L242.pcgdp_thous90USD_Scen_R_Y[["gSSP5"]] %>%
      add_title("Building Income Elasticity: gSSP5") %>%
      add_legacy_name("L242.IncomeElasticity_bld_gSSP5") ->
      L242.IncomeElasticity_bld_gSSP5

    L242.pcgdp_thous90USD_Scen_R_Y[["SSP1"]] %>%
      add_title("Building Income Elasticity: SSP1") %>%
      add_legacy_name("L242.IncomeElasticity_bld_SSP1") ->
      L242.IncomeElasticity_bld_SSP1

    L242.pcgdp_thous90USD_Scen_R_Y[["SSP2"]] %>%
      add_title("Building Income Elasticity: SSP2") %>%
      add_legacy_name("L242.IncomeElasticity_bld_SSP2") ->
      L242.IncomeElasticity_bld_SSP2

    L242.pcgdp_thous90USD_Scen_R_Y[["SSP3"]] %>%
      add_title("Building Income Elasticity: SSP3") %>%
      add_legacy_name("L242.IncomeElasticity_bld_SSP3") ->
      L242.IncomeElasticity_bld_SSP3

    L242.pcgdp_thous90USD_Scen_R_Y[["SSP4"]] %>%
      add_title("Building Income Elasticity: SSP4") %>%
      add_legacy_name("L242.IncomeElasticity_bld_SSP4") ->
      L242.IncomeElasticity_bld_SSP4

    L242.pcgdp_thous90USD_Scen_R_Y[["SSP5"]] %>%
      add_title("Building Income Elasticity: SSP5") %>%
      add_legacy_name("L242.IncomeElasticity_bld_SSP5") ->
      L242.IncomeElasticity_bld_SSP5

    L242.IncomeElasticity_bld_GCAM3 %>%
      add_title("Building Income Elasticity: GCAM3") %>%
      add_units("Unitless (% change in service demand / % change in income)") %>%
      add_comments("Uses previously calculated per-capita GDP assumptions of building elastciity") %>%
      add_comments("Building income elasticity for each GCAM region generated by linear interpolation of assumption data") %>%
      add_legacy_name("L242.IncomeElasticity_bld_GCAM3") %>%
      add_precursors("common/GCAM_region_names", "socioeconomics/A42.inc_elas",
                     "L101.Pop_thous_GCAM3_R_Y", "L102.gdp_mil90usd_GCAM3_R_Y") ->
      L242.IncomeElasticity_bld_GCAM3

    return_data(L242.IncomeElasticity_bld_gSSP1,
                L242.IncomeElasticity_bld_gSSP2,
                L242.IncomeElasticity_bld_gSSP3,
                L242.IncomeElasticity_bld_gSSP4,
                L242.IncomeElasticity_bld_gSSP5,
                L242.IncomeElasticity_bld_SSP1,
                L242.IncomeElasticity_bld_SSP2,
                L242.IncomeElasticity_bld_SSP3,
                L242.IncomeElasticity_bld_SSP4,
                L242.IncomeElasticity_bld_SSP5,
                L242.IncomeElasticity_bld_GCAM3)
  } else {
    stop("Unknown command")
  }
}
rohmin9122/gcam-korea-release documentation built on Nov. 26, 2020, 8:11 a.m.