From 6edd2b179b73ee0bb6891ce693e556759c9eb97c Mon Sep 17 00:00:00 2001 From: Karen Noiva Date: Tue, 2 Jul 2024 16:40:11 -0400 Subject: [PATCH 1/3] Updated documentation Updated package documentation to correct some things related to the new population data --- FrEDI/DESCRIPTION | 2 +- FrEDI/R/FrEDI-package.R | 6 ++--- FrEDI/R/data.R | 4 +-- FrEDI/R/run_fredi_sv.R | 55 +++++++++++++++++++---------------------- 4 files changed, 31 insertions(+), 36 deletions(-) diff --git a/FrEDI/DESCRIPTION b/FrEDI/DESCRIPTION index 20836454..9c44e0be 100644 --- a/FrEDI/DESCRIPTION +++ b/FrEDI/DESCRIPTION @@ -1,6 +1,6 @@ Package: FrEDI Title: The Framework for Evaluating Damages and Impacts (FrEDI) -Version: 4.0.1 +Version: 4.1.0 Authors@R: c(person("Corinne", "Hartin", email = "hartin.corinne@epa.gov", role = c("aut","cre"), diff --git a/FrEDI/R/FrEDI-package.R b/FrEDI/R/FrEDI-package.R index a4f930fe..845cc135 100644 --- a/FrEDI/R/FrEDI-package.R +++ b/FrEDI/R/FrEDI-package.R @@ -41,12 +41,12 @@ #' * __help__ and __html__. Contain documentation for functions available to the user, including function descriptions, lists of arguments and outputs, and examples. See `"html/00Index.html"` or the individual R help functions for more information about individual functions. #' * __Meta__. Contains RDS files (ending in `".rds"`) with information about the package contents. #' * __extdata__. __extdata/scenarios__ Contains four CSV files for users to test the function for importing data. For more information on importing scenarios for use with [FrEDI::run_fredi()], refer to documentation for the function [FrEDI::import_inputs()]. -#' * `"GCAM_scenario.csv"` contains a set of temperature scenarios that can be used with FrEDI, including the default temperature scenario used by both [FrEDI::run_fredi()] and [FrEDI::run_fredi_sv()] (labeled `"description="Hector_GCAM_v5.3_ECS_3.0_REF"` in `"slr_from_GCAM.csv"` and `"ECS_3.0_ref_0"` in the [FrEDI::gcamScenarios] dataset)). Also see the documentation for the [FrEDI::gcamScenarios] dataset for more information. -#' * `"State ICLUS Population.csv"` and `"Region ICLUS Population.csv"` contain the default population scenarios used by [FrEDI::run_fredi()] and [FrEDI::run_fredi_sv()], respectively (see [FrEDI::popScenario] and [FrEDI::popScenario_sv]). +#' * `"GCAM_scenario.csv"` contains a set of temperature scenarios that can be used with FrEDI, including the default temperature scenario used by both [FrEDI::run_fredi()] and [FrEDI::run_fredi_sv()]. Also see documentation for the [FrEDI::gcamScenarios] dataset for more information. +#' * `"State ICLUS Population.csv"` contains the default state population scenario used by [FrEDI::run_fredi()] (see [FrEDI::popScenario] and [FrEDI::popScenario_sv]). #' * `"slr_from_GCAM.csv"` contains global mean sea level rise heights in centimeters (created from the reference temperature scenario). #' * __extdata/sv__ Contains files used by the `FrEDI` SV module to calculate impacts. #' -#' The `FrEDI` package contains a loadable dataset with default results `defaultResults`, which contains annual impacts produced by [FrEDI::run_fredi()] for the with the default options and default scenarios (i.e., default temperature, GDP, and state population trajectories). Other loadable datasets provided by FrEDI are a set of driver scenarios ([FrEDI::gcamScenarios]), a state population scenario ([FrEDI::popScenario]) for use with [FrEDI::run_fredi()] or [FrEDI::import_inputs()], and a regional population scenario (`popScenario_sv`) for use with [FrEDI::run_fredi_sv()]. These data can be loaded into the workspace using the `data()` function. +#' The `FrEDI` package contains a loadable dataset with default results `defaultResults`, which contains annual impacts produced by [FrEDI::run_fredi()] for the with the default options and default scenarios (i.e., default temperature, GDP, and state population trajectories). Other loadable datasets provided by FrEDI are a set of driver scenarios ([FrEDI::gcamScenarios]) and a state population scenario ([FrEDI::popScenario]) for use with [FrEDI::run_fredi()] or [FrEDI::run_fredi_sv()], which can be loaded into the workspace using the `data()` function (e.g., `data(gcamScenarios)`). #' #' Typical use will involve `library(FrEDI)` or `require(FrEDI)`. #' diff --git a/FrEDI/R/data.R b/FrEDI/R/data.R index e5022202..8e51d13c 100644 --- a/FrEDI/R/data.R +++ b/FrEDI/R/data.R @@ -3,7 +3,7 @@ # #' A dataframe containing the default outputs of [FrEDI::run_fredi()] # -#' @format A data frame with 775,179 rows and 20 columns: +#' @format A data frame with 1,501,500 rows and 20 columns: #' \describe{ #' \item{sector}{Name of the associated sector.} #' \item{variant}{Name of the associated variant or adaptation (values are sector-specific).} @@ -36,7 +36,7 @@ #' #' The scenarios in this dataframe were created using [Hector](https://jgcri.github.io/hector/), an open-source, reduced-form global carbon-cycle climate model (Hartin et al., 2015) to model temperatures associated with emissions scenarios from the Global Change Analysis Model v5.3 (GCAM). The Global Change Analysis Model v5.3 ([GCAM](https://gcims.pnnl.gov/modeling/gcam-global-change-analysis-model)) is an open source model that represents the linkages between energy, water, land, climate and economic systems (Calvin et al., 2019). #' -#' These temperature scenarios have the original global temperatures. Users must convert to CONUS temperatures using the [FrEDI::convertTemps] (with argument `from="global"`) before passing the scenarios to [FrEDI::run_fredi()] or [FrEDI::run_fredi_sv()]. *Please note that the `gcamScenarios` should be subset to any of the individual scenarios specified in the `scenario` column before passing to [FrEDI::run_fredi()] (e.g., `gcamScenarios |> dplyr::filter(scenario=="ECS_3.0_ref_0")` for the reference scenario). +#' These temperature scenarios have the original global temperatures. Users must convert to CONUS temperatures using the [FrEDI::convertTemps] (with argument `from = "global"`) before passing the scenarios to [FrEDI::run_fredi()] or [FrEDI::run_fredi_sv()]. *Please note that the `gcamScenarios` should be subset to any of the individual scenarios specified in the `scenario` column before passing to [FrEDI::run_fredi()] (e.g., `gcamScenarios |> dplyr::filter(scenario=="Hector_GCAM_v5.3_ECS_3.0_REF")` for the default scenario). #' #' Calvin, K., Patel, P., Clarke, L., et al. 2019. GCAM v5.1: representing the linkages between energy, water, land, climate, and economic systems, Geosci. Model Dev., 12:677–698. https://doi.org/10.5194/gmd-12-677-2019. #' diff --git a/FrEDI/R/run_fredi_sv.R b/FrEDI/R/run_fredi_sv.R index 96b22fb9..3b9d3eed 100644 --- a/FrEDI/R/run_fredi_sv.R +++ b/FrEDI/R/run_fredi_sv.R @@ -2,51 +2,49 @@ #' Calculate climate change impacts on socially vulnerable (SV) populations throughout the 21st century for available sectors #' #' @description -#' `run_fredi_sv` allows users to project annual average climate change impacts throughout the 21st century (2010-2090) for socially vulnerable (SV) populations for available sectors. Users can run [FrEDI::run_fredi_sv()] for individual sectors to generate annual physical impacts for SV populations. [FrEDI::run_fredi_sv()] can be run with default population and climate (temperature and sea level rise trajectories) or provide custom trajectories. The output of [FrEDI::run_fredi_sv()] is an R data frame object containing annual average physical impacts at five-year increments for the period 2010 to 2090. Users have the option to write outputs to Excel files that provide additional visualization of SV outputs. +#' `run_fredi_sv` allows users to project annual average climate change impacts throughout the 21st century (2010-2100) for socially vulnerable (SV) populations for available sectors. Users can run [FrEDI::run_fredi_sv()] for individual sectors to generate annual physical impacts for SV populations. [FrEDI::run_fredi_sv()] can be run with default population and climate (temperature and sea level rise trajectories) or provide custom trajectories. The output of [FrEDI::run_fredi_sv()] is an R data frame object containing annual average physical impacts at five-year increments for the period 2010 to 2100. Users have the option to write outputs to Excel files that provide additional visualization of SV outputs. +#' +#' #' #' @param sector A character string indicating the sector for which the FrEDI SV module should calculate impacts (see [FrEDI::get_sv_sectorInfo()] for a list of available sectors). #' -#' @param driverInput A data frame of up to four custom scenarios for drivers (temperature or global mean sea level rise). `driverInput` requires a data frame with columns of `"year"` and `"scenario"`. The data frame must also include a third column: `"temp_C"` for temperature-driven sectors (containing temperature values in degrees Celsius of warming for the contiguous U.S.) or `"slr_cm"` for sea level rise (SLR)-driven sectors (containing values for global mean sea level rise in centimeters). Run `get_sv_sectorInfo(gcmOnly=TRUE)` to see temperature-driven sectors in the SV module and `get_sv_sectorInfo(slrOnly=TRUE)` to see SLR-driven scenarios. Users can also pass a data frame with all four columns (`"year"`, `"scenario"`, `"temp_C"`, and `"slr_cm"`), in which case [FrEDI::run_fredi_sv()] determines whether to use the `"temp_C"` or `"slr_cm"` column as the driver trajectory based on the specified sector. Driver inputs for all scenarios should start in the year 2000 or earlier. All scenarios must include at least two non-missing values (especially values before or at 2000 and at or after 2090). If any required columns are missing, [FrEDI::run_fredi_sv()] will use the default temperature or sea level rise scenario from [FrEDI::run_fredi()]. If the data frame passed to `driverInput` has more than four unique scenarios, [FrEDI::run_fredi_sv()] will only run the first four scenarios. +#' @param driverInput A data frame of up to four custom scenarios for drivers (temperature or global mean sea level rise). `driverInput` requires a data frame with columns of `"year"` and `"scenario"`. The data frame must also include a third column: `"temp_C"` for temperature-driven sectors (containing temperature values in degrees Celsius of warming for the contiguous U.S.) or `"slr_cm"` for sea level rise (SLR)-driven sectors (containing values for global mean sea level rise in centimeters). Run `get_sv_sectorInfo(gcmOnly=TRUE)` to see temperature-driven sectors in the SV module and `get_sv_sectorInfo(slrOnly=TRUE)` to see SLR-driven scenarios. Users can also pass a data frame with all four columns (`"year"`, `"scenario"`, `"temp_C"`, and `"slr_cm"`), in which case [FrEDI::run_fredi_sv()] determines whether to use the `"temp_C"` or `"slr_cm"` column as the driver trajectory based on the specified sector. Driver inputs for all scenarios should start in the year 2000 or earlier. All scenarios must include at least two non-missing values (especially values before or at 2000 and at or after 2100). If any required columns are missing, [FrEDI::run_fredi_sv()] will use the default temperature or sea level rise scenario from [FrEDI::run_fredi()]. If the data frame passed to `driverInput` has more than four unique scenarios, [FrEDI::run_fredi_sv()] will only run the first four scenarios. #' -#' @param popInput The input population scenario requires state-level population values. Population values must be greater than or equal to zero. -#' * The population scenario must be a data frame object with five columns with names `"year"`, `"region"`, `"state"`, `"postal"`, and `"state_pop"` containing the year, the NCA region name, the state name, the postal code abbreviation (e.g., "ME" for "Maine") for the state, and the state population, respectively. -#' * `popInput` only accepts a data frame with a single scenario; [FrEDI::run_fredi_sv()] uses the same population scenario for any and all driver scenarios in the data frame passed to `driverInput`. -#' * Population inputs must have at least one non-missing value in 2010 or earlier and at least one non-missing value in or after the final analysis year (2100). +#' @param popInput The input population scenario requires a data frame object with a single scenario of state-level population values. +#' * The population scenario must have five columns with names `"year"`, `"region"`, `"state"`, `"postal"`, and `"state_pop"` containing the year, the NCA region name, the state name, the postal code abbreviation (e.g., "ME" for "Maine") for the state, and the state population, respectively. +#' * `popInput` only accepts a a single scenario, in contrast to `driverInput`. In other words, [FrEDI::run_fredi_sv()] uses the same population scenario for any and all driver scenarios passed to `driverInput`. #' * If the user does not specify an input scenario for population (i.e., `popInput = NULL`, [FrEDI::run_fredi_sv()] uses a default population scenario. +#' * Population inputs must have at least one non-missing value in 2010 or earlier and at least one non-missing value in or after the final analysis year (2100). +#' * Population values must be greater than or equal to zero. +#' #' @param silent A logical (`TRUE/FALSE`) value indicating the level of messaging desired by the user (defaults to `silent=TRUE`). # @param return=TRUE A `TRUE/FALSE` value indicating whether to return the results as a data frame (default=`TRUE`). - -#' @param save A logical (`TRUE/FALSE`) value indicating whether to save the results to an Excel file (defaults to `save=FALSE`). #' -#' @param outpath A character string indicating a file directory to save the Excel file. Defaults to the working directory (i.e., `outpath=getwd()`). If the directory specified by `outpath` does not exist, [FrEDI::run_fredi_sv()] will attempt to create the specified directory. #' -#' @param overwrite A logical (`TRUE/FALSE`) value indicating whether to overwrite an existing Excel file if `save=TRUE` (defaults to `overwrite=FALSE`). If `overwrite=FALSE`, [FrEDI::run_fredi_sv()] will not automatically overwrite an existing Excel file; however, if a file exists and `overwrite=FALSE`, [FrEDI::run_fredi_sv()] will message the user and the user will have the option to overwrite the existing file. If `overwrite=TRUE` and the Excel file exists in the output directory, [FrEDI::run_fredi_sv()] will overwrite the existing file without messaging the user. #' -#' @param addDate A logical (`TRUE/FALSE`) value indicating whether to add the date to the name of the output Excel file if `save=TRUE` (defaults to `addDate=FALSE`). If `save=TRUE` and `addDate=TRUE`, [FrEDI::run_fredi_sv()] will append the system date to the beginning of the name of the outputs Excel file using the format `"%Y%m%d"` (see [base::format()] and [base::Sys.Date()] for additional information). -# @param libPath=.libPaths()[1] Path to R library containing the FrEDI package files. Defaults to the first path in `.libPaths()`. -#' -#' -#' @details [FrEDI::run_fredi_sv()] projects annual climate change impacts for socially vulnerable (SV) populations throughout the 21st century (2010-2090) for available sectors, using default or user-specified population, temperature, and sea level rise (SLR) trajectories. [FrEDI::run_fredi_sv()] is the main function for the FrEDI Social Vulnerability (SV) module in the [FrEDI] R package, described elsewhere (See for more information). The SV module extends the [FrEDI] framework to socially vulnerable populations using data underlying a 2021 U.S. Environmental Protection Agency (EPA) report on [Climate Change and Social Vulnerability in the United States](https://www.epa.gov/cira/social-vulnerability-report/). +#' @details [FrEDI::run_fredi_sv()] projects annual climate change impacts for socially vulnerable (SV) populations throughout the 21st century (2010-2100) for available sectors, using default or user-specified population, temperature, and sea level rise (SLR) trajectories. [FrEDI::run_fredi_sv()] is the main function for the FrEDI Social Vulnerability (SV) module in the [FrEDI] R package, described elsewhere (See for more information). The SV module extends the [FrEDI] framework to socially vulnerable populations using data underlying a 2021 U.S. Environmental Protection Agency (EPA) report on [Climate Change and Social Vulnerability in the United States](https://www.epa.gov/cira/social-vulnerability-report/). #' #' Users can run [FrEDI::run_fredi_sv()] to generate annual physical impacts for SV groups for individual sectors. When running [FrEDI::run_fredi_sv()], users must specify one of the sectors in the SV module; use [FrEDI::get_sv_sectorInfo()] for a list of available sectors. #' #' [FrEDI::run_fredi_sv()] can be run with default population and climate (temperature and SLR) trajectories or use [FrEDI::run_fredi_sv()] to run custom scenarios. Running [FrEDI::run_fredi_sv()] with custom climate scenarios requires passing a data frame of scenarios to the `driverInput` argument. [FrEDI::run_fredi_sv()] can also be run with a custom population scenario by passing a data frame of regional population trajectories to the `popInput` argument; unlike climate scenarios, [FrEDI::run_fredi_sv()] will only run a single scenario at a time. #' -#' * `driverInput` can take a data frame containing up to four custom scenarios for drivers (temperature or global mean sea level rise). `driverInput` requires a data frame with columns of `"year"` and `"scenario"`. The data frame must also include a third column: `"temp_C"` for temperature-driven sectors (containing temperature values in degrees Celsius of warming for the contiguous U.S.) or `"slr_cm"` for sea level rise (SLR)-driven sectors (containing values for global mean sea level rise in centimeters). Run `get_sv_sectorInfo(gcmOnly=TRUE)` to see temperature-driven sectors in the SV module and `get_sv_sectorInfo(slrOnly=TRUE)` to see SLR-driven scenarios. Users can also pass a data frame with all four columns (`"year"`, `"scenario"`, `"temp_C"`, and `"slr_cm"`), in which case [FrEDI::run_fredi_sv()] determines whether to use the `"temp_C"` or `"slr_cm"` column as the driver trajectory based on the specified sector. If any required columns are missing, [FrEDI::run_fredi_sv()] will use the default temperature or sea level rise scenario from [FrEDI::run_fredi()]. If the data frame passed to `driverInput` has more than four unique scenarios, [FrEDI::run_fredi_sv()] will only run the first four scenarios. +#' * `driverInput` can take a data frame containing up to four custom scenarios for drivers (temperature or global mean sea level rise). `driverInput` requires a data frame with columns of `"year"` and `"scenario"`. The data frame must also include a third column: `"temp_C"` for temperature-driven sectors (containing temperature values in degrees Celsius of warming for the contiguous U.S.) or `"slr_cm"` for sea level rise (SLR)-driven sectors (containing values for global mean sea level rise in centimeters). Run `get_sv_sectorInfo(gcmOnly = TRUE)` to see temperature-driven sectors in the SV module and `get_sv_sectorInfo(slrOnly = TRUE)` to see SLR-driven scenarios. Users can also pass a data frame with all four columns (`"year"`, `"scenario"`, `"temp_C"`, and `"slr_cm"`), in which case [FrEDI::run_fredi_sv()] determines whether to use the `"temp_C"` or `"slr_cm"` column as the driver trajectory based on the specified sector. If any required columns are missing, [FrEDI::run_fredi_sv()] will use the default temperature or sea level rise scenario from [FrEDI::run_fredi()]. If the data frame passed to `driverInput` has more than four unique scenarios, [FrEDI::run_fredi_sv()] will only run the first four scenarios. #' * Temperature inputs must be temperature change in degrees Celsius for the contiguous U.S. (use [FrEDI::convertTemps()] to convert global temperatures to CONUS temperatures before passing to `driverInput`) relative to a 1995 baseline (where 1995 is the central year of a 1986-2005 baseline period; values should start at zero in the year 1995). -#' * Sea level rise inputs must be in centimeters relative to a 2000 baseline (i.e., values should start at zero in the year 2000). Driver inputs for all scenarios should start in the year 2000 or earlier. All scenarios must include at least two non-missing values (especially values before or at 2000 and at or after 2090). -#' * `popInput` can take a data frame containing a single scenario with regional population trajectories for each of the seven regions for the contiguous U.S. (`"Midwest"`, `"Northeast"`, `"Northern Plains"`, `"Northwest"`, `"Southeast"`, `"Southern Plains"`, `"Southwest"`) as defined by the [National Climate Assessment (NCA)](https://scenarios.globalchange.gov/regions_nca4). The data frame passed to `popInput` should have columns `"year"`, `"region"`, and `"reg_pop"`, which respectively contain values for year, NCA region name, and regional population. `popInput` only accepts a data frame with a single scenario; [FrEDI::run_fredi_sv()] uses the same population scenario for any and all driver scenarios in the data frame passed to `driverInput`. If `popInput=NULL` (default), [FrEDI::run_fredi_sv()] will use the default regional population trajectories. The default regional population scenario is drawn from the Integrated Climate and Land Use Scenarios version 2 (ICLUSv2) model (Bierwagen et al, 2010; EPA 2017) under the Median variant projection of United Nations (United Nations, 2015). Note that the FrEDI SV default population scenario differs from the default population scenario used by [FrEDI::run_fredi()]. -#' -#' The output of [FrEDI::run_fredi_sv()] is an R data frame object containing NCA region-specific annual average physical impacts for socially vulnerable groups at five-year increments between 2010 and 2090. Users have the additional option to write results to an Excel file by setting `save=TRUE`; output Excel files provide basic visualizations of output data. Additional arguments provide more control over how the outputs are saved if `save=TRUE`: +#' * Sea level rise inputs must be in centimeters relative to a 2000 baseline (i.e., values should start at zero in the year 2000). Driver inputs for all scenarios should start in the year 2000 or earlier. All scenarios must include at least two non-missing values (especially values before or at 2000 and at or after 2100). +#' * The input population scenario requires a data frame object with a single scenario of state-level population values. +#' * The population scenario must have five columns with names `"year"`, `"region"`, `"state"`, `"postal"`, and `"state_pop"` containing the year, the NCA region name, the state name, the postal code abbreviation (e.g., "ME" for "Maine") for the state, and the state population, respectively. +#' * `popInput` only accepts a a single scenario, in contrast to `driverInput`. In other words, [FrEDI::run_fredi_sv()] uses the same population scenario for any and all driver scenarios passed to `driverInput`. +#' * If the user does not specify an input scenario for population (i.e., `popInput = NULL`, [FrEDI::run_fredi_sv()] uses a default population scenario. +#' * Population inputs must have at least one non-missing value in 2010 or earlier and at least one non-missing value in or after the final analysis year (2100). +#' * Population values must be greater than or equal to zero. +#' The default regional population scenario is drawn from the Integrated Climate and Land Use Scenarios version 2 (ICLUSv2) model (Bierwagen et al, 2010; EPA 2017) under the Median variant projection of United Nations (United Nations, 2015). Note that the FrEDI SV default population scenario differs from the default population scenario used by [FrEDI::run_fredi()]. #' -#' * `outpath` can be used to specify the directory in which to save an Excel output file. Defaults to `outpath=getwd()` (i.e., the working directory). If the directory specified by `outpath` does not exist, [FrEDI::run_fredi_sv()] will attempt to create the specified directory. -#' * `overwrite` can be used to force [FrEDI::run_fredi_sv()] to overwrite an existing Excel file in the output directory. If `overwrite=FALSE`, [FrEDI::run_fredi_sv()] will not automatically overwrite an existing Excel file; however, if a file exists and `overwrite=FALSE`, [FrEDI::run_fredi_sv()] will message the user and the user will have the option to overwrite the existing file. If `overwrite=TRUE` and the Excel file exists in the output directory, [FrEDI::run_fredi_sv()] will overwrite the existing file without messaging the user. -#' * `addDate` can be used to append the date to the output Excel file. If `save=TRUE` and `addDate=TRUE`, [FrEDI::run_fredi_sv()] will append the system date to the beginning of the name of the outputs Excel file using the format `"%Y%m%d"` (see [base::format()] and [base::Sys.Date()] for additional information). +#' The output of [FrEDI::run_fredi_sv()] is an R data frame object containing state-level annual physical impacts for socially vulnerable groups at five-year increments between 2010 and 2100. #' #' #' #' @return -#' The output of [FrEDI::run_fredi_sv()] is an R data frame object containing NCA region-specific annual average physical impacts for socially vulnerable groups at five-year increments between 2010 and 2090. An optional output of [FrEDI::run_fredi_sv()] is an Excel file containing the output data frame with basic visualizations of sector outputs. +#' The output of [FrEDI::run_fredi_sv()] is an R data frame object containing state-level annual physical impacts for socially vulnerable groups at five-year increments between 2010 and 2100. #' #' @examples #' ### Run SV Module with defaults without specifying sector @@ -64,17 +62,14 @@ #' ### Run SV Module with defaults for "Extreme Temperature" without saving #' df_sv <- run_fredi_sv(sector="Extreme Temperature") #' -#' ### Run SV Module with defaults for "Extreme Temperature" with saving and add date to file name -#' df_sv <- run_fredi_sv(sector="Extreme Temperature", save=T, addDate=T) -#' #' ### Load temperature scenarios #' load(gcamScenarios) #' #' ### Load population scenario #' load(popScenario) #' -#' ### Run SV Module for "Extreme Temperature" with custom population and temperature scenarios. Save and overwrite previous results -#' df_sv <- run_fredi_sv(sector="Extreme Temperature", driverInput = gcamScenarios, popInput = popScenario, save=T, addDate=T, overwrite = T) +#' ### Run SV Module for "Extreme Temperature" with custom population and temperature scenarios +#' df_sv <- run_fredi_sv(sector = "Extreme Temperature", driverInput = gcamScenarios, popInput = popScenario) #' #' @references #' Bierwagen, B., D. M. Theobald, C. R. Pyke, A. Choate, P. Groth, J. V. Thomas, and P. Morefield. 2010. “National housing and impervious surface scenarios for integrated climate impact assessments.” Proc. Natl. Acad. Sci. 107 (49): 20887–20892. https://doi.org/10.1073/pnas.1002096107. @@ -94,7 +89,7 @@ #' @md #' ###### run_fredi_sv ###### -### This function creates a data frame of annual average impacts over the years 2010-2090, from default values or scenario inputs, for a subset of FrEDI sectors as a function of SV group, sector, and region. +### This function creates a data frame of annual average impacts over the years 2010-2100, from default values or scenario inputs, for a subset of FrEDI sectors as a function of SV group, sector, and region. ### run_fredi_sv relies on the following helper functions: "interpolate_annual", "match_scalarValues","get_econAdjValues" , "calcScalars", "interpolate_tempBin" run_fredi_sv <- function( sector = NULL, ### Vector of sectors to get results for From 048a2defe01907b3a4e45dafb4a20ab0bff36c9a Mon Sep 17 00:00:00 2001 From: Karen Noiva Date: Tue, 2 Jul 2024 16:51:14 -0400 Subject: [PATCH 2/3] Update run_fredi_sv.R Added another check to delete impactsList if it exists --- FrEDI/R/run_fredi_sv.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/FrEDI/R/run_fredi_sv.R b/FrEDI/R/run_fredi_sv.R index 3b9d3eed..98cb350e 100644 --- a/FrEDI/R/run_fredi_sv.R +++ b/FrEDI/R/run_fredi_sv.R @@ -662,6 +662,8 @@ run_fredi_sv <- function( ### Add list names impacts_i <- impacts_i |> set_names(c_scenarios) if(exists_i){remove(list=c("impactsList"), inherits=T)} + exists_i <- "impactsList" |> exists() + if(exists_i){rm(impactsList)} ### Iterate over scenarios, calculate tract impacts for(scenario_j in c_scenarios) { From 8ec98f8eac8054bbc9032c7800befda3c7a3082d Mon Sep 17 00:00:00 2001 From: Karen Noiva Date: Tue, 2 Jul 2024 17:48:46 -0400 Subject: [PATCH 3/3] Changed output of FrEDI SV from region to state level - Changed output of FrEDI SV from region to state level - Cleaned up SV functions --- FrEDI/R/run_fredi_sv.R | 3 +- FrEDI/R/utils_sv.R | 174 +++++++++++++++++++---------------------- 2 files changed, 82 insertions(+), 95 deletions(-) diff --git a/FrEDI/R/run_fredi_sv.R b/FrEDI/R/run_fredi_sv.R index 98cb350e..8bcbd725 100644 --- a/FrEDI/R/run_fredi_sv.R +++ b/FrEDI/R/run_fredi_sv.R @@ -168,7 +168,8 @@ run_fredi_sv <- function( c_variantLabels <- df_sectorInfo |> pull(variant_label) c_popWtCol <- sectorInfo |> filter(sector == c_sector) |> pull(popWeightCol) |> tolower() c_modelType <- sectorInfo |> filter(sector == c_sector) |> pull(modelType) |> tolower() - df_validGroups <- svDemoInfo |> get_validGroups(df1 = svValidTypes, col0 = c_popWtCol) + # df_validGroups <- svDemoInfo |> get_validGroups(df1 = svValidTypes, col0 = c_popWtCol) + df_validGroups <- c_popWtCol |> get_validGroups() ###### Check Driver Inputs ###### ### Initialize whether to check for inputs diff --git a/FrEDI/R/utils_sv.R b/FrEDI/R/utils_sv.R index af81f57d..4edee504 100644 --- a/FrEDI/R/utils_sv.R +++ b/FrEDI/R/utils_sv.R @@ -128,7 +128,7 @@ get_countyPop <- function( ){ ### Get unique states and regions select0 <- c("region", "state") - pList0 <- df0 |> select(all_of(select0)) |> unique() + pList0 <- df0 |> select(all_of(select0)) |> unique() states0 <- pList0 |> pull(state) ### Iterate over states: @@ -170,11 +170,11 @@ calc_tractScaledImpacts <- function( .msg0 = "" ){ ### Messaging - msg0 <- .msg0 - msg1 <- msg0 |> paste("\t") - msg2 <- msg1 |> paste("\t") - msg3 <- msg2 |> paste("\t") - msgUser <- !silent + msg0 <- .msg0 + msg1 <- msg0 |> paste("\t") + msg2 <- msg1 |> paste("\t") + msg3 <- msg2 |> paste("\t") + msgUser <- !silent msg0 |> paste0("Calculating scaled impacts for each tract...") |> message() ### Names of functions @@ -217,18 +217,19 @@ calc_tractScaledImpacts <- function( calc_terciles <- function(data_x){ ### Probability values for terciles n_quants <- 3 - c_probs <- seq(0, 1, length.out=n_quants + 1) - c_quants <- quantile(data_x, na.rm=T, probs = c_probs) + c_probs <- 0 |> seq(1, length.out=n_quants + 1) + c_quants <- data_x |> quantile(na.rm=T, probs = c_probs) c_cutoff <- c_quants[3] + ### Return return(c_cutoff) } ###### calc_tractImpacts ###### ### Use this function to calculate tract level impacts calc_tractImpacts <- function( - scaledImpacts, ### Dataframe of scaled impacts by tract - sector, - popData, ### Dataframe of population projections + scaledImpacts, ### Dataframe of scaled impacts by tract + sector, ### Name of sector + popData, ### Dataframe of population projections svInfo = NULL, ### Dataframe of sv data svGroups = NULL, ### Character vector of sv group columns weightCol = NULL, @@ -239,149 +240,133 @@ calc_tractImpacts <- function( .testing = FALSE ){ ###### Constants ###### - # paste0("Calculating total impacts for each tract...") |> message() - x_sysTime1 <- Sys.time() - regions <- popData$region |> unique() - tracts <- scaledImpacts$fips |> unique() + regions <- popData |> pull(region) |> unique() + tracts <- scaledImpacts |> pull(fips ) |> unique() ###### Messages ###### - msg0 <- .msg0 - msg1 <- msg0 |> paste0("\t") - msg2 <- msg1 |> paste0("\t") - msg3 <- msg2 |> paste0("\t") - msgUser <- !silent - - ###### Column Names ###### - ### Other info - c_svDataDropCols <- c("svCounty") - c_svOtherDropCols <- c("state", "county", "geoid10") |> - c("ratioTract2CountyPop", "ratioState2RegionPop", "ratioCounty2StatePop") |> - c("region_pop", "state_pop", "county_pop") - c_svJoinPopCols <- c("region", "state", "geoid10") - c_svJoinImpactsCols <- c("fips", "year") - ### Columns to drop - c_svNACols <- c() - if (sector=="Air Quality - Childhood Asthma" ) {c_svNACols <- c("sv_noHS", "sv_plus65")} - else if(sector=="Air Quality - Premature Mortality") {c_svNACols <- c("sv_plus65")} - c_svGroupCols <- svGroups[svGroups %in% names(svInfo)] - c_svWeightCols <- c("children", "highRiskLabor", "sv_plus65") - c_svWeightCols1 <- c_svWeightCols |> (function(x){x[which(!(x %in% c("sv_plus65")))]})() - - ###### Other Info ###### - ### Eventually, import from svDemographics - c_sector <- sector - weightsCol <- weightCol - svGroupCols <- svGroups[svGroups %in% names(svInfo)] - # svGroups |> print(); svGroupCols |> print(); weightsCol |> print() + msg0 <- .msg0 + msg1 <- msg0 |> paste0("\t") + msg2 <- msg1 |> paste0("\t") + msg3 <- msg2 |> paste0("\t") + msgUser <- !silent ###### Format Data ###### popData <- popData |> filter(year %in% years) - scaledImpacts <- scaledImpacts |> filter(year %in% years) |> filter(!(driverUnit |> is.na())) + scaledImpacts <- scaledImpacts |> filter(year %in% years) + scaledImpacts <- scaledImpacts |> filter(!(driverUnit |> is.na())) ###### Total Impacts ###### msg0 |> paste0("Calculating total impacts for each tract...") |> message() ### Format svInfo - Add column for none and drop other columns - c_dropCols0 <- c("svCounty") - x_impacts <- svInfo |> mutate(none = 1) |> select(-all_of(c_dropCols0)) - rm(svInfo, c_dropCols0) + drop0 <- c("svCounty") + x_impacts <- svInfo |> mutate(none = 1) + x_impacts <- x_impacts |> select(-all_of(drop0)) + rm(svInfo, drop0) ### Join svInfo with population projections by region, state, geoid10 - c_joinCols0 <- c("region", "state", "geoid10") - x_impacts <- x_impacts |> left_join(popData, by = c(c_joinCols0)) - rm(popData, c_joinCols0) + join0 <- c("region", "state", "geoid10") + x_impacts <- x_impacts |> left_join(popData, by=c(join0)) + rm(popData, join0) ### Join svInfo with the impacts by fips number and drop missing values - x_impacts <- x_impacts |> left_join(scaledImpacts, by = c("year", "fips")) + join0 <- c("year", "fips") + x_impacts <- x_impacts |> left_join(scaledImpacts, by=c(join0)) x_impacts <- x_impacts |> filter(!(driverUnit |> is.na())) rm(scaledImpacts) ###### Population Weight ###### ### Add population weight column - c_weightCols <- c("children", "highRiskLabor") |> (function(y){y[y %in% names(x_impacts)]})() - x_impacts <- x_impacts |> mutate(popWeight = x_impacts[[weightsCol]]) - x_impacts <- x_impacts |> select(-all_of(c_weightCols)) - rm(c_weightCols) + drop0 <- c("children", "highRiskLabor") + rename0 <- c(weightCol) + renameTo <- c("popWeight") + # x_impacts <- x_impacts |> mutate(popWeight = x_impacts[[weightsCol]]) + x_impacts <- x_impacts |> rename_at(vars(rename0), ~renameTo) + x_impacts <- x_impacts |> select(-any_of(drop0)) + rm(drop0, rename0, renameTo) ###### Tract Population ###### ### Calculate total tract population and drop columns - c_dropCols1 <- c("state", "county", "geoid10", "region_pop", "state_pop", "county_pop") + # c_dropCols1 <- c("state", "county", "geoid10", "region_pop", "state_pop", "county_pop") + drop0 <- c("region_pop", "state_pop", "county_pop") x_impacts <- x_impacts |> mutate(tract_totPop = county_pop * ratioTract2CountyPop) - x_impacts <- x_impacts |> select(-any_of(c_dropCols1)) - rm(c_dropCols1) + x_impacts <- x_impacts |> select(-any_of(drop0)) + rm(drop0) ###### Non-Meaningful Groups ###### ### Convert values for non-meaningful SV groups to zero - if (sector=="Air Quality - Childhood Asthma" ) {c_svNACols <- c("sv_noHS", "sv_plus65")} - else if(sector=="Air Quality - Premature Mortality") {c_svNACols <- c("sv_plus65")} - else {c_svNACols <- c()} - x_impacts <- x_impacts |> mutate_at(vars(c_svNACols), function(z){0}) - rm(c_svNACols) + svPlus65 <- "sv_plus65" + svNoHS <- "sv_noHS" + do_plus65 <- sector |> str_detect("Air Quality") |> any() + do_noHS <- sector |> str_detect("Childhood Asthma") |> any() + mutate0 <- c() + if(do_plus65) mutate0 <- mutate0 |> c(svPlus65) + if(do_noHS ) mutate0 <- mutate0 |> c(svNoHS) + # if (sector=="Air Quality - Childhood Asthma" ) {mutate0 <- c("sv_noHS", "sv_plus65")} + # else if(sector=="Air Quality - Premature Mortality") {mutate0 <- c("sv_plus65")} + # else {mutate0 <- c()} + x_impacts <- x_impacts |> mutate_at(vars(mutate0), function(z){0}) + rm(svPlus65, svNoHS, do_plus65, do_noHS, mutate0) ###### National Terciles ###### - # x_impacts |> glimpse() if(msgUser) {msg1 |> paste0("Calculating national terciles...") |> message()} - # else {msg2 |> paste0("...") |> message()} ### Columns groupsNat0 <- c("year") tractNat0 <- c("national_highRiskTract") cutoffNat0 <- c("national_cutoff") scaledImpact0 <- c("sv_impact") - ### Calculate terciles and rename column + + ### Calculate terciles select0 <- c(groupsNat0, scaledImpact0) quants_national <- x_impacts |> select(all_of(select0)) quants_national <- quants_national |> group_by_at (vars(groupsNat0)) |> summarize_at(vars(scaledImpact0), calc_terciles) |> ungroup() + if(.testing){quants_national |> filter(year==2050) |> glimpse()} rm(select0) - ### Rename + + ### Rename column quants_national <- quants_national |> rename_at(vars(scaledImpact0), ~c(cutoffNat0)) - if(.testing){quants_national |> filter(year==2050) |> glimpse()} ### Join with national quantiles if(msgUser) {msg2 |> paste0("Joining national terciles to tract-level data...") |> message()} - # else {msg3 |> paste0(msg1, "...") |> message()} - x_impacts <- x_impacts |> left_join(quants_national, by = c(groupsNat0)) + x_impacts <- x_impacts |> left_join(quants_national, by=c(groupsNat0)) rm(quants_national) ### Figure out which tracts are high risk ### Calculate high risk populations if(msgUser) {msg2 |> paste0("Calculating national high risk populations...") |> message()} - # else {msg3 |> paste0(msg1, "...") |> message()} x_impacts[[tractNat0]] <- (x_impacts[[scaledImpact0]] > x_impacts[[cutoffNat0]]) * 1 - x_impacts <- x_impacts |> select(-all_of(cutoffNat0)); + x_impacts <- x_impacts |> select(-all_of(cutoffNat0)) rm(cutoffNat0) - # Sys.sleep(sleep) ###### Regional Terciles ###### if(msgUser) {msg1 |> paste0("Calculating regional terciles...") |> message()} - # else {msg3 |> paste0("...") |> message()} ### Columns groupsReg0 <- c(groupsNat0, "region") tractReg0 <- c("regional_highRiskTract") cutoffReg0 <- c("regional_cutoff") - ### 126 rows - ### Calculate terciles and rename column + ### Calculate terciles select0 <- c(groupsReg0, scaledImpact0) quants_regional <- x_impacts |> select(all_of(select0)) quants_regional <- quants_regional |> group_by_at (vars(groupsReg0)) |> summarize_at(vars(scaledImpact0), calc_terciles) |> ungroup() + if(.testing){quants_regional |> filter(year==2050) |> glimpse()} rm(select0) - ### Rename + + ### Rename column quants_regional <- quants_regional |> rename_at(vars(scaledImpact0), ~c(cutoffReg0)) - if(.testing){quants_regional |> filter(year==2050) |> glimpse()} + ### Join with regional quantiles if(msgUser){msg2 |> paste0("Joining regional terciles to tract-level data...") |> message()} - # else {msg3 |> paste0(msg1, "...") |> message()} - x_impacts <- x_impacts |> left_join(quants_regional, by = c(groupsReg0)); - rm(quants_regional); + x_impacts <- x_impacts |> left_join(quants_regional, by = c(groupsReg0)) + rm(quants_regional) + ### Regional High Risk Tracts ### Figure out which tracts are high risk ### Calculate high risk populations if(msgUser) {msg2 |> paste0("Calculating regional high risk populations...") |> message()} - # else {msg3 |> paste0("...") |> message()} x_impacts[[tractReg0]] <- (x_impacts[[scaledImpact0]] > x_impacts[[cutoffReg0]]) * 1 x_impacts <- x_impacts |> select(-all_of(cutoffReg0)) rm(cutoffReg0) - # Sys.sleep(sleep) ###### Total Impacts ###### ### - Impacts = population*popWeight @@ -425,11 +410,12 @@ calc_tractImpacts <- function( x_impacts[,popReg1] <- x_impacts[,c_impPop1] * x_impacts[[tractReg0]] rm(tractNat0, tractReg0) - ###### Regional Summaries ###### + ###### State Summaries ###### if(!.testing){ - if(msgUser){msg1 |> paste0( "Calculating regional summaries...") |> message()} + if(msgUser){msg1 |> paste0( "Calculating state summaries...") |> message()} sumCols0 <- c(c_impPop1, c_impact1) |> c(popNat1, popReg1) - groupCols0 <- c("region", "svGroupType", "driverUnit", "driverValue", "year") + # groupCols0 <- c("region", "svGroupType", "driverUnit", "driverValue", "year") + groupCols0 <- c("region", "state", "postal", "svGroupType", "driverUnit", "driverValue", "year") ### Group by the grouping columns and summarize the summary columns x_impacts <- x_impacts |> group_by_at (vars(groupCols0)) |> @@ -464,9 +450,9 @@ calc_tractImpacts <- function( ###### get_validGroups ###### get_validGroups <- function( - df0, ### svDemoInfo - df1, ### svValidTypes - col0 = "none" ### c_popWtCol + col0 = "none", ### c_popWtCol + df0 = svDataList[["svDemoInfo" ]], ### svDemoInfo + df1 = svDataList[["svValidTypes"]] ### svValidTypes ){ ### Column names old0 <- c("colName" , "valid_popWeightCols") @@ -474,7 +460,6 @@ get_validGroups <- function( ### Reshape svDemoInfo df0 <- df0 |> filter(colType %in% c("bipoc")) |> select(c(old0[1])) df0 <- df0 |> rename_at(vars(old0[1]), ~c(new0[1])) - # df0 <- df0 |> mutate(validGroups = "none") # children, highRiskLabor, sv_plus65, none df0 <- df0 |> mutate(validGroups = "children, highRiskLabor, sv_plus65, none") ### Reshape svValidTypes @@ -486,14 +471,15 @@ get_validGroups <- function( ### Calculate weight columns col0 <- col0 |> as.character() |> tolower() - groups0 <- df0$validGroups |> as.vector() |> as.character() |> tolower() - valid0 <- groups0 |> str_match(col0) |> unlist() |> as.vector() + groups0 <- df0 |> pull(validGroups) |> as.character() |> tolower() + valid0 <- groups0 |> str_detect(col0) # col0 |> print(); groups0 |> print(); valid0 |> print() + ### Add new columns df0 <- df0 |> mutate(weightCol = col0) df0 <- df0 |> mutate(validType = valid0) - df0 <- df0 |> mutate(valueAdj = (1 * !(validType |> is.na()))) - # df0 |> glimpse() + df0 <- df0 |> mutate(valueAdj = 1 * !(validType |> is.na())) + ### Return return(df0) }