Skip to content

Commit

Permalink
Replace missing PPPs by MERs instead of with 1
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneskoch94 committed Oct 14, 2024
1 parent f06ed78 commit 8ad74bf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 6 additions & 4 deletions R/adapt_source.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) %>%
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-06_replace_NAs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8ad74bf

Please sign in to comment.