From 8ad74bfb843c615d1cf39946e3978dc025c7bc84 Mon Sep 17 00:00:00 2001 From: Johannes Koch Date: Mon, 14 Oct 2024 18:12:00 +0200 Subject: [PATCH] Replace missing PPPs by MERs instead of with 1 --- R/adapt_source.R | 10 ++++++---- tests/testthat/test-06_replace_NAs.R | 9 +++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/R/adapt_source.R b/R/adapt_source.R index 7ac46a8..7ad906f 100644 --- a/R/adapt_source.R +++ b/R/adapt_source.R @@ -129,10 +129,12 @@ adapt_source <- function(gdp, source, with_regions, replace_NAs, require_year_co dplyr::arrange(.data$iso3c, .data$year) %>% dplyr::select(-"gd") - # Replace missing MER and PPP values with US values (=1) - source_adapted <- source_adapted %>% - tidyr::replace_na(list("MER (LCU per US$)" = 1, - "PPP conversion factor, GDP (LCU per international $)" = 1)) + # If there is no PPP data whatsoever for the country, use MERs + source_adapted <- source_adapted %>% + dplyr::mutate("PPP conversion factor, GDP (LCU per international $)" = + dplyr::if_else(is.na(.data$`PPP conversion factor, GDP (LCU per international $)`), + .data$`MER (LCU per US$)`, + .data$`PPP conversion factor, GDP (LCU per international $)`)) # If there is no deflator data whatsoever for the country, use US values ec <- dplyr::group_by(source_adapted, .data$iso3c) %>% diff --git a/tests/testthat/test-06_replace_NAs.R b/tests/testthat/test-06_replace_NAs.R index ce5c55c..473edd5 100644 --- a/tests/testthat/test-06_replace_NAs.R +++ b/tests/testthat/test-06_replace_NAs.R @@ -154,6 +154,15 @@ test_that("convertGDP replace_NAs = with_USA", { expect_true(!any(is.na(gdp_conv3$result$value))) }) +test_that("convertGDP replace_NAs = with_USA, no NAs for MADRAT countries", { + gdp <- tibble::tibble("iso3c" = madrat::toolGetMapping("regionmappingH12.csv")$CountryCode, + value = 1) %>% + convertGDP(unit_in = "constant 2005 US$MER", + unit_out = "constant 2017 Int$PPP", + replace_NAs = "with_USA") + expect_true(!any(is.na(gdp$value))) +}) + test_that("lin_int_ext", { x <- c(NA,NA,NA,NA,NA,NA,2,3,4,5,NA,7,8,NA,NA,NA,NA,NA,NA) expect_equal(lin_int_ext(x), -4:14)