8000 Add more model demand vectors & refactor validation function for industry models by MoLi7 · Pull Request #176 · USEPA/useeior · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add more model demand vectors & refactor validation function for industry models #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

8000
Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions R/DemandFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
#'Registry of functions that construct various demand vector in the form of as a named list with nested names
#'as keys and function name as values
DemandVectorFunctionRegistry <- list()
DemandVectorFunctionRegistry$Consumption$Complete <- "prepareConsumptionDemand"
# Production
DemandVectorFunctionRegistry$Production$Complete <- "prepareProductionDemand"
DemandVectorFunctionRegistry$Consumption$Household <- "prepareHouseholdDemand"
DemandVectorFunctionRegistry$Production$Domestic <- "prepareDomesticProductionDemand"
# Consumption
DemandVectorFunctionRegistry$Consumption$Complete <- "prepareConsumptionDemand"
DemandVectorFunctionRegistry$Consumption$Domestic <- "prepareDomesticConsumptionDemand"
DemandVectorFunctionRegistry$Consumption$Household <- "prepareHouseholdDemand"
# Import
DemandVectorFunctionRegistry$Import$Complete <- "prepareImportDemand"

#'Sums across sectors for a given set of codes/cols in a given final demand df
#'@param Y, a model Demand df
Expand Down Expand Up @@ -40,10 +45,24 @@ sumforConsumption <- function(model, Y) {
#'@param model, a model
#'@return A named vector with demand
prepareProductionDemand <- function(model) {
y_dc <- sumforConsumption(model, model$DomesticFinalDemand)
y_dc <- sumforConsumption(model, model$FinalDemand)
export_code <- model$FinalDemandMeta[model$FinalDemandMeta$Group=="Export", "Code_Loc"]
y_e <- sumDemandCols(model$FinalDemand, export_code)
changeinventories_code <- model$FinalDemandMeta[model$FinalDemandMeta$Group=="ChangeInventories", "Code_Loc"]
y_d_delta <- sumDemandCols(model$FinalDemand, changeinventories_code)
y_p <- y_dc + y_e + y_d_delta
return(y_p)
}

#'Prepares a demand vector representing domestic production
#'Formula for production vector: y_p <- y_dc + y_e + y_d_delta
#'@param model, a model
#'@return A named vector with demand
prepareDomesticProductionDemand <- function(model) {
y_dc <- sumforConsumption(model, model$DomesticFinalDemand)
export_code <- model$FinalDemandMeta[model$FinalDemandMeta$Group=="Export", "Code_Loc"]
y_e <- sumDemandCols(model$DomesticFinalDemand, export_code)
changeinventories_code <- model$FinalDemandMeta[model$FinalDemandMeta$Group=="ChangeInventories", "Code_Loc"]
y_d_delta <- sumDemandCols(model$DomesticFinalDemand, changeinventories_code)
y_p <- y_dc + y_e + y_d_delta
return(y_p)
Expand Down Expand Up @@ -75,6 +94,16 @@ prepareHouseholdDemand <- function(model) {
return(y_h)
}

#'Prepares a demand vector representing imports
#'@param model, a model
#'@return A named vector with demand
prepareImportDemand <- function(model) {
Y <- model$FinalDemand
import_code <- model$FinalDemandMeta[model$FinalDemandMeta$Group=="Import", "Code_Loc"]
y_i <- sumDemandCols(Y, import_code)
return(y_i)
}

#'A function to validate a user provided demand vector
#' @param dv a user provided demand vector
#' @param L, the L matrix for the given model, used as a reference
Expand Down
83 changes: 57 additions & 26 deletions R/ValidateModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,7 @@
#'@return A list with pass/fail validation result and the cell-by-cell relative diff matrix
#'@export
compareEandLCIResult <- function(model, use_domestic = FALSE, tolerance = 0.05) {
#Use L and FinalDemand unless use_domestic, in which case use L_d and DomesticFinalDemand
#c = diag(L%*%y)
if (use_domestic) {
f <- model$U_d[model$Commodities$Code_Loc, model$FinalDemandMeta$Code_Loc]
y <- as.matrix(formatDemandVector(rowSums(f), model$L_d))
c <- getScalingVector(model$L_d, y)
} else {
f <- model$U[model$Commodities$Code_Loc, model$FinalDemandMeta$Code_Loc]
y <- as.matrix(formatDemandVector(rowSums(f), model$L))
c <- getScalingVector(model$L, y)
}

# Prepare left side of the equation
CbS_cast <- standardizeandcastSatelliteTable(model$CbS,model)
B <- as.matrix(CbS_cast)
Chi <- generateChiMatrix(model, "Industry")
Expand All @@ -28,25 +17,49 @@ compareEandLCIResult <- function(model, use_domestic = FALSE, tolerance = 0.05)
}
B_chi <- B*Chi

##Prepare left side of the equation
# Generate E
E <- prepareEfromtbs(model)
E <- as.matrix(E[rownames(B), colnames(B)])

# Adjust E and B_chi if model is commodity-based
# Calculate scaling factor c
if (model$specs$CommodityorIndustryType=="Commodity") {
#transform E with commodity mix to put in commodity form
E <- t(model$C_m %*% t(E))
E <- t(model$C_m %*% t(E))
#Need to transform B_Chi to be in commodity form
B_chi <- B_chi %*% model$V_n
#Use L and FinalDemand unless use_domestic, in which case use L_d and DomesticFinalDemand
if (use_domestic) {
f <- model$U_d[model$Commodities$Code_Loc, model$FinalDemandMeta$Code_Loc]
y <- as.matrix(formatDemandVector(rowSums(f), model$L_d))
c <- getScalingVector(model$L_d, y)
} else {
f <- model$U[model$Commodities$Code_Loc, model$FinalDemandMeta$Code_Loc]
y <- as.matrix(formatDemandVector(rowSums(f), model$L))
c <- getScalingVector(model$L, y)
}
} else {
if (use_domestic) {
f <- model$DemandVectors$vectors$`2012_US_Production_Domestic`
y <- as.matrix(formatDemandVector(f, model$L_d))
c <- getScalingVector(model$L_d, y)
} else {
f <- model$DemandVectors$vectors$`2012_US_Production_Complete` + model$DemandVectors$vectors$`2012_US_Import_Complete`
y <- as.matrix(formatDemandVector(f, model$L))
c <- getScalingVector(model$L, y)
}
}

#LCI = B dot Chi %*% c
#LCI = B dot Chi %*% c
LCI <- t(calculateDirectPerspectiveLCI(B_chi, c))

# Calculate relative differences
rel_diff <- (LCI - E)/E

# Generate Pass/Fail comparison results
validation <- formatValidationResult(rel_diff, abs_diff = TRUE, tolerance)
# Add LCI and E to validation list
validation <- c(list("LCI" = LCI, "E" = E), validation)
return(validation)
}

Expand All @@ -59,21 +72,31 @@ compareEandLCIResult <- function(model, use_domestic = FALSE, tolerance = 0.05)
#'@return A list with pass/fail validation result and the cell-by-cell relative diff matrix
#'@export
compareOutputandLeontiefXDemand <- function(model, use_domestic=FALSE, tolerance=0.05) {
if (use_domestic) {
f <- model$U_d[model$Commodities$Code_Loc, model$FinalDemandMeta$Code_Loc]
y <- as.matrix(formatDemandVector(rowSums(f), model$L_d))
c <- getScalingVector(model$L_d, y)
} else {
f <- model$U[model$Commodities$Code_Loc, model$FinalDemandMeta$Code_Loc]
y <- as.matrix(formatDemandVector(rowSums(f), model$L))
c <- getScalingVector(model$L, y)
}

# Generate output and scaling vector
if(model$specs$CommodityorIndustryType == "Commodity") {
#determine if output to compare is commodity or industry
x <- model$q

if (use_domestic) {
f <- model$U_d[model$Commodities$Code_Loc, model$FinalDemandMeta$Code_Loc]
y <- as.matrix(formatDemandVector(rowSums(f), model$L_d))
c <- getScalingVector(model$L_d, y)
} else {
f <- model$U[model$Commodities$Code_Loc, model$FinalDemandMeta$Code_Loc]
y <- as.matrix(formatDemandVector(rowSums(f), model$L))
c <- getScalingVector(model$L, y)
}
} else {
x <- model$x

if (use_domestic) {
f <- model$DemandVectors$vectors$`2012_US_Production_Domestic`
y <- as.matrix(formatDemandVector(f, model$L_d))
c <- getScalingVector(model$L_d, y)
} else {
f <- model$DemandVectors$vectors$`2012_US_Production_Complete` + model$DemandVectors$vectors$`2012_US_Import_Complete`
y <- as.matrix(formatDemandVector(f, model$L))
c <- getScalingVector(model$L, y)
}
}
# Row names should be identical
if (!identical(rownames(c), names(x))) {
Expand All @@ -84,6 +107,8 @@ compareOutputandLeontiefXDemand <- function(model, use_domestic=FALSE, tolerance

# Generate Pass/Fail comparison results
validation <- formatValidationResult(rel_diff, abs_diff = TRUE, tolerance)
# Add c and x to validation list
validation <- c(list("c" = c, "x" = x), validation)
return(validation)
}

Expand All @@ -107,6 +132,8 @@ compareCommodityOutputandDomesticUseplusProductionDemand <- function(model, tole

# Generate Pass/Fail comparison results
validation <- formatValidationResult(rel_diff, abs_diff = TRUE, tolerance)
# Add q and x to validation list
validation <- c(list("q" = q, "x" = x), validation)
return(validation)
}

Expand All @@ -131,6 +158,8 @@ compareCommodityOutputXMarketShareandIndustryOutputwithCPITransformation <- func

# Generate Pass/Fail comparison results
validation <- formatValidationResult(rel_diff, abs_diff = TRUE, tolerance)
# Add q and x to validation list
validation <- c(list("q" = q, "x" = x), validation)
return(validation)
}

Expand Down Expand Up @@ -195,6 +224,8 @@ compareIndustryOutputinMakeandUse <- function(model) {

# Generate Pass/Fail comparison results
validation <- formatValidationResult(rel_diff, abs_diff = TRUE, tolerance)
# Add x_use and x_make to validation list
validation <- c(list("x_use" = x_use, "x_make" = x_make), validation)
return(validation)
}

Expand Down
32 changes: 16 additions & 16 deletions format_specs/Model.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ A fully constructed USEEIO model is an R named list that contains the following

## Notes

A _sector_ is either a commodity or industry, depending on the [model CommodityorIndustry Type](https://github.com/USEPA/useeior/blob/master/format_specs/ModelSpecifications.md#model-specifications). The _sector_ will be synononmous for that same CommodityorIndustryType for all tables in a given model in which _sector_ is used.
A _sector_ is either a commodity or industry, depending on the [model CommodityorIndustry Type](https://github.com/USEPA/useeior/blob/master/format_specs/ModelSpecification.md#model-specifications). The _sector_ will be synononmous for that same CommodityorIndustryType for all tables in a given model in which _sector_ is used.

Unless another year is specifically called out, all economic values are given in US dollars (USD) in the value the [model IOyear](https://github.com/USEPA/useeior/blob/master/format_specs/ModelSpecifications.md#model-specifications).
Unless another year is specifically called out, all economic values are given in US dollars (USD) in the value the [model IOyear](https://github.com/USEPA/useeior/blob/master/format_specs/ModelSpecification.md#model-specifications).

## Model
Items are listed in the order in which they appear in a built Model object in R.

| Item | Data Structure | Category | Description |
| --- | --- | --------- | ------ |
| specs | list | metadata | [Model specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md) |
| specs | list | metadata | [Model specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md) |
| crosswalk | data.frame | metadata | Sector [crosswalk](#crosswalk) |
| Commodities | data.frame | metadata | Commodity metadata in [sector meta format](#sector-meta) |
| Industries | data.frame | metadata | Industry metadata in [sector meta format](#sector-meta) |
Expand Down Expand Up @@ -144,19 +144,19 @@ The Indicators object contains meta and factors dataframes.

| Item | Type | Description |
| --- | --- | --------- |
| Name | str | [Indicator Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md#indicator-specifications) |
| Code | str | [Indicator Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md#indicator-specifications) |
| Group | str | [Indicator Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md#indicator-specifications) |
| Unit | str | [Indicator Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md#indicator-specifications) |
| SimpleUnit | str | [Indicator Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md#indicator-specifications) |
| SimpleName | str | [Indicator Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md#indicator-specifications) |
| Name | str | [Indicator Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md#indicator-specifications) |
| Code | str | [Indicator Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md#indicator-specifications) |
| Group | str | [Indicator Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md#indicator-specifications) |
| Unit | str | [Indicator Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md#indicator-specifications) |
| SimpleUnit | str | [Indicator Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md#indicator-specifications) |
| SimpleName | str | [Indicator Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md#indicator-specifications) |

#### factors
A data table of the characterization factors for indicators included in the model

| Item | Type | Description |
| --- | --- | --------- |
| Indicator | str | Matches the [Name](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md#indicator-specifications) of the indicator |
| Indicator | str | Matches the [Name](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md#indicator-specifications) of the indicator |
| Flowable | str | [Federal Elementary Flow List](https://github.com/USEPA/Federal-LCA-Commons-Elementary-Flow-List/blob/master/format%20specs/FlowList.md) |
| Context | str | [Federal Elementary Flow List](https://github.com/USEPA/Federal-LCA-Commons-Elementary-Flow-List/blob/master/format%20specs/FlowList.md) |
| Unit | str | [Federal Elementary Flow List](https://github.com/USEPA/Federal-LCA-Commons-Elementary-Flow-List/blob/master/format%20specs/FlowList.md) |
Expand All @@ -174,11 +174,11 @@ The DemandVector object contains the demand vectors and a metadata table.

| Item | Type | Description |
| --- | --- | --------- |
| Type | str | [Demand Vector Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md#demand-vector-specifications) |
| Year | int | [Demand Vector Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md#demand-vector-specifications) |
| System | str | [Demand Vector Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md#demand-vector-specifications) |
| Location | str | [Demand Vector Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md#demand-vector-specifications) |
| Name | str | [Demand Vector Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md#demand-vector-specifications) |
| Type | str | [Demand Vector Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md#demand-vector-specifications) |
| Year | int | [Demand Vector Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md#demand-vector-specifications) |
| System | str | [Demand Vector Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md#demand-vector-specifications) |
| Location | str | [Demand Vector Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md#demand-vector-specifications) |
| Name | str | [Demand Vector Specifications](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md#demand-vector-specifications) |
| ID | str | Year_Location_Type_System |

### Model Component Matrices
Expand All @@ -189,7 +189,7 @@ When used in matrix indices, items below take the following format:
| --- | --------- |
| sector (commodity or industry) | [Code_Loc](#sector-meta) (e.g. `1111A0/US`) |
| flow | [Flowable/Context/Unit](#satellite-tables) (e.g. `Carbon dioxide/emission/air/kg`) |
| indicator | [Name](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecifications.md#indicator-specifications) (e.g. `Greenhouse Gases`) |
| indicator | [Name](https://github.com/USEPA/useeior/tree/master/format_specs/ModelSpecification.md#indicator-specifications) (e.g. `Greenhouse Gases`) |


#### Output vectors
Expand Down
18 changes: 14 additions & 4 deletions inst/extdata/modelspecs/USEEIOv2.0is-GHG_nodisagg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,19 @@ Indicators:
Primary: TRUE

DemandVectors:
Consumption:
Type: "Consumption"
Production:
Type: "Production"
Year: 2012
System: "Complete"
Location: "US"
Production:
DomesticProduction:
Type: "Production"
Year: 2012
System: "Domestic"
Location: "US"
Consumption:
Type: "Consumption"
Year: 2012
System: "Complete"
Location: "US"
HouseholdConsumption:
Expand All @@ -74,4 +79,9 @@ DemandVectors:
Type: "Consumption"
Year: 2012
System: "Domestic"
Location: "US"
Location: "US"
Import:
Type: "Import"
Year: 2012
System: "Complete"
Location: "US"
2 changes: 1 addition & 1 deletion man/DemandVectorFunctionRegistry.Rd

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

19 changes: 19 additions & 0 deletions man/prepareDomesticProductionDemand.Rd

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

17 changes: 17 additions & 0 deletions man/prepareImportDemand.Rd

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

0