From 9d6a99591506835e9ba16fe0be5dc04f8f9f1a3c Mon Sep 17 00:00:00 2001 From: Ben Young Date: Thu, 15 Aug 2024 15:44:19 -0400 Subject: [PATCH 01/10] implement RoW portion of results matrix for single region model --- R/CalculationFunctions.R | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/R/CalculationFunctions.R b/R/CalculationFunctions.R index 5ca57ce4..dd8183da 100644 --- a/R/CalculationFunctions.R +++ b/R/CalculationFunctions.R @@ -146,20 +146,21 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de r3 <- model$M_m %*% diag(as.vector(y_m)) if (use_domestic_requirements) { - result$LCI_f <- r1 - } else { - result$LCI_f <- r1 + r2 + r3 + r2[] <- 0 + r3[] <- 0 } + result$LCI_f <- cbind(r1 + r2, r3) # Term 3 is assigned to RoW # Calculate Final Perspective LCIA (matrix with direct impacts in form of sector x impacts) logging::loginfo("Calculating Final Perspective LCIA with external import factors...") result$LCIA_f <- model$C %*% result$LCI_f result$LCI_f <- t(result$LCI_f) result$LCIA_f <- t(result$LCIA_f) + row_names <- c(colnames(model$M_m), gsub("/US", "/RoW", colnames(model$M_m))) colnames(result$LCI_f) <- rownames(model$M_m) - rownames(result$LCI_f) <- colnames(model$M_m) + rownames(result$LCI_f) <- row_names colnames(result$LCIA_f) <- rownames(model$D) - rownames(result$LCIA_f) <- colnames(model$D) + rownames(result$LCIA_f) <- row_names # Add household emissions to results if applicable if(household_emissions) { @@ -177,20 +178,20 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de r3 <- t(model$M_m %*% diag(as.vector(y_m))) # Emissions from imported goods consumed as final products if (use_domestic_requirements) { - result$LCI_d <- r1 - } else { - result$LCI_d <- r1 + r2 + r3 + r2[] <- 0 + r3[] <- 0 } - + result$LCI_d <- rbind(r1, r2 + r3) # Term 2 and Term 3 are assigned to RoW # Calculate Direct Perspective LCIA (matrix with direct impacts in form of sector x impacts) logging::loginfo("Calculating Direct Perspective LCIA with external import factors...") result$LCIA_d <- model$C %*% t(result$LCI_d) result$LCIA_d <- t(result$LCIA_d) + row_names <- c(colnames(model$M_m), gsub("/US", "/RoW", colnames(model$M_m))) colnames(result$LCI_d) <- rownames(model$M_m) - rownames(result$LCI_d) <- colnames(model$M_m) + rownames(result$LCI_d) <- row_names colnames(result$LCIA_d) <- rownames(model$D) - rownames(result$LCIA_d) <- colnames(model$D) + rownames(result$LCIA_d) <- row_names # Add household emissions to results if applicable if(household_emissions) { From 8582c81bc0dc5697c21b17d078553a688937b298 Mon Sep 17 00:00:00 2001 From: Ben Young Date: Thu, 15 Aug 2024 16:18:24 -0400 Subject: [PATCH 02/10] incorporate RoW for result matrix in two region models --- R/CalculationFunctions.R | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/R/CalculationFunctions.R b/R/CalculationFunctions.R index dd8183da..9ca78be4 100644 --- a/R/CalculationFunctions.R +++ b/R/CalculationFunctions.R @@ -135,6 +135,14 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de hh_lcia <- calculateHouseholdEmissions(model, f=(y_d + y_m), location, characterized=TRUE) } + if(model$specs$IODataSource=="stateior") { + row_names <- c(colnames(model$M_m), + gsub("/.*", "/RoW", colnames(model$M_m[, 1:(nrow(y_d)/2)]))) + } else { + row_names <- c(colnames(model$M_m), + gsub("/.*", "/RoW", colnames(model$M_m))) + } + # Calculate Final perspective results if(perspective == "FINAL") { # Calculate Final Perspective LCI (a matrix with total impacts in form of sector x flows) @@ -149,6 +157,14 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de r2[] <- 0 r3[] <- 0 } + + if(model$specs$IODataSource=="stateior") { + # collapse third term for SoI and RoUS + z <- r3[, 1:(ncol(r3)/2)] + r3[, ((ncol(r3)/2)+1):ncol(r3)] + # rowSums(z) == rowSums(r3) + r3 <- z + } + result$LCI_f <- cbind(r1 + r2, r3) # Term 3 is assigned to RoW # Calculate Final Perspective LCIA (matrix with direct impacts in form of sector x impacts) logging::loginfo("Calculating Final Perspective LCIA with external import factors...") @@ -156,7 +172,6 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de result$LCI_f <- t(result$LCI_f) result$LCIA_f <- t(result$LCIA_f) - row_names <- c(colnames(model$M_m), gsub("/US", "/RoW", colnames(model$M_m))) colnames(result$LCI_f) <- rownames(model$M_m) rownames(result$LCI_f) <- row_names colnames(result$LCIA_f) <- rownames(model$D) @@ -181,13 +196,23 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de r2[] <- 0 r3[] <- 0 } + + if(model$specs$IODataSource=="stateior") { + # collapse second and third term for SoI and RoUS + z <- r3[1:(nrow(r3)/2), ] + r3[((nrow(r3)/2)+1):nrow(r3), ] + # colSums(z) == colSums(r3) + r3 <- z + z <- r2[1:(nrow(r2)/2), ] + r2[((nrow(r2)/2)+1):nrow(r2), ] + # colSums(z) == colSums(r2) + r2 <- z + } + result$LCI_d <- rbind(r1, r2 + r3) # Term 2 and Term 3 are assigned to RoW # Calculate Direct Perspective LCIA (matrix with direct impacts in form of sector x impacts) logging::loginfo("Calculating Direct Perspective LCIA with external import factors...") result$LCIA_d <- model$C %*% t(result$LCI_d) result$LCIA_d <- t(result$LCIA_d) - row_names <- c(colnames(model$M_m), gsub("/US", "/RoW", colnames(model$M_m))) colnames(result$LCI_d) <- rownames(model$M_m) rownames(result$LCI_d) <- row_names colnames(result$LCIA_d) <- rownames(model$D) From 455b406050dac84623b7a1b086fa8f6da0f70808 Mon Sep 17 00:00:00 2001 From: WesIngwersen Date: Fri, 16 Aug 2024 09:38:31 -0400 Subject: [PATCH 03/10] Add show_RoW param to calculateEEIOModel and propogate through calculcateExternalImportFactors. Direct perspective calculation not working - matrix dimension problem with C %*% t(LCI_d) --- R/CalculationFunctions.R | 67 ++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/R/CalculationFunctions.R b/R/CalculationFunctions.R index 9ca78be4..0fd98a30 100644 --- a/R/CalculationFunctions.R +++ b/R/CalculationFunctions.R @@ -13,14 +13,16 @@ #' @param use_domestic_requirements A logical value: if TRUE, use domestic demand and L_d matrix; #' if FALSE, use complete demand and L matrix. #' @param household_emissions, bool, if TRUE, include calculation of emissions from households +#' @param show_RoW, bool, if TRUE, include rows for commodities in RoW, e.g. `111CA/RoW` in result objects. +#' Only valid currently for models with ExternalImportFactors. #' @export #' @return A list with LCI and LCIA results (in data.frame format) of the EEIO model. calculateEEIOModel <- function(model, perspective, demand = "Production", location = NULL, - use_domestic_requirements = FALSE, household_emissions = FALSE) { + use_domestic_requirements = FALSE, household_emissions = FALSE, show_RoW = FALSE) { if (!is.null(model$specs$ExternalImportFactors) && model$specs$ExternalImportFactors) { result <- calculateResultsWithExternalFactors(model, perspective, demand, location = location, use_domestic_requirements = use_domestic_requirements, - household_emissions = household_emissions) + household_emissions = household_emissions, show_RoW = show_RoW) } else { # Standard model results calculation f <- prepareDemandVectorForStandardResults(model, demand, location, use_domestic_requirements) @@ -125,7 +127,8 @@ prepareDemandVectorForImportResults <- function(model, demand = "Production", lo #' @param household_emissions, bool, if TRUE, include calculation of emissions from households #' @return A list with LCI and LCIA results (in data.frame format) of the EEIO model. calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", demand = "Consumption", location = NULL, - use_domestic_requirements = FALSE, household_emissions = FALSE) { + use_domestic_requirements = FALSE, household_emissions = FALSE, + show_RoW = FALSE) { result <- list() y_d <- prepareDemandVectorForStandardResults(model, demand, location = location, use_domestic_requirements = TRUE) y_m <- prepareDemandVectorForImportResults(model, demand, location = location) @@ -135,14 +138,17 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de hh_lcia <- calculateHouseholdEmissions(model, f=(y_d + y_m), location, characterized=TRUE) } - if(model$specs$IODataSource=="stateior") { - row_names <- c(colnames(model$M_m), - gsub("/.*", "/RoW", colnames(model$M_m[, 1:(nrow(y_d)/2)]))) + if(show_RoW) { + if(model$specs$IODataSource=="stateior") { + row_names <- c(colnames(model$M_m), + gsub("/.*", "/RoW", colnames(model$M_m[, 1:(nrow(y_d)/2)]))) + } else { + row_names <- c(colnames(model$M_m), + gsub("/.*", "/RoW", colnames(model$M_m))) + } } else { - row_names <- c(colnames(model$M_m), - gsub("/.*", "/RoW", colnames(model$M_m))) + row_names <- colnames(model$M_m) } - # Calculate Final perspective results if(perspective == "FINAL") { # Calculate Final Perspective LCI (a matrix with total impacts in form of sector x flows) @@ -158,14 +164,18 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de r3[] <- 0 } - if(model$specs$IODataSource=="stateior") { - # collapse third term for SoI and RoUS - z <- r3[, 1:(ncol(r3)/2)] + r3[, ((ncol(r3)/2)+1):ncol(r3)] - # rowSums(z) == rowSums(r3) - r3 <- z + if(show_RoW) { + if(model$specs$IODataSource=="stateior") { + # collapse third term for SoI and RoUS + z <- r3[, 1:(ncol(r3)/2)] + r3[, ((ncol(r3)/2)+1):ncol(r3)] + # rowSums(z) == rowSums(r3) + r3 <- z + } + result$LCI_f <- cbind(r1 + r2, r3) # Term 3 is assigned to RoW + } else { + result$LCI_f <- r1 + r2 + r3 # Term 3 is assigned to RoW } - - result$LCI_f <- cbind(r1 + r2, r3) # Term 3 is assigned to RoW + # Calculate Final Perspective LCIA (matrix with direct impacts in form of sector x impacts) logging::loginfo("Calculating Final Perspective LCIA with external import factors...") result$LCIA_f <- model$C %*% result$LCI_f @@ -197,17 +207,20 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de r3[] <- 0 } - if(model$specs$IODataSource=="stateior") { - # collapse second and third term for SoI and RoUS - z <- r3[1:(nrow(r3)/2), ] + r3[((nrow(r3)/2)+1):nrow(r3), ] - # colSums(z) == colSums(r3) - r3 <- z - z <- r2[1:(nrow(r2)/2), ] + r2[((nrow(r2)/2)+1):nrow(r2), ] - # colSums(z) == colSums(r2) - r2 <- z - } - - result$LCI_d <- rbind(r1, r2 + r3) # Term 2 and Term 3 are assigned to RoW + if(show_RoW) { + if(model$specs$IODataSource=="stateior") { + # collapse second and third term for SoI and RoUS + z <- r3[1:(nrow(r3)/2), ] + r3[((nrow(r3)/2)+1):nrow(r3), ] + # colSums(z) == colSums(r3) + r3 <- z + z <- r2[1:(nrow(r2)/2), ] + r2[((nrow(r2)/2)+1):nrow(r2), ] + # colSums(z) == colSums(r2) + r2 <- z + } + result$LCI_d <- cbind(r1, r2 + r3) # Term 2 and Term 3 are assigned to RoW + } else { + result$LCI_d <- r1 + r2 + r3 # All three terms combined and regions do not change + } # Calculate Direct Perspective LCIA (matrix with direct impacts in form of sector x impacts) logging::loginfo("Calculating Direct Perspective LCIA with external import factors...") result$LCIA_d <- model$C %*% t(result$LCI_d) From f9f1ab437de25c41a620622201eac9af40a16054 Mon Sep 17 00:00:00 2001 From: WesIngwersen Date: Fri, 16 Aug 2024 09:51:36 -0400 Subject: [PATCH 04/10] should be rbind because in d the results are transposed --- R/CalculationFunctions.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/CalculationFunctions.R b/R/CalculationFunctions.R index 0fd98a30..982563f9 100644 --- a/R/CalculationFunctions.R +++ b/R/CalculationFunctions.R @@ -217,7 +217,7 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de # colSums(z) == colSums(r2) r2 <- z } - result$LCI_d <- cbind(r1, r2 + r3) # Term 2 and Term 3 are assigned to RoW + result$LCI_d <- rbind(r1, r2 + r3) # Term 2 and Term 3 are assigned to RoW } else { result$LCI_d <- r1 + r2 + r3 # All three terms combined and regions do not change } From 336cc55e0484ba089d9fe68a10491f46e646ede9 Mon Sep 17 00:00:00 2001 From: Ben Young Date: Fri, 16 Aug 2024 11:50:32 -0400 Subject: [PATCH 05/10] udpate documentation --- R/CalculationFunctions.R | 3 ++- man/calculateEEIOModel.Rd | 6 +++++- man/calculateResultsWithExternalFactors.Rd | 5 ++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/R/CalculationFunctions.R b/R/CalculationFunctions.R index 982563f9..efe60522 100644 --- a/R/CalculationFunctions.R +++ b/R/CalculationFunctions.R @@ -14,7 +14,7 @@ #' if FALSE, use complete demand and L matrix. #' @param household_emissions, bool, if TRUE, include calculation of emissions from households #' @param show_RoW, bool, if TRUE, include rows for commodities in RoW, e.g. `111CA/RoW` in result objects. -#' Only valid currently for models with ExternalImportFactors. +#' Only valid currently for models with ExternalImportFactors. #' @export #' @return A list with LCI and LCIA results (in data.frame format) of the EEIO model. calculateEEIOModel <- function(model, perspective, demand = "Production", location = NULL, @@ -125,6 +125,7 @@ prepareDemandVectorForImportResults <- function(model, demand = "Production", lo #' @param location, str optional location code for demand vector, required for two-region models #' @param use_domestic_requirements bool, if TRUE, return only domestic portion of results #' @param household_emissions, bool, if TRUE, include calculation of emissions from households +#' @param show_RoW, bool, if TRUE, include rows for commodities in RoW, e.g. `111CA/RoW` in result objects. #' @return A list with LCI and LCIA results (in data.frame format) of the EEIO model. calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", demand = "Consumption", location = NULL, use_domestic_requirements = FALSE, household_emissions = FALSE, diff --git a/man/calculateEEIOModel.Rd b/man/calculateEEIOModel.Rd index 05f2cd24..467d7dc2 100644 --- a/man/calculateEEIOModel.Rd +++ b/man/calculateEEIOModel.Rd @@ -11,7 +11,8 @@ calculateEEIOModel( demand = "Production", location = NULL, use_domestic_requirements = FALSE, - household_emissions = FALSE + household_emissions = FALSE, + show_RoW = FALSE ) } \arguments{ @@ -31,6 +32,9 @@ numeric values in USD with the same dollar year as model.} if FALSE, use complete demand and L matrix.} \item{household_emissions, }{bool, if TRUE, include calculation of emissions from households} + +\item{show_RoW, }{bool, if TRUE, include rows for commodities in RoW, e.g. `111CA/RoW` in result objects. +Only valid currently for models with ExternalImportFactors.} } \value{ A list with LCI and LCIA results (in data.frame format) of the EEIO model. diff --git a/man/calculateResultsWithExternalFactors.Rd b/man/calculateResultsWithExternalFactors.Rd index cae7f106..2c3cbfd9 100644 --- a/man/calculateResultsWithExternalFactors.Rd +++ b/man/calculateResultsWithExternalFactors.Rd @@ -12,7 +12,8 @@ calculateResultsWithExternalFactors( demand = "Consumption", location = NULL, use_domestic_requirements = FALSE, - household_emissions = FALSE + household_emissions = FALSE, + show_RoW = FALSE ) } \arguments{ @@ -29,6 +30,8 @@ results with the sectors consumed by the final user.} \item{use_domestic_requirements}{bool, if TRUE, return only domestic portion of results} \item{household_emissions, }{bool, if TRUE, include calculation of emissions from households} + +\item{show_RoW, }{bool, if TRUE, include rows for commodities in RoW, e.g. `111CA/RoW` in result objects.} } \value{ A list with LCI and LCIA results (in data.frame format) of the EEIO model. From 07998ce8b149cce58e9d6330798da11c6dc34ff8 Mon Sep 17 00:00:00 2001 From: Ben Young Date: Fri, 16 Aug 2024 11:50:58 -0400 Subject: [PATCH 06/10] avoid extra rows if location is set for single region model --- R/CalculationFunctions.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/CalculationFunctions.R b/R/CalculationFunctions.R index efe60522..ac5f46d8 100644 --- a/R/CalculationFunctions.R +++ b/R/CalculationFunctions.R @@ -559,6 +559,10 @@ calculateHouseholdEmissions <- function(model, f, location, characterized=FALSE) logging::logwarn("Household emissions not found in this model") return(NULL) } + if(length(model$specs$ModelRegionAcronyms) == 1) { + # Set location as NULL for single region model + location <- NULL + } codes <- model$FinalDemandMeta[model$FinalDemandMeta$Group%in%c("Household"), "Code_Loc"] if (!is.null(location)) { other_code <- codes[!grepl(location, codes)] From ed279a0ebf6016ae5fe13e5bb255f4f84c001d83 Mon Sep 17 00:00:00 2001 From: Ben Young Date: Fri, 16 Aug 2024 11:52:25 -0400 Subject: [PATCH 07/10] add clarity to matrix subsetting --- R/CalculationFunctions.R | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/R/CalculationFunctions.R b/R/CalculationFunctions.R index ac5f46d8..a69a9a71 100644 --- a/R/CalculationFunctions.R +++ b/R/CalculationFunctions.R @@ -141,8 +141,9 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de if(show_RoW) { if(model$specs$IODataSource=="stateior") { + sector_count <- nrow(y_d)/2 row_names <- c(colnames(model$M_m), - gsub("/.*", "/RoW", colnames(model$M_m[, 1:(nrow(y_d)/2)]))) + gsub("/.*", "/RoW", colnames(model$M_m[, 1:sector_count]))) } else { row_names <- c(colnames(model$M_m), gsub("/.*", "/RoW", colnames(model$M_m))) @@ -155,12 +156,12 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de # Calculate Final Perspective LCI (a matrix with total impacts in form of sector x flows) logging::loginfo("Calculating Final Perspective LCI with external import factors...") - # parentheses used to denote (domestic) and (import) components r1 <- model$B %*% model$L_d %*% diag(as.vector(y_d)) r2 <- model$M_m %*% model$A_m %*% model$L_d %*% diag(as.vector(y_d)) r3 <- model$M_m %*% diag(as.vector(y_m)) if (use_domestic_requirements) { + # zero out the import results r2[] <- 0 r3[] <- 0 } @@ -168,13 +169,13 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de if(show_RoW) { if(model$specs$IODataSource=="stateior") { # collapse third term for SoI and RoUS - z <- r3[, 1:(ncol(r3)/2)] + r3[, ((ncol(r3)/2)+1):ncol(r3)] + z <- r3[, 1:sector_count] + r3[, (sector_count+1):(sector_count*2)] # rowSums(z) == rowSums(r3) r3 <- z } result$LCI_f <- cbind(r1 + r2, r3) # Term 3 is assigned to RoW } else { - result$LCI_f <- r1 + r2 + r3 # Term 3 is assigned to RoW + result$LCI_f <- r1 + r2 + r3 # All three terms combined and regions do not change } # Calculate Final Perspective LCIA (matrix with direct impacts in form of sector x impacts) @@ -204,6 +205,7 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de r3 <- t(model$M_m %*% diag(as.vector(y_m))) # Emissions from imported goods consumed as final products if (use_domestic_requirements) { + # zero out the import results r2[] <- 0 r3[] <- 0 } @@ -211,10 +213,10 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de if(show_RoW) { if(model$specs$IODataSource=="stateior") { # collapse second and third term for SoI and RoUS - z <- r3[1:(nrow(r3)/2), ] + r3[((nrow(r3)/2)+1):nrow(r3), ] + z <- r3[1:sector_count, ] + r3[(sector_count+1):(sector_count*2), ] # colSums(z) == colSums(r3) r3 <- z - z <- r2[1:(nrow(r2)/2), ] + r2[((nrow(r2)/2)+1):nrow(r2), ] + z <- r2[1:sector_count, ] + r2[(sector_count+1):(sector_count*2), ] # colSums(z) == colSums(r2) r2 <- z } From 7ffeca3b2aaf93841543bd10922c4b45237f3dae Mon Sep 17 00:00:00 2001 From: Ben Young Date: Fri, 16 Aug 2024 12:31:15 -0400 Subject: [PATCH 08/10] refactor results calculation to reduce duplication #312 --- R/CalculationFunctions.R | 127 ++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 76 deletions(-) diff --git a/R/CalculationFunctions.R b/R/CalculationFunctions.R index a69a9a71..21bb55bd 100644 --- a/R/CalculationFunctions.R +++ b/R/CalculationFunctions.R @@ -151,96 +151,71 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de } else { row_names <- colnames(model$M_m) } - # Calculate Final perspective results + + ## Description of result components apply to both FINAL and DIRECT perspectives + # r1 - Domestic emissions from domestic production + # r2 - Emissions from imported goods consumed as intermediate products + # r3 - Emissions from imported goods consumed as final products + if(perspective == "FINAL") { # Calculate Final Perspective LCI (a matrix with total impacts in form of sector x flows) - logging::loginfo("Calculating Final Perspective LCI with external import factors...") - + logging::loginfo("Calculating Final Perspective LCI and LCIA with external import factors...") + subscript <- "f" r1 <- model$B %*% model$L_d %*% diag(as.vector(y_d)) r2 <- model$M_m %*% model$A_m %*% model$L_d %*% diag(as.vector(y_d)) - r3 <- model$M_m %*% diag(as.vector(y_m)) - - if (use_domestic_requirements) { - # zero out the import results - r2[] <- 0 - r3[] <- 0 - } - - if(show_RoW) { - if(model$specs$IODataSource=="stateior") { - # collapse third term for SoI and RoUS - z <- r3[, 1:sector_count] + r3[, (sector_count+1):(sector_count*2)] - # rowSums(z) == rowSums(r3) - r3 <- z - } - result$LCI_f <- cbind(r1 + r2, r3) # Term 3 is assigned to RoW - } else { - result$LCI_f <- r1 + r2 + r3 # All three terms combined and regions do not change - } - - # Calculate Final Perspective LCIA (matrix with direct impacts in form of sector x impacts) - logging::loginfo("Calculating Final Perspective LCIA with external import factors...") - result$LCIA_f <- model$C %*% result$LCI_f - result$LCI_f <- t(result$LCI_f) - result$LCIA_f <- t(result$LCIA_f) - - colnames(result$LCI_f) <- rownames(model$M_m) - rownames(result$LCI_f) <- row_names - colnames(result$LCIA_f) <- rownames(model$D) - rownames(result$LCIA_f) <- row_names - - # Add household emissions to results if applicable - if(household_emissions) { - result$LCI_f <- rbind(result$LCI_f, hh) - result$LCIA_f <- rbind(result$LCIA_f, hh_lcia) - } - + } else { # Calculate direct perspective results. # Calculate Direct Perspective LCI (a matrix with total impacts in form of sector x flows) - logging::loginfo("Calculating Direct + Imported Perspective LCI with external import factors...") + logging::loginfo("Calculating Direct + Imported Perspective LCI and LCIA with external import factors...") + subscript <- "d" s <- getScalingVector(model$L_d, y_d) + r1 <- t(calculateDirectPerspectiveLCI(model$B, s)) + r2 <- t(calculateDirectPerspectiveLCI(model$M_m, (model$A_m %*% model$L_d %*% y_d))) + } + r3 <- model$M_m %*% diag(as.vector(y_m)) - r1 <- calculateDirectPerspectiveLCI(model$B, s) # Domestic emissions from domestic production - r2 <- calculateDirectPerspectiveLCI(model$M_m, (model$A_m %*% model$L_d %*% y_d)) # Emissions from imported goods consumed as intermediate products - r3 <- t(model$M_m %*% diag(as.vector(y_m))) # Emissions from imported goods consumed as final products - - if (use_domestic_requirements) { - # zero out the import results - r2[] <- 0 - r3[] <- 0 - } + if (use_domestic_requirements) { + # zero out the import results + r2[] <- 0 + r3[] <- 0 + } - if(show_RoW) { - if(model$specs$IODataSource=="stateior") { + if(show_RoW) { + if(model$specs$IODataSource=="stateior") { + # collapse third term for SoI and RoUS + r3 <- r3[, 1:sector_count] + r3[, (sector_count+1):(sector_count*2)] + + if(perspective == "DIRECT") { # collapse second and third term for SoI and RoUS - z <- r3[1:sector_count, ] + r3[(sector_count+1):(sector_count*2), ] - # colSums(z) == colSums(r3) - r3 <- z - z <- r2[1:sector_count, ] + r2[(sector_count+1):(sector_count*2), ] - # colSums(z) == colSums(r2) - r2 <- z + r2 <- r2[, 1:sector_count] + r2[, (sector_count+1):(sector_count*2)] } - result$LCI_d <- rbind(r1, r2 + r3) # Term 2 and Term 3 are assigned to RoW + } + if(perspective == "DIRECT") { + LCI <- cbind(r1, r2 + r3) # Term 2 and Term 3 are assigned to RoW } else { - result$LCI_d <- r1 + r2 + r3 # All three terms combined and regions do not change - } - # Calculate Direct Perspective LCIA (matrix with direct impacts in form of sector x impacts) - logging::loginfo("Calculating Direct Perspective LCIA with external import factors...") - result$LCIA_d <- model$C %*% t(result$LCI_d) - result$LCIA_d <- t(result$LCIA_d) - - colnames(result$LCI_d) <- rownames(model$M_m) - rownames(result$LCI_d) <- row_names - colnames(result$LCIA_d) <- rownames(model$D) - rownames(result$LCIA_d) <- row_names - - # Add household emissions to results if applicable - if(household_emissions) { - result$LCI_d <- rbind(result$LCI_d, hh) - result$LCIA_d <- rbind(result$LCIA_d, hh_lcia) + LCI <- cbind(r1 + r2, r3) # Term 3 is assigned to RoW } + } else { + LCI <- r1 + r2 + r3 # All three terms combined and regions do not change } - + + # Calculate LCIA (matrix with direct impacts in form of sector x impacts) + LCIA <- model$C %*% LCI + LCI <- t(LCI) + LCIA <- t(LCIA) + + colnames(LCI) <- rownames(model$M_m) + rownames(LCI) <- row_names + colnames(LCIA) <- rownames(model$D) + rownames(LCIA) <- row_names + + # Add household emissions to results if applicable + if(household_emissions) { + LCI <- rbind(LCI, hh) + LCIA <- rbind(LCIA, hh_lcia) + } + result[[paste0("LCI_", subscript)]] <- LCI + result[[paste0("LCIA_", subscript)]] <- LCIA return(result) } From 27ad877044e9f75e1ab81d8bfbca6fa9afb7bc94 Mon Sep 17 00:00:00 2001 From: Ben Young Date: Fri, 16 Aug 2024 12:37:48 -0400 Subject: [PATCH 09/10] move household emissions calc to end --- R/CalculationFunctions.R | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/R/CalculationFunctions.R b/R/CalculationFunctions.R index 21bb55bd..b7baf4b5 100644 --- a/R/CalculationFunctions.R +++ b/R/CalculationFunctions.R @@ -133,12 +133,7 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de result <- list() y_d <- prepareDemandVectorForStandardResults(model, demand, location = location, use_domestic_requirements = TRUE) y_m <- prepareDemandVectorForImportResults(model, demand, location = location) - - if(household_emissions) { - hh <- calculateHouseholdEmissions(model, f=(y_d + y_m), location, characterized=FALSE) - hh_lcia <- calculateHouseholdEmissions(model, f=(y_d + y_m), location, characterized=TRUE) - } - + if(show_RoW) { if(model$specs$IODataSource=="stateior") { sector_count <- nrow(y_d)/2 @@ -211,6 +206,8 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de # Add household emissions to results if applicable if(household_emissions) { + hh <- calculateHouseholdEmissions(model, f=(y_d + y_m), location, characterized=FALSE) + hh_lcia <- calculateHouseholdEmissions(model, f=(y_d + y_m), location, characterized=TRUE) LCI <- rbind(LCI, hh) LCIA <- rbind(LCIA, hh_lcia) } From 9b7e2cd49805cff13398ce2d9cb68a3006265add Mon Sep 17 00:00:00 2001 From: Ben Young Date: Fri, 16 Aug 2024 16:01:44 -0400 Subject: [PATCH 10/10] add row for RoW to household emissions result #312 --- R/CalculationFunctions.R | 19 ++++++++++++++----- man/calculateHouseholdEmissions.Rd | 11 ++++++++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/R/CalculationFunctions.R b/R/CalculationFunctions.R index b7baf4b5..0ed65e13 100644 --- a/R/CalculationFunctions.R +++ b/R/CalculationFunctions.R @@ -206,8 +206,8 @@ calculateResultsWithExternalFactors <- function(model, perspective = "FINAL", de # Add household emissions to results if applicable if(household_emissions) { - hh <- calculateHouseholdEmissions(model, f=(y_d + y_m), location, characterized=FALSE) - hh_lcia <- calculateHouseholdEmissions(model, f=(y_d + y_m), location, characterized=TRUE) + hh <- calculateHouseholdEmissions(model, f=(y_d + y_m), location, characterized=FALSE, show_RoW=show_RoW) + hh_lcia <- calculateHouseholdEmissions(model, f=(y_d + y_m), location, characterized=TRUE, show_RoW=show_RoW) LCI <- rbind(LCI, hh) LCIA <- rbind(LCIA, hh_lcia) } @@ -527,8 +527,10 @@ calculateMarginSectorImpacts <- function(model) { #' numeric values in USD with the same dollar year as model. #' @param location, str optional location code for demand vector, required for two-region models #' @param characterized, bool, TRUE to characterize using C matrix, FALSE to show LCI +#' @param show_RoW, bool, if TRUE, include rows for commodities in RoW, e.g. `111CA/RoW` in result objects. +#' Only valid currently for models with ExternalImportFactors. #' @return A result vector with rows for final demand sector(s) -calculateHouseholdEmissions <- function(model, f, location, characterized=FALSE) { +calculateHouseholdEmissions <- function(model, f, location, characterized=FALSE, show_RoW=FALSE) { if(!"B_h" %in% names(model)) { logging::logwarn("Household emissions not found in this model") return(NULL) @@ -550,12 +552,19 @@ calculateHouseholdEmissions <- function(model, f, location, characterized=FALSE) } rownames(hh) <- codes + # Create a matrix of 0 values for potential addition to household emissions matrix + mat <- matrix(0, nrow=nrow(hh), ncol=ncol(hh)) + if(!is.null(location)) { - # add in 0 values for 2nd location for household emissions - mat <- matrix(0, nrow=nrow(hh), ncol=ncol(hh)) + # add in 0 values for RoUS rownames(mat) <- other_code hh <- rbind(hh, mat) } + if(show_RoW) { + # add in 0 values for RoW + rownames(mat) <- gsub("/.*", "/RoW", codes) + hh <- rbind(hh, mat) + } return(hh) } diff --git a/man/calculateHouseholdEmissions.Rd b/man/calculateHouseholdEmissions.Rd index 3794956e..9a64adae 100644 --- a/man/calculateHouseholdEmissions.Rd +++ b/man/calculateHouseholdEmissions.Rd @@ -4,7 +4,13 @@ \alias{calculateHouseholdEmissions} \title{Calculate household emissions from B_h} \usage{ -calculateHouseholdEmissions(model, f, location, characterized = FALSE) +calculateHouseholdEmissions( + model, + f, + location, + characterized = FALSE, + show_RoW = FALSE +) } \arguments{ \item{model}{A complete EEIO model: a list with USEEIO model components and attributes.} @@ -15,6 +21,9 @@ numeric values in USD with the same dollar year as model.} \item{location, }{str optional location code for demand vector, required for two-region models} \item{characterized, }{bool, TRUE to characterize using C matrix, FALSE to show LCI} + +\item{show_RoW, }{bool, if TRUE, include rows for commodities in RoW, e.g. `111CA/RoW` in result objects. +Only valid currently for models with ExternalImportFactors.} } \value{ A result vector with rows for final demand sector(s)