Skip to content

Commit

Permalink
Merge pull request #19 from USEPA/ver-0.2.0
Browse files Browse the repository at this point in the history
Prepare v0.2.0 release
  • Loading branch information
MoLi7 authored Aug 18, 2022
2 parents bfe84c4 + 3e88f1c commit 3bffa79
Show file tree
Hide file tree
Showing 39 changed files with 2,401 additions and 8,441 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/render-model-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
- uses: actions/checkout@v2
with:
fetch-depth: 0
ref: dev
ref: 'ver-0.2.0'
# - run: git checkout HEAD^

- uses: r-lib/actions/setup-pandoc@v2
Expand All @@ -69,17 +69,17 @@ jobs:
- name: Render model validation Rmarkdown file
run: |
install.packages("rmarkdown")
for (year in 2012:2017) {
for (year in 2018:2020) {
rmarkdown::render("inst/doc/ValidateModel.Rmd", output_dir = "inst/doc/output", output_file = paste0("ValidateModel_", year, ".md"))
}
shell: Rscript {0}

- name: Commit model validation results
run: |
git fetch origin dev
git pull origin dev
git fetch origin ver-0.2.0
git pull origin ver-0.2.0
git config user.name "GitHub Actions Bot"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add inst/doc/output/ValidateModel*.md
git commit -m "Update model validation documents"
git push origin dev
git push origin ver-0.2.0
38 changes: 21 additions & 17 deletions .github/workflows/save-raw-data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@ jobs:
generate-save-data:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.script-run.script }}, ${{ matrix.script-run.year }} (${{ matrix.config.os}}, ${{ matrix.config.r}})
name: ${{ matrix.script-run }}, ${{ matrix.data-year }} (${{ matrix.config.os}}, ${{ matrix.config.r}})

strategy:
fail-fast: false
matrix:
config:
- {os: ubuntu-latest, r: 'release'}
script-run:
- {script: data-raw/BEAData.R}
- {script: data-raw/CensusData.R}
- {script: data-raw/EIAData.R}
- {script: data-raw/FAFData.R}
- {script: data-raw/USASpendingData.R, year: 2012}
- {script: data-raw/USASpendingData.R, year: 2013}
- {script: data-raw/USASpendingData.R, year: 2014}
- {script: data-raw/USASpendingData.R, year: 2015}
- {script: data-raw/USASpendingData.R, year: 2016}
- {script: data-raw/USASpendingData.R, year: 2017}
- data-raw/BEAData.R
- data-raw/CensusData.R
- data-raw/EIAData.R
- data-raw/FAFData.R
- data-raw/USASpendingData.R
data-year:
- 2012
- 2013
- 2014
- 2015
- 2016
- 2017
- 2018
- 2019
- 2020


# env:
# R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
Expand Down Expand Up @@ -53,15 +59,13 @@ jobs:
sudo rm -rf "/usr/local/share/boost"
sudo rm -rf "$AGENT_TOOLSDIRECTORY"
# Load package
- name: Load stateior
run: Rscript -e 'devtools::load_all'

# Generate data and save as .rds files
- name: Generate data and save as .rds files (metadata saved as .json files)
run: |
year <- ${{ matrix.script-run.year }}
source("${{ matrix.script-run.script }}")
devtools::load_all()
year <- ${{ matrix.data-year }}
options(timeout = 1000)
source("${{ matrix.script-run }}")
shell: Rscript {0}

# Upload .rds files
Expand Down
19 changes: 16 additions & 3 deletions .github/workflows/save-state-supply-model-data.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This workflow is designed to be manually triggered only.
name: Save state supply model data as .rds
name: Save state supply model data as .rds in parallel runs

on:
workflow_dispatch: # allow manual trigger
Expand All @@ -8,13 +8,25 @@ jobs:
generate-save-data:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os}} (${{ matrix.config.r}})
name: ${{ matrix.script-run }}, ${{ matrix.data-year }} (${{ matrix.config.os}}, ${{ matrix.config.r}})

strategy:
fail-fast: false
matrix:
config:
- {os: ubuntu-latest, r: 'release'}
script-run:
- data-raw/StateSupplyModel.R
data-year:
- 2012
- 2013
- 2014
- 2015
- 2016
- 2017
- 2018
- 2019
- 2020

# env:
# R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
Expand Down Expand Up @@ -46,7 +58,8 @@ jobs:
- name: Load stateior, generate and save state supply IO data
run: |
devtools::load_all()
source("data-raw/StateSupplyModel.R")
year <- ${{ matrix.data-year }}
source("${{ matrix.script-run }}")
shell: Rscript {0}

# Upload .rds files
Expand Down
19 changes: 16 additions & 3 deletions .github/workflows/save-state-use-model-data.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This workflow is designed to be manually triggered only.
name: Save state use model data as .rds
name: Save state use model data as .rds in parallel runs

