Skip to content
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

Updates to disaggregation to support electricity #164

Merged
merged 121 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
89f02cb
Added electricity disagg model specs yml file
jvendries Jul 14, 2021
eb3702e
added ElectricityDisaggregationDetail.yml file
jvendries Jul 14, 2021
ed4909e
Added ElectricityDisaggregation_Sectors.csv file which speficies the …
jvendries Jul 14, 2021
2683646
Added electricityDisaggregationDetail_Make.csv file
jvendries Jul 14, 2021
3c818d7
Added ElectricityDisaggregationDetail_Use.csv file
jvendries Jul 14, 2021
f1a2d75
Reorganized electricity disaggregation file structure to match waste …
jvendries Jul 14, 2021
aa5c4de
Include potential aggregation before disaggregation in disagg specs
bl-young Jul 15, 2021
4af65b3
add if loop to handle aggregation
jvendries Aug 2, 2021
6aadd88
add aggregation.r file with functionality for aggregating makeTransac…
jvendries Aug 2, 2021
4240231
commenting out if loop for aggregation in disaggregateModel for conti…
jvendries Aug 2, 2021
5315a6f
added functionality to aggregate crosswalk, industry lists
jvendries Aug 3, 2021
369b163
edited aggregate value added function
jvendries Aug 24, 2021
61e88fa
function description updates in disaggfunctions
jvendries Aug 25, 2021
6be1a90
edited aggregateMultiYearOutput fuction
jvendries Aug 25, 2021
85c5bca
edited aggregate outputs, aggregate multiyear cpi, and aggSatelliteta…
jvendries Aug 31, 2021
505b80b
added comments marking places where edits are needed for handling dis…
jvendries Aug 31, 2021
cbb4ac3
adding call to aggregate satellite function
jvendries Sep 1, 2021
c68b252
edited input files for disaggregation of electricity
jvendries Sep 1, 2021
b7325a9
add env allocation factors
bl-young Sep 1, 2021
23a2451
add flag for env ratio disaggregation
bl-young Sep 6, 2021
86f4bda
refac disaggregateSatelliteTable to better address allocation approac…
bl-young Sep 6, 2021
42c18ed
move loop for multiple disaggregations into LoadSatellites
bl-young Sep 6, 2021
d5997b2
drop location code from waste env disaggregation df
bl-young Sep 7, 2021
f397865
apply env allocation ratios per flow
bl-young Sep 7, 2021
389c8a2
added yml files for secuential disaggregation of waste and electricity
jvendries Sep 7, 2021
0f0fc10
Merge branch 'electricity_disagg' of https://github.com/USEPA/useeior…
jvendries Sep 7, 2021
563eafe
changed format of aggregation in yml specs file and the code in aggre…
jvendries Sep 8, 2021
d363c33
Changed DisaggregationSpecs line to point to the two individual disag…
jvendries Sep 8, 2021
1a4d4c9
append multiple disaggregations to the model from individual config f…
bl-young Sep 8, 2021
38c400c
fix typo
bl-young Sep 8, 2021
d2c5d3b
change aggregation format in WasteElectricityDisaggregation yml
jvendries Sep 8, 2021
44c2a52
Merge branch 'electricity_disagg' of https://github.com/USEPA/useeior…
jvendries Sep 8, 2021
d54ddef
edited disaggfunctions to work with sequential aggregation
jvendries Sep 10, 2021
30aad78
cleanup and formatting
bl-young Sep 10, 2021
2151a24
Merge branch 'electricity_disagg' into electricity_disagg1
bl-young Sep 13, 2021
987b211
Merge pull request #161 from USEPA/electricity_disagg1
bl-young Sep 13, 2021
5f4488b
add category, subcategory and temporary description fields for elec d…
bl-young Sep 13, 2021
18e356b
cleanup logging messages
bl-young Sep 13, 2021
b1626ce
Merge branch 'master' into electricity_disagg
bl-young Sep 13, 2021
a2395d3
Merge branch 'master' into electricity_disagg
bl-young Sep 15, 2021
8275aa7
Deleted electricity yml and sequential electricity & waste yml files …
jvendries Sep 15, 2021
7b80ca7
Added electricity disaggregation yml and electricity and waste disagg…
jvendries Sep 15, 2021
5d684a9
Moved disaggregation setup steps to their own function.
jvendries Sep 15, 2021
a2243a6
Changed sequential disaggregation yml file from USEEIOv2.1_Waste_Elec…
jvendries Sep 16, 2021
62ede20
Removed commented section referring to moved function
jvendries Sep 16, 2021
9bffe94
Created functions to get disaggregation and aggregation specs that ca…
jvendries Sep 16, 2021
7aa271c
consolidate specifiedUse and specifiedMake disagg by moving some step…
bl-young Sep 17, 2021
d862b1a
rename disaggAllocations to applyAllocation, consolidate generation o…
bl-young Sep 17, 2021
1f1aa75
fix typos and remove unneeded variables
bl-young Sep 17, 2021
6bbc97e
move error handling to disaggregateUseTable
bl-young Sep 17, 2021
5ec80cf
move error handling to disaggregateMake
bl-young Sep 17, 2021
7e1a3e8
refactor applyAllocation to pass the original table as a parameter to…
bl-young Sep 17, 2021
cea4734
write new helper functions to consolidate duplicate code
bl-young Sep 17, 2021
7e23bd4
consolidate uniformUse and uniformMake disagg with assembleTable
bl-young Sep 17, 2021
ccb9394
consolidate uniformMake and uniformUse disagg into single function
bl-young Sep 17, 2021
1d72efc
removing unneeded variables
bl-young Sep 17, 2021
ab68c50
create new function to calculateIndustryCommodityOutput
bl-young Sep 17, 2021
38ab23b
revise to use new calculateIndustryCommodityOutput function
bl-young Sep 17, 2021
cbb9f63
Changed check for aggregation and check for disaggregation from check…
jvendries Sep 17, 2021
8f946d6
Merged getAggregationSpecs and getDisaggregationSpecs into one functi…
jvendries Sep 17, 2021
3a07b82
Merge branch 'electricity_disagg' into refac_disagg
bl-young Sep 17, 2021
0e3fc21
Merge pull request #162 from USEPA/refac_disagg
bl-young Sep 17, 2021
aea0e6c
Merge branch 'master' into electricity_disagg
bl-young Sep 17, 2021
e50cb50
update env disagg by ratio to use UUID instead of flow name
bl-young Sep 17, 2021
521ed72
fix typo in model spec
bl-young Sep 22, 2021
b8b8372
delete combined disagg specs
bl-young Sep 22, 2021
43f0694
delete testing model spec
bl-young Sep 22, 2021
96656c1
update function docstrings
bl-young Sep 27, 2021
cb73aaf
update documentation files and add new files for aggregate functions
bl-young Sep 27, 2021
91ce064
Fix typo in logging message
MoLi7 Sep 28, 2021
7afc578
update generateModelSectorSchema to handle multiple disaggregations
bl-young Sep 29, 2021
5f60707
move generateModelSectorSchema to utility functions
bl-young Sep 29, 2021
2b5ff21
remove IO year from schema
bl-young Sep 29, 2021
7a1285f
conditional sectorschema based on disagg specs
bl-young Sep 29, 2021
5411061
generate new crosswalk column with model schema when disaggregation p…
bl-young Sep 29, 2021
72599ae
Merge branch 'electricity_disagg' of https://github.com/USEPA/useeior…
bl-young Sep 29, 2021
04c8f1a
Fix typos
jvendries Oct 4, 2021
92137f2
Adding aggregation specs folder and file for electricity separate fro…
jvendries Oct 4, 2021
f72f492
Changes in aggregation functionality to look for specs in its own sub…
jvendries Oct 5, 2021
ffc0069
Changed if statement that checks for aggregation for satellite tables…
jvendries Oct 5, 2021
e430584
assign active model crosswalk sector list as USEEIO for all models
bl-young Oct 6, 2021
6526ed1
update aggregateSatelliteTable and mapfromNAICS... to always aggregat…
bl-young Oct 6, 2021
50fe9f9
use "USEEIO" column for active sector list in crosswalk
bl-young Oct 6, 2021
9c6f4cb
update disaggregation of crosswalk by merging in new sectors to resol…
bl-young Oct 6, 2021
2c8f1ab
when aggregating crosswalk, replace sectors in existing cw rows inste…
bl-young Oct 6, 2021
b55a8ba
resolve #166 by updating row names to remove location code
bl-young Oct 6, 2021
d3550cb
use and rename the exisiting model crosswalk when writing to excel
bl-young Oct 6, 2021
a6870c0
Fixing function descriptions and removing block comments containing o…
jvendries Oct 6, 2021
fa3039f
Change aggregation and disaggregation specs structure to place space …
jvendries Oct 6, 2021
60e32a9
Changed AggSpecs list structure to be analogous to DisaggSpecs list s…
jvendries Oct 6, 2021
be62e3f
Remove obsolete and commented code
jvendries Oct 6, 2021
c976346
Removed electricity aggregation parameters from disaggregation specs …
jvendries Oct 6, 2021
d89924d
Added aggregation specs line to USEEIOv2.1.yml file
jvendries Oct 6, 2021
86bc9ef
handle crosswalk disaggregation when a sector to be disaggregated doe…
bl-young Oct 7, 2021
9ef8800
Resolve conflicts from merging electricity_disagg_refactor into elect…
jvendries Oct 7, 2021
77e8600
consolidate function to collapse TBS
bl-young Oct 7, 2021
0a82b6c
update sat table aggregation to replace sector codes and names with n…
bl-young Oct 7, 2021
6718ae8
Merge branch 'electricity_disagg' of https://github.com/USEPA/useeior…
bl-young Oct 7, 2021
3a1c3c1
change function name to aggregateSectorsinTBS
bl-young Oct 7, 2021
bf282a1
conform TBS to standard earlier to avoid missing fields across satell…
bl-young Oct 7, 2021
ddfe510
add back in another conformTBStoStandardSatTable to avoid tables bein…
bl-young Oct 7, 2021
1bc015b
move updating of sector names to collapseTBS
bl-young Oct 8, 2021
b30a8fd
consolidate logic for agg/disagg of satellite table
bl-young Oct 8, 2021
1a61369
update documentation
bl-young Oct 8, 2021
2b41fb4
update agg logging message
bl-young Oct 8, 2021
f6618f9
clean up
bl-young Oct 8, 2021
682135b
Merge branch 'issue_166' into electricity_disagg
bl-young Oct 8, 2021
5188a90
disaggregate prior to mapping NAICS to BEA to avoid incorrect disaggr…
bl-young Oct 8, 2021
da345b6
update disaggregation of satellite table to be more flexible to the n…
bl-young Oct 8, 2021
7a6ea77
prevent already disaggregated sectors from dropping when updating BEA…
bl-young Oct 9, 2021
391119a
fix error in appending lost flows to dataframe on data loss
bl-young Oct 9, 2021
f03b3f6
avoid allocation error when no UUIDs in sattable
bl-young Oct 9, 2021
fdc7ce4
fix error preventing disaggregation when flow present in EnvFileDF bu…
bl-young Oct 10, 2021
2579982
assign allocating sectors prior to subsetting envDF
bl-young Oct 10, 2021
85a3c38
update the sector and summary fields to match the aggregated target s…
bl-young Oct 10, 2021
4a934f0
Update usage of model crosswalk in visualization function
MoLi7 Oct 12, 2021
e4c3975
Re-org aggregation and disaggregation in load IO table step
MoLi7 Oct 12, 2021
633bdfb
add manual allocations for energy flows and SF6
bl-young Oct 13, 2021
c515469
revise logging messages
bl-young Oct 14, 2021
c4a4f79
add env allocations for "compounds"
bl-young Oct 14, 2021
9068a39
update function docstrings
bl-young Oct 14, 2021
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
370 changes: 370 additions & 0 deletions R/AggregationFunctions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,370 @@
#' Aggregate a model based on specified source file
#' @param model Model file loaded with IO tables
#' @return An aggregated model.
aggregateModel <- function (model){

logging::loginfo("Initializing Aggregation of IO tables...")
for (aggSpec in model$AggregationSpecs){
#aggregating economic tables
model$MakeTransactions <- aggregateMakeTable(model, aggSpec)
model$UseTransactions <- aggregateUseTable(model, aggSpec)
model$DomesticUseTransactions <- aggregateUseTable(model, aggSpec, domestic = TRUE)
model$UseValueAdded <- aggregateVA(model, aggSpec)
#model$FinalDemand <- aggregateFD(model, aggSpec) #todo
#model$DomesticFinalDemand <- aggregateFD(model, aggSpec) #todo
#model$MarginSectors <- aggregateMarginSectors(model, aggSpec) #todo
#model$Margins <- aggregateMargins(model, aggSpec)

#model$ValueAddedMeta <- aggregateVAMeta(model, aggSpec) #todo
#model$FinalDemandMeta <- aggregateFDMeta(model, aggSpec) #todo

#aggregating Crosswalk
model$crosswalk <- aggregateMasterCrosswalk(model, aggSpec)

#obtaining indeces to aggregate sectors in remaining model objects
agg <- aggSpec$Sectors

mainComIndex <- getIndex(model$Commodities$Code_Loc, agg[1])#first item in Aggregation is the sector to aggregate to, not to be removed
mainIndIndex <- getIndex(model$Industries$Code_Loc, agg[1])
comIndecesToAggregate <- which(model$Commodities$Code_Loc %in% agg[-1]) #find com indeces containing references to the sectors to be aggregated
indIndecesToAggregate <- which(model$Industries$Code_Loc %in% agg[-1]) #find ind indeces containing references to the sectors to be aggregated

#aggregating (i.e. removing) sectors from model lists
#aggregate Industry lists
if(length(indIndecesToAggregate)!=0){

model$Industries <- removeRowsFromList(model$Industries, indIndecesToAggregate)
model$MultiYearIndustryCPI <- aggregateMultiYearCPI(model, mainIndIndex, indIndecesToAggregate, "Industry")
model$MultiYearIndustryOutput <- aggregateMultiYearOutput(model$MultiYearIndustryOutput, mainIndIndex, indIndecesToAggregate)
}

#aggregate Commodity lists
if(length(comIndecesToAggregate !=0)){
model$Commodities <- removeRowsFromList(model$Commodities, comIndecesToAggregate)
model$MultiYearCommodityCPI <- aggregateMultiYearCPI(model, mainIndIndex, indIndecesToAggregate, "Commodity")
model$MultiYearIndustryOutput <- aggregateMultiYearOutput(model$MultiYearIndustryOutput, mainComIndex, comIndecesToAggregate)
}

model <- calculateIndustryCommodityOutput(model)

}

return(model)
}


