Skip to content

Commit 3627a38

Browse files
committed
Merge branch 'master' into waste_disagg_update
2 parents 02e8b4e + b042a75 commit 3627a38

File tree

69 files changed

+808
-79
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+808
-79
lines changed

.github/workflows/generate-2R-model.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
model:
99
description: "Model"
1010
required: true
11-
default: "GAEEIOv1.0-s-WAT-12"
11+
default: "GAEEIOv1.0-GHG-19"
1212

1313
jobs:
1414
build:
@@ -21,7 +21,7 @@ jobs:
2121
R_KEEP_PKG_SOURCE: yes
2222

2323
steps:
24-
- uses: actions/checkout@v3
24+
- uses: actions/checkout@v4
2525
with:
2626
fetch-depth: 0
2727

@@ -52,14 +52,14 @@ jobs:
5252

5353
- name: Upload check results
5454
if: failure() # if any of the previous steps fail, export a log
55-
uses: actions/upload-artifact@main
55+
uses: actions/upload-artifact@v4
5656
with:
5757
name: results
5858
path: check
5959

6060
- name: Upload files
6161
if: always()
62-
uses: actions/upload-artifact@v3.1.1
62+
uses: actions/upload-artifact@v4
6363
with:
6464
name: "${{ github.event.inputs.model }}"
6565
path: | # uses local user data dir for ubuntu

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: useeior
22
Type: Package
33
Title: USEEIO R modeling software
4-
Version: 1.7.0
5-
Date: 2024-11-5
4+
Version: 1.7.1
5+
Date: 2025-4-28
66
Authors@R: c(
77
person("Ben","Young", email="[email protected]", role="aut"),
88
person("Jorge","Vendries", email="[email protected]", role="aut"),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export(disaggregateTotalToDirectAndTier1)
1616
export(extractAndFormatDemandVector)
1717
export(formatDemandVector)
1818
export(generateModelIdentifier)
19+
export(getSectorLinkages)
1920
export(heatmapSatelliteTableCoverage)
2021
export(heatmapSectorRanking)
2122
export(normalizeResultMatrixByTotalImpacts)

R/BuildModel.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ buildTwoRegionModels <- function(modelname, configpaths = NULL, validate = FALSE
334334
model <- loadDemandVectors(model)
335335
model <- constructEEIOMatrices(model)
336336
if (validate) {
337-
print2RValidationResults(model)
337+
printValidationResults(model)
338338
}
339339
model_ls[[state]] <- model
340340
},

R/CalculationFunctions.R

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,98 @@ calculateTotalImpactbyTier1Purchases <- function(model, impact, opt_impact='indi
634634
})
635635
return(totalImpactPerPurchase)
636636
}
637+
638+
639+
#' Function to get backward or forward economic linkages from a USEEIO model,
640+
#' Provides results based on input demand, sorted by total (direct+indirect)
641+
#' Backward linkages use A and L matrices
642+
#' Forward linkages use Ghosh counterparts to A and L
643+
#' @param model An EEIO model object with model specs, IO tables, satellite tables,
644+
#' and indicators loaded
645+
#' @param demand A demand vector, can be name of a built-in model demand vector,
646+
#' e.g. "Production" or "Consumption",
647+
#' or an actual demand vector with names as one or more model sectors and
648+
#' numeric values in USD with the same dollar year as model.
649+
#' @param type "backward" linkages use A and L matrices "forward" linkages use Ghosh
650+
#' counterparts to A and L
651+
#' @param use_domestic_requirements A logical value: if TRUE, use domestic demand
652+
#' and L_d matrix; if FALSE, use complete demand and L matrix.
653+
#' @param location str optional location code for demand vector, required for
654+
#' two-region models
655+
#' @param cutoff numeric value, shows the cutoff value for sorted results.
656+
#' Values smaller than cutoff are not shown
657+
#' @return dataframe of direct, indirect, and total economic linkages
658+
#' @export
659+
getSectorLinkages <- function(model, demand, type = "backward",
660+
location = NULL, use_domestic_requirements = FALSE,
661+
cutoff = 0.01) {
662+
f <- prepareDemandVectorForStandardResults(model, demand, location,
663+
use_domestic_requirements)
664+
if(type=="backward") {
665+
if (use_domestic_requirements) {
666+
tier1 <- model$A_d %*%f
667+
all <- model$L_d %*%f
668+
} else {
669+
tier1 <- model$A%*%f
670+
all <- model$L%*%f
671+
}
672+
} else if(type=="forward") {
673+
Ghosh_B <- calculateGhoshB(model, use_domestic_requirements)
674+
Ghosh_G <- calculateGhoshG(Ghosh_B)
675+
tier1 <- Ghosh_B%*%f
676+
all <- Ghosh_G%*%f
677+
}
678+
tier2andbeyond <- all-tier1
679+
680+
# Remove the additional "1" from the Leontief or Ghosh to avoid double counting indirect
681+
tier2andbeyond <- tier2andbeyond-f
682+
total <- tier1 + tier2andbeyond
683+
684+
linkages <- data.frame(direct=tier1, indirect=tier2andbeyond, total=total)
685+
linkages$Name <- model$Commodities[match(rownames(linkages),
686+
model$Commodities$Code_Loc),"Name"]
687+
# Filter by share > 1%
688+
linkages <- linkages[linkages$total > cutoff, ]
689+
# Sort them
690+
linkages <- linkages[order(linkages$total, decreasing = TRUE), ]
691+
return(linkages)
692+
}
693+
694+
#' Function to get supply side Ghosh matrix B. B can be calculated based on commodity-by-commodity or
695+
#' industry-by-industry direct requirement matrix: (Miller and Blair)
696+
#' for industry-by-industry requirements: B = inverse(qhat) * A(industry-by-industry)*qhat,
697+
#' where q is the total commodity output
698+
#' for commodity-by-commodity requirements: B = inverse(xhat) * A(industry-by-industry)*xhat,
699+
#' where x is the total inudstry output
700+
#' @param model An EEIO model object with model specs, IO tables, satellite tables, and indicators loaded
701+
#' @param use_domestic_requirements A logical value: if TRUE, use domestic demand and L_d matrix;
702+
#' if FALSE, use complete demand and L matrix.
703+
#' @return B, Ghosh B matrix
704+
calculateGhoshB <- function(model, use_domestic_requirements = FALSE) {
705+
q <- model$q # total commodity output
706+
x <- model$x # total industry output
707+
if(use_domestic_requirements) {
708+
A <- model$A_d
709+
} else {
710+
A <- model$A
711+
}
712+
if(model$specs$CommodityorIndustryType == "Commodity") {
713+
B <- solve(diag(q)) %*% A %*% diag(q)
714+
row.names(B) <- model$Commodities$Code_Loc
715+
colnames(B) <- model$Commodities$Code_Loc
716+
} else if(model$specs$CommodityorIndustryType == "Industry") {
717+
B <- solve(diag(x)) %*% A %*% diag(x)
718+
row.names(B) <- model$Industries$Code_Loc
719+
colnames(B) <- model$Industries$Code_Loc
720+
}
721+
722+
return(B)
723+
}
724+
725+
#' Function to calculate inverse of Ghosh matrix B, returns the inverse G
726+
#' @param GhoshB Ghosh matrix B
727+
#' @return G, Ghosh matrix G
728+
calculateGhoshG <- function(GhoshB) {
729+
G <- useeior:::calculateLeontiefInverse(GhoshB)
730+
return(G)
731+
}

