From 758ad3de6aa7a1eb136dd32d06b66d139693c33e Mon Sep 17 00:00:00 2001 From: WesIngwersen Date: Wed, 1 Apr 2026 17:34:41 -0400 Subject: [PATCH 1/5] Remove stop for getting production demand --- R/DemandFunctions.R | 1 - R/StateiorFunctions.R | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/R/DemandFunctions.R b/R/DemandFunctions.R index 107baaa5..0e9dfd28 100644 --- a/R/DemandFunctions.R +++ b/R/DemandFunctions.R @@ -92,7 +92,6 @@ prepareDomesticProductionDemand <- function(model, location) { prepareImportProductionDemand <- function(model, location) { if (model$specs$IODataSource == "stateior") { y_m_p <- prepare2RDemand(model, location, domestic = FALSE) - stop("Import production demand not yet implemented for 2R models.") } else { # Note that model$mu (i.e., ITA) is not included in import production demand # because it is included in Domestic Production Demand diff --git a/R/StateiorFunctions.R b/R/StateiorFunctions.R index 8e6721a4..381f5c39 100644 --- a/R/StateiorFunctions.R +++ b/R/StateiorFunctions.R @@ -135,7 +135,7 @@ generate2RDirectRequirementsfromUseWithTrade <- function(model, domestic){ } -#' Prepares a production demand vector representing production for two region models +#' Prepares a demand vector for two region models #' Demand for SoI = SoI2SoI + RoUS2SoI #' Demand for RoUS = SoI2RoUS + RoUS2RoUS #' @param model An EEIO model object with model specs and IO tables loaded From 928b6718f8e91f429e6889cd066acdb27393d28f Mon Sep 17 00:00:00 2001 From: WesIngwersen Date: Thu, 2 Apr 2026 11:37:58 -0400 Subject: [PATCH 2/5] Add `import` boolean and if clause to get import matrix like Use_values from which final demand is extracted using existing code --- R/StateiorFunctions.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/R/StateiorFunctions.R b/R/StateiorFunctions.R index 381f5c39..81bc345e 100644 --- a/R/StateiorFunctions.R +++ b/R/StateiorFunctions.R @@ -143,7 +143,7 @@ generate2RDirectRequirementsfromUseWithTrade <- function(model, domestic){ #' @param domestic A logical parameter indicating whether to generate domestic demand vector. #' @param demand_type A str indicating whether demand is Production or Consumption #' @return A named vector with demand -prepare2RDemand <- function(model, location, domestic, demand_type = "Production") { +prepare2RDemand <- function(model, location, domestic, import=FALSE, demand_type = "Production") { # Get state abbreviations, e.g., "US-ME" and "RoUS" state_abb <- sub(".*/","",model$FinalDemandMeta$Code_Loc) ## Extract characters after / state_abb <- unique(state_abb) @@ -154,9 +154,13 @@ prepare2RDemand <- function(model, location, domestic, demand_type = "Production # TODO: CHANGE domestic FROM BOOLEAN TO STRING WITH VALUES 'domestic', 'production', # and 'import', so we can calculate the import matrix in the following if else if else block use_table <- model$DomesticUseTransactionswithTrade + } else if(import) { + # Subtracts domestic from total for each of 4 objects of the transactions lists + use_table <- Map(`-`, model$UseTransactionswithTrade, model$DomesticUseTransactionswithTrade) } else { use_table <- model$UseTransactionswithTrade } + # Getting list of final demand columns used for the appropriate demand if(demand_type == "Production") { FD_columns <- unlist(sapply(list("HouseholdDemand", "InvestmentDemand", From 86292747c7335b254c144ecb82d29670914baf68 Mon Sep 17 00:00:00 2001 From: WesIngwersen Date: Thu, 2 Apr 2026 11:38:50 -0400 Subject: [PATCH 3/5] drop warning about production vector use with coupled models --- R/CalculationFunctions.R | 4 ---- 1 file changed, 4 deletions(-) diff --git a/R/CalculationFunctions.R b/R/CalculationFunctions.R index 30b450df..8139877d 100644 --- a/R/CalculationFunctions.R +++ b/R/CalculationFunctions.R @@ -93,10 +93,6 @@ prepareDemandVectorForImportResults <- function(model, demand = "Production", lo } # Calculate import demand vector y_m. if(demand == "Production") { - # This option left in for validation purposes. - logging::loginfo(paste0("Warning: Production demand vector not recommended ", - "for estimating results for models with external ", - "Import Factors.")) y_m <- prepareImportProductionDemand(model, location = location) } else if(demand == "Consumption") { y_m <- prepareImportConsumptionDemand(model, location = location) From 23c29d28a52dedb5374b37c3749657712462f748 Mon Sep 17 00:00:00 2001 From: WesIngwersen Date: Thu, 2 Apr 2026 11:39:21 -0400 Subject: [PATCH 4/5] use new `import` flag when calling prepareImportProductionDemand --- R/DemandFunctions.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/DemandFunctions.R b/R/DemandFunctions.R index 0e9dfd28..05cdd968 100644 --- a/R/DemandFunctions.R +++ b/R/DemandFunctions.R @@ -91,7 +91,7 @@ prepareDomesticProductionDemand <- function(model, location) { #' @return A named vector with demand prepareImportProductionDemand <- function(model, location) { if (model$specs$IODataSource == "stateior") { - y_m_p <- prepare2RDemand(model, location, domestic = FALSE) + y_m_p <- prepare2RDemand(model, location, domestic = FALSE, import = TRUE) } else { # Note that model$mu (i.e., ITA) is not included in import production demand # because it is included in Domestic Production Demand From 3d69ce613decd72bcbf78507ff39eaa6682bfa0d Mon Sep 17 00:00:00 2001 From: jvendries Date: Wed, 8 Apr 2026 10:58:31 -0400 Subject: [PATCH 5/5] In prepare2RDemand, removed domestic vs. non-domestic difference in calculation of SoI2SoI and RoUS2RoUS portions of two region models. Needs to be tested for consumption demand --- R/StateiorFunctions.R | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/R/StateiorFunctions.R b/R/StateiorFunctions.R index 81bc345e..970f2887 100644 --- a/R/StateiorFunctions.R +++ b/R/StateiorFunctions.R @@ -180,22 +180,24 @@ prepare2RDemand <- function(model, location, domestic, import=FALSE, demand_type # State with SoI if(location == state_abb[1]) { # calculate final demand for SoI - if(domestic) { - SoI2SoI_y <- rowSums(use_table[["SoI2SoI"]][, c(FD_columns, ita_column, "ExportResidual")]) - } else { - SoI2SoI_y <- rowSums(use_table[["SoI2SoI"]][, c(FD_columns, "ExportResidual")]) - } + # if(domestic) { + # SoI2SoI_y <- rowSums(use_table[["SoI2SoI"]][, c(FD_columns, ita_column, "ExportResidual")]) + # } else { + # SoI2SoI_y <- rowSums(use_table[["SoI2SoI"]][, c(FD_columns, "ExportResidual")]) + # } + SoI2SoI_y <- rowSums(use_table[["SoI2SoI"]][, c(FD_columns, ita_column, "ExportResidual")]) RoUS2SoI_y <- rowSums(use_table[["RoUS2SoI"]][, c(FD_columns, ita_column)]) y <- c(SoI2SoI_y, RoUS2SoI_y) # For RoUS } else if(location == state_abb[2]) { # calculate final demand for RoUS - if(domestic) { - RoUS2RoUS_y <- rowSums(use_table[["RoUS2RoUS"]][, c(FD_columns, ita_column, "ExportResidual")]) - } else { - RoUS2RoUS_y <- rowSums(use_table[["RoUS2RoUS"]][, c(FD_columns, "ExportResidual")]) - } + # if(domestic) { + # RoUS2RoUS_y <- rowSums(use_table[["RoUS2RoUS"]][, c(FD_columns, ita_column, "ExportResidual")]) + # } else { + # RoUS2RoUS_y <- rowSums(use_table[["RoUS2RoUS"]][, c(FD_columns, "ExportResidual")]) + # } + RoUS2RoUS_y <- rowSums(use_table[["RoUS2RoUS"]][, c(FD_columns, ita_column, "ExportResidual")]) SoI2RoUS_y <- rowSums(use_table[["SoI2RoUS"]][, c(FD_columns, ita_column)]) y <- c(SoI2RoUS_y, RoUS2RoUS_y) } @@ -254,7 +256,7 @@ print2RValidationResults <- function(model) { twoRegionResults_ls$finalResultsProductionComplete <- finalResultsProductionComplete twoRegionResults_ls$finalResultsConsumptionComplete <- finalResultsConsumptionComplete - # return(twoRegionResults_ls) + return(twoRegionResults_ls) }