From 541ff85c86fa9b4071fe794324b0db727d5f933e Mon Sep 17 00:00:00 2001
From: jvendries <jvendries@hotmail>
Date: Fri, 17 Nov 2023 10:49:37 -0500
Subject: [PATCH] For models with external import factors,

Dropped U_n_m and UseTransactions_m at end of model build

Removed model$DomesticFDWithITA object (not needed)

Corrected description of coupled model approach at the beginning of buildModelwithImportFactors function

Removed unneeded commented code
---
 R/BuildModel.R  |  7 ++++++-
 R/IOFunctions.R | 29 +++++------------------------
 2 files changed, 11 insertions(+), 25 deletions(-)

diff --git a/R/BuildModel.R b/R/BuildModel.R
index 0e31f561..7989d6b2 100644
--- a/R/BuildModel.R
+++ b/R/BuildModel.R
@@ -145,7 +145,12 @@ constructEEIOMatrices <- function(model) {
   mat_to_remove <- c("MakeTransactions", "UseTransactions", "DomesticUseTransactions",
                      "UseValueAdded", "FinalDemand", "DomesticFinalDemand",
                      "InternationalTradeAdjustment", "CommodityOutput", "IndustryOutput",
-                     "U_n", "U_d_n")
+                     "U_n", "U_d_n") 
+  # Drop U_n_m, UseTransactions_m for models with external import factors
+  if(!is.null(model$specs$ExternalImportFactors)){
+    mat_to_remove <- c(mat_to_remove, "U_n_m", "UseTransactions_m")
+  }
+  
   if (model$specs$CommodityorIndustryType=="Industry") {
     mat_to_remove <- c(mat_to_remove,
                        c("FinalDemandbyCommodity", "DomesticFinalDemandbyCommodity",
diff --git a/R/IOFunctions.R b/R/IOFunctions.R
index fc114b1f..493c2109 100644
--- a/R/IOFunctions.R
+++ b/R/IOFunctions.R
@@ -238,11 +238,11 @@ generateInternationalTradeAdjustmentVector <- function(Use, Import, model) {
 #' @param model, An EEIO model object with model specs and crosswalk table loaded
 #' @return A model object with explicit import components.
 buildModelwithImportFactors <- function(model) {
-  # Deriving the economic component of the Swedish equation for import factors: f^(d+m) = S^d*L^d*y^d + Q^t*A^m*L^d*y^d + Q^t*y^m + f^h
-  # S and Q are the environmental intensity matrices, so getting rid of those components we would have the following (where x is the economic component of f)
-  # x^(d+m) = L^d*y^d + A^m*L^d*y^d + y^m + f^h
+  # Deriving the economic component of the Swedish equation for import factors: f^(d+m) = s^d*L^d*y^d + Q^t*A^m*L^d*y^d + Q^t*y^m + f^h
+  # s^d are the domestic direct environmental coefficients, and Q are the environmental import multipliers, s_m*L_m. Dropping s_d and s_m we get
+  # x^(d+m) = s^d*L^d*y^d + L^m*A^m*L^d*y^d + L^m*y^m + f^h
   # Since f^h is not currently part of the useeior model calculations, we drop it:
-  # x^(d+m) = L^d*y^d + A^m*L^d*y^d + y^m 
+  # x^(d+m) = L^d*y^d + L^m*A^m*L^d*y^d + L^m*y^m 
   # The resulting expression should be equivalent to the x = L*y such that
   # x^(d+m) = x = L*y
   
@@ -253,10 +253,8 @@ buildModelwithImportFactors <- function(model) {
   model$U_n_m <- normalizeIOTransactions(model$UseTransactions_m, model$IndustryOutput) #normalized imported Use
   
   # Including InternationalTradeAdjustment in DomesticFinalDemand for import factors calculations
-  model$DomesticFDWithITA <- model$DomesticFinalDemand # TODO: Remove after testing calculation functions without this object
+
   if(model$specs$IODataSource != "stateior") {
-    model$DomesticFDWithITA[,"F050/US"] <- model$InternationalTradeAdjustment
-    
     FD_col_index <- which(colnames(model$ImportMatrix) %in% model$FinalDemandMeta$Code_Loc)
     model$ImportFinalDemand <- model$ImportMatrix[,FD_col_index]
   }else{
@@ -285,23 +283,6 @@ buildModelwithImportFactors <- function(model) {
 
   model$M_m <- M_m
   
-  # # Check that import values are the same as the original import data
-  # # Note that this check is not meant to be included in the code
-  # Import <- get(paste("Summary_Import", model$specs$IOYear, "BeforeRedef", sep = "_"))*1E6
-  # Import <- Import[,!names(Import) %in% c("T001", "T004")] # Remove t001 and t004 columns.
-  # Import <- Import[c(model$Industries$Code, model$FinalDemandMeta$Code)] # Reorder final demand columns to match final demand meta
-  # rownames(Import) <-rownames(model$UseTransactions_m)
-  # importColNames <- c(colnames(model$UseTransactions_m), colnames(model$DomesticFDWithITA))
-  # colnames(Import) <- importColNames
-  # 
-  # useCheck <- model$UseTransactions_m - Import[,1:71]
-  # sum(sum(useCheck)) == 0 # should be TRUE as should all.equal(model$UseTransactions_m, Import[,1:71])
-  # 
-  # importCheck <- model$ImportFinalDemand - Import[72:91]
-  # sum(sum(importCheck)) == 0 # should be TRUE as should all.equal(model$ImportFinalDemand, Import[,72:91])
-  # 
-  # model$ImportMatrix <- Import #Temporary addition for validation checks
-  
   return(model)
 }