#' Obtain aggregation specs from input files
#' @param model Model file loaded with IO tables
#' @return A model with the specified aggregation and disaggregation specs.
getAggregationSpecs <- function (model){

model$AggregationSpecs <- vector(mode = 'list')

for (configFile in model$specs$AggregationSpecs){
logging::loginfo(paste0("Loading aggregation specification file for ", configFile, "..."))
config <- getConfiguration(configFile, "agg")
if('Aggregation' %in% names(config)){
model$AggregationSpecs <- append(model$AggregationSpecs, config$Aggregation)
}

}

return(model)
}

#' Aggregate satellite tables from static file based on specs
#' @param model A complete EEIO model: a list with USEEIO model components and attributes.
#' @param aggregationSpecs Specifications for aggregation
#' @param sattable A standardized satellite table with resource and emission names from original sources.
#' @param sat The abbreviation for the satellite table.
#' @return A standardized satellite table with aggregated sectors added.
aggregateSectorsinTBS <- function (model, aggregationSpecs, sattable, sat){

newSatTable <- sattable
agg <- aggregationSpecs$Sectors

#variable to determine length of Code substring, i.e., code length minus geographic identifier and separator character (e.g. "/US")
codeLength <- nchar(gsub("/.*", "", agg[1]))
aggCodes <- substr(agg,1,codeLength)

if(any(newSatTable$Sector %in% aggCodes[-1])) {
newSatTable$Sector[which(newSatTable$Sector %in% aggCodes[-1])] <- aggCodes[1]
newSatTable <- collapseTBS(newSatTable, model)
}

return(newSatTable)
}

