Skip to content

Commit

Permalink
Merge branch 'issue_291' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	R/ValidateModel.R
#	tests/test_model_build.R
  • Loading branch information
bl-young committed May 17, 2024
2 parents eee9807 + 98b848b commit 9eeb772
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 34 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(aggregateResultMatrix)
export(aggregateResultMatrixbyRow)
export(barplotFloworImpactFractionbyRegion)
export(barplotIndicatorScoresbySector)
export(buildIOModel)
export(buildModel)
export(buildTwoRegionModels)
export(calculateEEIOModel)
Expand Down
17 changes: 12 additions & 5 deletions R/BuildModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,13 @@ buildTwoRegionModels <- function(modelname, configpaths = NULL, validate = FALSE
return(model_ls)
}

#' Build an EIO model with economic components only.
#' Build an IO model with economic components only.
#' @param modelname Name of the model from a config file.
#' @param configpaths str vector, paths (including file name) of model configuration file
#' and optional agg/disagg configuration file(s). If NULL, built-in config files are used.
#' @return A list of EIO model with only economic components
buildEIOModel <- function(modelname, configpaths = NULL) {
#' @return A list of IO model with only economic components
#' @export
buildIOModel <- function(modelname, configpaths = NULL) {
model <- initializeModel(modelname, configpaths)
model <- loadIOData(model, configpaths)
model <- loadDemandVectors(model)
Expand All @@ -356,8 +357,14 @@ buildEIOModel <- function(modelname, configpaths = NULL) {
colnames(model$U_d) <- colnames(model$U)
model[c("U", "U_d")] <- lapply(model[c("U", "U_d")],
function(x) ifelse(is.na(x), 0, x))
model$U_n <- generateDirectRequirementsfromUse(model, domestic = FALSE) #normalized Use
model$U_d_n <- generateDirectRequirementsfromUse(model, domestic = TRUE) #normalized DomesticUse

if (model$specs$IODataSource=="stateior") {
model$U_n <- generate2RDirectRequirementsfromUseWithTrade(model, domestic = FALSE)
model$U_d_n <- generate2RDirectRequirementsfromUseWithTrade(model, domestic = TRUE)
} else {
model$U_n <- generateDirectRequirementsfromUse(model, domestic = FALSE) #normalized Use
model$U_d_n <- generateDirectRequirementsfromUse(model, domestic = TRUE) #normalized DomesticUse
}
model$q <- model$CommodityOutput
model$x <- model$IndustryOutput
model$mu <- model$InternationalTradeAdjustment
Expand Down
5 changes: 5 additions & 0 deletions R/StateiorFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ print2RValidationResults <- function(model) {
# Check that Production demand can be run without errors
cat("\nChecking that production demand vectors do not produce errors for 2-R models.\n")

if(is.null(model$B)) {
# Stop validation as no satellite tables
return()
}

# Creating 2-R Production Complete demand vector
f <- model$DemandVectors$vectors[endsWith(names(model$DemandVectors$vectors), "Production_Complete")][[1]]
f <- (f + model$DemandVectors$vectors[endsWith(names(model$DemandVectors$vectors), "Production_Complete")][[2]])
Expand Down
26 changes: 14 additions & 12 deletions R/ValidateModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -312,18 +312,20 @@ printValidationResults <- function(model) {
print(paste("Number of sectors passing:",econval$N_Pass))
print(paste("Number of sectors failing:",econval$N_Fail))
print(paste("Sectors failing:", paste(unique(econval$Failure$rownames), collapse = ", ")))

print("Validate that flow totals by commodity (E_c) can be recalculated (within 1%) using the model satellite matrix (B), market shares matrix (V_n), total requirements matrix (L), and demand vector (y) for US production")
modelval <- compareEandLCIResult(model, tolerance = 0.01)
print(paste("Number of flow totals by commodity passing:",modelval$N_Pass))
print(paste("Number of flow totals by commodity failing:",modelval$N_Fail))
print(paste("Sectors with flow totals failing:", paste(unique(modelval$Failure$variable), collapse = ", ")))

print("Validate that flow totals by commodity (E_c) can be recalculated (within 1%) using the model satellite matrix (B), market shares matrix (V_n), total domestic requirements matrix (L_d), and demand vector (y) for US production")
dom_val <- compareEandLCIResult(model, use_domestic=TRUE, tolerance = 0.01)
print(paste("Number of flow totals by commodity passing:",dom_val$N_Pass))
print(paste("Number of flow totals by commodity failing:",dom_val$N_Fail))
print(paste("Sectors with flow totals failing:", paste(unique(dom_val$Failure$variable), collapse = ", ")))

if(!is.null(model$B)) {
print("Validate that flow totals by commodity (E_c) can be recalculated (within 1%) using the model satellite matrix (B), market shares matrix (V_n), total requirements matrix (L), and demand vector (y) for US production")
modelval <- compareEandLCIResult(model, tolerance = 0.01)
print(paste("Number of flow totals by commodity passing:",modelval$N_Pass))
print(paste("Number of flow totals by commodity failing:",modelval$N_Fail))
print(paste("Sectors with flow totals failing:", paste(unique(modelval$Failure$variable), collapse = ", ")))

print("Validate that flow totals by commodity (E_c) can be recalculated (within 1%) using the model satellite matrix (B), market shares matrix (V_n), total domestic requirements matrix (L_d), and demand vector (y) for US production")
dom_val <- compareEandLCIResult(model, use_domestic=TRUE, tolerance = 0.01)
print(paste("Number of flow totals by commodity passing:",dom_val$N_Pass))
print(paste("Number of flow totals by commodity failing:",dom_val$N_Fail))
print(paste("Sectors with flow totals failing:", paste(unique(dom_val$Failure$variable), collapse = ", ")))
}

print("Validate that commodity output are properly transformed to industry output via MarketShare")
q_x_val <- compareCommodityOutputXMarketShareandIndustryOutputwithCPITransformation(model, tolerance = 0.01)
Expand Down
27 changes: 16 additions & 11 deletions R/WriteModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ writeModeltoXLSX <- function(model, outputfolder) {
prepareWriteDirs(model, dirs)
writeModelMetadata(model, dirs)
metadata_tabs <- c("demands", "flows", "indicators", "sectors")
if(is.null(model$SatelliteTables)){
metadata_tabs <- metadata_tabs[metadata_tabs != "flows"]
}
if(is.null(model$Indicators)){
metadata_tabs <- metadata_tabs[metadata_tabs != "indicators"]
}
Expand Down Expand Up @@ -269,17 +272,19 @@ writeModelMetadata <- function(model, dirs) {
utils::write.csv(sectors, paste0(dirs$model, "/sectors.csv"), na = "",
row.names = FALSE, fileEncoding = "UTF-8")

# Write flows to csv
flows <- model$SatelliteTables$flows
flows$ID <- apply(flows[, c("Flowable", "Context", "Unit")], 1, FUN = joinStringswithSlashes)
names(flows)[names(flows) == 'FlowUUID'] <- 'UUID'
flows <- flows[order(flows$ID),]
flows$Index <- c(1:nrow(flows)-1)
flows <- flows[, fields$flows]
#checkNamesandOrdering(flows$ID, rownames(model$B),
# "flows in flows.csv and rows in B matrix")
utils::write.csv(flows, paste0(dirs$model, "/flows.csv"), na = "",
row.names = FALSE, fileEncoding = "UTF-8")
if(!is.null(model$SatelliteTables)) {
# Write flows to csv
flows <- model$SatelliteTables$flows
flows$ID <- apply(flows[, c("Flowable", "Context", "Unit")], 1, FUN = joinStringswithSlashes)
names(flows)[names(flows) == 'FlowUUID'] <- 'UUID'
flows <- flows[order(flows$ID),]
flows$Index <- c(1:nrow(flows)-1)
flows <- flows[, fields$flows]
#checkNamesandOrdering(flows$ID, rownames(model$B),
# "flows in flows.csv and rows in B matrix")
utils::write.csv(flows, paste0(dirs$model, "/flows.csv"), na = "",
row.names = FALSE, fileEncoding = "UTF-8")
}

# Write years to csv
years <- data.frame(ID=colnames(model$Rho), stringsAsFactors = FALSE)
Expand Down
12 changes: 6 additions & 6 deletions man/buildEIOModel.Rd → man/buildIOModel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/test_model_build.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ m <- "USEEIOv2.0.1-411"
model <- buildModel(m)
printValidationResults(model)

## USEEIOv2.0.1-411 Detail model with waste disaggregation (Economic only)
m <- "USEEIOv2.0.1-411"
model <- buildIOModel(m)
printValidationResults(model)
writeModeltoXLSX(model, ".")

## USEEIOv2.0.1-i-411 Detail, industry model with waste disaggregation
model <- useeior:::initializeModel(m)
model$specs$Model <- "USEEIOv2.0.1-i-411"
Expand Down Expand Up @@ -135,6 +141,11 @@ model <- buildModel(m, configpaths = file.path(cfg))
printValidationResults(model)
writeModeltoXLSX(model, ".")

## StateEEIOv1.0 Two-region Summary model (Economic only)
model <- buildIOModel(m, configpaths = file.path(cfg))
printValidationResults(model)
writeModeltoXLSX(model, ".")

## StateEEIOv1.0 Two-region Summary model with Import Factors
cfg <- c(paste0("modelspecs/", m, ".yml"),
"import_factors_summary_2019.csv"
Expand Down Expand Up @@ -175,3 +186,4 @@ printValidationResults(model)
# cfg <- paste0("modelspecs/", m, ".yml")
# modelProxy <- buildModel(m, configpaths = file.path(cfg))
# printValidationResults(model)

0 comments on commit 9eeb772

Please sign in to comment.