on:
workflow_dispatch: # allow manual trigger
Expand All @@ -8,13 +8,25 @@ jobs:
generate-save-data:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os}} (${{ matrix.config.r}})
name: ${{ matrix.script-run }}, ${{ matrix.data-year }} (${{ matrix.config.os}}, ${{ matrix.config.r}})

strategy:
fail-fast: false
matrix:
config:
- {os: ubuntu-latest, r: 'release'}
script-run:
- data-raw/StateUseModel.R
data-year:
- 2012
- 2013
- 2014
- 2015
- 2016
- 2017
- 2018
- 2019
- 2020

# env:
# R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
Expand Down Expand Up @@ -46,7 +58,8 @@ jobs:
- name: Load stateior, generate and save state use IO data
run: |
devtools::load_all()
source("data-raw/StateUseModel.R")
year <- ${{ matrix.data-year }}
source("${{ matrix.script-run }}")
shell: Rscript {0}

# Upload .rds files
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/save-two-region-model-data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ jobs:
- 2015
- 2016
- 2017
# - 2018
# - 2019
# - 2020
- 2018
- 2019
- 2020

# env:
# R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: stateior
Type: Package
Title: US State Input-Output (stateio) R modeling software
Version: 0.1.0
Date: 2022-07-29
Version: 0.2.0
Date: 2022-05-17
Authors@R: c(
person("Mo", "Li", email="[email protected]", role="aut"),
person("Joao-Pedro", "Ferreira", email="[email protected]", role= c("aut")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export(buildTwoRegionUseModel)
export(loadStateIODataFile)
export(plotICFandRPC)
export(plotResidual)
export(writeStateIODatatoCSV)
import(ggplot2)
importFrom(magrittr,"%>%")
50 changes: 34 additions & 16 deletions R/BuildModel.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Define model version
model_ver <- utils::packageDescription(pkg = "stateior", fields = "Version")

#' Build a state supply model for all 52 states/regions (including DC and Overseas)
#' for a given year
#' @description Build a state supply model for all 52 states/regions
Expand Down Expand Up @@ -194,7 +197,8 @@ buildStateUseModel <- function(year) {
US_ITA <- generateInternationalTradeAdjustmentVector("Summary", year)
# Calculate state ITA by allocating US ITA via state/US COR (commodity output ratio)
CommodityOutput <- loadStateIODataFile(paste0("State_Summary_CommodityOutput_",
year))
year),
ver = model_ver)
for (state in states) {
cor <- CommodityOutput[[state]] / rowSums(US_Use[commodities, c(industries, FD_cols)])
model[["Use"]][[state]][commodities, "F051"] <- US_ITA*cor
Expand Down Expand Up @@ -301,13 +305,15 @@ buildTwoRegionUseModel <- function(state, year, ioschema, iolevel,
SoI_DomesticUse <- loadStateIODataFile(paste0("State_",
iolevel,
"_DomesticUse_",
year))[[state]][commodities, ]
year),
ver = model_ver)[[state]][commodities, ]
# Load state commodity output
logging::loginfo("Loading state commodity output...")
SoI_CommodityOutput <- loadStateIODataFile(paste0("State_",
iolevel,
"_CommodityOutput_",
year))[[state]]
year),
ver = model_ver)[[state]]

# 2 - Generate 2-region ICFs
logging::loginfo("Generating two-region interregional commodity flow (ICF) ratios...")
Expand Down Expand Up @@ -494,7 +500,8 @@ buildTwoRegionUseModel <- function(state, year, ioschema, iolevel,
if (!domestic) {
# Load US and SoI Use, calcuate RoUS_Use
US_Use <- getNationalUse("Summary", year)
SoI_Use <- loadStateIODataFile(paste0("State_", iolevel, "_Use_", year))[[state]]
SoI_Use <- loadStateIODataFile(paste0("State_", iolevel, "_Use_", year),
ver = model_ver)[[state]]
RoUS_Use <- US_Use - SoI_Use[commodities, c(industries, FD_cols)]
# Calculate SoI_Import and RoUS_Import
SoI_Import <- SoI_Use[commodities, c(industries, FD_cols)] - SoI_DomesticUse[commodities, c(industries, FD_cols)]
Expand Down Expand Up @@ -582,11 +589,13 @@ assembleTwoRegionIO <- function(year, iolevel) {
State_IndustryOutput_ls <- loadStateIODataFile(paste0("State_",
iolevel,
"_IndustryOutput_",
year))
year),
ver = model_ver)
State_CommodityOutput_ls <- loadStateIODataFile(paste0("State_",
iolevel,
"_CommodityOutput_",
year))
year),
ver = model_ver)
# Assemble two-region IO tables
TwoRegionIO <- list()
for (state in sort(c(state.name, "District of Columbia"))) {
Expand Down Expand Up @@ -695,7 +704,8 @@ buildFullTwoRegionIOTable <- function(state, year, ioschema, iolevel) {
logging::loginfo("Loading SoI Make table...")
# SoI Make
SoI_Make <- loadStateIODataFile(paste0("State_",iolevel,
"_Make_", year))[[state]]
"_Make_", year),
ver = model_ver)[[state]]
rownames(SoI_Make) <- paste0(getBEASectorCodeLocation("Industry", state, "Summary"),
"/Industry")
colnames(SoI_Make) <- paste0(getBEASectorCodeLocation("Commodity", state, "Summary"),
Expand All @@ -704,7 +714,8 @@ buildFullTwoRegionIOTable <- function(state, year, ioschema, iolevel) {
SoI_CommodityOutput <- loadStateIODataFile(paste0("State_",
iolevel,
"_CommodityOutput_",
year))[[state]]
year),
ver = model_ver)[[state]]

