8000 add function to support custom demand vectors by bl-young · Pull Request #174 · USEPA/useeior · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add function to support custom demand vectors #174

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 5 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export(compareCommodityOutputandDomesticUseplusProductionDemand)
export(compareEandLCIResult)
export(compareFlowTotals)
export(compareOutputandLeontiefXDemand)
export(extractAndFormatDemandVector)
export(generateModelIdentifier)
export(heatmapSatelliteTableCoverage)
export(heatmapSectorRanking)
Expand Down
20 changes: 20 additions & 0 deletions R/DemandFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,23 @@ formatDemandVector <- function(dv,L) {
d[match(names(dv),names(d))] <- dv
return(d)
}

#' Read demand vector from a csv file and format for use in model calculations
#' @param file_path str, path to csv file containing demand data
#' @param demand_name str, name of demand data as field header
#' @param model, a model
#' @return a demand vector formatted for use in calculating model results
#' @export
extractAndFormatDemandVector <- function(file_path, demand_name, model){
demand_df <- read.csv(file_path, stringsAsFactors = FALSE)
row.names(demand_df) <- demand_df$Code
demand_df$Code <- NULL
y <- na.omit(demand_df[,demand_name])
names(y) <- row.names(demand_df)
if(isDemandVectorValid(y, model$L)) {
y <- formatDemandVector(y, model$L)
} else {
stop("Format of the demand vecto 10000 r is invalid.")
}
return(y)
}
5 changes: 5 additions & 0 deletions examples/USEEIO2s-GHG.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ model <- buildModel("USEEIOv2.0s-GHG")
# result using total US production
result <- calculateEEIOModel(model, perspective = "DIRECT", demand = "Production")

# Calculate a sector by sector result matrix for a specified indicator based on one of the demand
# vectors included in the model
y <- model[["DemandVectors"]][["vectors"]][["2012_US_Consumption_Complete"]]
impact_result <- calculateConsumptionContributiontoImpact(y, model, indicator = "Greenhouse Gases")

# Adjust N matrix (direct + indirect impacts per dollar) to 2018 dollar and purchaser price
N_adj <- adjustResultMatrixPrice("N", currency_year = 2012, purchaser_price = FALSE, model)

34 changes: 17 additions & 17 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 @@ -167,18 +167,18 @@ The Indicators object contains meta and factors dataframes.
The DemandVector object contains the demand vectors and a metadata table.

#### vectors
A data table of the demand vectors included in the model
A data table of the demand vectors included in the model. Each vector is in the format of a named vector with names assigned in [Code_Loc](#sector-meta) (e.g. `1111A0/US`) format.

#### meta
A table of metadata for the demand vectors included in the model.

| 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
21 changes: 21 additions & 0 deletions man/extractAndFormatDemandVector.Rd

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

0