#' Aggregate MultiYear CPI model objects
#' @param model A complete EEIO model: a list with USEEIO model components and attributes.
#' @param mainIdex Index to aggregate the others to.
#' @param indecesToAggregate List of indeces to aggregate.
#' @param type String to designate either commodity or industry
#' @return newCPI A dataframe with the aggregatded CPI values by year.
aggregateMultiYearCPI <- function(model, mainIndex, indecesToAggregate, type){

if(type == "Industry"){
originalCPI <- model$MultiYearIndustryCPI
originalOutput <- model$MultiYearIndustryOutput

} else {
originalCPI <- model$MultiYearCommodityCPI
originalOutput <- model$MultiYearCommodityOutput
}

newCPI <- originalCPI

aggOutputs <- originalOutput[mainIndex,] + colSums(originalOutput[indecesToAggregate,])#get aggregated total for all relevant indeces (denominator for ratios)

mainIndexOutputRatios <- originalOutput[mainIndex,] / aggOutputs #get ratio of sector to be aggregated to

aggIndecesOutputRatios <- sweep(as.matrix(originalOutput[indecesToAggregate,]),2, as.matrix(aggOutputs), "/") #get ratios of sectors to be aggregated (removed)

aggIndecesOutputRatios <- data.frame(aggIndecesOutputRatios)#convert back to df.
colnames(aggIndecesOutputRatios) <- colnames(mainIndexOutputRatios)

newCPI[mainIndex,] <- newCPI[mainIndex,]*mainIndexOutputRatios + colSums(newCPI[indecesToAggregate,]*aggIndecesOutputRatios)#calculating weighted average for main CPI row

newCPI <- removeRowsFromList(newCPI, indecesToAggregate)

return(newCPI)

}


