Skip to content

Commit

Permalink
Merge pull request #323 from USEPA/revert-317-issue_311
Browse files Browse the repository at this point in the history
Revert "derive an M matrix for IEF models"
  • Loading branch information
bl-young authored Nov 1, 2024
2 parents 4ace170 + 031e992 commit 1d88467
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 54 deletions.
25 changes: 1 addition & 24 deletions R/ExternalImportFactors.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ castImportFactors <- function(IFTable, model) {
buildModelwithImportFactors <- function(model, configpaths = NULL) {
# (see Palm et al. 2019)

logging::loginfo("Building A_m (import requirements) accounting for international trade adjustment in domestic final demand.\n")
logging::loginfo("Building Import A (A_m) accounting for ITA in Domestic FD.\n")
# Re-derive import values in Use and final demand
# _m denotes import-related structures
model$UseTransactions_m <- model$UseTransactions - model$DomesticUseTransactions
Expand All @@ -126,28 +126,5 @@ buildModelwithImportFactors <- function(model, configpaths = NULL) {

model$M_m <- M_m

model$M <- deriveMMatrix(model)

return(model)
}

#' Derives an aggregate M matrix from M_d and M_m based on the ratio of commodity output to total imports.
#' @param model, An EEIO model object with model specs and crosswalk table loaded
#' @return An M matrix of flows x sector
deriveMMatrix <- function(model) {
logging::loginfo("Deriving M matrix (total emissions and resource use per dollar) ...")
q <- model$q
import_code <- model$FinalDemandMeta[model$FinalDemandMeta$Group=="Import", "Code_Loc"]
# derive total imports (m) from the Use table
U_m <- model$U - model$U_d
# Exclude imports col when calculating total imports
m <- rowSums(U_m[model$Commodities$Code_Loc, !(colnames(U_m) %in% import_code)]) # drop VA

dr <- q / (q + m)
mr <- 1 - dr
# Derive M by taking the ratio of domestic vs imported goods
M <- model$M_d %*% diag(as.vector(dr)) + model$M_m %*% diag(as.vector(mr))
colnames(M) <- colnames(model$M_d)

return(M)
}
13 changes: 0 additions & 13 deletions R/ValidateModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -474,19 +474,6 @@ validateImportFactorsApproach <- function(model, demand = "Consumption"){
cat("assuming model$M = model$M_m.\n")
print(all.equal(LCIA_dm, LCIA))

cat("\nValidating that the derived M matrix has all values between M_d and M_m\n")

# Validate that M is between M_m and M_d
a <- signif(model$M_m, 6)
b <- signif(model$M, 6)
c <- signif(model$M_d, 6)
z <- ((b > a) & (b > c)) | ((b < a) & (b < c))
if(sum(z) == 0) {
print(TRUE)
} else {
comm <- colSums(z) > 0
print(paste0("Failures: ", names(comm)[comm == TRUE]))
}
}

#' Validate the calculation of household_emissions
Expand Down
17 changes: 0 additions & 17 deletions man/deriveMMatrix.Rd

This file was deleted.

0 comments on commit 1d88467

Please sign in to comment.