Skip to content

Commit

Permalink
Merge pull request #258 from USEPA/release-v1.3.0
Browse files Browse the repository at this point in the history
Release v1.3.0
  • Loading branch information
bl-young authored Aug 15, 2023
2 parents 169ae5a + a3c5e3b commit 798cd72
Show file tree
Hide file tree
Showing 162 changed files with 3,187 additions and 631 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: R CI/CD test #R-CMD-check

on:
push:
branches: [main, master]
branches: [main, master, develop]
paths-ignore:
- '**.md' # prevent md files (e.g., README.md) in any repo dir from trigering workflow
- '**.bib'
Expand All @@ -15,7 +15,7 @@ on:
- '.gitignore'
- '.gitattributes'
pull_request:
branches: [master] # develop
branches: [master, develop]
paths-ignore:
- '**.md'
- '**.bib'
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: useeior
Type: Package
Title: USEEIO R modeling software
Version: 1.2.2
Date: 2023-7-14
Version: 1.3.0
Date: 2023-7-21
Authors@R: c(
person("Mo","Li", email="[email protected]", role="aut"),
person("Wesley","Ingwersen", email="[email protected]", role= c("aut", "cre")),
Expand Down
21 changes: 19 additions & 2 deletions R/AdjustPrice.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ calculateProducerbyPurchaserPriceRatio <- function(model) {
TWR_CPI <- useeior::Sector_CPI_IO[c("48TW", "42", "44RT"), ]
TWR_CPI_ratio <- TWR_CPI[, year]/TWR_CPI[, as.character(model$specs$IOYear)]
TWRValue <- sweep(Margins[, c("Transportation", "Wholesale", "Retail")], 2, TWR_CPI_ratio, "*")
# Calculate PurchasersValue
PurchasersValue <- rowSums(Margins[, c("ProducersValue", "Transportation", "Wholesale", "Retail")])
# Generate PRObyPURRatios, or phi vector
PHI[, year] <- ProducersValue/(ProducersValue + rowSums(TWRValue))
}
Expand Down Expand Up @@ -86,3 +84,22 @@ adjustMultiplierPriceType <- function(matrix, currency_year, model) {
return(matrix_new)
}

#' Calculate basic to producer price ratio.
#' @param model A complete EEIO model: a list with USEEIO model components and attributes.
#' @return A dataframe of basic to producer price ratio.
calculateBasicbyProducerPriceRatio <- function(model) {
# Get TaxLessSubsidies table
TaxLessSubsidies <- merge(model$TaxLessSubsidies, model$Rho,
by.x = "Code_Loc", by.y = 0, all.y = TRUE)
TaxLessSubsidies <- TaxLessSubsidies[match(rownames(model$Rho),
TaxLessSubsidies$Code_Loc), ]
# Prepare ratio table Tau using the structure of Rho
Tau <- model$Rho
for (year in colnames(model$Rho)) {
# Adjust BasicValue from model$specs$IOyear to currency year using model$Rho
BasicValue <- TaxLessSubsidies$BasicValue * (TaxLessSubsidies[, year]/TaxLessSubsidies[, as.character(model$specs$IOYear)])
Tau[, year] <- BasicValue/TaxLessSubsidies$ProducerValue
}
Tau[is.na(Tau)] <- 1
return(Tau)
}
78 changes: 78 additions & 0 deletions R/BuildModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ constructEEIOMatrices <- function(model) {
logging::loginfo("Calculating Phi matrix (producer over purchaser price ratio)...")
model$Phi <- calculateProducerbyPurchaserPriceRatio(model)

# Calculate basic over producer price ratio.
logging::loginfo("Calculating Tau matrix (basic over producer price ratio)...")
model$Tau <- calculateBasicbyProducerPriceRatio(model)

#Clean up model elements not written out or used in further functions to reduce clutter
mat_to_remove <- c("MakeTransactions", "UseTransactions", "DomesticUseTransactions",
"UseValueAdded", "FinalDemand", "DomesticFinalDemand",
Expand Down Expand Up @@ -223,3 +227,77 @@ createCfromFactorsandBflows <- function(factors,B_flows) {
return(C)
}

#' Build an EIO 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) {
model <- initializeModel(modelname, configpaths)
model <- loadIOData(model, configpaths)
model <- loadDemandVectors(model)

model$V <- as.matrix(model$MakeTransactions) # Make
model$C_m <- generateCommodityMixMatrix(model) # normalized t(Make)
model$V_n <- generateMarketSharesfromMake(model) # normalized Make
if (model$specs$CommodityorIndustryType=="Industry") {
FinalDemand_df <- model$FinalDemandbyCommodity
DomesticFinalDemand_df <- model$DomesticFinalDemandbyCommodity
} else {
FinalDemand_df <- model$FinalDemand
DomesticFinalDemand_df <- model$DomesticFinalDemand
}
model$U <- as.matrix(dplyr::bind_rows(cbind(model$UseTransactions,
FinalDemand_df),
model$UseValueAdded)) # Use
model$U_d <- as.matrix(dplyr::bind_rows(cbind(model$DomesticUseTransactions,
DomesticFinalDemand_df),
model$UseValueAdded)) # DomesticUse
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
model$q <- model$CommodityOutput
model$x <- model$IndustryOutput
model$mu <- model$InternationalTradeAdjustment
if(model$specs$CommodityorIndustryType == "Commodity") {
logging::loginfo("Building commodity-by-commodity A matrix (direct requirements)...")
model$A <- model$U_n %*% model$V_n
logging::loginfo("Building commodity-by-commodity A_d matrix (domestic direct requirements)...")
model$A_d <- model$U_d_n %*% model$V_n
} else if(model$specs$CommodityorIndustryType == "Industry") {
logging::loginfo("Building industry-by-industry A matrix (direct requirements)...")
model$A <- model$V_n %*% model$U_n
logging::loginfo("Building industry-by-industry A_d matrix (domestic direct requirements)...")
model$A_d <- model$V_n %*% model$U_d_n
}

if(model$specs$ModelType == "EEIO-IH"){
model$A <- hybridizeAMatrix(model)
model$A_d <- hybridizeAMatrix(model, domestic=TRUE)
}

# Calculate total requirements matrix as Leontief inverse (L) of A
logging::loginfo("Calculating L matrix (total requirements)...")
I <- diag(nrow(model$A))
I_d <- diag(nrow(model$A_d))
model$L <- solve(I - model$A)
logging::loginfo("Calculating L_d matrix (domestic total requirements)...")
model$L_d <- solve(I_d - model$A_d)

# Calculate year over model IO year price ratio
logging::loginfo("Calculating Rho matrix (price year ratio)...")
model$Rho <- calculateModelIOYearbyYearPriceRatio(model)

# Calculate producer over purchaser price ratio.
logging::loginfo("Calculating Phi matrix (producer over purchaser price ratio)...")
model$Phi <- calculateProducerbyPurchaserPriceRatio(model)

# Calculate basic over producer price ratio.
logging::loginfo("Calculating Tau matrix (basic over producer price ratio)...")
model$Tau <- calculateBasicbyProducerPriceRatio(model)

logging::loginfo("EIO model build complete.")
return(model)
}
230 changes: 230 additions & 0 deletions R/DataDocumentation.R
Original file line number Diff line number Diff line change
Expand Up @@ -740,3 +740,233 @@
#' \item{NAICS_2007_Code}{text code}
#' }
"MasterCrosswalk2007"

#' Detail 2012 Supply (2012 schema)
#' @format A dataframe with 406 obs. and 417 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Detail_Supply_2012"

#' Detail 2012 Use (under the Supply-Use framework, 2012 schema)
#' @format A dataframe with 414 obs. and 426 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Detail_Use_SUT_2012"

#' Summary 2010 Supply (2012 schema)
#' @format A dataframe with 74 obs. and 83 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Supply_2010"

#' Summary 2011 Supply (2012 schema)
#' @format A dataframe with 74 obs. and 83 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Supply_2011"

#' Summary 2012 Supply (2012 schema)
#' @format A dataframe with 74 obs. and 83 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Supply_2012"

#' Summary 2013 Supply (2012 schema)
#' @format A dataframe with 74 obs. and 83 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Supply_2013"

#' Summary 2014 Supply (2012 schema)
#' @format A dataframe with 74 obs. and 83 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Supply_2014"

#' Summary 2015 Supply (2012 schema)
#' @format A dataframe with 74 obs. and 83 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Supply_2015"

#' Summary 2016 Supply (2012 schema)
#' @format A dataframe with 74 obs. and 83 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Supply_2016"

#' Summary 2017 Supply (2012 schema)
#' @format A dataframe with 74 obs. and 83 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Supply_2017"

#' Summary 2018 Supply (2012 schema)
#' @format A dataframe with 74 obs. and 83 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Supply_2018"

#' Summary 2019 Supply (2012 schema)
#' @format A dataframe with 74 obs. and 83 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Supply_2019"

#' Summary 2020 Supply (2012 schema)
#' @format A dataframe with 74 obs. and 83 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Supply_2020"

#' Summary 2010 Use (2012 schema)
#' @format A dataframe with 82 obs. and 92 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Use_SUT_2010"

#' Summary 2011 Use (2012 schema)
#' @format A dataframe with 82 obs. and 92 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Use_SUT_2011"

#' Summary 2012 Use (2012 schema)
#' @format A dataframe with 82 obs. and 92 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Use_SUT_2012"

#' Summary 2013 Use (2012 schema)
#' @format A dataframe with 82 obs. and 92 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Use_SUT_2013"

#' Summary 2014 Use (2012 schema)
#' @format A dataframe with 82 obs. and 92 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Use_SUT_2014"

#' Summary 2015 Use (2012 schema)
#' @format A dataframe with 82 obs. and 92 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Use_SUT_2015"

#' Summary 2016 Use (2012 schema)
#' @format A dataframe with 82 obs. and 92 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Use_SUT_2016"

#' Summary 2017 Use (2012 schema)
#' @format A dataframe with 82 obs. and 92 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Use_SUT_2017"

#' Summary 2018 Use (2012 schema)
#' @format A dataframe with 82 obs. and 92 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Use_SUT_2018"

#' Summary 2019 Use (2012 schema)
#' @format A dataframe with 82 obs. and 92 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Use_SUT_2019"

#' Summary 2020 Use (2012 schema)
#' @format A dataframe with 82 obs. and 92 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Summary_Use_SUT_2020"

#' Sector 2010 Supply (2012 schema)
#' @format A dataframe with 18 obs. and 27 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Supply_2010"

#' Sector 2011 Supply (2012 schema)
#' @format A dataframe with 18 obs. and 27 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Supply_2011"

#' Sector 2012 Supply (2012 schema)
#' @format A dataframe with 18 obs. and 27 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Supply_2012"

#' Sector 2013 Supply (2012 schema)
#' @format A dataframe with 18 obs. and 27 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Supply_2013"

#' Sector 2014 Supply (2012 schema)
#' @format A dataframe with 18 obs. and 27 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Supply_2014"

#' Sector 2015 Supply (2012 schema)
#' @format A dataframe with 18 obs. and 27 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Supply_2015"

#' Sector 2016 Supply (2012 schema)
#' @format A dataframe with 18 obs. and 27 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Supply_2016"

#' Sector 2017 Supply (2012 schema)
#' @format A dataframe with 18 obs. and 27 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Supply_2017"

#' Sector 2018 Supply (2012 schema)
#' @format A dataframe with 18 obs. and 27 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Supply_2018"

#' Sector 2019 Supply (2012 schema)
#' @format A dataframe with 18 obs. and 27 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Supply_2019"

#' Sector 2020 Supply (2012 schema)
#' @format A dataframe with 18 obs. and 27 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Supply_2020"

#' Sector 2010 Use (2012 schema)
#' @format A dataframe with 26 obs. and 22 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Use_SUT_2010"

#' Sector 2011 Use (2012 schema)
#' @format A dataframe with 26 obs. and 22 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Use_SUT_2011"

#' Sector 2012 Use (2012 schema)
#' @format A dataframe with 26 obs. and 22 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Use_SUT_2012"

#' Sector 2013 Use (2012 schema)
#' @format A dataframe with 26 obs. and 22 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Use_SUT_2013"

#' Sector 2014 Use (2012 schema)
#' @format A dataframe with 26 obs. and 22 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Use_SUT_2014"

#' Sector 2015 Use (2012 schema)
#' @format A dataframe with 26 obs. and 22 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Use_SUT_2015"

#' Sector 2016 Use (2012 schema)
#' @format A dataframe with 26 obs. and 22 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Use_SUT_2016"

#' Sector 2017 Use (2012 schema)
#' @format A dataframe with 26 obs. and 22 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Use_SUT_2017"

#' Sector 2018 Use (2012 schema)
#' @format A dataframe with 26 obs. and 22 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Use_SUT_2018"

#' Sector 2019 Use (2012 schema)
#' @format A dataframe with 26 obs. and 22 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Use_SUT_2019"

#' Sector 2020 Use (2012 schema)
#' @format A dataframe with 26 obs. and 22 variables
#' @source \url{https://apps.bea.gov/industry/iTables\%20Static\%20Files/AllTablesSUP.zip}
"Sector_Use_SUT_2020"
Loading

0 comments on commit 798cd72

Please sign in to comment.