#TODO: rewrite this function to use matrix calculations when possible
#' Aggregate the MakeTable based on specified source file
#' @param model Model file loaded with IO tables
#' @param aggregationSpecs Specifications for aggregation
#' @return An aggregated MakeTable.
aggregateMakeTable <- function(model, aggregationSpecs){

agg <- aggregationSpecs$Sector

count <- 1


for (sector in agg){

if(count == 1){
count <- count + 1
next #first sector in agg is the one we are aggregating to, so skip
}

model$MakeTransactions <- aggregateSector(model, agg[1], agg[count], "Make")
count <- count + 1
}

#remove rows and cols from model
agg <- agg[-1]
model$MakeTransactions <- model$MakeTransactions[!(rownames(model$MakeTransactions)) %in% agg,] #remove rows from model that have the same rownames as values in agg list
model$MakeTransactions <- model$MakeTransactions[,!(colnames(model$MakeTransactions)) %in% agg] #as above but with cols

return(model$MakeTransactions)

}


#TODO: rewrite this function to use matrix calculations when possible
#' Aggregate the UseTable based on specified source file
#' @param model Model file loaded with IO tables
#' @param aggregationSpecs Specifications for aggregation
#' @param domestic Boolean to indicate whether to aggregate the UseTransactions or DomesticUseTransactions table
#' @return An aggregated UseTransactions or DomesticUseTransactions Table.
aggregateUseTable <- function(model, aggregationSpecs, domestic = FALSE){

agg <- aggregationSpecs$Sectors

count <- 1

for (sector in agg){

if(count == 1){
count <- count + 1
next #first sector in agg is the one we are aggregating to, so skip
}

if(domestic == TRUE){
model$DomesticUseTransactions <- aggregateSector(model, agg[1], agg[count], "Use", domestic)
}else{
model$UseTransactions <- aggregateSector(model, agg[1], agg[count], "Use", domestic = FALSE)
}

count <- count + 1
}

#remove rows and cols from model
agg <- agg[-1]

if(domestic == TRUE){

table <- model$DomesticUseTransactions[!(rownames(model$DomesticUseTransactions)) %in% agg,] #remove rows from model that have the same rownames as values in agg list
table <- model$DomesticUseTransactions[,!(colnames(model$DomesticUseTransactions)) %in% agg] #as above but with cols

}else{
table <- model$UseTransactions[!(rownames(model$UseTransactions)) %in% agg,] #remove rows from model that have the same rownames as values in agg list
table <- model$UseTransactions[,!(colnames(model$UseTransactions)) %in% agg] #as above but with cols

}

return(table)

}


