From 183495a57c314b354b30f2e9348e4944051e4441 Mon Sep 17 00:00:00 2001 From: Brandon Istenes Date: Tue, 10 Dec 2024 23:30:25 -0500 Subject: [PATCH 1/3] Add get employment table function. Improve loadStateIODataFile docs --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/StateSupplyFunctions.R | 17 +++++++++++++++++ R/UtilityFunctions.R | 7 ++++++- man/getStateEmploymentTable.Rd | 17 +++++++++++++++++ man/loadStateIODataFile.Rd | 4 +++- 6 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 man/getStateEmploymentTable.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 12b1ea70..afba51b6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -44,6 +44,6 @@ Remotes: License: file LICENSE Encoding: UTF-8 LazyData: true -RoxygenNote: 7.2.0 +RoxygenNote: 7.3.1 Suggests: testthat (>= 2.1.0) diff --git a/NAMESPACE b/NAMESPACE index 69ca41a8..6af465fb 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ export(buildFullTwoRegionIOTable) export(buildStateSupplyModel) export(buildStateUseModel) export(buildTwoRegionUseModel) +export(getStateEmploymentTable) export(getVectorOfCodes) export(loadStateIODataFile) export(plotICFandRPC) diff --git a/R/StateSupplyFunctions.R b/R/StateSupplyFunctions.R index 21bb3e73..28865c8b 100644 --- a/R/StateSupplyFunctions.R +++ b/R/StateSupplyFunctions.R @@ -295,6 +295,23 @@ estimateStateCommodityOutputRatiofromAlternativeSources <- function(year) { return(StateCommodityOutputRatio) } +#' Get a table of state employment by BEA Summary sector. +#' @param year A numeric value specifying the year of interest. +#' @return A data frame containing employment count by state and BEA Summary sector. +#' @export +getStateEmploymentTable <- function(year) { + BEAStateEmp <- loadStateIODataFile(paste0("State_Employment_", year)) + loadBEAStateDatatoBEASummaryMapping("Employment") + EmptoBEAmapping <- loadBEAStateDatatoBEASummaryMapping("Employment") + BEAStateEmp <- merge(BEAStateEmp[, c("GeoName", "LineCode", as.character(year))], + EmptoBEAmapping, by = "LineCode") + BEAStateEmp <- stats::aggregate(BEAStateEmp[, as.character(year)], + by = list(BEAStateEmp$BEA_2012_Summary_Code, + BEAStateEmp$GeoName), sum) + colnames(BEAStateEmp) <- c("BEA_2012_Summary_Code", "State", "Emp") + return(BEAStateEmp) +} + #' Load BEA State Employment data from pre-saved .rds files and State Employment #' FlowBySector data from flowsa. #' Map to BEA Summary sectors. diff --git a/R/UtilityFunctions.R b/R/UtilityFunctions.R index 21eb13ad..83a24952 100644 --- a/R/UtilityFunctions.R +++ b/R/UtilityFunctions.R @@ -275,6 +275,11 @@ findLatestStateIODatainLocalDirectory <- function(filename) { } #' Load StateIO data file from Data Commons or local data directory. +#' +#' If the file is not found locally, it will be downloaded from Data Commons. Available +#' files can be found on https://dmap-data-commons-ord.s3.amazonaws.com/index.html#stateio/. +#' These include Use, DomesticUse, and IndustryOutput tables, among others. +#' #' @param filename A string specifying filename, e.g. "State_Summary_Use_2017". #' @param ver A string specifying version of the data, default is NULL, can be "v0.1.0". #' @return A StateIO data product (usually a list of dataframes). @@ -300,7 +305,7 @@ loadStateIODataFile <- function(filename, ver = NULL) { "not found in local data directory, either.")) message("Please confirm ", filename, " is correctly spelled. ", "You should be able to find the correctly spelled file on ", - "https://dmap-data-commons-ord.s3.amazonaws.com/index.html?prefix=stateio/. ", + "https://dmap-data-commons-ord.s3.amazonaws.com/index.html#stateio/. ", "If it's not found there, please open an issue at ", "https://github.com/USEPA/stateior/issues/new ", "and inform package maintainers.\n", diff --git a/man/getStateEmploymentTable.Rd b/man/getStateEmploymentTable.Rd new file mode 100644 index 00000000..4e023797 --- /dev/null +++ b/man/getStateEmploymentTable.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/StateSupplyFunctions.R +\name{getStateEmploymentTable} +\alias{getStateEmploymentTable} +\title{Get a table of state employment by BEA Summary sector.} +\usage{ +getStateEmploymentTable(year) +} +\arguments{ +\item{year}{A numeric value specifying the year of interest.} +} +\value{ +A data frame containing employment count by state and BEA Summary sector. +} +\description{ +Get a table of state employment by BEA Summary sector. +} diff --git a/man/loadStateIODataFile.Rd b/man/loadStateIODataFile.Rd index 8d51601a..ab2f7803 100644 --- a/man/loadStateIODataFile.Rd +++ b/man/loadStateIODataFile.Rd @@ -15,5 +15,7 @@ loadStateIODataFile(filename, ver = NULL) A StateIO data product (usually a list of dataframes). } \description{ -Load StateIO data file from Data Commons or local data directory. +If the file is not found locally, it will be downloaded from Data Commons. Available +files can be found on https://dmap-data-commons-ord.s3.amazonaws.com/index.html#stateio/. +These include Use, DomesticUse, and IndustryOutput tables, among others. } From 2f0d5e562f2f1f595ef2f1f5b45bd4712da2dc75 Mon Sep 17 00:00:00 2001 From: Brandon Istenes Date: Thu, 12 Dec 2024 12:32:33 -0500 Subject: [PATCH 2/3] Use getStateEmploymentTable in getStateEmploymentbyBEASummary --- R/StateSupplyFunctions.R | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/R/StateSupplyFunctions.R b/R/StateSupplyFunctions.R index 28865c8b..60a21efc 100644 --- a/R/StateSupplyFunctions.R +++ b/R/StateSupplyFunctions.R @@ -301,7 +301,6 @@ estimateStateCommodityOutputRatiofromAlternativeSources <- function(year) { #' @export getStateEmploymentTable <- function(year) { BEAStateEmp <- loadStateIODataFile(paste0("State_Employment_", year)) - loadBEAStateDatatoBEASummaryMapping("Employment") EmptoBEAmapping <- loadBEAStateDatatoBEASummaryMapping("Employment") BEAStateEmp <- merge(BEAStateEmp[, c("GeoName", "LineCode", as.character(year))], EmptoBEAmapping, by = "LineCode") @@ -318,17 +317,7 @@ getStateEmploymentTable <- function(year) { #' @param year A numeric value between 2007 and 2017 specifying the year of interest. #' @return A data frame contains State Employment by BEA Summary. getStateEmploymentbyBEASummary <- function(year) { - # BEA State Emp - BEAStateEmp <- loadStateIODataFile(paste0("State_Employment_", year), - ver = model_ver) - EmptoBEAmapping <- loadBEAStateDatatoBEASummaryMapping("Employment") - BEAStateEmp <- merge(BEAStateEmp[, c("GeoName", "LineCode", as.character(year))], - EmptoBEAmapping, by = "LineCode") - # Aggregate StateEmployment by BEA - BEAStateEmp <- stats::aggregate(BEAStateEmp[, as.character(year)], - by = list(BEAStateEmp$BEA_2012_Summary_Code, - BEAStateEmp$GeoName), sum) - colnames(BEAStateEmp) <- c("BEA_2012_Summary_Code", "State", "Emp") + BEAStateEmp <- getStateEmploymentTable(year) # Employment FlowBySector from flowsa EmpFBS <- getFlowsaData("Employment", year) EmpFBS <- mapFlowBySectorfromNAICStoBEA(EmpFBS, year, "Summary") From 80d5fe656b8121a24c6b4cddbdcf09e7c077a518 Mon Sep 17 00:00:00 2001 From: Brandon Istenes Date: Thu, 12 Dec 2024 12:54:38 -0500 Subject: [PATCH 3/3] Add documentation for employment and industry output --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index fba50d71..3ca84964 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,28 @@ writeStateIODatatoCSV <- ("State_Summary_DomesticUse_2012", "Georgia", outputfol writeStateIODatatoCSV <- ("TwoRegion_Summary_DomesticUse_2012", "Georgia", outputfolder) ``` +Employment data is also available for each state and year, containing employment by BEA Summary sector. + +```r +Employment_2012 <- getStateEmploymentTable(2012) +# BEA_2012_Summary_Code State Emp +# 1 111CA Alabama 38191 +# 2 113FF Alabama 15773 +# 3 211 Alabama 4334 +# 4 212 Alabama 8205 +``` + +Industry output data is also available, and can be used to calculate the direct requirements matrix. + +```r +OneRegionIndustryOutput_2012 <- loadStateIODataFile("State_Summary_IndustryOutput_2012") +GA_IndustryOutput_2012 <- OneRegionIndustryOutput_2012[["Georgia"]] +# Drop extra rows from the DomesticUse table; these do not correspond to actual BEA Summary industries. +GA_DomesticUse_2012 <- GA_DomesticUse_2012[!rownames(GA_DomesticUse_2012) %in% c("V001", "V002", "V003", "Used", "Other"), ] +# Calculate the direct requirements matrix +GA_DirectRequirements_2012 <- GA_DomesticUse_2012 / GA_IndustryOutput_2012$Output +``` + ### Use for Developers (usage type #2) For studying, replicating or modify the code, users will want to clone or copy the source code and review the code in R. See more in [Instructions for Developers](https://github.com/USEPA/stateior/wiki/Instructions-for-developers).