diff --git a/DESCRIPTION b/DESCRIPTION index b61d269..1a729de 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: GDPuc Title: Easily Convert GDP Data -Version: 0.11.1 +Version: 1.0.0 Date: 2023-07-13 Authors@R: person("Johannes", "Koch", , "jokoch@pik-potsdam.de", role = c("aut", "cre")) @@ -42,4 +42,4 @@ VignetteBuilder: Config/testthat/edition: 3 Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 diff --git a/NAMESPACE b/NAMESPACE index f2a3393..edff7de 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,8 @@ # Generated by roxygen2: do not edit by hand +export(convertCPI) export(convertGDP) +export(convertSingle) export(print_source_info) importFrom(magrittr,"%>%") importFrom(rlang,"!!") diff --git a/R/adapt_source.R b/R/adapt_source.R index 6b1f4b8..c316049 100644 --- a/R/adapt_source.R +++ b/R/adapt_source.R @@ -1,3 +1,20 @@ +adapt_source_USA <- function(gdp, source, replace_NAs) { + source_USA <- source %>% + dplyr::filter(.data$iso3c == "USA") %>% + dplyr::select("year", "USA_deflator" = "GDP deflator") + + source_adapted <- source %>% + dplyr::left_join(source_USA, by = dplyr::join_by("year")) %>% + dplyr::mutate("GDP deflator" = .data$USA_deflator) + + if (!is.null(replace_NAs) && replace_NAs[1] == "with_USA") { + source_USA <- source %>% dplyr::filter(.data$iso3c == "USA") + source_adapted <- purrr::map(unique(gdp$iso3c), ~dplyr::mutate(source_USA, "iso3c" = .x)) %>% + purrr::list_rbind() + } + source_adapted +} + # adapt_source <- function(gdp, source, with_regions, replace_NAs) { rlang::check_installed(c("zoo"), reason = "in order for 'replace_NAs' to work.") diff --git a/R/check_user_input.R b/R/check_user_input.R index 9c1a6d6..1afc909 100644 --- a/R/check_user_input.R +++ b/R/check_user_input.R @@ -2,21 +2,23 @@ # # check_user_input performs some checks on the function arguments. # @return TRUE or an Error -check_user_input <- function(gdp, unit_in, unit_out, source, with_regions, replace_NAs, verbose, return_cfs) { +check_user_input <- function(gdp, + unit_in, + unit_out, + source, + use_USA_deflator_for_all, + with_regions, + replace_NAs, + verbose, + return_cfs) { - # Check the gdp argument check_gdp(gdp) - # Check the unit_in and unit_out arguments check_unit_in_out(unit_in, unit_out) - # Check the source argument source <- check_source(source) - # Check the with_regions argument. Depends on unit_in, unit_out, and the source tibble + check_use_USA_deflator_for_all(use_USA_deflator_for_all, unit_in, unit_out) check_with_regions(unit_in, unit_out, source, with_regions) - # Check the replaceNAs argument. Depends on the with_regions argument. check_replace_NAs(with_regions, replace_NAs) - # Check the verbose argument check_verbose(verbose) - # Check the return_cfs argument check_return_cfs(return_cfs) TRUE @@ -59,6 +61,7 @@ check_unit_in_out <- function(unit_in, unit_out) { "current Int\\$PPP", "constant .... LCU", "constant .... US\\$MER", + "constant .... \u20ac", "constant .... Int\\$PPP" ) if (!is.character(unit_in) || !any(sapply(valid_units, grepl, unit_in))) { @@ -95,9 +98,22 @@ check_source <- function(source) { if (!all(required_cols_in_source %in% colnames(source))) { abort("Invalid 'source' argument. Required columns are: {paste(required_cols_in_source, collapse = '; ')}") } + if (nrow(dplyr::distinct(source)) != nrow(dplyr::distinct(source, .data$iso3c , .data$year))) { + abort("Invalid 'source' argument. Duplicate iso3c - year pairs found.") + } source } +# Check input parameter 'verbose' +check_use_USA_deflator_for_all <- function(use_USA_deflator_for_all, unit_in, unit_out) { + if (!is.logical(use_USA_deflator_for_all)) { + abort("Invalid 'use_USA_deflator_for_all' argument. Has to be either TRUE or FALSE.") + } + if (use_USA_deflator_for_all && any(grepl("current", c(unit_in, unit_out)))) { + abort("Setting 'use_USA_deflator_for_all' to TRUE should only be applied between conversion of constant units.") + } +} + # Check input parameter 'with_regions' check_with_regions <- function(unit_in, unit_out, source, with_regions) { if (!is.null(with_regions)) { @@ -129,9 +145,9 @@ check_replace_NAs <- function(with_regions, replace_NAs) { "convertGDP(replace_NAs = '\"linear_regional_average\" has been replaced by c(\"linear\", \"regional_average\")')" ) } - if (!all(replace_NAs %in% c(NA, 0, 1, "no_conversion", "linear", "regional_average"))) { - abort("Invalid 'replace_NAs' argument. Has to be either NULL, NA, 0, 1, no_conversion, linear, regional_average or \\ - a combination of the above.") + if (!all(replace_NAs %in% c(NA, 0, 1, "no_conversion", "linear", "regional_average", "with_USA"))) { + abort("Invalid 'replace_NAs' argument. Has to be either NULL, NA, 0, 1, no_conversion, linear, \\ + regional_average, with_USA or a combination of the above.") } if (length(replace_NAs) > 1 && replace_NAs[1] != "linear") { abort("Invalid 'replace_NAs' argument. The only accepted combinations of arguments start with 'linear', e.g. \\ diff --git a/R/composite_conversions.R b/R/composite_conversions.R index b7f4909..ecd8b0b 100644 --- a/R/composite_conversions.R +++ b/R/composite_conversions.R @@ -6,17 +6,24 @@ # Convert from current LCU to constant Int$PPP base y current_LCU_2_constant_IntPPP_base_y <- function(gdp, base_y, source) { gdp %>% - current_LCU_2_constant_LCU_base_y(base_y, source) %>% + current_LCU_2_constant_LCU_base_y(base_y, source) %>e% constant_LCU_2_constant_IntPPP(base_y, source) } # Convert from current LCU to constant US$MER base y current_LCU_2_constant_USMER_base_y <- function(gdp, base_y, source) { gdp %>% - current_LCU_2_constant_LCU_base_y(base_y, source) %>% + current_LCU_2_constant_LCU_base_y(base_y, source) %>e% constant_LCU_2_constant_USMER(base = base_y, source) } +# Convert from current LCU to constant € base y +current_LCU_2_constant_EURO_base_y <- function(gdp, base_y, source) { + gdp %>% + current_LCU_2_constant_USMER_base_y(base_y, source) %>e% + constant_USMER_2_constant_EURO(base = base_y, source) +} + #------------------------------------------------------------ #------------------------------------------------------------ #------------------------------------------------------------ @@ -25,30 +32,28 @@ current_LCU_2_constant_USMER_base_y <- function(gdp, base_y, source) { # Convert from current Int$PPP to current US$MER current_IntPPP_2_current_USMER <- function(gdp, source) { gdp %>% - current_IntPPP_2_current_LCU(source) %>% + current_IntPPP_2_current_LCU(source) %>e% current_LCU_2_current_USMER(source) } # Convert from current Int$PPP to constant LCU base y current_IntPPP_2_constant_LCU_base_y <- function(gdp, base_y, source) { gdp %>% - current_IntPPP_2_current_LCU(source) %>% + current_IntPPP_2_current_LCU(source) %>e% current_LCU_2_constant_LCU_base_y(base_y, source) } # Convert from current Int$PPP to constant Int$PPP base y current_IntPPP_2_constant_IntPPP_base_y <- function(gdp, base_y, source) { gdp %>% - current_IntPPP_2_current_LCU(source) %>% - current_LCU_2_constant_LCU_base_y(base_y, source) %>% + current_IntPPP_2_constant_LCU_base_y(base_y, source) %>e% constant_LCU_2_constant_IntPPP(base = base_y, source) } # Convert from current Int$PPP to constant US$MER base y current_IntPPP_2_constant_USMER_base_y <- function(gdp, base_y, source) { gdp %>% - current_IntPPP_2_current_LCU(source) %>% - current_LCU_2_constant_LCU_base_y(base_y, source) %>% + current_IntPPP_2_constant_LCU_base_y(base_y, source) %>e% constant_LCU_2_constant_USMER(base = base_y, source) } @@ -60,32 +65,38 @@ current_IntPPP_2_constant_USMER_base_y <- function(gdp, base_y, source) { # Convert from current US$MER to current Int$PPP current_USMER_2_current_IntPPP <- function(gdp, source) { gdp %>% - current_USMER_2_current_LCU(source) %>% + current_USMER_2_current_LCU(source) %>e% current_LCU_2_current_IntPPP(source) } # Convert from current US$MER to constant LCU base y current_USMER_2_constant_LCU_base_y <- function(gdp, base_y, source) { gdp %>% - current_USMER_2_current_LCU(source) %>% + current_USMER_2_current_LCU(source) %>e% current_LCU_2_constant_LCU_base_y(base_y, source) } # Convert from current US$MER to constant US$MER base y current_USMER_2_constant_USMER_base_y <- function(gdp, base_y, source) { gdp %>% - current_USMER_2_constant_LCU_base_y(base_y, source) %>% + current_USMER_2_constant_LCU_base_y(base_y, source) %>e% constant_LCU_2_constant_USMER(base = base_y, source) } # Convert from current US$MER to constant Int$PPP base y current_USMER_2_constant_IntPPP_base_y <- function(gdp, base_y, source) { gdp %>% - current_USMER_2_current_LCU(source) %>% - current_LCU_2_constant_LCU_base_y(base_y, source) %>% + current_USMER_2_constant_LCU_base_y(base_y, source) %>e% constant_LCU_2_constant_IntPPP(base = base_y, source) } +# Convert from current US$MER to constant € base y +current_USMER_2_constant_EURO_base_y <- function(gdp, base_y, source) { + gdp %>% + current_USMER_2_constant_USMER_base_y(base_y, source) %>e% + constant_USMER_2_constant_EURO(base = base_y, source) +} + #------------------------------------------------------------ #------------------------------------------------------------ #------------------------------------------------------------ @@ -94,21 +105,21 @@ current_USMER_2_constant_IntPPP_base_y <- function(gdp, base_y, source) { # Convert from constant LCU base x to current Int$PPP constant_LCU_base_x_2_current_IntPPP <- function(gdp, base_x, source) { gdp %>% - constant_LCU_base_x_2_current_LCU(base_x, source) %>% + constant_LCU_base_x_2_current_LCU(base_x, source) %>e% current_LCU_2_current_IntPPP(source) } # Convert from constant LCU base x to current Int$PPP constant_LCU_base_x_2_current_USMER <- function(gdp, base_x, source) { gdp %>% - constant_LCU_base_x_2_current_LCU(base_x, source) %>% + constant_LCU_base_x_2_current_LCU(base_x, source) %>e% current_LCU_2_current_USMER(source) } # Convert from constant LCU in one base year to constant Int$PPP of another constant_LCU_base_x_2_constant_IntPPP_base_y <- function(gdp, base_x, base_y, source) { gdp %>% - constant_LCU_base_x_2_constant_LCU_base_y(base_x, base_y, source) %>% + constant_LCU_base_x_2_constant_LCU_base_y(base_x, base_y, source) %>e% constant_LCU_2_constant_IntPPP(base = base_y, source) } @@ -116,10 +127,17 @@ constant_LCU_base_x_2_constant_IntPPP_base_y <- function(gdp, base_x, base_y, so # Convert from constant LCU in one base year to constant US$MER of another constant_LCU_base_x_2_constant_USMER_base_y <- function(gdp, base_x, base_y, source) { gdp %>% - constant_LCU_base_x_2_constant_LCU_base_y(base_x, base_y, source) %>% + constant_LCU_base_x_2_constant_LCU_base_y(base_x, base_y, source) %>e% constant_LCU_2_constant_USMER(base = base_y, source) } +# Convert from constant LCU in one base year to constant € of another +constant_LCU_base_x_2_constant_EURO_base_y <- function(gdp, base_x, base_y, source) { + gdp %>% + constant_LCU_base_x_2_constant_USMER_base_y(base_x, base_y, source) %>e% + constant_USMER_2_constant_EURO(base = base_y, source) +} + #------------------------------------------------------------ #------------------------------------------------------------ #------------------------------------------------------------ @@ -128,49 +146,52 @@ constant_LCU_base_x_2_constant_USMER_base_y <- function(gdp, base_x, base_y, sou # Convert from constant Int$PPP base year x to current LCU constant_IntPPP_base_x_2_current_LCU <- function(gdp, base_x, source) { gdp %>% - constant_IntPPP_2_constant_LCU(base = base_x, source) %>% + constant_IntPPP_2_constant_LCU(base = base_x, source) %>e% constant_LCU_base_x_2_current_LCU(base_x, source) } # Convert from constant Int$PPP base year x to current US$MER constant_IntPPP_base_x_2_current_USMER <- function(gdp, base_x, source) { gdp %>% - constant_IntPPP_2_constant_LCU(base = base_x, source) %>% - constant_LCU_base_x_2_current_LCU(base_x, source) %>% + constant_IntPPP_base_x_2_current_LCU(base_x, source) %>e% current_LCU_2_current_USMER(source) } # Convert from constant Int$PPP base year x to current Int$PPP constant_IntPPP_base_x_2_current_IntPPP <- function(gdp, base_x, source) { gdp %>% - constant_IntPPP_2_constant_LCU(base = base_x, source) %>% - constant_LCU_base_x_2_current_LCU(base_x, source) %>% + constant_IntPPP_base_x_2_current_LCU(base_x, source) %>e% current_LCU_2_current_IntPPP(source) } # Convert from constant Int$PPP in one base year to constant LCU of another constant_IntPPP_base_x_2_constant_LCU_base_y <- function(gdp, base_x, base_y, source) { gdp %>% - constant_IntPPP_2_constant_LCU(base = base_x, source) %>% + constant_IntPPP_2_constant_LCU(base = base_x, source) %>e% constant_LCU_base_x_2_constant_LCU_base_y(base_x, base_y, source) } # Convert constant Int$PPP series from one base year to another constant_IntPPP_base_x_2_constant_IntPPP_base_y <- function(gdp, base_x, base_y, source) { gdp %>% - constant_IntPPP_2_constant_LCU(base = base_x, source) %>% - constant_LCU_base_x_2_constant_LCU_base_y(base_x, base_y, source) %>% + constant_IntPPP_base_x_2_constant_LCU_base_y(base_x, base_y, source) %>e% constant_LCU_2_constant_IntPPP(base = base_y, source) } # Convert from constant Int$PPP in one base year to constant US$MER of another constant_IntPPP_base_x_2_constant_USMER_base_y <- function(gdp, base_x, base_y, source) { gdp %>% - constant_IntPPP_2_constant_LCU(base = base_x, source) %>% - constant_LCU_base_x_2_constant_LCU_base_y(base_x, base_y, source) %>% + constant_IntPPP_base_x_2_constant_LCU_base_y(base_x, base_y, source) %>e% constant_LCU_2_constant_USMER(base = base_y, source) } +# Convert from constant Int$PPP in one base year to constant € of another +constant_IntPPP_base_x_2_constant_EURO_base_y <- function(gdp, base_x, base_y, source) { + gdp %>% + constant_IntPPP_base_x_2_constant_USMER_base_y(base_x, base_y, source) %>e% + constant_USMER_2_constant_EURO(base = base_y, source) +} + #------------------------------------------------------------ #------------------------------------------------------------ #------------------------------------------------------------ @@ -179,47 +200,102 @@ constant_IntPPP_base_x_2_constant_USMER_base_y <- function(gdp, base_x, base_y, # Convert from constant US$MER base year x to current LCU constant_USMER_base_x_2_current_LCU <- function(gdp, base_x, source) { gdp %>% - constant_USMER_2_constant_LCU(base = base_x, source) %>% + constant_USMER_2_constant_LCU(base = base_x, source) %>e% constant_LCU_base_x_2_current_LCU(base_x, source) } # Convert from constant US$MER base year x to current Int$PPP constant_USMER_base_x_2_current_IntPPP <- function(gdp, base_x, source) { gdp %>% - constant_USMER_2_constant_LCU(base = base_x, source) %>% - constant_LCU_base_x_2_current_LCU(base_x, source) %>% + constant_USMER_base_x_2_current_LCU(base_x, source) %>e% current_LCU_2_current_IntPPP(source) } # Convert from constant US$MER base year x to current US$MER constant_USMER_base_x_2_current_USMER <- function(gdp, base_x, source) { gdp %>% - constant_USMER_2_constant_LCU(base = base_x, source) %>% - constant_LCU_base_x_2_current_LCU(base_x, source) %>% + constant_USMER_base_x_2_current_LCU(base_x, source) %>e% current_LCU_2_current_USMER(source) } -# Convert constant US$MER series from one base year to another -constant_USMER_base_x_2_constant_USMER_base_y <- function(gdp, base_x, base_y, source) { - gdp %>% - constant_USMER_2_constant_LCU(base = base_x, source) %>% - constant_LCU_base_x_2_constant_LCU_base_y(base_x, base_y, source) %>% - constant_LCU_2_constant_USMER(base = base_y, source) -} - # Convert from constant US$MER in one base year to constant LCU of another constant_USMER_base_x_2_constant_LCU_base_y <- function(gdp, base_x, base_y, source) { gdp %>% - constant_USMER_2_constant_LCU(base = base_x, source) %>% + constant_USMER_2_constant_LCU(base = base_x, source) %>e% constant_LCU_base_x_2_constant_LCU_base_y(base_x, base_y, source) } +# Convert constant US$MER series from one base year to another +constant_USMER_base_x_2_constant_USMER_base_y <- function(gdp, base_x, base_y, source) { + gdp %>% + constant_USMER_base_x_2_constant_LCU_base_y(base_x, base_y, source) %>e% + constant_LCU_2_constant_USMER(base = base_y, source) +} + # Convert from constant US$MER in one base year to constant Int$PPP of another constant_USMER_base_x_2_constant_IntPPP_base_y <- function(gdp, base_x, base_y, source) { gdp %>% - constant_USMER_2_constant_LCU(base = base_x, source) %>% - constant_LCU_base_x_2_constant_LCU_base_y(base_x, base_y, source) %>% + constant_USMER_base_x_2_constant_LCU_base_y(base_x, base_y, source) %>e% constant_LCU_2_constant_IntPPP(base = base_y, source) } +# Convert from constant US$MER in one base year to constant € of another +constant_USMER_base_x_2_constant_EURO_base_y <- function(gdp, base_x, base_y, source) { + gdp %>% + constant_USMER_base_x_2_constant_USMER_base_y(base_x, base_y, source) %>e% + constant_USMER_2_constant_EURO(base = base_y, source) +} + +#------------------------------------------------------------ +#------------------------------------------------------------ +#------------------------------------------------------------ +# Unit_in = constant € +# Convert from constant € base year x to current LCU +constant_EURO_base_x_2_current_LCU <- function(gdp, base_x, source) { + gdp %>% + constant_EURO_2_constant_USMER(base = base_x, source) %>e% + constant_USMER_base_x_2_current_LCU(base_x, source) +} + +# Convert from constant € base year x to current Int$PPP +constant_EURO_base_x_2_current_IntPPP <- function(gdp, base_x, source) { + gdp %>% + constant_EURO_2_constant_USMER(base = base_x, source) %>e% + constant_USMER_base_x_2_current_IntPPP(base_x, source) +} + +# Convert from constant € base year x to current US$MER +constant_EURO_base_x_2_current_USMER <- function(gdp, base_x, source) { + gdp %>% + constant_EURO_2_constant_USMER(base = base_x, source) %>e% + constant_USMER_base_x_2_current_USMER(base_x, source) +} + +# Convert constant € series from one base year to another +constant_EURO_base_x_2_constant_USMER_base_y <- function(gdp, base_x, base_y, source) { + gdp %>% + constant_EURO_2_constant_USMER(base = base_x, source) %>e% + constant_USMER_base_x_2_constant_USMER_base_y(base_x, base_y, source) +} + +# Convert from constant € in one base year to constant LCU of another +constant_EURO_base_x_2_constant_LCU_base_y <- function(gdp, base_x, base_y, source) { + gdp %>% + constant_EURO_2_constant_USMER(base = base_x, source) %>e% + constant_USMER_base_x_2_constant_LCU_base_y(base_x, base_y, source) +} + +# Convert from constant € in one base year to constant Int$PPP of another +constant_EURO_base_x_2_constant_IntPPP_base_y <- function(gdp, base_x, base_y, source) { + gdp %>% + constant_EURO_2_constant_USMER(base = base_x, source) %>e% + constant_USMER_base_x_2_constant_IntPPP_base_y(base_x, base_y, source) +} + +# Convert from constant € in one base year to constant € of another +constant_EURO_base_x_2_constant_EURO_base_y <- function(gdp, base_x, base_y, source) { + gdp %>% + constant_EURO_2_constant_USMER(base = base_x, source) %>e% + constant_USMER_base_x_2_constant_EURO_base_y(base_x, base_y, source) +} diff --git a/R/convertGDP.R b/R/convertGDP.R index 9edc1bc..7d752b7 100644 --- a/R/convertGDP.R +++ b/R/convertGDP.R @@ -39,6 +39,7 @@ #' \item "constant YYYY LCU" #' \item "constant YYYY Int$PPP" #' \item "constant YYYY US$MER" +#' \item "constant YYYY €" #' } #' where YYYY should be replaced with a year e.g. "2010" or "2017". #' @param unit_out A string with the outgoing GDP unit, one of: @@ -49,12 +50,16 @@ #' \item "constant YYYY LCU" #' \item "constant YYYY Int$PPP" #' \item "constant YYYY US$MER" +#' \item "constant YYYY €" #' } #' where YYYY should be replaced with a year e.g. "2010" or "2017". #' @param source A string referring to a package internal data frame containing the conversion factors, or #' a data-frame that exists in the calling environment. #' Use [print_source_info()](https://pik-piam.github.io/GDPuc/reference/print_source_info.html) #' to learn about the available sources. +#' @param use_USA_deflator_for_all TRUE or FALSE (default). If TRUE, then only the USA deflator is used to adjust for +#' inflation, regardless of the country codes provided. This is a very specific deviation from the correct conversion +#' process, which nevertheless is often used in the integrated assessment community. Use carefully! #' @param with_regions NULL or a data-frame. The data-frame should be "country to region #' mapping": one column named "iso3c" with iso3c country codes, and one column named #' "region" with region codes to which the countries belong. Any regions in the gdp @@ -71,6 +76,8 @@ #' \item "regional_average": missing conversion factors in the source object are replaced with #' the regional average of the region to which the country belongs. This requires a region-mapping to #' be passed to the function, see the with_regions argument. +#' \item "with USA": missing conversion factors in the source object are replaced with +#' the conversion factors of the USA. #' } #' Can also be a vector with "linear" as first element, e.g. c("linear", 0) or c("linear", "no_conversion"), #' in which case, the operations are done in sequence. @@ -82,14 +89,22 @@ #' @return The gdp argument, with the values in the "value" column, converted to unit_out. If the argument #' return_cfs is TRUE, then a list is returned with the converted GDP under "result", and the conversion #' factors used under "cfs". -#' @seealso The [countrycode](https://github.com/vincentarelbundock/countrycode) -#' package to convert country codes. -#' @importFrom magrittr %>% +#' @seealso The [countrycode](https://github.com/vincentarelbundock/countrycode) package to convert country codes. +#' @examples +#' my_tbble <- tibble::tibble(iso3c = "FRA", +#' year = 2013, +#' value = 100) +#' +#' convertGDP(gdp = my_tbble, +#' unit_in = "current LCU", +#' unit_out = "constant 2015 Int$PPP") +#' #' @export convertGDP <- function(gdp, unit_in, unit_out, source = "wb_wdi", + use_USA_deflator_for_all = FALSE, with_regions = NULL, replace_NAs = NULL, verbose = getOption("GDPuc.verbose", default = FALSE), @@ -110,7 +125,7 @@ convertGDP <- function(gdp, } # Transform user input for internal use, while performing some last consistency checks - internal <- transform_user_input(gdp, unit_in, unit_out, source, with_regions, replace_NAs) + internal <- transform_user_input(gdp, unit_in, unit_out, source, use_USA_deflator_for_all, with_regions, replace_NAs) # Avoid NOTE in package check for CRAN . <- NULL @@ -118,7 +133,9 @@ convertGDP <- function(gdp, f <- paste0(internal$unit_in, "_2_", internal$unit_out) %>% gsub(" ", "_", .) %>% gsub("_YYYY", "", .) %>% - gsub("\\$", "", .) + gsub("\\$", "", .) %>% + # \u20ac is the ascii code for the € sign + gsub("\u20ac", "EURO", .) # Get list of function arguments a <- list("gdp" = internal$gdp, "source" = internal$source) %>% @@ -154,3 +171,47 @@ convertGDP <- function(gdp, return(x) } } + +#' @describeIn convertGDP Short cut for `convertGDP(..., source = "wb_wdi_cpi")` +#' @param ... Arguments passed on to `convertGDP()` +#' @examples +#' # Convert using the CPI as deflator. +#' convertGDP(gdp = my_tbble, +#' unit_in = "current LCU", +#' unit_out = "constant 2015 Int$PPP", +#' source = "wb_wdi_cpi") +#' # Or using the shortcut `convertCPI()` +#' convertCPI(gdp = my_tbble, +#' unit_in = "current LCU", +#' unit_out = "constant 2015 Int$PPP") +#' +#' @export +convertCPI <- function(...) convertGDP(..., source = "wb_wdi_cpi") + +#' @describeIn convertGDP Convert a single value, while specifying iso3c code and year. Simpler than creating a +#' single row tibble. +#' @param x Number to convert +#' @param iso3c Country code +#' @param year Year of value. Is given the default value of 2000, since it only plays a role when converting from +#' or to current currencies. This facilitates the conversion between constant currencies. +#' @examples +#' # Convert a single value quickly +#' convertSingle(x = 100, +#' iso3c = "FRA", +#' year = 2013, +#' unit_in = "current LCU", +#' unit_out = "constant 2015 Int$PPP") +#' @export +convertSingle <- function(x, + iso3c, + year = 2000, + ...) { + tib <- tibble::tibble("iso3c" = iso3c, "year" = year, "value" = x) + tib_c <- convertGDP(gdp = tib, ...) + + if (tibble::is_tibble(tib_c)) { + return(tib_c$value) + } else { + return(tib_c) + } +} diff --git a/R/elemental_conversions.R b/R/elemental_conversions.R index 5c1c624..7cdf3ae 100644 --- a/R/elemental_conversions.R +++ b/R/elemental_conversions.R @@ -273,3 +273,54 @@ constant_USMER_2_constant_LCU <- function(gdp, base, source) { dplyr::mutate(value = .data$value * .data$`MER (LCU per US$)`, .keep = "unused") } + +# Convert from constant US$MER to constant EURO +constant_USMER_2_constant_EURO <- function(gdp, base, source) { + MER_EURO_per_DOLLAR <- source %>% + dplyr::filter(.data$year == base, .data$iso3c == "DEU") %>% + dplyr::pull(.data$`MER (LCU per US$)`) + + MER_base <- source %>% + dplyr::filter(.data$year == base) %>% + dplyr::mutate("MER (LCU per US$)" = MER_EURO_per_DOLLAR) %>% + dplyr::select("iso3c", "MER (LCU per US$)") + + cli_elemental(from = glue::glue("constant {base} US$MER"), + to = glue::glue("constant {base} \u20ac"), + with = glue::glue("{base} MER"), + unit = "(\u20ac per US$)", + val = dplyr::filter(MER_base, .data$iso3c %in% unique(gdp$iso3c))) + + gdp %>% + dplyr::left_join(MER_base, by = "iso3c") %>% + dplyr::mutate(value = .data$value * .data$`MER (LCU per US$)`, + .keep = "unused") +} + + +#------------------------------------------------------------ +#------------------------------------------------------------ +#------------------------------------------------------------ +# Unit_in = constant € + +constant_EURO_2_constant_USMER <- function(gdp, base, source) { + MER_EURO_per_DOLLAR <- source %>% + dplyr::filter(.data$year == base, .data$iso3c == "DEU") %>% + dplyr::pull(.data$`MER (LCU per US$)`) + + MER_base <- source %>% + dplyr::filter(.data$year == base) %>% + dplyr::mutate("MER (LCU per US$)" = MER_EURO_per_DOLLAR) %>% + dplyr::select("iso3c", "MER (LCU per US$)") + + cli_elemental(from = glue::glue("constant {base} \u20ac"), + to = glue::glue("constant {base} US$MER"), + with = glue::glue("{base} MER"), + unit = "(\u20ac per US$)", + val = dplyr::filter(MER_base, .data$iso3c %in% unique(gdp$iso3c))) + + gdp %>% + dplyr::left_join(MER_base, by = "iso3c") %>% + dplyr::mutate(value = .data$value / .data$`MER (LCU per US$)`, + .keep = "unused") +} diff --git a/R/print_source_info.R b/R/print_source_info.R index 208b926..f7cdc30 100644 --- a/R/print_source_info.R +++ b/R/print_source_info.R @@ -7,27 +7,38 @@ #' html-link and an associated note. Calling the function without any argument will print information on all #' available sources. #' -#' @param source The name of one of the internal sources: +#' @param source Empty, or the name of one of the internal sources: #' \enumerate{ #' \item "wb_wdi" #' \item "wb_wdi_linked" +#' \item "wb_wdi_cpi" #' } #' @return No return value, called for side effects. +#' @examples +#' print_source_info() +#' #' @export print_source_info <- function(source) { if (missing(source)) cli::cli_alert_info("Sources available:") if (missing(source) || source == "wb_wdi") { cli_source_info(name = "wb_wdi", origin = "The World Bank's World Development Indicator Database", - date = "Downloaded on the 22 of Oktober 2021", + date = "Downloaded on the 2nd of January 2024", html = "https://databank.worldbank.org/source/world-development-indicators", - note = "Uses the standard deflator.") + note = "Uses the GDP deflator.") } if (missing(source) || source == "wb_wdi_linked") { cli_source_info(name = "wb_wdi_linked", origin = "The World Bank's World Development Indicator Database", - date = "Downloaded on the 22 of Oktober 2021", + date = "Downloaded on the 2nd of January 2024", + html = "https://databank.worldbank.org/source/world-development-indicators", + note = "Uses the linked GDP deflator.") + } + if (missing(source) || source == "wb_wdi_cpi") { + cli_source_info(name = "wb_wdi_cpi", + origin = "The World Bank's World Development Indicator Database", + date = "Downloaded on the 2nd of January 2024", html = "https://databank.worldbank.org/source/world-development-indicators", - note = "Uses the linked deflator.") + note = "Uses the CPI as deflator.") } } diff --git a/R/sysdata.rda b/R/sysdata.rda index 759d574..6da5cd5 100644 Binary files a/R/sysdata.rda and b/R/sysdata.rda differ diff --git a/R/transform_user_input.R b/R/transform_user_input.R index e80cf7c..113fd26 100644 --- a/R/transform_user_input.R +++ b/R/transform_user_input.R @@ -1,10 +1,10 @@ # Transform user input for package internal use -transform_user_input <- function(gdp, unit_in, unit_out, source, with_regions, replace_NAs) { +transform_user_input <- function(gdp, unit_in, unit_out, source, use_USA_deflator_for_all, with_regions, replace_NAs) { . <- NULL # Convert to tibble, if necessary if (class(gdp)[1] == "magpie") { - gdp <- mag_2_tibb(gdp) + gdp <- tibble::as_tibble(gdp) %>% dplyr::rename(tidyselect::any_of(c("iso3c" = "region"))) } # Extract base years if they exist, and adjust string @@ -68,9 +68,13 @@ transform_user_input <- function(gdp, unit_in, unit_out, source, with_regions, r abort("Incompatible 'gdp' and 'source'. No information in source {crayon::bold(source_name)} for years in 'gdp'.") } - # Use different source if required - if (!is.null(replace_NAs) && !any(sapply(c(NA, 0, "no_conversion"), setequal, replace_NAs))) { - source <- adapt_source(gdp, source, with_regions, replace_NAs) + # Use different source if required by the replace_NAs argument + if (use_USA_deflator_for_all || + (!is.null(replace_NAs) && !any(sapply(c(NA, 0, "no_conversion"), setequal, replace_NAs))) ) { + if (use_USA_deflator_for_all || replace_NAs[1] == "with_USA") source <- adapt_source_USA(gdp, source, replace_NAs) + if (!is.null(replace_NAs) && !any(sapply(c(NA, 0, "no_conversion", "with_USA"), setequal, replace_NAs))){ + source <- adapt_source(gdp, source, with_regions, replace_NAs) + } source_name <- paste0(source_name, "_adapted") } diff --git a/R/ui.R b/R/ui.R index 16125bf..01c52b3 100644 --- a/R/ui.R +++ b/R/ui.R @@ -22,7 +22,7 @@ cli_elemental <- function(from, to, with, unit, val) { cli::cli({ cli::cli_text("{.strong {from}} {crayon::green(cli::symbol$arrow_right)} {.strong {to}}") cli::cli_text("{crayon::blue(with)}{cli::qty(as.character(values))}{?s} in {unit} used:") - cli::cli_dl(c(values)) + cli::cli_dl(c(signif(values, 6))) }) } cli_inform(my_cli) diff --git a/R/utils.R b/R/utils.R index d6370ca..750a254 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,3 +1,6 @@ +# Eager pipe. Used to make sure the messages of the elemental conversion are printed in the right order. +`%>e%` <- magrittr::pipe_eager_lexical + smart_select_iso3c <- function(gdp) { gdp %>% dplyr::select(tidyselect::vars_select_helpers$where( @@ -17,12 +20,3 @@ smart_select_year <- function(gdp) { )) %>% colnames() } - -mag_2_tibb <- function(gdp) { - gdp %>% - magclass::as.data.frame() %>% - tibble::as_tibble() %>% - dplyr::select(-"Cell") %>% - dplyr::rename("iso3c" = "Region", "year" = "Year", "value" = "Value") %>% - dplyr::mutate(year = as.integer(as.character(.data$year))) -} diff --git a/README.Rmd b/README.Rmd index a223674..f5b9b0c 100644 --- a/README.Rmd +++ b/README.Rmd @@ -24,7 +24,7 @@ knitr::opts_chunk$set( GDPuc (a.k.a. the GDP unit-converter) provides a simple function to convert GDP time-series data from one unit to another. -**To note:** The default conversion parameters are from the World Bank's World Development Indicators (WDI) database (see [link](https://databank.worldbank.org/source/world-development-indicators)). The current parameters are from **October 2021**, with the next update planned for October 2022. +**To note:** The default conversion parameters are from the World Bank's World Development Indicators (WDI) database (see [link](https://databank.worldbank.org/source/world-development-indicators)). The current parameters are from **January 2nd 2024**, with the next update planned for January 2025. ## Installation diff --git a/README.md b/README.md index 9ed56a1..4b316ee 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ convert GDP time-series data from one unit to another. **To note:** The default conversion parameters are from the World Bank’s World Development Indicators (WDI) database (see [link](https://databank.worldbank.org/source/world-development-indicators)). -The current parameters are from **October 2021**, with the next update -planned for October 2022. +The current parameters are from **January 2nd 2024**, with the next +update planned for January 2025. ## Installation diff --git a/data-raw/create_internal_sources.R b/data-raw/create_internal_sources.R index 7a067df..0db9b47 100644 --- a/data-raw/create_internal_sources.R +++ b/data-raw/create_internal_sources.R @@ -15,7 +15,8 @@ my_vars <- c( "Population, total", "GDP deflator (base year varies by country)", "GDP deflator: linked series (base year varies by country)", - "DEC alternative conversion factor (LCU per US$)" + "DEC alternative conversion factor (LCU per US$)", + "Consumer price index (2010 = 100)" ) my_info <- WDI::WDIsearch( @@ -32,21 +33,24 @@ my_info <- WDI::WDIsearch( tibble::as_tibble() # Download data, remove aggregates and do some pivoting and renaming -my_data <- WDI::WDI(indicator = my_info$indicator, extra = TRUE) %>% - tibble::as_tibble() %>% - dplyr::filter(!is.na(region) & region != "Aggregates") %>% - dplyr::arrange(iso3c, year) %>% - dplyr::select(iso3c, year, tidyselect::contains(my_info$indicator)) %>% - tidyr::pivot_longer(cols = tidyselect::contains(my_info$indicator), names_to = "id") %>% - dplyr::mutate(name = stringr::str_replace_all(id, my_info$name %>% - rlang::set_names(paste0("^", my_info$indicator, "$")))) %>% - dplyr::select(iso3c, year, id, name, value) +my_data <- purrr::map2(my_info$indicator, + my_info$name, + ~ WDI::WDI(indicator = .x, extra = TRUE) %>% + tibble::as_tibble() %>% + dplyr::filter(!is.na(region) & region != "Aggregates") %>% + dplyr::arrange(iso3c, year) %>% + dplyr::select(iso3c, year, tidyselect::contains(.x)) %>% + tidyr::pivot_longer(cols = tidyselect::contains(.x), names_to = "id") %>% + dplyr::mutate(name = .y) %>% + dplyr::select(iso3c, year, id, name, value)) %>% + purrr::list_rbind() wb_wdi <- my_data %>% dplyr::select(-id) %>% tidyr::pivot_wider(names_from = name) %>% dplyr::mutate(`GDP deflator: linked series` = `GDP deflator: linked series (base year varies by country)` / 100, `GDP deflator` = `GDP deflator (base year varies by country)` / 100, + `CPI` = `Consumer price index (2010 = 100)` / 100, `MER (LCU per US$)` = `DEC alternative conversion factor (LCU per US$)`) wb_wdi_linked <- wb_wdi %>% @@ -56,9 +60,16 @@ wb_wdi_linked <- wb_wdi %>% `PPP conversion factor, GDP (LCU per international $)`, `MER (LCU per US$)`) +wb_wdi_cpi <- wb_wdi %>% + dplyr::select(iso3c, + year, + `GDP deflator` = `CPI`, + `PPP conversion factor, GDP (LCU per international $)`, + `MER (LCU per US$)`) + # For now, IMF is removed due to copyright issues #usethis::use_data(imf_weo, wb_wdi, wb_wdi_linked, internal = TRUE, overwrite = TRUE) -usethis::use_data(wb_wdi, wb_wdi_linked, internal = TRUE, overwrite = TRUE) +usethis::use_data(wb_wdi, wb_wdi_linked, wb_wdi_cpi, internal = TRUE, overwrite = TRUE) diff --git a/man/GDPuc-package.Rd b/man/GDPuc-package.Rd index 5c2df6e..3e410b5 100644 --- a/man/GDPuc-package.Rd +++ b/man/GDPuc-package.Rd @@ -10,5 +10,18 @@ Convert GDP time series data from one unit to another. All common GDP units are included, i.e. current and constant local currency units, US$ via market exchange rates and international dollars via purchasing power parities. +} +\seealso{ +Useful links: +\itemize{ + \item \url{https://github.com/pik-piam/GDPuc} + \item \url{https://pik-piam.github.io/GDPuc/} + \item Report bugs at \url{https://github.com/pik-piam/GDPuc/issues} +} + +} +\author{ +\strong{Maintainer}: Johannes Koch \email{jokoch@pik-potsdam.de} + } \keyword{internal} diff --git a/man/convertGDP.Rd b/man/convertGDP.Rd index a78d051..6d77e87 100644 --- a/man/convertGDP.Rd +++ b/man/convertGDP.Rd @@ -2,6 +2,8 @@ % Please edit documentation in R/convertGDP.R \name{convertGDP} \alias{convertGDP} +\alias{convertCPI} +\alias{convertSingle} \title{Convert GDP data} \usage{ convertGDP( @@ -9,11 +11,16 @@ convertGDP( unit_in, unit_out, source = "wb_wdi", + use_USA_deflator_for_all = FALSE, with_regions = NULL, replace_NAs = NULL, verbose = getOption("GDPuc.verbose", default = FALSE), return_cfs = FALSE ) + +convertCPI(...) + +convertSingle(x, iso3c, year = 2000, ...) } \arguments{ \item{gdp}{A tibble, data frame or magpie object, the latter of which @@ -34,6 +41,7 @@ package to be installed. The data-frame needs to have at least 3 columns: \item "constant YYYY LCU" \item "constant YYYY Int$PPP" \item "constant YYYY US$MER" +\item "constant YYYY €" } where YYYY should be replaced with a year e.g. "2010" or "2017".} @@ -45,6 +53,7 @@ where YYYY should be replaced with a year e.g. "2010" or "2017".} \item "constant YYYY LCU" \item "constant YYYY Int$PPP" \item "constant YYYY US$MER" +\item "constant YYYY €" } where YYYY should be replaced with a year e.g. "2010" or "2017".} @@ -53,6 +62,10 @@ a data-frame that exists in the calling environment. Use \href{https://pik-piam.github.io/GDPuc/reference/print_source_info.html}{print_source_info()} to learn about the available sources.} +\item{use_USA_deflator_for_all}{TRUE or FALSE (default). If TRUE, then only the USA deflator is used to adjust for +inflation, regardless of the country codes provided. This is a very specific deviation from the correct conversion +process, which nevertheless is often used in the integrated assessment community. Use carefully!} + \item{with_regions}{NULL or a data-frame. The data-frame should be "country to region mapping": one column named "iso3c" with iso3c country codes, and one column named "region" with region codes to which the countries belong. Any regions in the gdp @@ -70,6 +83,8 @@ For the extrapolation, the closest 5 data points are used. \item "regional_average": missing conversion factors in the source object are replaced with the regional average of the region to which the country belongs. This requires a region-mapping to be passed to the function, see the with_regions argument. +\item "with USA": missing conversion factors in the source object are replaced with +the conversion factors of the USA. } Can also be a vector with "linear" as first element, e.g. c("linear", 0) or c("linear", "no_conversion"), in which case, the operations are done in sequence.} @@ -80,6 +95,15 @@ GDPuc.verbose option, which is FALSE if not set to TRUE by the user.} \item{return_cfs}{TRUE or FALSE. Set to TRUE to additionally return a tibble with the conversion factors used. In that case a list is returned with the converted GDP under "result", and the conversion factors used under "cfs".} + +\item{...}{Arguments passed on to \code{convertGDP()}} + +\item{x}{Number to convert} + +\item{iso3c}{Country code} + +\item{year}{Year of value. Is given the default value of 2000, since it only plays a role when converting from +or to current currencies. This facilitates the conversion between constant currencies.} } \value{ The gdp argument, with the values in the "value" column, converted to unit_out. If the argument @@ -108,7 +132,40 @@ The base year of the deflator can be any year, and can be country-specific. wit PPP exchange rate values. } } +\section{Functions}{ +\itemize{ +\item \code{convertCPI()}: Short cut for \code{convertGDP(..., source = "wb_wdi_cpi")} + +\item \code{convertSingle()}: Convert a single value, while specifying iso3c code and year. Simpler than creating a +single row tibble. + +}} +\examples{ + my_tbble <- tibble::tibble(iso3c = "FRA", + year = 2013, + value = 100) + + convertGDP(gdp = my_tbble, + unit_in = "current LCU", + unit_out = "constant 2015 Int$PPP") + + # Convert using the CPI as deflator. + convertGDP(gdp = my_tbble, + unit_in = "current LCU", + unit_out = "constant 2015 Int$PPP", + source = "wb_wdi_cpi") + # Or using the shortcut `convertCPI()` + convertCPI(gdp = my_tbble, + unit_in = "current LCU", + unit_out = "constant 2015 Int$PPP") + + # Convert a single value quickly + convertSingle(x = 100, + iso3c = "FRA", + year = 2013, + unit_in = "current LCU", + unit_out = "constant 2015 Int$PPP") +} \seealso{ -The \href{https://github.com/vincentarelbundock/countrycode}{countrycode} -package to convert country codes. +The \href{https://github.com/vincentarelbundock/countrycode}{countrycode} package to convert country codes. } diff --git a/man/print_source_info.Rd b/man/print_source_info.Rd index 8564196..b7eb945 100644 --- a/man/print_source_info.Rd +++ b/man/print_source_info.Rd @@ -7,10 +7,11 @@ print_source_info(source) } \arguments{ -\item{source}{The name of one of the internal sources: +\item{source}{Empty, or the name of one of the internal sources: \enumerate{ \item "wb_wdi" \item "wb_wdi_linked" +\item "wb_wdi_cpi" }} } \value{ @@ -23,3 +24,7 @@ Print detailed information on conversion factor sources to the screen. Informati html-link and an associated note. Calling the function without any argument will print information on all available sources. } +\examples{ +print_source_info() + +} diff --git a/tests/testthat/setup-global_vars.R b/tests/testthat/setup-global_vars.R index 41ed278..d360f3a 100644 --- a/tests/testthat/setup-global_vars.R +++ b/tests/testthat/setup-global_vars.R @@ -1,10 +1,12 @@ # Base year of constant US$MER GDP series in wdi data -year_USMER <- 2010 +year_USMER <- 2015 regex_var_USMER <- paste("GDP \\(constant", year_USMER, "US\\$\\)") +var_USMER <- paste("GDP (constant", year_USMER, "US$)") # Base year of constant Int$PPP GDP series in wb_wdi year_IntPPP <- 2017 regex_var_IntPPP <- paste("GDP, PPP \\(constant", year_IntPPP, "international \\$\\)") +var_IntPPP <- paste("GDP, PPP (constant", year_IntPPP, "international $)") -# WSM = Samoa has inconsistent WDI data... -bad_countries <- c("WSM") +# PAN = Panama has inconsistent WDI data... +bad_countries <- c("PAN") diff --git a/tests/testthat/test-check_user_input.R b/tests/testthat/test-01_check_user_input.R similarity index 57% rename from tests/testthat/test-check_user_input.R rename to tests/testthat/test-01_check_user_input.R index 2d4bfe7..fb53ba6 100644 --- a/tests/testthat/test-check_user_input.R +++ b/tests/testthat/test-01_check_user_input.R @@ -51,11 +51,29 @@ test_that("with_regions argument", { unit_out <- "current US$MER" s <- wb_wdi - expect_error(check_user_input(gdp, unit_in, unit_out, source = s, with_regions = "blabla")) + expect_error(check_user_input(gdp, + unit_in, + unit_out, + source = s, + use_USA_deflator_for_all = FALSE, + with_regions = "blabla")) + with_regions <- tibble::tibble("blabla" = "FRA", "region" = "USA") - expect_error(check_user_input(gdp, unit_in, unit_out, source = s, with_regions = with_regions)) + expect_error(check_user_input(gdp, + unit_in, + unit_out, + source = s, + use_USA_deflator_for_all = FALSE, + with_regions = with_regions)) + with_regions <- tibble::tibble("iso3c" = "FRA", "region" = "USA") - expect_error(check_user_input(gdp, unit_in, "current LCU", source = s, with_regions = with_regions)) + expect_error(check_user_input(gdp, + unit_in, + "current LCU", + source = s, + use_USA_deflator_for_all = FALSE, + with_regions = with_regions)) + my_bad_source <- wb_wdi %>% dplyr::select( "iso3c", "year", @@ -64,7 +82,12 @@ test_that("with_regions argument", { "PPP conversion factor, GDP (LCU per international $)" ) s <- my_bad_source - expect_error(check_user_input(gdp, unit_in, unit_out, source = s, with_regions = with_regions)) + expect_error(check_user_input(gdp, + unit_in, + unit_out, + source = s, + use_USA_deflator_for_all = FALSE, + with_regions = with_regions)) }) test_that("replace_NAs argument", { @@ -74,18 +97,42 @@ test_that("replace_NAs argument", { unit_out <- "current US$MER" s <- wb_wdi - expect_error(check_user_input(gdp, unit_in, unit_out, source = s, replace_NAs = 2, with_regions = NULL), + expect_error(check_user_input(gdp, + unit_in, + unit_out, + source = s, + use_USA_deflator_for_all = FALSE, + replace_NAs = 2, + with_regions = NULL), glue::glue("Invalid 'replace_NAs' argument. Has to be either NULL, NA, 0, 1, no_conversion, linear, \\ - regional_average or a combination of the above.")) - expect_error(check_user_input(gdp, unit_in, unit_out, source = s, replace_NAs = c(0, 1), with_regions = NULL), + regional_average, with_USA or a combination of the above.")) + expect_error(check_user_input(gdp, + unit_in, + unit_out, + source = s, + use_USA_deflator_for_all = FALSE, + replace_NAs = c(0, 1), + with_regions = NULL), glue::glue("Invalid 'replace_NAs' argument. The only accepted combinations of arguments start with \\ 'linear', e.g. c\\('linear', 'no_conversion'\\).")) expect_error( - check_user_input(gdp, unit_in, unit_out, source = s, replace_NAs = "linear_regional_average", with_regions = NULL) + check_user_input(gdp, + unit_in, + unit_out, + source = s, + use_USA_deflator_for_all = FALSE, + replace_NAs = "linear_regional_average", + with_regions = NULL) ) expect_error( - check_user_input(gdp, unit_in, unit_out, source = s, replace_NAs = "regional_average", with_regions = NULL), + check_user_input(gdp, + unit_in, + unit_out, + source = s, + use_USA_deflator_for_all = FALSE, + replace_NAs = "regional_average", + with_regions = NULL), glue::glue("Using 'regional_average' requires a region mapping. The 'with_regions' argument can't be NULL.") ) }) @@ -97,14 +144,21 @@ test_that("boolean arguments", { unit_out <- "current US$MER" s <- wb_wdi - expect_error(check_user_input(gdp, unit_in, unit_out, source = s, - with_regions = NULL, replace_NAs = NULL, verbose = "blabla")) + expect_error(check_user_input(gdp, + unit_in, + unit_out, + source = s, + use_USA_deflator_for_all = FALSE, + with_regions = NULL, + replace_NAs = NULL, + verbose = "blabla")) expect_error(check_user_input( gdp, unit_in, unit_out, source = s, + use_USA_deflator_for_all = FALSE, with_regions = NULL, replace_NAs = NULL, verbose = TRUE, diff --git a/tests/testthat/test-transform_user_input.R b/tests/testthat/test-02_transform_user_input.R similarity index 100% rename from tests/testthat/test-transform_user_input.R rename to tests/testthat/test-02_transform_user_input.R diff --git a/tests/testthat/test-elemental_conversions.R b/tests/testthat/test-03_elemental_conversions.R similarity index 69% rename from tests/testthat/test-elemental_conversions.R rename to tests/testthat/test-03_elemental_conversions.R index 7183a48..3c8bc2b 100644 --- a/tests/testthat/test-elemental_conversions.R +++ b/tests/testthat/test-03_elemental_conversions.R @@ -3,7 +3,7 @@ #------------------------------------------------------------ # Unit_in = current_LCU -test_that("current_LCU_2_constant_LCU_base_x wb_wdi", { +test_that("current_LCU_2_constant_LCU_base_y", { country_base_years <- wb_wdi %>% dplyr::filter(`GDP deflator (base year varies by country)` == 100) %>% dplyr::select("iso3c", "year") @@ -39,41 +39,41 @@ test_that("current_LCU_2_constant_LCU_base_x wb_wdi", { } }) -# test_that("current_LCU_2_constant_LCU_base_y linked series", { -# country_base_years <- wb_wdi_linked %>% -# dplyr::filter(`GDP deflator` == 1) %>% -# dplyr::select("iso3c", "year") -# -# my_years <- country_base_years %>% -# dplyr::group_by(year) %>% -# dplyr::count() %>% -# dplyr::arrange(-n) %>% -# dplyr::pull(year) -# -# for (my_base_year in my_years) { -# my_countries <- country_base_years %>% -# dplyr::filter(year == my_base_year) %>% -# dplyr::pull(iso3c) -# -# gdp_in <- wb_wdi %>% -# dplyr::filter(iso3c %in% my_countries, -# !is.na(`GDP (constant LCU)`)) %>% -# dplyr::select("iso3c", "year", "value" = `GDP: linked series (current LCU)`) -# -# gdp_conv <- current_LCU_2_constant_LCU_base_y(gdp_in, my_base_year, wb_wdi_linked) %>% -# dplyr::filter(!is.na(value)) -# -# gdp_out <- wb_wdi %>% -# dplyr::right_join(gdp_conv, by = c("iso3c", "year")) %>% -# dplyr::select("iso3c", "year", "value" = `GDP (constant LCU)`) -# -# for (country in unique(gdp_conv$iso3c)) { -# expect_equal(gdp_conv %>% dplyr::filter(iso3c == country), -# gdp_out %>% dplyr::filter(iso3c == country), -# label = country) -# } -# } -# }) +test_that("current_LCU_2_constant_LCU_base_y linked series", { + country_base_years <- wb_wdi_linked %>% + dplyr::filter(`GDP deflator` == 1) %>% + dplyr::select("iso3c", "year") + + my_years <- country_base_years %>% + dplyr::group_by(year) %>% + dplyr::count() %>% + dplyr::arrange(-n) %>% + dplyr::pull(year) + + for (my_base_year in my_years) { + my_countries <- country_base_years %>% + dplyr::filter(year == my_base_year) %>% + dplyr::pull(iso3c) + + gdp_in <- wb_wdi %>% + dplyr::filter(iso3c %in% my_countries, + !is.na(`GDP (constant LCU)`)) %>% + dplyr::select("iso3c", "year", "value" = `GDP: linked series (current LCU)`) + + gdp_conv <- current_LCU_2_constant_LCU_base_y(gdp_in, my_base_year, wb_wdi_linked) %>% + dplyr::filter(!is.na(value)) + + gdp_out <- wb_wdi %>% + dplyr::right_join(gdp_conv, by = c("iso3c", "year")) %>% + dplyr::select("iso3c", "year", "value" = `GDP (constant LCU)`) + + for (country in unique(gdp_conv$iso3c)) { + expect_equal(gdp_conv %>% dplyr::filter(iso3c == country), + gdp_out %>% dplyr::filter(iso3c == country), + label = country) + } + } +}) test_that("current_LCU_2_current_IntPPP", { gdp_in <- wb_wdi %>% @@ -179,7 +179,7 @@ test_that("constant_LCU_2_constant_IntPPP", { gdp_in <- wb_wdi %>% dplyr::filter(iso3c %in% my_countries, - dplyr::if_all(tidyselect::matches(regex_var_IntPPP), ~!is.na(.x))) %>% + !dplyr::if_any(tidyselect::matches(regex_var_IntPPP), ~ is.na(.x))) %>% dplyr::select("iso3c", "year", "value" = `GDP (constant LCU)`) gdp_conv <- constant_LCU_2_constant_IntPPP(gdp_in, year_IntPPP, wb_wdi) %>% @@ -207,7 +207,7 @@ test_that("constant_LCU_2_constant_USMER", { gdp_in <- wb_wdi %>% dplyr::filter(iso3c %in% my_countries, - dplyr::if_all(tidyselect::matches(regex_var_USMER), ~!is.na(.x))) %>% + !dplyr::if_any(tidyselect::matches(regex_var_USMER), ~ is.na(.x))) %>% dplyr::select("iso3c", "year", "value" = `GDP (constant LCU)`) gdp_conv <- constant_LCU_2_constant_USMER(gdp_in, year_USMER, wb_wdi) %>% @@ -292,3 +292,68 @@ test_that("constant_USMER_2_constant_LCU", { } }) +test_that("constant_USMER_2_constant_EURO", { + country_base_years <- wb_wdi %>% + dplyr::filter(`GDP deflator (base year varies by country)` == 100) %>% + dplyr::select("iso3c", "year") + + euro_countries <- c("AUT", "BEL", "HRV", "CYP", "EST", "FIN", "FRA", "DEU", "GRC", "IRL", + "ITA", "LVA", "LTU", "LUX", "NLD", "PRT", "SVK", "SVN", "ESP") + + my_countries <- country_base_years %>% + dplyr::filter(year == year_USMER, iso3c %in% euro_countries) %>% + dplyr::pull(iso3c) + + gdp_in <- wb_wdi %>% + dplyr::filter(iso3c %in% my_countries, + !is.na(`GDP (constant LCU)`)) %>% + dplyr::select("iso3c", "year", "value" = tidyselect::matches(regex_var_USMER)) + + gdp_conv <- constant_USMER_2_constant_EURO(gdp_in, year_USMER, wb_wdi) %>% + dplyr::filter(!is.na(value)) + + gdp_out <- constant_USMER_2_constant_LCU(gdp_in, year_USMER, wb_wdi) %>% + dplyr::filter(!is.na(value)) + + for (country in unique(gdp_conv$iso3c)) { + expect_equal(gdp_conv %>% dplyr::filter(iso3c == country), + gdp_out %>% dplyr::filter(iso3c == country), + label = country) + } +}) + + +#------------------------------------------------------------ +#------------------------------------------------------------ +#------------------------------------------------------------ +# Unit_in = constant_EURO + +test_that("constant_EURO_2_constant_USMER", { + country_base_years <- wb_wdi %>% + dplyr::filter(`GDP deflator (base year varies by country)` == 100) %>% + dplyr::select("iso3c", "year") + + euro_countries <- c("AUT", "BEL", "HRV", "CYP", "EST", "FIN", "FRA", "DEU", "GRC", "IRL", + "ITA", "LVA", "LTU", "LUX", "NLD", "PRT", "SVK", "SVN", "ESP") + + my_countries <- country_base_years %>% + dplyr::filter(year == year_USMER, iso3c %in% euro_countries) %>% + dplyr::pull(iso3c) + + gdp_in <- wb_wdi %>% + dplyr::filter(iso3c %in% my_countries, + !is.na(`GDP (constant LCU)`)) %>% + dplyr::select("iso3c", "year", "value" = tidyselect::matches(regex_var_USMER)) + + gdp_conv <- constant_EURO_2_constant_USMER(gdp_in, year_USMER, wb_wdi) %>% + dplyr::filter(!is.na(value)) + + gdp_out <- constant_LCU_2_constant_USMER(gdp_in, year_USMER, wb_wdi) %>% + dplyr::filter(!is.na(value)) + + for (country in unique(gdp_conv$iso3c)) { + expect_equal(gdp_conv %>% dplyr::filter(iso3c == country), + gdp_out %>% dplyr::filter(iso3c == country), + label = country) + } +}) diff --git a/tests/testthat/test-composite_conversions.R b/tests/testthat/test-04_composite_conversions.R similarity index 96% rename from tests/testthat/test-composite_conversions.R rename to tests/testthat/test-04_composite_conversions.R index 6170d71..19c288b 100644 --- a/tests/testthat/test-composite_conversions.R +++ b/tests/testthat/test-04_composite_conversions.R @@ -5,7 +5,7 @@ test_that("current_LCU_2_constant_IntPPP_base_y", { gdp_in <- wb_wdi %>% - dplyr::filter(dplyr::if_all(tidyselect::matches(regex_var_IntPPP), ~!is.na(.x))) %>% + dplyr::filter(!is.na(!!rlang::sym(var_IntPPP))) %>% dplyr::select("iso3c", "year", "value" = `GDP (current LCU)`) gdp_conv <- current_LCU_2_constant_IntPPP_base_y(gdp_in, year_IntPPP, wb_wdi) %>% @@ -25,7 +25,7 @@ test_that("current_LCU_2_constant_IntPPP_base_y", { test_that("current_LCU_2_constant_USMER_base_y", { gdp_in <- wb_wdi %>% - dplyr::filter(dplyr::if_all(tidyselect::matches(regex_var_USMER), ~!is.na(.x))) %>% + dplyr::filter(!is.na(!!rlang::sym(var_USMER))) %>% dplyr::select("iso3c", "year", "value" = `GDP (current LCU)`) gdp_conv <- current_LCU_2_constant_USMER_base_y(gdp_in, year_USMER, wb_wdi)%>% @@ -119,7 +119,7 @@ test_that("current_IntPPP_2_constant_LCU_base_y", { test_that("current_IntPPP_2_constant_IntPPP", { gdp_in <- wb_wdi %>% - dplyr::filter(dplyr::if_all(tidyselect::matches(regex_var_IntPPP), ~!is.na(.x))) %>% + dplyr::filter(!is.na(!!rlang::sym(var_IntPPP))) %>% # Convert the PPP current series from one based on the linked LCU series # to one based on the standard LCU series dplyr::mutate(value = `GDP, PPP (current international $)` * @@ -145,7 +145,7 @@ test_that("current_IntPPP_2_constant_IntPPP", { test_that("current_IntPPP_2_constant_USMER", { gdp_in <- wb_wdi %>% - dplyr::filter(dplyr::if_all(tidyselect::matches(regex_var_USMER), ~!is.na(.x))) %>% + dplyr::filter(!is.na(!!rlang::sym(var_USMER))) %>% # Convert the PPP current series from one based on the linked LCU series # to one based on the standard LCU series dplyr::mutate(value = `GDP, PPP (current international $)` * @@ -201,7 +201,7 @@ test_that("current_USMER_2_current_IntPPP", { test_that("current_USMER_2_constant_USMER", { gdp_in <- wb_wdi %>% - dplyr::filter(dplyr::if_all(tidyselect::matches(regex_var_USMER), ~!is.na(.x))) %>% + dplyr::filter(!is.na(!!rlang::sym(var_USMER))) %>% dplyr::select("iso3c", "year", "value" = `GDP (current US$)`) gdp_conv <- current_USMER_2_constant_USMER_base_y(gdp_in, year_USMER, wb_wdi) %>% @@ -220,7 +220,7 @@ test_that("current_USMER_2_constant_USMER", { test_that("current_USMER_2_constant_IntPPP", { gdp_in <- wb_wdi %>% - dplyr::filter(dplyr::if_all(tidyselect::matches(regex_var_IntPPP), ~!is.na(.x))) %>% + dplyr::filter(!is.na(!!rlang::sym(var_IntPPP))) %>% dplyr::select("iso3c", "year", "value" = `GDP (current US$)`) gdp_conv <- current_USMER_2_constant_IntPPP_base_y(gdp_in, year_IntPPP, wb_wdi) %>% @@ -343,7 +343,7 @@ test_that("constant_LCU_base_x_2_constant_IntPPP_base_y", { gdp_in <- wb_wdi %>% dplyr::filter(iso3c %in% my_countries, - dplyr::if_all(tidyselect::matches(regex_var_IntPPP), ~!is.na(.x))) %>% + !is.na(!!rlang::sym(var_IntPPP))) %>% dplyr::select("iso3c", "year", "value" = `GDP (constant LCU)`) gdp_conv <- constant_LCU_base_x_2_constant_IntPPP_base_y(gdp_in, my_base_year, year_IntPPP, wb_wdi) %>% @@ -380,7 +380,7 @@ test_that("constant_LCU_base_x_2_constant_USMER_base_y", { gdp_in <- wb_wdi %>% dplyr::filter(iso3c %in% my_countries, - dplyr::if_all(tidyselect::matches(regex_var_USMER), ~!is.na(.x))) %>% + !is.na(!!rlang::sym(var_USMER))) %>% dplyr::select("iso3c", "year", "value" = `GDP (constant LCU)`) gdp_conv <- constant_LCU_base_x_2_constant_USMER_base_y(gdp_in, my_base_year, year_USMER, wb_wdi) %>% @@ -503,7 +503,7 @@ test_that("constant_IntPPP_base_x_2_constant_LCU_base_y", { test_that("constant_IntPPP_base_x_2_constant_USMER_base_y", { gdp_in <- wb_wdi %>% - dplyr::filter(dplyr::if_all(tidyselect::matches(regex_var_USMER), ~!is.na(.x))) %>% + dplyr::filter(!is.na(!!rlang::sym(var_USMER))) %>% dplyr::select("iso3c", "year", "value" = tidyselect::matches(regex_var_IntPPP)) gdp_conv <- constant_IntPPP_base_x_2_constant_USMER_base_y(gdp_in, year_IntPPP, year_USMER, wb_wdi)%>% @@ -593,7 +593,7 @@ test_that("constant_USMER_base_x_2_current_USMER", { test_that("constant_USMER_base_x_2_constant_IntPPP_base_y", { gdp_in <- wb_wdi %>% - dplyr::filter(dplyr::if_all(tidyselect::matches(regex_var_IntPPP), ~!is.na(.x))) %>% + dplyr::filter(!is.na(!!rlang::sym(var_IntPPP))) %>% dplyr::select("iso3c", "year", "value" = tidyselect::matches(regex_var_USMER)) gdp_conv <- constant_USMER_base_x_2_constant_IntPPP_base_y(gdp_in, year_USMER, year_IntPPP, wb_wdi)%>% diff --git a/tests/testthat/test-convertGDP.R b/tests/testthat/test-05_convertGDP.R similarity index 91% rename from tests/testthat/test-convertGDP.R rename to tests/testthat/test-05_convertGDP.R index 4035b24..f64fb26 100644 --- a/tests/testthat/test-convertGDP.R +++ b/tests/testthat/test-05_convertGDP.R @@ -1,6 +1,7 @@ test_that("convertGDP", { gdp_in <- wb_wdi %>% - dplyr::filter(!is.na(`GDP, PPP (constant 2017 international $)`)) %>% + dplyr::filter(!iso3c %in% bad_countries, + !is.na(`GDP, PPP (constant 2017 international $)`)) %>% dplyr::select("iso3c", "year", "value" = `GDP (current LCU)`) gdp_conv <- convertGDP(gdp_in, "current LCU", "constant 2017 Int$PPP") %>% @@ -15,12 +16,14 @@ test_that("convertGDP", { test_that("convertGDP different column names", { gdp_in1 <- wb_wdi %>% - dplyr::filter(!is.na(`GDP, PPP (constant 2017 international $)`)) %>% + dplyr::filter(!iso3c %in% bad_countries, + !is.na(`GDP, PPP (constant 2017 international $)`)) %>% dplyr::select("r"=iso3c, year, "value" = `GDP (current LCU)`) gdp_in1b <- dplyr::mutate(gdp_in1, r = "") gdp_in2 <- wb_wdi %>% - dplyr::filter(!is.na(`GDP, PPP (constant 2017 international $)`)) %>% + dplyr::filter(!iso3c %in% bad_countries, + !is.na(`GDP, PPP (constant 2017 international $)`)) %>% dplyr::select(iso3c, "y" = year, "value" = `GDP (current LCU)`) gdp_in2b <- dplyr::mutate(gdp_in2, y = "") @@ -65,7 +68,7 @@ test_that("convertGDP magpie object", { years = c(2001, 2002), names = c("ssp1", "ssp2"), fill = 100) - magclass::getSets(gdp_in)[1] <- c("r") + magclass::getSets(gdp_in)[1] <- c("iso3c") gdp_conv <- convertGDP(gdp_in, "current LCU", "constant 2017 Int$PPP") @@ -86,7 +89,8 @@ test_that("convertGDP data.frame object", { test_that("convertGDP unit_in == unit_out", { gdp_in <- wb_wdi %>% - dplyr::filter(!is.na(`GDP, PPP (constant 2017 international $)`)) %>% + dplyr::filter(!iso3c %in% bad_countries, + !is.na(`GDP, PPP (constant 2017 international $)`)) %>% dplyr::select(iso3c, year, "value" = `GDP: linked series (current LCU)`) expect_message(convertGDP(gdp_in, "current LCU", "current LCU", verbose = TRUE), diff --git a/tests/testthat/test-replace_NAs.R b/tests/testthat/test-06_replace_NAs.R similarity index 86% rename from tests/testthat/test-replace_NAs.R rename to tests/testthat/test-06_replace_NAs.R index 165cba8..80c1d0b 100644 --- a/tests/testthat/test-replace_NAs.R +++ b/tests/testthat/test-06_replace_NAs.R @@ -55,27 +55,27 @@ test_that("convertGDP replace missing conversion factors", { }) test_that("convertGDP replace_NAs = NA", { - # wb_wi does not have info for ABW in 2019 - gdp <- tidyr::expand_grid("iso3c" = c("ABW", "DEU", "USA"), + # wb_wi does not have info for AFG in 2022 + gdp <- tidyr::expand_grid("iso3c" = c("AFG", "DEU", "USA"), "year" = c(2010, 2015, 2025), "SSP" = c("SSP1", "SSP2"), "value" = 100) expect_warning(convertGDP(gdp, unit_in = "constant 2005 Int$PPP", - unit_out = "constant 2019 US$MER")) + unit_out = "constant 2022 US$MER")) expect_silent(convertGDP(gdp, unit_in = "constant 2005 Int$PPP", - unit_out = "constant 2019 US$MER", + unit_out = "constant 2022 US$MER", replace_NAs = NA)) gdp_1 <- suppressWarnings(convertGDP(gdp, unit_in = "constant 2005 Int$PPP", - unit_out = "constant 2019 US$MER")) + unit_out = "constant 2022 US$MER")) gdp_2 <- convertGDP(gdp, unit_in = "constant 2005 Int$PPP", - unit_out = "constant 2019 US$MER", + unit_out = "constant 2022 US$MER", replace_NAs = NA) expect_equal(gdp_1, gdp_2) @@ -83,36 +83,36 @@ test_that("convertGDP replace_NAs = NA", { test_that("convertGDP replace_NAs = 'no_conversion'", { - # wb_wi does not have info for ABW in 2019 - gdp <- tidyr::expand_grid("iso3c" = c("ABW", "DEU", "USA"), + # wb_wi does not have info for AFG in 2022 + gdp <- tidyr::expand_grid("iso3c" = c("AFG", "DEU", "USA"), "year" = c(2010, 2015, 2025), "SSP" = c("SSP1", "SSP2"), "value" = 100) expect_warning(convertGDP(gdp, unit_in = "constant 2005 Int$PPP", - unit_out = "constant 2019 US$MER")) + unit_out = "constant 2022 US$MER")) gdp_conv <- convertGDP(gdp, unit_in = "constant 2005 Int$PPP", - unit_out = "constant 2019 US$MER", + unit_out = "constant 2022 US$MER", replace_NAs = "no_conversion") expect_identical(gdp[1:6,], gdp_conv[1:6,]) }) test_that("convertGDP replace_NAs = linear", { - # wb_wi does not have info for ABW in 2019 - gdp <- tidyr::expand_grid("iso3c" = c("ABW", "DEU", "USA"), + # wb_wi does not have info for AFG in 2022 + gdp <- tidyr::expand_grid("iso3c" = c("AFG", "DEU", "USA"), "year" = c(2010, 2015, 2025), "SSP" = c("SSP1", "SSP2"), "value" = 100) expect_warning(convertGDP(gdp, unit_in = "constant 2005 Int$PPP", - unit_out = "constant 2019 US$MER")) + unit_out = "constant 2022 US$MER")) gdp_conv <- convertGDP(gdp, unit_in = "constant 2005 Int$PPP", - unit_out = "constant 2019 US$MER", + unit_out = "constant 2022 US$MER", replace_NAs = "linear") expect_true(!any(is.na(gdp_conv$value))) diff --git a/tests/testthat/test-verbose.R b/tests/testthat/test-07_verbose.R similarity index 100% rename from tests/testthat/test-verbose.R rename to tests/testthat/test-07_verbose.R diff --git a/tests/testthat/test-warn.R b/tests/testthat/test-08_warn.R similarity index 100% rename from tests/testthat/test-warn.R rename to tests/testthat/test-08_warn.R diff --git a/tests/testthat/test-get_conversion_factors.R b/tests/testthat/test-09_get_conversion_factors.R similarity index 100% rename from tests/testthat/test-get_conversion_factors.R rename to tests/testthat/test-09_get_conversion_factors.R diff --git a/tests/testthat/test-print_source_info.R b/tests/testthat/test-10_print_source_info.R similarity index 71% rename from tests/testthat/test-print_source_info.R rename to tests/testthat/test-10_print_source_info.R index 609ac44..0d64847 100644 --- a/tests/testthat/test-print_source_info.R +++ b/tests/testthat/test-10_print_source_info.R @@ -3,7 +3,9 @@ test_that("multiplication works", { expect_message( expect_message( expect_message( - print_source_info() + expect_message( + print_source_info() + ) ) ) )