#TODO: rewrite this function to use matrix calculations when possible
#' Aggregate the MakeTable based on specified source file
#' @param model A complete EEIO model: a list with USEEIO model components and attributes.
#' @param aggregationSpecs Specifications for aggregation
#' @return An aggregated MakeTable.
aggregateVA <- function(model, aggregationSpecs){

agg <- aggregationSpecs$Sectors

count <- 1

for (sector in agg){

if(count == 1){
count <- count + 1
next #first sector in agg is the one we are aggregating to, so skip
}

model$UseValueAdded <- aggregateSector(model, agg[1], agg[count], "VA")
count <- count + 1
}

#remove rows and cols from model
agg <- agg[-1]
model$UseValueAdded <- model$UseValueAdded[!(rownames(model$UseValueAdded)) %in% agg,] #remove rows from model that have the same rownames as values in agg list
model$UseValueAdded <- model$UseValueAdded[,!(colnames(model$UseValueAdded)) %in% agg] #as above but with cols

return(model$UseValueAdded)

}



#' Aggregate a sector in a table
#' @param model Model file loaded with IO tables
#' @param mainSector Sector to aggregate to (string)
#' @param sectorToRemove Sector to be aggregated into mainSector, then removed from table (string)
#' @param tableType String to designate either Make or Use table
#' @param domestic Boolean to indicate whether to aggregate the UseTransactions or DomesticUseTransactions table
#' @return aggregated table
aggregateSector <- function(model, mainSector, sectorToRemove, tableType, domestic = FALSE){
#get correct indeces
if(tableType == "Use") {
mainRowIndex <- getIndex(model$Commodities$Code_Loc, mainSector)
mainColIndex <- getIndex(model$Industries$Code_Loc, mainSector)

removeRowIndex <- getIndex(model$Commodities$Code_Loc, sectorToRemove)
removeColIndex <- getIndex(model$Industries$Code_Loc, sectorToRemove)

if(domestic == TRUE) {
table <- model$DomesticUseTransactions
} else {
table <- model$UseTransactions
}

} else if(tableType == "Make") {
mainRowIndex <- getIndex(model$Industries$Code_Loc, mainSector)
mainColIndex <- getIndex(model$Commodities$Code_Loc, mainSector)

removeRowIndex <- getIndex(model$Industries$Code_Loc, sectorToRemove)
removeColIndex <- getIndex(model$Commodities$Code_Loc, sectorToRemove)

table <- model$MakeTransactions

} else if(tableType == "VA") {
mainRowIndex <- getIndex(model$ValueAddedMeta$Code_Loc, mainSector)
mainColIndex <- getIndex(model$Industries$Code_Loc, mainSector)

removeRowIndex <- getIndex(model$ValueAddedMeta$Code_Loc, sectorToRemove)
removeColIndex <- getIndex(model$Industries$Code_Loc, sectorToRemove)

table <- model$UseValueAdded

} else {
#continue
}

if(length(removeRowIndex) != 0 && length(mainRowIndex) !=0){#if there a row to remove and merge with the main sector
table[mainRowIndex,] <- table[mainRowIndex,] + table[removeRowIndex,]#add rows together
table[removeRowIndex,] <- table[removeRowIndex,] - table[removeRowIndex,]# subtract row from itself to ensure table total is unchanged
}

if(length(removeColIndex)!=0 && length(mainColIndex) !=0){#if there a col to remove and merge with the main sector
table[,mainColIndex] <- table[,mainColIndex] + table[,removeColIndex]#add cols together
table[,removeColIndex] <- table[,removeColIndex] - table[,removeColIndex]# subtract col from itself to ensure table total is unchanged#need to check this line to make sure sum removecol = 0

}

return(table)
}