logging::loginfo("Generating RoUS Make table...")
# RoUS Make
Expand All @@ -726,7 +737,8 @@ buildFullTwoRegionIOTable <- function(state, year, ioschema, iolevel) {
logging::loginfo("Loading two-region Domestic Use tables...")
# Two-region Use Transaction
TwoRegionUse <- loadStateIODataFile(paste("TwoRegion", iolevel, "DomesticUse",
year, sep = "_"))[[state]]
year, sep = "_"),
ver = model_ver)[[state]]
TwoRegionUseTrans_cols <- unlist(lapply(c(state, "RoUS"),
function(x)
getBEASectorCodeLocation("Industry",
Expand All @@ -751,8 +763,9 @@ buildFullTwoRegionIOTable <- function(state, year, ioschema, iolevel) {
logging::loginfo("Calculating SoI and RoUS international imports by industry...")
# International imports by industry
SoI_Use <- loadStateIODataFile(paste("State", iolevel, "Use", year,
sep = "_"))[[state]][commodities,
c(industries, FD_cols)]
sep = "_"),
ver = model_ver)[[state]][commodities,
c(industries, FD_cols)]
US_Use <- getNationalUse(iolevel, year)
US_Import <- loadDatafromUSEEIOR(paste(iolevel, "Import", year, "BeforeRedef",
sep = "_"))*1E6
Expand All @@ -769,7 +782,8 @@ buildFullTwoRegionIOTable <- function(state, year, ioschema, iolevel) {
logging::loginfo("Calculating SoI and RoUS gross value added by industry...")
# GVA
GVA_rows <- getVectorOfCodes(iolevel, "ValueAdded")
SoI_GVA_ls <- loadStateIODataFile(paste("State", iolevel, "Use", year, sep = "_"))
SoI_GVA_ls <- loadStateIODataFile(paste("State", iolevel, "Use", year, sep = "_"),
ver = model_ver)
SoI_GVA <- SoI_GVA_ls[[state]][GVA_rows, c(industries, FD_cols)]
RoUS_GVA <- Reduce("+", SoI_GVA_ls)[GVA_rows, c(industries, FD_cols)] - SoI_GVA
TwoRegionGVA <- cbind(SoI_GVA, RoUS_GVA)
Expand All @@ -785,12 +799,14 @@ buildFullTwoRegionIOTable <- function(state, year, ioschema, iolevel) {
logging::loginfo("Loading foreign expenditure by U.S. residents and spending in the U.S. by nonresidents...")
# Foreign expenditure by U.S. residents
ForeignExp <- loadStateIODataFile(paste("State_ForeignExpenditureByResident",
year, sep = "_"))
year, sep = "_"),
ver = model_ver)
SoIForeignExp <- ForeignExp[ForeignExp$GeoName == state, as.character(year)]
RoUSForeignExp <- ForeignExp[ForeignExp$GeoName == "United States", as.character(year)] - SoIForeignExp
# Spending in the U.S. by nonresidents
DomesticExp <- loadStateIODataFile(paste("State_DomesticExpenditureByNonresident",
year, sep = "_"))
year, sep = "_"),
ver = model_ver)
SoIDomesticExp <- DomesticExp[DomesticExp$GeoName == state, as.character(year)]
RoUSDomesticExp <- DomesticExp[DomesticExp$GeoName == "United States", as.character(year)] - SoIDomesticExp

Expand All @@ -811,9 +827,11 @@ buildFullTwoRegionIOTable <- function(state, year, ioschema, iolevel) {

# Append total output column
tworegion_co_filename <- paste("TwoRegion", iolevel, "CommodityOutput", year, sep = "_")
TwoRegionCommodityOutput <- loadStateIODataFile(tworegion_co_filename)[[state]]
TwoRegionCommodityOutput <- loadStateIODataFile(tworegion_co_filename,
ver = model_ver)[[state]]
tworegion_io_filename <- paste("TwoRegion", iolevel, "IndustryOutput", year, sep = "_")
TwoRegionIndustryOutput <- loadStateIODataFile(tworegion_io_filename)[[state]]
TwoRegionIndustryOutput <- loadStateIODataFile(tworegion_io_filename,
ver = model_ver)[[state]]
FullTwoRegionTable[, "Total Output"] <- c(TwoRegionCommodityOutput,
TwoRegionIndustryOutput)

Expand Down
Loading

0 comments on commit 3bffa79

Please sign in to comment.