R/DisaggregateFunctions.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ disaggregateModel <- function (model){
5656
#Disaggregating Crosswalk
5757
model$crosswalk <- disaggregateMasterCrosswalk(model, disagg)
5858

59-
#Disaggregate Margins
60-
model$Margins <- disaggregateMargins(model, disagg)
59+
if (model$specs$IODataSource != "stateior") {
60+
#Disaggregate Margins, one region models only see #290
61+
model$Margins <- disaggregateMargins(model, disagg)
62+
}
6163
model$TaxLessSubsidies <- disaggregateTaxLessSubsidies(model, disagg)
6264

6365
# Transform model FinalDemand, DomesticFinalDemand, and InternationalTradeAdjustment to by-industry form

R/LoadIOTables.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,11 @@ loadCommodityandIndustryOutput <- function(model) {
380380
model$IndustryOutput <- getTwoRegionIOData(model, "IndustryOutput")
381381
model$CommodityOutput <- getTwoRegionIOData(model, "CommodityOutput")
382382
# Load multi-year industry and commodity output
383-
years <- as.character(2012:2020)
383+
if(model$specs$BaseIOSchema == 2017) {
384+
years <- as.character(2017:2023)
385+
} else {
386+
years <- as.character(2012:2020)
387+
}
384388
tmpmodel <- model
385389
model$MultiYearIndustryOutput <- as.data.frame(model$IndustryOutput)[, FALSE]
386390
model$MultiYearCommodityOutput <- as.data.frame(model$CommodityOutput)[, FALSE]

R/StateiorFunctions.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
#' @return A list of two-region IO data of model iolevel and year.
1313
getTwoRegionIOData <- function(model, dataname) {
1414
# Define state, year and iolevel
15+
alias <- ifelse(!is.na(model$specs$Alias), model$specs$Alias, NULL)
1516
if(!"US-DC" %in% model$specs$ModelRegionAcronyms) {
1617
state <- state.name[state.abb == gsub(".*-", "", model$specs$ModelRegionAcronyms[1])]
1718
} else {
1819
state <- "District of Columbia"
1920
}
2021
# Define data file name
2122
filename <- paste(lapply(c("TwoRegion", model$specs$BaseIOLevel, dataname,
22-
model$specs$DisaggregationSpecs, model$specs$IOYear,
23+
alias, model$specs$IOYear,
2324
model$specs$IODataVersion),
2425
function(x) x[!is.na(x)]), collapse = "_")
2526
# Adjust filename to fit what is on the Data Commons
@@ -33,6 +34,7 @@ getTwoRegionIOData <- function(model, dataname) {
3334
filename <- gsub(dataname, "UsewithTrade", filename)
3435
}
3536
# Load data
37+
logging::loginfo(paste0("Loading ", filename))
3638
TwoRegionIOData <- readRDS(loadDataCommonsfile(paste0("stateio/", filename, ".rds")))
3739
# Keep SoI and RoUS only
3840
TwoRegionIOData <- TwoRegionIOData[[state]]

R/ValidateModel.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,10 @@ testCalculationFunctions <- function(model) {
527527
print("Error in calculateSectorContributiontoImpact()")
528528
}
529529

530-
demand = model$DemandVectors$vectors[[1]]
530+
linkages <- getSectorLinkages(model, demand="Consumption", type="forward",
531+
location = model$specs$ModelRegionAcronyms[[1]])
532+
533+
demand <- model$DemandVectors$vectors[[1]]
531534
result <- calculateSectorPurchasedbySectorSourcedImpact(y=demand, model, indicator)
532535
if(model$specs$IODataSource != "stateior") {
533536
# not working for 2R mode

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<!-- badges: start -->
33
[![R CI/CD test](https://github.com/USEPA/useeior/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/USEPA/useeior/actions/workflows/R-CMD-check.yaml)
44
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html)
5-
[![useeior v1.0.0](http://img.shields.io/badge/useeior%20v1.0.0-10.5281/zenodo.6370101-blue.svg)](https://doi.org/10.5281/zenodo.6370101)
5+
[![useeior DOI](http://img.shields.io/badge/useeior%20DOI-10.5281/zenodo.7020213-blue.svg)](https://doi.org/10.5281/zenodo.7020213)
66
[![useeior paper](http://img.shields.io/badge/useeior%20paper-10.3390/app12094469-blue.svg)](https://doi.org/10.3390/app12094469)
77
<!-- badges: end -->
88

0 commit comments

Comments
 (0)