#' Return the index where a sector occurrs in a sectorList
#' @param sectorList Dataframe (of strings) to match the index of the sector param
#' @param sector String of the sector to look the index for
#' @return Index of sector in sectorList
getIndex <- function(sectorList, sector){
index <- which(sectorList %in% sector)

return(index)

}


#' Aggregate the MasterCrosswalk on the selected sectors
#' @param model A complete EEIO model: a list with USEEIO model components and attributes.
#' @param aggregationSpecs Specifications for aggregation
#' @return crosswalk with aggregated sectors removed
aggregateMasterCrosswalk <- function (model, aggregationSpecs){

agg <- aggregationSpecs$Sectors

new_cw <- model$crosswalk #variable to return with complete changes to crosswalk#temp

secLength <- regexpr(pattern ='/',agg[1]) - 1 #used to determine the length of the sector codes. E.g., detail would be 6, while summary would generally be 3 though variable, and sector would be variable
# Update the sector and summary fields to match the aggregated sector
new_cw$BEA_Sector[which(new_cw$USEEIO %in% substr(agg[-1],1,secLength))] <- new_cw$BEA_Sector[new_cw$USEEIO == substr(agg[1],1,secLength)][1]
new_cw$BEA_Summary[which(new_cw$USEEIO %in% substr(agg[-1],1,secLength))] <- new_cw$BEA_Summary[new_cw$USEEIO == substr(agg[1],1,secLength)][1]
# Update the value in USEEIO column with the aggregated sector
new_cw$USEEIO[which(new_cw$USEEIO %in% substr(agg[-1],1,secLength))] <- substr(agg[1],1,secLength)

return(new_cw)

}

#' Remove specific rows from the specified list object in the model
#' @param sectorList Model object to be aggregated
#' @param indencesToAggregate List of indeces of sectors to remove from list (i.e. aggregated sectors)
#' @return An aggregated sectorList
removeRowsFromList <- function(sectorList, indecesToAggregate){

newList <- sectorList[-(indecesToAggregate),]

return(newList)

}


#' Aggregate MultiYear Output model objects
#' @param originalOutput MultiYear Output dataframe
#' @param mainIdex Index to aggregate the others to.
#' @param indecesToAggregate List of indeces to aggregate.
#' @return model A dataframe with the disaggregated GDPGrossOutputIO by year.
aggregateMultiYearOutput <- function(originalOutput, mainIndex, indecesToAggregate){

newOutput <- originalOutput

newOutput[mainIndex,] <- originalOutput[mainIndex,]+colSums(originalOutput[indecesToAggregate,])
newOutput <- newOutput[-(indecesToAggregate),]

return(newOutput)

}



Loading