diff --git a/.buildlibrary b/.buildlibrary index 50f362f..be3242f 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '2996376' +ValidationKey: '3971400' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' @@ -6,3 +6,4 @@ AcceptedWarnings: AcceptedNotes: ~ allowLinterWarnings: yes AddInReadme: inst/README.md +enforceVersionUpdate: no diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 870f216..f6ea5d4 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-pandoc@v2 @@ -23,7 +23,6 @@ jobs: - uses: r-lib/actions/setup-r-dependencies@v2 with: extra-packages: | - gamstransfer=?ignore any::lucode2 any::covr any::madrat @@ -36,7 +35,7 @@ jobs: # gms, goxygen, GDPuc) will usually have an outdated binary version # available; by using extra-packages we get the newest version - - uses: actions/setup-python@v4 + - uses: actions/setup-python@v5 with: python-version: 3.9 @@ -49,6 +48,11 @@ jobs: shell: Rscript {0} run: lucode2:::validkey(stopIfInvalid = TRUE) + - name: Verify that lucode2::buildLibrary was successful + if: github.event_name == 'pull_request' + shell: Rscript {0} + run: lucode2:::isVersionUpdated() + - name: Checks shell: Rscript {0} run: | diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6073834..62f13da 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ exclude: '^tests/testthat/_snaps/.*$' repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: 2c9f875913ee60ca25ce70243dc24d5b6415598c # frozen: v4.6.0 hooks: - id: check-case-conflict - id: check-json @@ -15,7 +15,7 @@ repos: - id: mixed-line-ending - repo: https://github.com/lorenzwalthert/precommit - rev: v0.3.2.9027 + rev: 7910e0323d7213f34275a7a562b9ef0fde8ce1b9 # frozen: v0.4.2 hooks: - id: parsable-R - id: deps-in-desc diff --git a/CITATION.cff b/CITATION.cff index 6be518d..9d049b4 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -2,8 +2,8 @@ cff-version: 1.2.0 message: If you use this software, please cite it using the metadata from this file. type: software title: 'mrdrivers: Create GDP and Population Scenarios' -version: 1.5.2 -date-released: '2023-12-22' +version: 2.0.0 +date-released: '2024-05-14' abstract: Create GDP and population scenarios This package constructs the GDP and population scenarios used as drivers in both the REMIND and MAgPIE models. authors: diff --git a/DESCRIPTION b/DESCRIPTION index 653e81e..54cbbb7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: mrdrivers Type: Package Title: Create GDP and Population Scenarios -Version: 1.5.2 +Version: 2.0.0 Authors@R: c(person(given = "Johannes", family = "Koch", email = "jokoch@pik-potsdam.de", @@ -22,7 +22,7 @@ Imports: countrycode, dplyr, lifecycle, - GDPuc (>= 0.8.0), + GDPuc (>= 1.0.0), glue, magrittr, purrr, @@ -47,7 +47,7 @@ Suggests: yaml Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 -Date: 2023-12-22 +RoxygenNote: 7.3.1 +Date: 2024-05-14 Config/testthat/edition: 3 VignetteBuilder: knitr diff --git a/R/calcDriver.R b/R/calcDriver.R index 3c03058..b67f93a 100644 --- a/R/calcDriver.R +++ b/R/calcDriver.R @@ -130,11 +130,15 @@ calcScenarioConstructor <- function(driver, supplementary = TRUE) # Give weight same names as data, so that aggregate doesn't mess up data dim getNames(weight$x) <- getNames(harmonizedData$x) - # Make sure weight and harmonizedData have the same yearly resolution. Sometimes x has more years than weigth, - # thus the intersect operation. Then if weight has more years than x, only years that exist in x are used. - # (this applies specifically to the noCovid and ISIMIP scenarios) - harmonizedData$x <- harmonizedData$x[, intersect(getYears(harmonizedData$x), getYears(weight$x)), ] - weight$x <- weight$x[, getYears(harmonizedData$x), ] + # Make sure weight and harmonizedData have the same yearly resolution. + # Sometimes weght has more years than x, thus the intersect operation. + weight$x <- weight$x[, intersect(getYears(harmonizedData$x), getYears(weight$x)), ] + # If x has more years than weight, add these years and interpolate + missingYears <- getYears(harmonizedData$x)[! getYears(harmonizedData$x) %in% getYears(weight$x)] + weight$x <- add_columns(weight$x, missingYears, dim = 2, fill = 0) + weight$x <- weight$x[, sort(getYears(weight$x)), ] + weight$x <- toolInterpolateAndExtrapolate(weight$x, extrapolate = FALSE) + description <- glue("{description} Associated {weight$description}") } @@ -168,26 +172,49 @@ calcHarmonizedData <- function(driver, scenario, pastData, futureData, harmoniza # Depending on the setup, the scenario construction either requires 'past' and 'future' scenarios, or not! # For example, many GDP scenarios are actually constructed as GDPpc scenarios, and then simply multiplied with # population scenarios. - if (pastData != "-") { - past <- calcOutput("PastData", driver = driver, pastData = pastData, aggregate = FALSE, supplementary = TRUE, ...) - } - if (futureData != "-") { - future <- calcOutput("FutureData", - driver = driver, - futureData = futureData, - aggregate = FALSE, - supplementary = TRUE, - ...) - } + past <- if (pastData != "-") { + calcOutput("PastData", driver = driver, pastData = pastData, aggregate = FALSE, supplementary = TRUE, ...) + } else NULL + future <- if (futureData != "-") { + calcOutput("FutureData", driver = driver, futureData = futureData, aggregate = FALSE, supplementary = TRUE, ...) + } else NULL - args <- c(list(...), as.list(environment())) switch( driver, - "Population" = calcOutput("PopulationHarmonized", args = args, aggregate = FALSE, supplementary = TRUE), - "GDP" = calcOutput("GDPHarmonized", args = args, aggregate = FALSE, supplementary = TRUE), - "GDPpc" = calcOutput("GDPpcHarmonized", args = args, aggregate = FALSE, supplementary = TRUE), - "Labour" = calcOutput("LabourHarmonized", args = args, aggregate = FALSE, supplementary = TRUE), - "Urban" = calcOutput("UrbanHarmonized", args = args, aggregate = FALSE, supplementary = TRUE) + "Population" = calcOutput("PopulationHarmonized", + harmonization = harmonization, + past = past, + future = future, + aggregate = FALSE, + supplementary = TRUE, + ...), + "GDP" = calcOutput("GDPHarmonized", + harmonization = harmonization, + past = past, + future = future, + scenario = scenario, + aggregate = FALSE, + supplementary = TRUE, + ...), + "GDPpc" = calcOutput("GDPpcHarmonized",harmonization = harmonization, + past = past, + future = future, + scenario = scenario, + aggregate = FALSE, + supplementary = TRUE, + ...), + "Labour" = calcOutput("LabourHarmonized",harmonization = harmonization, + past = past, + future = future, + aggregate = FALSE, + supplementary = TRUE, + ...), + "Urban" = calcOutput("UrbanHarmonized",harmonization = harmonization, + past = past, + future = future, + aggregate = FALSE, + supplementary = TRUE, + ...) ) } diff --git a/R/calcGDP.R b/R/calcGDP.R index a523f54..25f690c 100644 --- a/R/calcGDP.R +++ b/R/calcGDP.R @@ -18,10 +18,7 @@ #' #' @param unit A string specifying the unit of GDP. Can be either: #' \itemize{ -#' \item "constant 2005 Int$PPP" (default): Scenarios are constructed in constant 2005 Int$PPP. -#' \item "constant 2005 US$MER": Scenarios are constructed in constant 2005 Int$PPP and then converted with -#' [GDPuc::convertGDP()]. -#' \item "constant 2017 Int$PPP": Scenarios are constructed in constant 2017 Int$PPP. +#' \item "constant 2017 Int$PPP" (default): Scenarios are constructed in constant 2017 Int$PPP. #' \item "constant 2017 US$MER": Scenarios are constructed in constant 2017 Int$PPP and then converted with #' [GDPuc::convertGDP()]. #' } @@ -48,9 +45,6 @@ #' #' # Return only the SSP2EU GDP scenario #' calcOutput("GDP", scenario = "SSP2EU") -#' -#' # Return the now-outdated GDP scenarios used before summer 2021, -#' calcOutput("GDP", scenario = "SSPsOld", extension2150 = "constant", average2020 = FALSE) #' } #' calcGDP <- function(scenario = c("SSPs", "SDPs", "SSP2EU"), @@ -60,25 +54,15 @@ calcGDP <- function(scenario = c("SSPs", "SDPs", "SSP2EU"), # Check user input toolCheckUserInput(driver = "GDP", args = c(list(...), as.list(environment()))) - # GDP scenarios are constructed in PPPs. If MERs are desired, scenarios with the - # same base year but in PPPs are constructed, and converted to MERs at the end. - constructUnit <- unit - if (grepl("^constant .* US\\$MER$", unit)) { - constructUnit <- paste0("constant ", substr(unit, 10, 13), " Int$PPP") - } - + # GDP scenarios are constructed in 2017 Int$PPP, and converted, if necessary, at the end. gdp <- calcOutput("Driver", driver = "GDP", scenario = scenario, - unit = constructUnit, + unit = "constant 2017 Int$PPP", aggregate = FALSE, supplementary = TRUE, ...) - if (average2020 && any(grepl("SSPsOld", scenario))) { - warning("Average 2020 is not compatible with SSPsOld. Setting to FALSE.") - average2020 <- FALSE - } if (average2020) { # For REMIND, the consensus is to average the 2020 value so as to dampen the effect of the COVID shock. (The # reasoning being that REMIND uses 5-year time steps, and that the year-in-itself should represent the 2,5 years @@ -97,10 +81,17 @@ calcGDP <- function(scenario = c("SSPs", "SDPs", "SSP2EU"), message("The 2020 value is an an avergae over the 2018-2022 time period!! Only returning 5 year time steps.") } - if (constructUnit != unit) { + # Convert to US$MER if required + if (grepl("US$MER", unit)) { # Convert by interpolating and extrapolating missing conversion factors when possible. - gdp$x <- GDPuc::convertGDP(gdp$x, constructUnit, unit, replace_NAs = c("linear", "no_conversion")) + gdp$x <- GDPuc::convertGDP(gdp$x, + unit_in = "constant 2017 Int$PPP", + unit_out = "constant 2017 US$MER", + replace_NAs = c("linear", "no_conversion")) } + # Temporary shifting to 2005 prices, using only the US deflator for all countries, and neglecting any changes in + # PPPs or MERs. + if (grepl("2005", unit)) gdp$x <- gdp$x * 0.8121123 list(x = gdp$x, weight = gdp$weight, unit = glue("mil. {unit}"), description = gdp$description) } diff --git a/R/calcGDPFuture.R b/R/calcGDPFuture.R index b7feab8..48d0f21 100644 --- a/R/calcGDPFuture.R +++ b/R/calcGDPFuture.R @@ -6,10 +6,9 @@ #' \item "SSP2EU": Combined SSP2 and Eurostat (for the EU countries) source #' \item "SDPs": #' \item "MI": Missing island dataset -#' \item "OECD": OECD #' } #' See the "Combining data sources with '-'" section below for how to combine data sources. -calcGDPFuture <- function(GDPFuture = "SSPs-MI", unit = "constant 2005 Int$PPP") { # nolint +calcGDPFuture <- function(GDPFuture, unit) { # nolint # Check user input toolCheckUserInput("GDPFuture", as.list(environment())) # Call calcInternalGDPFuture function the appropriate number of times (map) and combine (reduce) @@ -27,6 +26,7 @@ calcInternalGDPFuture <- function(GDPFuture, unit) { # nolint data <- switch( GDPFuture, "SSPs" = calcOutput("InternalGDPFutureSSPs", unit = unit, aggregate = FALSE, supplementary = TRUE), + "SSP2" = calcOutput("InternalGDPFutureSSP2", unit = unit, aggregate = FALSE, supplementary = TRUE), "SSP2EU" = calcOutput("InternalGDPFutureSSP2EU", unit = unit, aggregate = FALSE, supplementary = TRUE), "SDPs" = calcOutput("InternalGDPFutureSDPs", unit = unit, aggregate = FALSE, supplementary = TRUE), "MI" = calcOutput("InternalGDPMI", unit = unit, aggregate = FALSE, supplementary = TRUE), @@ -43,12 +43,10 @@ calcInternalGDPFuture <- function(GDPFuture, unit) { # nolint # Functions ###################################################################################### calcInternalGDPFutureSSPs <- function(unit) { + # Read in gdp SSP projections (in billions) and convert to millions data <- readSource("SSP", subtype = "gdp") * 1000 - - # Refactor names - data <- collapseNames(data) - getSets(data)[3] <- "variable" - getNames(data) <- paste0("gdp_", gsub("_v[[:alnum:],[:punct:]]*", "", getNames(data))) + # Add gdp_ to variable dimension + getNames(data) <- paste0("gdp_", getNames(data)) # GDPFutureSSP is constructed in PPPs. if (grepl("^constant .* US\\$MER$", unit)) { @@ -57,36 +55,36 @@ calcInternalGDPFutureSSPs <- function(unit) { constructUnit <- unit } - # The default construct unit is "constant 2005 Int$PPP". If another unit is + # THIS MAY BE DEPRECATED IN THE NEAR FUTURE + # The default construct unit is "constant 2017 Int$PPP". If another unit is # demanded, then some modifications have to be done. - if (constructUnit != "constant 2005 Int$PPP") { + if (constructUnit != "constant 2017 Int$PPP") { # Construct SSP pathways in constant YYYY Int$PPP. # For the near future, convert using current conversion factors. # After that the scenarios are built by converting the US GDP, and building # the other countries in relation to the US so that by 2100, they have the - # same ratio as in 2005 Int$PPP. - data2005PPP <- data + # same ratio as in 2017 Int$PPP. + data2017PPP <- data - # The near future is defined hear by the next 15 years, or until 10 years after the last - # imf prediction. + # The near future is defined hear by the next 15 years, or until 10 years after the last imf prediction. c15 <- max(getYears(readSource("IMF", "GDPpc"), as.integer = TRUE)) + 10 - y1 <- getYears(data2005PPP)[getYears(data2005PPP, as.integer = TRUE) <= c15] - dataNearFut <- data2005PPP[, y1, ] %>% - GDPuc::convertGDP("constant 2005 Int$PPP", unit, replace_NAs = c("linear", "no_conversion")) + y1 <- getYears(data2017PPP)[getYears(data2017PPP, as.integer = TRUE) <= c15] + dataNearFut <- data2017PPP[, y1, ] %>% + GDPuc::convertGDP("constant 2017 Int$PPP", unit, replace_NAs = c("linear", "no_conversion")) - y2 <- getYears(data2005PPP)[getYears(data2005PPP, as.integer = TRUE) > c15 & - getYears(data2005PPP, as.integer = TRUE) < 2100] - dataFarFut <- data2005PPP[, y2, ] * NA + y2 <- getYears(data2017PPP)[getYears(data2017PPP, as.integer = TRUE) > c15 & + getYears(data2017PPP, as.integer = TRUE) < 2100] + dataFarFut <- data2017PPP[, y2, ] * NA - # Convert to 2017 Int$PPP using the 2017 value of base 2005 GDP deflator - # (in constant 2017 LCU per constant 2005 LCU) of the USA + # Convert to 2005 Int$PPP using the 2005 value of base 2017 GDP deflator + # (in constant 2005 LCU per constant 2017 LCU) of the USA # LONGTERM: allow other PPP units - data2100 <- data2005PPP[, 2100, ] * 1.23304244543521 + data2100 <- data2017PPP[, 2100, ] * GDPuc::convertSingle(1, "USA", "2010", "constant 2017 LCU", "constant 2005 LCU") - data2017PPP <- mbind(dataNearFut, dataFarFut, data2100) + data2005 <- mbind(dataNearFut, dataFarFut, data2100) - ratio <- data2005PPP / data2017PPP + ratio <- data2017PPP / data2005 # For interpolation to work, the last and first values have to be non-NA/non-NaN ratio[, 2100, ][is.na(ratio[, 2100, ])] <- 0 # The first 2 years of the SSP data set are incomplete. For countries that only lack data in these first 2 years, @@ -101,11 +99,11 @@ calcInternalGDPFutureSSPs <- function(unit) { dplyr::ungroup() %>% as.magpie(tidy = TRUE) - data2017PPP <- data2005PPP / ratio - data2017PPP[is.na(data2017PPP)] <- data2005PPP[is.na(data2017PPP)] + data2005 <- data2017PPP / ratio + data2005[is.na(data2005)] <- data2017PPP[is.na(data2005)] # Above should probably be "<- 0" ################## - data <- data2017PPP + data <- data2005 } # If unit was in $MER @@ -116,6 +114,11 @@ calcInternalGDPFutureSSPs <- function(unit) { list(x = data, weight = NULL, unit = glue("mil. {unit}"), description = "SSP projections") } +calcInternalGDPFutureSSP2 <- function(unit) { + data <- calcOutput("InternalGDPFutureSSPs", unit = unit, aggregate = FALSE)[, , "gdp_SSP2"] + list(x = data, weight = NULL, unit = glue("mil. {unit}"), description = "SSP2 projections") +} + calcInternalGDPFutureSDPs <- function(unit) { dataSSP1 <- calcOutput("InternalGDPFutureSSPs", unit = unit, aggregate = FALSE)[, , "gdp_SSP1"] # nolint @@ -127,22 +130,32 @@ calcInternalGDPFutureSDPs <- function(unit) { } calcInternalGDPFutureSSP2EU <- function(unit) { - dataSSP2EU <- readSource("ARIADNE", "gdp_corona") %>% - GDPuc::convertGDP("constant 2005 Int$PPP", unit, replace_NAs = c("linear", "no_conversion")) - dataSSP <- calcOutput("InternalGDPFutureSSPs", unit = unit, aggregate = FALSE) + euCountries <- toolGetEUcountries() - # Get EU-27 countries - euCountries <- toolGetEUcountries(onlyWithARIADNEgdpData = TRUE) + dataSSP2EU <- readSource("EurostatPopGDP", "GDP")[euCountries, , ] %>% + GDPuc::convertGDP("constant 2015 Int$PPP", unit, replace_NAs = c("linear", "no_conversion")) + grShort <- readSource("EurostatPopGDP", "GDPgr_projections_short")[euCountries, , ] + grLong <- readSource("EurostatPopGDP", "GDPgr_projections_long")[euCountries, , ] + + dataSSP2EU <- add_columns(dataSSP2EU, dim = 2, addnm = getYears(grShort)[!getYears(grShort) %in% getYears(dataSSP2EU)]) + dataSSP2EU <- add_columns(dataSSP2EU, dim = 2, addnm = getYears(grLong)[!getYears(grLong) %in% getYears(dataSSP2EU)]) + + for (y in getYears(grShort, as.integer = TRUE)) { + dataSSP2EU[, y, ] <- dataSSP2EU[, y - 1, ] * (1 + grShort[euCountries, y, ] / 100) + } + for (y in getYears(grLong, as.integer = TRUE)) { + dataSSP2EU[, y, ] <- dataSSP2EU[, y - 1, ] * (1 + grLong[euCountries, y, ] / 100) + } - # Get common years - cy <- intersect(getYears(dataSSP), getYears(dataSSP2EU)) + dataSSP2 <- calcOutput("InternalGDPFutureSSPs", unit = unit, aggregate = FALSE)[, , "gdp_SSP2"] # Start with the SSP2 scenario until 2100. Change the name, and overwrite the EUR # countries with the Eurostat data. - data <- dataSSP[, , "gdp_SSP2"] %>% setNames("gdp_SSP2EU") - data[euCountries, , ] <- 0 - data[euCountries, cy, ] <- dataSSP2EU[euCountries, cy, ] - list(x = data, weight = NULL, unit = glue("mil. {unit}"), description = "ARIADNE projections") + cy <- intersect(getYears(dataSSP2), getYears(dataSSP2EU)) + data <- dataSSP2[, cy, ] %>% setNames("gdp_SSP2EU") + data[euCountries, cy, ] <- dataSSP2EU[, cy, ] + data[is.na(data)] <- 0 + list(x = data, weight = NULL, unit = glue("mil. {unit}"), description = "Eurostat projections") } calcInternalGDPMI <- function(unit) { diff --git a/R/calcGDPHarmonized.R b/R/calcGDPHarmonized.R index ab256d5..88246fb 100644 --- a/R/calcGDPHarmonized.R +++ b/R/calcGDPHarmonized.R @@ -1,17 +1,16 @@ #' Get Harmonized GDP Data #' -#' @param args Arguments passed on to harmonization functions +#' @inheritParams calcGDPpcHarmonized #' @inherit madrat::calcOutput return #' @keywords internal -calcGDPHarmonized <- function(args) { - harmonizedData <- switch(args$harmonization, - "GDPpcWithPop" = toolMultiplyGDPpcWithPop(args$scenario, args$unit), - "calibSSP2EU" = toolGDPHarmonizeSSP2EU(args$past, args$future, args$unit), - "past_transition" = toolHarmonizePastTransition(args$past$x, args$future$x, yEnd = 2050, aslist = TRUE), - stop(glue("Bad input for calcGDPHarmonized. Argument harmonization = '{args$harmonization}' is invalid. \\ +calcGDPHarmonized <- function(harmonization, past, future, scenario, unit, ...) { + harmonizedData <- switch(harmonization, + "GDPpcWithPop" = toolMultiplyGDPpcWithPop(scenario, unit), + "calibSSP2EU" = toolGDPHarmonizeSSP2EU(past, future, unit), + stop(glue("Bad input for calcGDPHarmonized. Argument harmonization = '{harmonization}' is invalid. \\ Possible values are: 'GDPpcWithPop', 'calibsSSP2EU' or 'past_transition'.")) ) - list(x = harmonizedData$x, weight = NULL, unit = glue("mil. {args$unit}"), description = harmonizedData$description) + list(x = harmonizedData$x, weight = NULL, unit = glue("mil. {unit}"), description = harmonizedData$description) } @@ -37,38 +36,26 @@ toolMultiplyGDPpcWithPop <- function(scenario, unit) { toolGDPHarmonizeSSP2EU <- function(past, future, unit) { # We explicitly use the bezier Extension for SSP2 here, but only for harmonization purposes. # We return only up until 2100. - ssps <- calcOutput("GDP", - scenario = "SSPs", - unit = "constant 2005 Int$PPP", - extension2150 = "bezier", - average2020 = FALSE, - aggregate = FALSE, - supplementary = TRUE) - ssp2Data <- ssps$x[, , "gdp_SSP2"] + ssp2Data <- calcOutput("GDP", + scenario = "SSP2", + unit = "constant 2017 Int$PPP", + extension2150 = "bezier", + average2020 = FALSE, + aggregate = FALSE) # For SSP2EU: EU countries are equal to past - euCountries <- toolGetEUcountries(onlyWithARIADNEgdpData = TRUE) - ssp2EUData <- ssp2Data[euCountries, getYears(ssp2Data)[getYears(ssp2Data, as.integer = TRUE) <= 2100], ] + euCountries <- toolGetEUcountries() + ssp2EUData <- ssp2Data[euCountries, , ] ssp2EUData[, , ] <- 0 - ssp2EUData[, getYears(past$x), ] <- past$x[euCountries, , ] + ssp2EUData[, getYears(past$x), ] <- past$x[euCountries, , ] + ssp2EUData[, getYears(future$x), ] <- future$x[euCountries, , ] - # Use GDP growth rates of eurostat for the years 2022, 2023, 2024 - gr <- readSource("EurostatPopGDP", "GDPgr_projections") - ssp2EUData[, 2022, ] <- ssp2EUData[, 2021, ] * (1 + gr[euCountries, 2022, ] / 100) - ssp2EUData[, 2023, ] <- ssp2EUData[, 2022, ] * (1 + gr[euCountries, 2023, ] / 100) - ssp2EUData[, 2024, ] <- ssp2EUData[, 2023, ] * (1 + gr[euCountries, 2024, ] / 100) - - # After 2024 use growth rates from future object - pastYears <- getYears(ssp2EUData)[getYears(ssp2EUData, as.integer = TRUE) <= 2024] - cy <- union(pastYears, getYears(future$x)) - ssp2EUData[, cy, ] <- toolHarmonizePastGrFuture(ssp2EUData[, pastYears, ], - future$x[euCountries, , ]) - - # After 2070, transition to SSP2 values by 2150 - pastYears <- getYears(ssp2EUData)[getYears(ssp2EUData, as.integer = TRUE) <= 2070] - combinedSSP2EU <- toolHarmonizePastTransition(ssp2EUData[, pastYears, ], - ssp2Data[euCountries, , ], - 2150) + # After last year of future, transition to SSP2 values by 2150 + pastYears <- getYears(ssp2EUData)[getYears(ssp2EUData, as.integer = TRUE) <= max(getYears(future$x, as.integer = TRUE))] + combinedSSP2EU <- toolHarmonizePast(ssp2EUData[, pastYears, ], + ssp2Data[euCountries, , ], + method = "transition", + yEnd = 2150) combined <- ssp2Data combined[euCountries, , ] <- 0 @@ -79,8 +66,7 @@ toolGDPHarmonizeSSP2EU <- function(past, future, unit) { list(x = combined, description = glue("equal to SSP2 in all countries except for EU countries. \\ - For EU countries use {past$description} until 2021, \\ - growth rates from Eurostat until 2024, \\ - growth rates from {future$description} until 2070, \\ + For EU countries use {past$description} until {max(getYears(past$x, as.integer = TRUE))}, \\ + {future$description} until {max(getYears(future$x, as.integer = TRUE))}, \\ and converge to 2150 (bezier-extended) SSP2 values thereafter.")) } diff --git a/R/calcGDPPast.R b/R/calcGDPPast.R index 3431d50..eb59cf2 100644 --- a/R/calcGDPPast.R +++ b/R/calcGDPPast.R @@ -13,13 +13,6 @@ #' \item "WDI": World development indicators from the World Bank #' \item "MI": Missing island dataset #' \item "Eurostat": Eurostat -#' \item The IHME/James data set. This data set is tertiary source that uses available secondary sources (e.g. WDI -#' and PWT) to create completed GDP per capita time series -#' (See [paper](https://pophealthmetrics.biomedcentral.com/articles/10.1186/1478-7954-10-12)). -#' To access, pass one of the subtypes -#' "IHME_USD05_PPP_pc", "IHME_USD05_MER_pc", "IMF_USD05_PPP_pc", "PENN_USD05_PPP_pc", "WB_USD05_PPP_pc", -#' "MADDISON_USD05_PPP_pc", "WB_USD05_MER_pc", "IMF_USD05_MER_pc", "UN_USD05_MER_pc". In all cases, the per -#' capita GDP will be multiplied by WDI population data to get absolute GDP data. #' } #' @inheritParams calcGDP #' @inheritSection calcScenarioConstructor Combining data sources with "-" @@ -38,22 +31,14 @@ calcGDPPast <- function(GDPPast = "WDI-MI", unit = "constant 2005 Int$PPP") { # # Internal Function ###################################################################################### calcInternalGDPPast <- function(GDPPast, unit) { # nolint - # Check input argument - if (!GDPPast %in% c("WDI", "Eurostat", "MI", "PWT", - # All called through readJames - "IHME_USD05_PPP_pc", "IHME_USD05_MER_pc", "IMF_USD05_PPP_pc", - "PENN_USD05_PPP_pc", "WB_USD05_PPP_pc", "MADDISON_USD05_PPP_pc", - "WB_USD05_MER_pc", "IMF_USD05_MER_pc", "UN_USD05_MER_pc")) { - stop("Bad input for calcGDPPast. Invalid 'GDPPast' argument.") - } - # Call appropriate calcInternalGDPPast function. - data <- switch(GDPPast, - "WDI" = calcOutput("InternalGDPPastWDI", unit = unit, aggregate = FALSE), - "Eurostat" = calcOutput("InternalGDPPastEurostat", unit = unit, aggregate = FALSE), - "MI" = calcOutput("InternalGDPMI", unit = unit, aggregate = FALSE), - "PWT" = calcOutput("InternalGDPPastPWT", unit = unit, aggregate = FALSE), - calcOutput("InternalGDPPastJames", subtype = GDPPast, aggregate = FALSE)) + data <- switch( + GDPPast, + "WDI" = calcOutput("InternalGDPPastWDI", unit = unit, aggregate = FALSE), + "Eurostat" = calcOutput("InternalGDPPastEurostat", unit = unit, aggregate = FALSE), + "MI" = calcOutput("InternalGDPMI", unit = unit, aggregate = FALSE), + stop("Bad input for calcGDPPast. Invalid 'GDPPast' argument.") + ) data <- toolFinishingTouches(data) @@ -80,29 +65,16 @@ calcInternalGDPPastWDI <- function(unit) { calcInternalGDPPastEurostat <- function(unit) { euCountries <- toolGetEUcountries() - data <- readSource("EurostatPopGDP", "GDP") %>% - GDPuc::convertGDP("constant 2005 Int$PPP", unit, replace_NAs = c("linear", "no_conversion")) %>% - # Keep only EUR countries - `[`(euCountries, , ) + data <- readSource("EurostatPopGDP", "GDP")[euCountries, , ] %>% + GDPuc::convertGDP("constant 2015 Int$PPP", unit, replace_NAs = c("linear", "no_conversion")) - data <- fillWithWBFromJames2019(data[euCountries, , ], unit) + data <- fillWithWBFromJames2019(data, unit) data <- data %>% toolCountryFill(fill = 0) %>% suppressMessages() getNames(data) <- glue("gdp in {unit}") list(x = data, weight = NULL, unit = unit, description = "Eurostat data") } -calcInternalGDPPastJames <- function(subtype) { - gdpPPPpc <- readSource("James", subtype = subtype) - pop <- readSource("WDI", subtype = "SP.POP.TOTL") - years <- intersect(getYears(gdpPPPpc), getYears(pop)) - data <- gdpPPPpc[, years, ] * pop[, years, ] - getNames(data) <- substr(subtype, 1, (nchar(subtype) - 3)) - getNames(data) <- "gdp" # ?? - - list(x = data, weight = NULL, unit = "constant 2005 Int$PPP", description = "GDP from IHME,James") -} - # Use the James2019 WB_USD05_PPP_pc series to fill in past data until 1960. # Using mainly growth rates, since conversion of James2019 data into 2005 Int$PPP not certain to be correct. fillWithWBFromJames2019 <- function(data, unit) { @@ -138,10 +110,3 @@ fillWithWBFromJames2019 <- function(data, unit) { } x } - -calcInternalGDPPastPWT <- function(unit) { - data <- readSource("PWT")[, , "rgdpna"] - data <- GDPuc::convertGDP(data, "constant 2005 Int$PPP", unit, replace_NAs = c("linear", "no_conversion")) - getNames(data) <- glue("gdp in {unit}") - list(x = data, weight = NULL, unit = unit, description = "GDP from PWT") -} diff --git a/R/calcGDPpc.R b/R/calcGDPpc.R index bb45055..c6639ca 100644 --- a/R/calcGDPpc.R +++ b/R/calcGDPpc.R @@ -10,28 +10,18 @@ calcGDPpc <- function(scenario = c("SSPs", "SDPs", "SSP2EU"), # Check user input toolCheckUserInput(driver = "GDPpc", args = c(list(...), as.list(environment()))) - # GDPpc scenarios are constructed in PPPs. If MERs are desired, scenarios with the - # same base year but in PPPs are constructed, and converted to MERs at the end. - constructUnit <- unit - if (grepl("^constant .* US\\$MER$", unit)) { - constructUnit <- paste0("constant ", substr(unit, 10, 13), " Int$PPP") - } - + # GDPpc scenarios are constructed in 2017 Int$PPP, and converted, if necessary, at the end. gdppc <- calcOutput("Driver", driver = "GDPpc", scenario = scenario, - unit = constructUnit, + unit = "constant 2017 Int$PPP", popAsWeight = TRUE, aggregate = FALSE, supplementary = TRUE, ...) - if (average2020 && any(grepl("SSPsOld", scenario))) { - warning("Average 2020 is not compatible with SSPsOld. Setting to FALSE.") - average2020 <- FALSE - } if (average2020) { - # For REMIND, the concensus is to avergae the 2020 value so as to dampen the effect of the COVID shock. (The + # For REMIND, the consensus is to average the 2020 value so as to dampen the effect of the COVID shock. (The # reasoning being that REMIND uses 5-year time steps, and that the year-in-itself should represent the 2,5 years # before and after.) # The dampening is supposed to take place on GDP. So for GDP per capita in 2020 to be consistent with the dampened @@ -39,7 +29,7 @@ calcGDPpc <- function(scenario = c("SSPs", "SDPs", "SSP2EU"), # since it would lead to inconsistency at the end.) This is a bit hacky... gdp2020 <- calcOutput("GDP", scenario = scenario, - unit = constructUnit, + unit = "constant 2017 Int$PPP", average2020 = TRUE, naming = "scenario", extension2150 = "none", @@ -65,10 +55,16 @@ calcGDPpc <- function(scenario = c("SSPs", "SDPs", "SSP2EU"), message("The 2020 value is an an avergae over the 2018-2022 time period!!") } - if (constructUnit != unit) { + # Convert to US$MER if required + if (grepl("US$MER", unit)) { # Convert by interpolating and extrapolating missing conversion factors when possible. - gdppc$x <- GDPuc::convertGDP(gdppc$x, constructUnit, unit, replace_NAs = c("linear", "no_conversion")) + gdppc$x <- GDPuc::convertGDP(gdppc$x, + unit_in = "constant 2017 Int$PPP", + unit_out = "constant 2017 US$MER", + replace_NAs = c("linear", "no_conversion")) } + # Temporary shifting to 2005 prices, using only the US deflator + if (grepl("2005", unit)) gdppc$x <- gdppc$x * 0.8121123 list(x = gdppc$x, weight = gdppc$weight, unit = unit, description = gdppc$description) } diff --git a/R/calcGDPpcFuture.R b/R/calcGDPpcFuture.R index c19a71a..93541cd 100644 --- a/R/calcGDPpcFuture.R +++ b/R/calcGDPpcFuture.R @@ -7,16 +7,17 @@ #' \item "MI": Missing island dataset #' } #' See the "Combining data sources with '-'" section below for how to combine data sources. -calcGDPpcFuture <- function(GDPpcFuture = "SSPsOld-MI", # nolint - unit = "constant 2005 Int$PPP") { +calcGDPpcFuture <- function(GDPpcFuture, unit) { # nolint data <- switch( GDPpcFuture, - "SSPsOld" = toolGDPpcFutureSSPsOld(unit), - "SDPs" = toolGDPpcFutureSDPs(unit), - "SDPs-MI" = toolGDPpcFutureSDPs(unit, mi = TRUE), - "SSPsOld-MI" = toolGDPpcFutureSSPsOld(unit, mi = TRUE), - "MI" = toolGDPpcMI(unit), + "SSPs" = toolGDPpcFutureSSPs(unit), + "SSP2" = toolGDPpcFutureSSPs(unit)[, , "gdppc_SSP2"], + "SDPs" = toolGDPpcFutureSDPs(unit), + "SDPs-MI" = toolGDPpcFutureSDPs(unit, mi = TRUE), + "SSPs-MI" = toolGDPpcFutureSSPs(unit, mi = TRUE), + "SSP2-MI" = toolGDPpcFutureSSPs(unit, mi = TRUE)[, , "gdppc_SSP2"], + "MI" = toolGDPpcMI(unit), stop("Bad input for calcGDPFuture. Invalid 'GDPFuture' argument.") ) @@ -31,14 +32,13 @@ calcGDPpcFuture <- function(GDPpcFuture = "SSPsOld-MI", # nolint list(x = data, weight = weight, unit = unit, description = glue("{GDPpcFuture} projections")) } -toolGDPpcFutureSSPsOld <- function(unit, mi = FALSE) { +toolGDPpcFutureSSPs <- function(unit, mi = FALSE) { h1 <- if (mi) "SSPs-MI" else "SSPs" - h2 <- if (mi) "SSPsOld-MI" else "SSPsOld" gdp <- calcOutput("GDPFuture", GDPFuture = h1, unit = unit, aggregate = FALSE) gdp <- setNames(gdp, c("gdppc_SSP1", "gdppc_SSP2", "gdppc_SSP3", "gdppc_SSP4", "gdppc_SSP5")) - pop <- calcOutput("PopulationFuture", PopulationFuture = h2, aggregate = FALSE) + pop <- calcOutput("PopulationFuture", PopulationFuture = h1, aggregate = FALSE) pop <- setNames(pop, c("gdppc_SSP1", "gdppc_SSP2", "gdppc_SSP3", "gdppc_SSP4", "gdppc_SSP5")) years <- intersect(getYears(gdp), getYears(pop)) @@ -48,7 +48,7 @@ toolGDPpcFutureSSPsOld <- function(unit, mi = FALSE) { } toolGDPpcFutureSDPs <- function(unit, mi = FALSE) { - dataSSP1 <- toolGDPpcFutureSSPsOld(unit, mi)[, , "gdppc_SSP1"] + dataSSP1 <- toolGDPpcFutureSSPs(unit, mi)[, , "gdppc_SSP1"] purrr::map(c("SDP", "SDP_EI", "SDP_RC", "SDP_MC"), ~ setNames(dataSSP1, gsub("SSP1", .x, getNames(dataSSP1)))) %>% diff --git a/R/calcGDPpcHarmonized.R b/R/calcGDPpcHarmonized.R index b45fef7..58fd8c3 100644 --- a/R/calcGDPpcHarmonized.R +++ b/R/calcGDPpcHarmonized.R @@ -1,21 +1,21 @@ - #' Get Harmonized GDPpc Data #' -#' @param args Arguments passed on to harmonization functions +#' @param harmonization description +#' @param past description +#' @param future description +#' @param scenario description +#' @param unit description #' @inherit madrat::calcOutput return #' @keywords internal -calcGDPpcHarmonized <- function(args) { +calcGDPpcHarmonized <- function(harmonization, past, future, scenario, unit, ...) { harmonizedData <- switch( - args$harmonization, - "calibSSPs" = toolGDPpcHarmonizeSSP(args$past, args$future, args$unit, yEnd = 2100), - "calibSDPs" = toolGDPpcHarmonizeSDP(args$unit), - "GDPoverPop" = toolDivideGDPbyPop(args$scenario, args$unit), - "calibNoCovid" = toolGDPpcHarmonizeSSP(args$past, args$future, args$unit, yEnd = 2100, noCovid = TRUE), - "calibLongCovid" = toolGDPpcHarmonizeLongCovid(args$unit), - "calibShortCovid" = toolGDPpcHarmonizeShortCovid(args$unit), - stop(glue("Bad input for calcGDPpcHarmonized. Argument harmonization = '{args$harmonization}' is invalid.")) + harmonization, + "calibSSPs" = toolGDPpcHarmonizeSSP(past, future, unit, yEnd = 2100), + "calibSDPs" = toolGDPpcHarmonizeSDP(unit), + "GDPoverPop" = toolDivideGDPbyPop(scenario, unit), + stop(glue("Bad input for calcGDPpcHarmonized. Argument harmonization = '{harmonization}' is invalid.")) ) - list(x = harmonizedData$x, weight = NULL, unit = args$unit, description = harmonizedData$description) + list(x = harmonizedData$x, weight = NULL, unit = unit, description = harmonizedData$description) } @@ -24,47 +24,31 @@ calcGDPpcHarmonized <- function(args) { ###################################################################################### # GDPpc Harmonization Functions ###################################################################################### -toolGDPpcHarmonizeSSP <- function(past, future, unit, yEnd, noCovid = FALSE) { - - if (!noCovid) { - # Get IMF short-term income projections and fill missing with SSP2 - imfGDPpc <- readSource("IMF", "GDPpc") - } else { - # noCovid = TRUE leads to a counterfactual scenario where no Covid shock is experienced - ## Use past data only until 2019 - myYears <- getYears(past$x)[getYears(past$x, as.integer = TRUE) <= 2019] - past$x <- past$x[, myYears, ] - ## Get pre-covid IMF short-term income projections and fill missing with SSP2 - imfGDPpc <- readSource("IMF", "GDPpc", "WEOOct2019all.xls") - } +toolGDPpcHarmonizeSSP <- function(past, future, unit, yEnd) { + # Get IMF short-term income projections and fill missing with SSP2 + imfGDPpc <- readSource("IMF", "GDPpc") - fill <- calcOutput("GDPpcFuture", GDPpcFuture = "SSPsOld-MI", unit = unit, aggregate = FALSE)[, , "gdppc_SSP2"] + fill <- calcOutput("GDPpcFuture", GDPpcFuture = "SSPs-MI", unit = unit, aggregate = FALSE)[, , "gdppc_SSP2"] imfGDPpc <- imfGDPpc %>% toolFillWith(fill) %>% toolInterpolateAndExtrapolate() # Use short term IMF growth rates (here, as far as possible) - tmpGDPpc <- toolHarmonizePastGrFuture(past$x, imfGDPpc) + tmpGDPpc <- toolHarmonizePast(past$x, imfGDPpc, method = "growth") - # Transform into tibble, combine past and future tibbles - tmpGDPpc <- tmpGDPpc %>% - tibble::as_tibble() %>% - dplyr::select(-"variable") %>% - dplyr::group_by(.data$iso3c) %>% - dplyr::filter(!all(.data$value == 0)) %>% - dplyr::ungroup() + # Transform into tibble + tmpGDPpc <- tmpGDPpc %>% tibble::as_tibble() %>% dplyr::select(-"variable") # Make sure to add the last IMF year to the future SSP data, just in case it's not there. That is the year from which - # convergence begins. + # convergence begins. Also drop countries with no projection data for now. yStart <- max(getYears(imfGDPpc, as.integer = TRUE)) + missingC <- where(future$x == 0)$true$regions futureGDPpcTbl <- future$x %>% magclass::time_interpolate(yStart, integrate_interpolated_years = TRUE) %>% dplyr::as_tibble() %>% - dplyr::group_by(.data$iso3c) %>% - dplyr::filter(!all(.data$value == 0)) %>% - dplyr::ungroup() + dplyr::filter(!.data$iso3c %in% missingC) - combinedGDPpc <- tidyr::expand_grid(iso3c = unique(tmpGDPpc$iso3c), + combinedGDPpc <- tidyr::expand_grid(iso3c = unique(futureGDPpcTbl$iso3c), year = unique(c(tmpGDPpc$year, futureGDPpcTbl$year)), variable = unique(futureGDPpcTbl$variable)) %>% dplyr::left_join(tmpGDPpc, by = c("iso3c", "year")) %>% @@ -84,6 +68,9 @@ toolGDPpcHarmonizeSSP <- function(past, future, unit, yEnd, noCovid = FALSE) { as.magpie() %>% toolCountryFill(fill = 0) + # Add past data for countries with no projection data + combinedGDPpc[missingC, getYears(past$x)] <- past$x[missingC, , ] + lastPastYear <- max(getYears(past$x, as.integer = TRUE)) list(x = combinedGDPpc, @@ -139,58 +126,6 @@ toolDivideGDPbyPop <- function(scenario, unit) { {pop$description}")) } -toolGDPpcHarmonizeShortCovid <- function(unit) { - - gdppcSSPs <- calcOutput("GDPpc", - scenario = "SSPs", - unit = unit, - extension2150 = "none", - average2020 = FALSE, - aggregate = FALSE) - - gdppcNoCovid <- calcOutput("GDPpc", # nolint - scenario = "noCovid", - unit = unit, - extension2150 = "none", - average2020 = FALSE, - aggregate = FALSE) - - # Use SSPs until the last year of the IMF predictions, afterwards converge to NoCovid by 2030 - yIMF <- max(getYears(readSource("IMF", "GDPpc"), as.integer = TRUE)) - gdppcSSPs <- gdppcSSPs[, getYears(gdppcSSPs, as.integer = TRUE)[getYears(gdppcSSPs, as.integer = TRUE) <= yIMF], ] - x <- mbind(purrr::map(1:5, ~toolHarmonizePastTransition(gdppcSSPs[, , .x], gdppcNoCovid[, , .x], yEnd = 2030))) - - list(x = x, - description = glue("use past data, short term growth rates from IMF and afterwards \\ - transition to noCovid until 2030.")) -} - -toolGDPpcHarmonizeLongCovid <- function(unit) { - - gdppcSSPs <- calcOutput("GDPpc", - scenario = "SSPs", - unit = unit, - extension2150 = "none", - average2020 = FALSE, - aggregate = FALSE) - - gdppcNoCovid <- calcOutput("GDPpc", # nolint - scenario = "noCovid", - unit = unit, - extension2150 = "none", - average2020 = FALSE, - aggregate = FALSE) - - # Use SSPs until the last year of the IMF predictions, afterwards use NoCovid growth rates - yIMF <- max(getYears(readSource("IMF", "GDPpc"), as.integer = TRUE)) - gdppcSSPs <- gdppcSSPs[, getYears(gdppcSSPs, as.integer = TRUE)[getYears(gdppcSSPs, as.integer = TRUE) <= yIMF], ] - x <- mbind(purrr::map(1:5, ~toolHarmonizePastGrFuture(gdppcSSPs[, , .x], gdppcNoCovid[, , .x]))) - - list(x = x, - description = glue("use past data, short term growth rates from IMF and afterwards \\ - transition to noCovid until 2100.")) -} - diff --git a/R/calcGDPpcPast.R b/R/calcGDPpcPast.R index c4121fc..25b6976 100644 --- a/R/calcGDPpcPast.R +++ b/R/calcGDPpcPast.R @@ -6,7 +6,7 @@ #' \item "MI": Missing island dataset #' } #' See the "Combining data sources with '-'" section below for how to combine data sources. -calcGDPpcPast <- function(GDPpcPast = "WDI-MI", unit = "constant 2005 Int$PPP") { # nolint +calcGDPpcPast <- function(GDPpcPast = "WDI-MI", unit = "constant 2017 Int$PPP") { # nolint # Call appropriate calcGDPPast function. data <- switch(GDPpcPast, "WDI" = cGDPpcFromGDPAndPop(GDPpcPast, unit), diff --git a/R/calcLabourFuture.R b/R/calcLabourFuture.R index feb20f5..0f2cfaf 100644 --- a/R/calcLabourFuture.R +++ b/R/calcLabourFuture.R @@ -18,9 +18,9 @@ calcInternalLabourFuture <- function(LabourFuture) { # nolint x <- switch( LabourFuture, "SSPs" = calcOutput("InternalLabourFutureSSPs", aggregate = FALSE), + "SSP2" = calcOutput("InternalLabourFutureSSPs", aggregate = FALSE)[, , "lab_SSP2"], "SDPs" = calcOutput("InternalLabourFutureSDPs", aggregate = FALSE), "SSP2EU" = calcOutput("InternalLabourFutureSSP2EU", aggregate = FALSE), - "SSPsOld" = calcOutput("InternalLabourFutureSSPsOld", aggregate = FALSE), stop("Bad input for calcLabour. Invalid 'LabourFuture' argument.") ) @@ -40,17 +40,9 @@ calcInternalLabourFuture <- function(LabourFuture) { # nolint # Functions ###################################################################################### calcInternalLabourFutureSSPs <- function() { - x <- readSource("SSP", "lab2018Update") * 1e-3 - - # lab2018Update only starts in 2015. However data is needed back until 2005. - # Use old Labour growth rates to fill in. - y <- calcOutput("Labour", scenario = "SSPsOld", extension2150 = "none", aggregate = FALSE) - y <- y[, 1:3, paste0("pop_", getNames(x))] - getNames(y) <- getNames(x) - getSets(y) <- getSets(x) - x <- toolHarmonizeFutureGrPast(future = x, past = y) - getSets(x) <- c("iso3c", "year", "variable") - + x <- readSource("SSP", "lab")[, , c("SSP1", "SSP2", "SSP3", "SSP4", "SSP5")] + # Remove years which only contain 0 + x <- x[, !apply(x, 2, function(y) all(y == 0)), ] getNames(x) <- paste0("lab_", getNames(x)) list(x = x, weight = NULL, unit = "million", description = "Labor from SSPs") } @@ -85,64 +77,3 @@ calcInternalLabourFutureSSP2EU <- function() { labSSP2EU[is.na(labSSP2EU)] <- 0 list(x = labSSP2EU, weight = NULL, unit = "million", description = "Labor from SSP2EU") } - - -# Legacy -calcInternalLabourFutureSSPsOld <- function() { # nolint - aged <- tidyr::expand_grid("Population", - c("Male", "Female"), - c("Aged15-19", "Aged20-24", "Aged25-29", "Aged30-34", "Aged35-39", - "Aged40-44", "Aged45-49", "Aged50-54", "Aged55-59", "Aged60-64")) %>% - purrr::pmap_chr(paste, sep = "|") - - readSource("SSP", subtype = "all")[, , aged] - - data <- collapseNames(readSource("SSP", subtype = "all")[, , aged][, , "IIASA-WiC POP"]) - data <- dimSums(data, dim = 3.2) - - getNames(data) <- paste("pop_", gsub("_v[[:alnum:],[:punct:]]*", "", getNames(data)), sep = "") - # change name of "SSP4d" to "SSP4 - getNames(data) <- sub("SSP4d", "SSP4", getNames(data)) - - timeExtend <- c("y2105", "y2110", "y2115", "y2120", "y2125", "y2130", "y2135", "y2140", "y2145", "y2150") - data <- time_interpolate(data, timeExtend, extrapolation_type = "constant", integrate_interpolated_years = TRUE) - - # delete 0s - data <- data[, c("y2000", "y2005"), , invert = TRUE] - # extrapolate data for 2005 - data <- time_interpolate(data, c("y2005"), extrapolation_type = "linear", integrate_interpolated_years = TRUE) - - # add SDP, SDP_EI, SDP_MC, SDP_RC scenario as copy of SSP1, might be substituted by real data later - if ("pop_SSP1" %in% getNames(data, dim = 1)) { - if (!("pop_SDP" %in% getNames(data, dim = 1))) { - dataSDP <- data[, , "pop_SSP1"] - getNames(dataSDP) <- gsub("pop_SSP1", "pop_SDP", getNames(dataSDP)) - data <- mbind(data, dataSDP) - } - if (!("pop_SDP_EI" %in% getNames(data, dim = 1))) { - dataSDPEI <- data[, , "pop_SSP1"] - getNames(dataSDPEI) <- gsub("pop_SSP1", "pop_SDP_EI", getNames(dataSDPEI)) - data <- mbind(data, dataSDPEI) - } - if (!("pop_SDP_MC" %in% getNames(data, dim = 1))) { - dataSDPMC <- data[, , "pop_SSP1"] - getNames(dataSDPMC) <- gsub("pop_SSP1", "pop_SDP_MC", getNames(dataSDPMC)) - data <- mbind(data, dataSDPMC) - } - if (!("pop_SDP_RC" %in% getNames(data, dim = 1))) { - dataSDPRC <- data[, , "pop_SSP1"] - getNames(dataSDPRC) <- gsub("pop_SSP1", "pop_SDP_RC", getNames(dataSDPRC)) - data <- mbind(data, dataSDPRC) - } - } - # add SSP2EU scenario as copy of SSP2, might be substituted by real data later - if ("pop SSP2" %in% getNames(data, dim = 1)) { - if (!("pop_SSP2EU" %in% getNames(data, dim = 1))) { - dataSSP2EU <- data[, , "pop_SSP1"] - getNames(dataSSP2EU) <- gsub("pop_SSP1", "pop_SSP2EU", getNames(dataSSP2EU)) - data <- mbind(data, dataSSP2EU) - } - } - - list(x = data, weight = NULL, unit = "million", description = "Labor from SSPsOld") -} diff --git a/R/calcLabourHarmonized.R b/R/calcLabourHarmonized.R index e34af78..675050f 100644 --- a/R/calcLabourHarmonized.R +++ b/R/calcLabourHarmonized.R @@ -1,18 +1,20 @@ #' Get Harmonized Labour Data #' -#' @param args Arguments passed on to harmonization functions +#' @inheritParams calcGDPpcHarmonized #' @inherit madrat::calcOutput return #' @keywords internal -calcLabourHarmonized <- function(args) { +calcLabourHarmonized <- function(harmonization, past, future, ...) { # Combine "past" and "future" time series. harmonizedData <- switch( - args$harmonization, - args$future$x + harmonization, + "pastAndLevel" = toolHarmonizePast(past$x, future$x, method = "level"), + stop(glue("Bad input for calcLabourHarmonized Argument harmonization = '{harmonization}' is invalid.")) ) # Get description of harmonization function. description <- switch( - args$harmonization, - "No description available.." + harmonization, + "pastAndLevel" = glue("use {past$description} until {max(getYears(past$x, as.integer = TRUE))} \\ + and then switch directly to {future$description}."), ) list(x = harmonizedData, weight = NULL, unit = "million", description = description) } diff --git a/R/calcLabourPast.R b/R/calcLabourPast.R index 6038f42..e734c62 100644 --- a/R/calcLabourPast.R +++ b/R/calcLabourPast.R @@ -17,15 +17,24 @@ calcLabourPast <- function(LabourPast = "WDI") { # nolint calcInternalLabourPast <- function(LabourPast) { # nolint x <- switch( LabourPast, - "WDI" = readSource("WDI", "SP.POP.1564.TO"), + "WDI" = readSource("WDI", "SP.POP.1564.TO"), + "SSPs" = calcOutput("InternalLabourPastSSPs", aggregate = FALSE), stop("Bad input for calcLabour. Invalid 'LabourPast' argument.") ) # Apply finishing touches to combined time-series x <- toolFinishingTouches(x) - # Hopefully temporary: rename lab scnearios pop. Necessary for REMIND to work. + # Hopefully temporary: rename lab scenarios pop. Necessary for REMIND to work. getNames(x) <- sub("lab_", "pop_", getNames(x)) - list(x = x, weight = NULL, unit = "million", description = glue("Working age population data.")) -} \ No newline at end of file + list(x = x, weight = NULL, unit = "million", description = glue("{LabourPast} data.")) +} + +calcInternalLabourPastSSPs <- function() { + x <- readSource("SSP", "lab")[, , "Historical Reference"] + # Remove years which only contain 0 + x <- x[, !apply(x, 2, function(y) all(y == 0)), ] + getNames(x) <- paste0("lab_", getNames(x)) + list(x = x, weight = NULL, unit = "million", description = "Labor from SSPs") +} diff --git a/R/calcPopulationFuture.R b/R/calcPopulationFuture.R index 7394879..10c7e02 100644 --- a/R/calcPopulationFuture.R +++ b/R/calcPopulationFuture.R @@ -8,7 +8,6 @@ #' \item "SDPs": #' \item "UN_PopDiv": United Nations #' \item "MI": Missing island dataset -#' \item "SSPsOld": Old SSPs from the IIASA database #' } calcPopulationFuture <- function(PopulationFuture = "SSPs-UN_PopDiv-MI") { # nolint # Check user input @@ -27,11 +26,11 @@ calcInternalPopulationFuture <- function(PopulationFuture) { # nolint data <- switch( PopulationFuture, "SSPs" = calcOutput("InternalPopulationFutureSSPs", aggregate = FALSE, supplementary = TRUE), + "SSP2" = calcOutput("InternalPopulationFutureSSP2", aggregate = FALSE, supplementary = TRUE), "SSP2EU" = calcOutput("InternalPopulationFutureSSP2EU", aggregate = FALSE, supplementary = TRUE), "SDPs" = calcOutput("InternalPopulationFutureSDPs", aggregate = FALSE, supplementary = TRUE), "UN_PopDiv" = calcOutput("InternalPopulationFutureUN_PopDiv", aggregate = FALSE, supplementary = TRUE), "MI" = calcOutput("InternalPopMI", aggregate = FALSE, supplementary = TRUE), - "SSPsOld" = calcOutput("InternalPopulationFutureSSPsOld", aggregate = FALSE, supplementary = TRUE), stop("Bad input for PopulationFuture. Invalid 'PopulationFuture' argument.") ) @@ -45,11 +44,16 @@ calcInternalPopulationFuture <- function(PopulationFuture) { # nolint # Functions ###################################################################################### calcInternalPopulationFutureSSPs <- function() { # nolint - data <- readSource("SSP", "pop2018Update") * 1e-3 + data <- readSource("SSP", "pop") getNames(data) <- paste0("pop_", getNames(data)) list(x = data, weight = NULL, unit = "million", description = "SSP projections") } +calcInternalPopulationFutureSSP2 <- function() { # nolint + data <- calcOutput("InternalPopulationFutureSSPs", aggregate = FALSE)[, , "pop_SSP2"] + list(x = data, weight = NULL, unit = "million", description = "SSP2 projections") +} + calcInternalPopulationFutureSDPs <- function() { # nolint data_SSP1 <- calcOutput("InternalPopulationFutureSSPs", aggregate = FALSE)[, , "pop_SSP1"] # nolint @@ -81,17 +85,6 @@ calcInternalPopulationFutureSSP2EU <- function() { # nolint description = "SSP2 projections for non-EU countries, and EUROSTAT projections for EU countries") } -calcInternalPopulationFutureSSPsOld <- function() { # nolint - data <- readSource("SSP", "pop") - - # Refactor names - data <- collapseNames(data) - getNames(data) <- paste0("pop_", gsub("_v[[:alnum:],[:punct:]]*", "", getNames(data))) - getNames(data) <- sub("SSP4d", "SSP4", getNames(data)) - - list(x = data, weight = NULL, unit = "million", description = "old SSP projections") -} - calcInternalPopulationFutureUN_PopDiv <- function() { # nolint data <- readSource("UN_PopDiv", "medium") * 1e-3 getNames(data) <- "pop_medium_variant" diff --git a/R/calcPopulationHarmonized.R b/R/calcPopulationHarmonized.R index 59fa692..94f74ab 100644 --- a/R/calcPopulationHarmonized.R +++ b/R/calcPopulationHarmonized.R @@ -1,16 +1,15 @@ #' Get Harmonized Population Data #' -#' @param args Arguments passed on to harmonization functions +#' @inheritParams calcGDPpcHarmonized #' @inherit madrat::calcOutput return #' @keywords internal -calcPopulationHarmonized <- function(args) { +calcPopulationHarmonized <- function(harmonization, past, future, yEnd, ...) { # Combine "past" and "future" time series. - harmonizedData <- switch(args$harmonization, - "withPEAPandFuture" = toolHarmonizeWithPEAPandFuture(args$past, args$future), - "calibSSP2EU" = toolHarmonizeSSP2EU(args$past, args$future), - "calibISIMIP" = toolHarmonizeISIMIP(args$past, args$future, yEnd = 2030), - "past_transition" = toolHarmonizePastTransition(args$past$x, args$future$x, yEnd = 2050, aslist = TRUE), - stop(glue("Bad input for calcPopulationHarmonized. Argument harmonization = '{args$harmonization}' is invalid.")) + harmonizedData <- switch(harmonization, + "withPEAPandFuture" = toolHarmonizeWithPEAPandFuture(past, future), + "calibSSP2EU" = toolHarmonizeSSP2EU(past, future), + "calibISIMIP" = toolHarmonizeISIMIP(past, future, yEnd = if(rlang::is_missing(yEnd)) 2030 else yEnd), + stop(glue("Bad input for calcPopulationHarmonized. Argument harmonization = '{harmonization}' is invalid.")) ) list(x = harmonizedData$x, weight = NULL, unit = "million", description = harmonizedData$description) } @@ -22,11 +21,11 @@ toolHarmonizeWithPEAPandFuture <- function(past, future) { shortTerm <- readSource("PEAP") fill <- readSource("MissingIslands", subtype = "pop", convert = FALSE) shortTerm <- shortTerm %>% toolFillWith(fill) %>% toolInterpolateAndExtrapolate() - lastYearIMF <- max(getYears(readSource("IMF"), as.integer = TRUE)) + lastYearIMF <- max(getYears(readSource("IMF", "GDPpc"), as.integer = TRUE)) shortTerm <- shortTerm[, getYears(shortTerm, as.integer = TRUE) <= lastYearIMF, ] # Use PEAP growth rates until last year of IMF WEO data, and future growth rates after that - x <- past$x %>% toolHarmonizePastGrFuture(shortTerm) %>% toolHarmonizePastGrFuture(future$x) + x <- past$x %>% toolHarmonizePast(shortTerm, method = "growth") %>% toolHarmonizePast(future$x, method = "growth") lastPastYear <- max(getYears(past$x, as.integer = TRUE)) list(x = x, @@ -41,7 +40,7 @@ toolHarmonizeSSP2EU <- function(past, future) { # For EUR countries use only growth rates of EUROSTAT projections (load in fresh: future only has 5 year steps) euCountries <- toolGetEUcountries() dataEurostat <- readSource("EurostatPopGDP", "population_projections") * 1e-6 - x <- toolHarmonizePastGrFuture(past$x[euCountries, , ], dataEurostat[euCountries, , ]) + x <- toolHarmonizePast(past$x[euCountries, , ], dataEurostat[euCountries, , ], method = "growth") harmonizedData$x[euCountries, , ] <- x[euCountries, getYears(harmonizedData$x), ] list(x = harmonizedData$x, @@ -51,15 +50,9 @@ toolHarmonizeSSP2EU <- function(past, future) { } toolHarmonizeISIMIP <- function(past, future, yEnd) { - # Extend past by the first year of future, to make sure there is 1 year overlap between past and future - data20xx <- calcOutput("PopulationFuture", PopulationFuture = "UN_PopDiv-MI", aggregate = FALSE)[, 1, ] - getNames(data20xx) <- getNames(past$x) - past$x <- mbind(past$x, data20xx) - - # Then use toolHarmonizePastTransition - x <- toolHarmonizePastTransition(past$x, future$x, yEnd) + x <- toolHarmonizePast(past$x, future$x, method = "transition", yEnd = yEnd) list(x = x, - description = glue("use {past$description} until 2020, UN_PopDiv projections for 2021, \\ + description = glue("use {past$description} until {max(getYears(past$x, as.integer = TRUE))}, \\ and converge towards {future$description} by {yEnd}.")) } diff --git a/R/calcPopulationPast.R b/R/calcPopulationPast.R index b0d7aef..2b26955 100644 --- a/R/calcPopulationPast.R +++ b/R/calcPopulationPast.R @@ -76,9 +76,10 @@ calcInternalPopulationPastEurostat <- function() { # nolint if (max(yearsWith0) > max(yearsWithout0)) { futureYears <- getYears(dataWDI)[getYears(dataWDI) >= min(yearsWithout0)] futureYearsWithou0 <- futureYears[futureYears %in% yearsWithout0] - dataEurostat[c, futureYears, ] <- toolHarmonizePastGrFuture( + dataEurostat[c, futureYears, ] <- toolHarmonizePast( past = dataEurostat[c, futureYearsWithou0, ], - future = dataWDI[c, , ] + future = dataWDI[c, , ], + method = "growth" ) } } diff --git a/R/calcRatioPPP2MER.R b/R/calcRatioPPP2MER.R index dbb939f..073b6e7 100644 --- a/R/calcRatioPPP2MER.R +++ b/R/calcRatioPPP2MER.R @@ -1,27 +1,20 @@ #' MER over PPP ratio #' -#' Get a conversion factor to convert GDP in constant 2005 Int$PPP into constant 2005 US$MER. -#' Use the when argument to switch the year of the conversion factor. +#' Get a conversion factor to convert GDP in constant 2017 Int$PPP into constant 2017 US$MER. +#' Use the when argument to switch the year of the conversion factor. Source = WDI. #' -#' @param from A string indicating the source. Can be either "WDI" (default) or "OECD". -#' @param when An integer (defaults to 2005) specifying the year of the PPP2MER factor. +#' @param when An integer (defaults to 2017) specifying the year of the PPP2MER factor. #' @inherit madrat::calcOutput return #' @seealso [madrat::calcOutput()] #' @examples \dontrun{ #' calcOutput("RatioPPP2MER") #' } #' -calcRatioPPP2MER <- function(from = "WDI", when = 2005) { +calcRatioPPP2MER <- function(when = 2017) { - if (from == "WDI") { - data <- readSource("WDI", "PA.NUS.PPPC.RF")[, when, ] - # Replace 0s with 1s. This was done previously. Other solutions here should be taken into consideration. - data[data == 0] <- 1 - } else if (from == "OECD") { - data <- readSource("OECD", subtype = "ratioPM") - } else { - stop("Bad input for calcRatioPPP2MER. Invalid 'from' argument.") - } + data <- readSource("WDI", "PA.NUS.PPPC.RF")[, when, ] + # Replace 0s with 1s. This was done previously. Other solutions here should be taken into consideration. + data[data == 0] <- 1 weight <- calcOutput("GDPPast", aggregate = FALSE)[, when, ] @@ -34,6 +27,6 @@ calcRatioPPP2MER <- function(from = "WDI", when = 2005) { weight = weight, unit = glue::glue("constant {when} US$MER / constant {when} Int$PPP"), description = glue::glue("Ratio of GDP in constant {when} US$MER over GDP in constant {when} Int$PPP (source: \\ - {from}). Can be used to convert between GDP at constant {when} Int$PPP and GDP at \\ + WDI). Can be used to convert between GDP at constant {when} Int$PPP and GDP at \\ constant {when} US$MER.")) } diff --git a/R/calcUrbanFuture.R b/R/calcUrbanFuture.R index 2b35773..458d9da 100644 --- a/R/calcUrbanFuture.R +++ b/R/calcUrbanFuture.R @@ -11,6 +11,7 @@ calcUrbanFuture <- function(UrbanFuture = "SSPs") { # nolint data <- switch( UrbanFuture, "SSPs" = calcOutput("InternalUrbanFutureSSPs", aggregate = FALSE), + "SSP2" = calcOutput("InternalUrbanFutureSSPs", aggregate = FALSE)[, , "urb_SSP2"], "SDPs" = calcOutput("InternalUrbanFutureSDPs", aggregate = FALSE), "SSP2EU" = calcOutput("InternalUrbanFutureSSP2EU", aggregate = FALSE), stop("Bad input for UrbanFuture. Invalid 'UrbanFuture' argument.") @@ -34,20 +35,13 @@ calcUrbanFuture <- function(UrbanFuture = "SSPs") { # nolint ###################################################################################### # Functions ###################################################################################### -# Calculates a time series of urban shares, using SSP projections Currently, -# SSP data does not differentiate between SSPs and has some inconsistencies +# Calculates a time series of urban shares, using SSP projections. Currently has some inconsistencies # with WDI in 2010 calcInternalUrbanFutureSSPs <- function() { - data <- collapseNames(readSource("SSP", "urb")) / 100 - getSets(data)[3] <- "variable" - getNames(data) <- paste0("urb_", gsub("_v[[:alnum:],[:punct:]]*", "", getNames(data))) - - - # Remove years which only contain 0s as entries - data <- data[, !apply(data, 2, function(x) all(x == 0)), ] - - timeInter <- paste0("y", seq(2015, 2095, by = 10)) - data <- time_interpolate(data, timeInter, integrate_interpolated_years = TRUE) + data <- readSource("SSP", "urb") / 100 + getNames(data) <- paste0("urb_", getNames(data)) + # Add some missing years + data <- time_interpolate(data, seq(2025, 2095, by = 10), integrate_interpolated_years = TRUE) list(x = data, weight = NULL, unit = "per 1", description = "Urban data from SSP") } @@ -66,8 +60,7 @@ calcInternalUrbanFutureSDPs <- function() { sspUrb <- calcOutput("UrbanFuture", UrbanFuture = "SSPs", aggregate = FALSE) # nolint - data <- purrr::imap(urbanMapping, mapSHAPEurban, sspUrb = sspUrb) %>% - mbind() + data <- purrr::imap(urbanMapping, mapSHAPEurban, sspUrb = sspUrb) %>% mbind() list(x = data, weight = NULL, unit = "per 1", description = "Urban data from SDP") } diff --git a/R/calcUrbanHarmonized.R b/R/calcUrbanHarmonized.R index 426aa93..448b108 100644 --- a/R/calcUrbanHarmonized.R +++ b/R/calcUrbanHarmonized.R @@ -1,28 +1,27 @@ #' Get Harmonized Urban Data #' -#' @param args Arguments passed on to harmonization functions +#' @inheritParams calcGDPpcHarmonized #' @inherit madrat::calcOutput return #' @keywords internal -calcUrbanHarmonized <- function(args) { +calcUrbanHarmonized <- function(harmonization, past, future, ...) { # Combine "past" and "future" time series. harmonizedData <- switch( - args$harmonization, - "past" = toolHarmonizePast(args$past$x, args$future$x), - stop(glue("Bad input for calcUrbanHarmonized. Argument harmonization = '{args$harmonization}' is invalid.")) + harmonization, + "pastAndLevel" = toolHarmonizePast(past$x, future$x, method = "level"), + "pastAndGrowth" = toolHarmonizePast(past$x, future$x, method = "growth"), + "pastAndTransition" = toolHarmonizePast(past$x, future$x, method = "transition", yEnd = 2100), + stop(glue("Bad input for calcUrbanHarmonized. Argument harmonization = '{harmonization}' is invalid.")) ) - - # Cap urban share at 99%. - harmonizedData[harmonizedData > 0.99] <- 0.99 - # Get description of harmonization function. description <- switch( - args$harmonization, - "past" = args$pastData, + harmonization, + "pastAndLevel" = glue("use {past$description} until {max(getYears(past$x, as.integer = TRUE))} \\ + and then switch directly to {future$description}."), + "" ) - list(x = harmonizedData, - weight = NULL, - unit = "share of population", - description = glue("Urban population data. Datasource for the Past: {args$pastData}.\\ - Datasource for the Future: {args$futureData}. Calibrated to {description}")) + # Cap urban share at 99%. + harmonizedData[harmonizedData > 0.99] <- 0.99 + + list(x = harmonizedData, weight = NULL, unit = "share of population", description = description) } diff --git a/R/readARIADNE.R b/R/readARIADNE.R deleted file mode 100644 index 816e06a..0000000 --- a/R/readARIADNE.R +++ /dev/null @@ -1,100 +0,0 @@ -#' Read ARIADNE Reference Scenario -#' -#' Read ARIADNE Reference Scenario data from various .xls files as magpie object -#' -#' @param subtype data subtype. Either "population", "gdp", or "gdp_corona" -#' -#' @seealso [madrat::readSource()] -#' -#' @return magpie object of ARIADNE reference scenario data by country -#' @order 1 -readARIADNE <- function(subtype) { - - switch(subtype, - "population" = rARIADNEPopulation(), - "gdp" = rARIADNEGDP(corona = FALSE), - "gdp_corona" = rARIADNEGDP(corona = TRUE), - stop("Bad input for readARIADNE. Invalid 'subtype' argument.")) -} - - -###################################################################################### -# Functions -###################################################################################### -rARIADNEPopulation <- function() { - readxl::read_excel("POP_EU-27_Eurostat.xlsx", range = "B12:T46", sheet = "Pop_Total") %>% - suppressMessages() %>% - dplyr::rename("eurostat" = "...1") %>% - dplyr::slice(-c(28:30)) %>% - dplyr::mutate(eurostat = c(.data$eurostat[1:27], "IS", "LI", "NO", "CH")) %>% - tidyr::pivot_longer(-1, names_to = "year") %>% - dplyr::mutate(value = .data$value * 1e-6, - variable = factor("Population (million)"), - .before = "value") %>% - as.magpie(spatial = 1) -} - -rARIADNEGDP <- function(corona) { - gdpSheet <- suppressMessages(readxl::read_excel("GDP_Base_Corona_EU-28_V02.xlsx", range = "A2:AL30")) %>% - # Drop columns with only NAs - dplyr::select(tidyselect::vars_select_helpers$where(~ !all(is.na(.x)))) %>% - # Pivot columns - tidyr::pivot_longer(cols = tidyselect::starts_with("2"), names_to = "year") %>% - dplyr::rename("eurostat" = "Regions") - - # Split off years that apply to both the base and corona scenarios, and duplicate - gdp1 <- gdpSheet %>% - dplyr::filter(nchar(.data$year) == 4) %>% - dplyr::rename("base" = "value") %>% - dplyr::mutate("corona" = .data$base, year = as.integer(.data$year)) - - # Identify base and corona scenarios - gdp2 <- gdpSheet %>% - dplyr::filter(!nchar(.data$year) == 4) %>% - tidyr::separate(.data$year, c("year", "scen")) %>% - dplyr::mutate(year = as.integer(.data$year), - scen = ifelse(dplyr::row_number() %% 2 == 0, "corona", "base")) %>% - tidyr::pivot_wider(names_from = "scen") - - # Combine and add variable description column - gdp <- dplyr::bind_rows(gdp1, gdp2) %>% - dplyr::arrange(.data$eurostat, .data$year) %>% - dplyr::mutate(variable = "GDP|MER (million euro 2005/yr)") %>% - dplyr::relocate("variable", .after = "year") - - # Choose which scenario - gdp <- if (corona) dplyr::select(gdp, -"base") else dplyr::select(gdp, -"corona") - - as.magpie(gdp, spatial = "eurostat", temporal = "year", tidy = TRUE) -} - - -#' @rdname readARIADNE -#' @order 2 -#' @param x MAgPIE object returned by readARIADNE -convertARIADNE <- function(x, subtype) { - - # Convert region codons from Eurostat to iso3c - getItems(x, 1) <- countrycode::countrycode(getItems(x, 1), "eurostat", "iso3c") - getSets(x)[1] <- "iso3c" - - if (subtype %in% c("gdp", "gdp_corona")) { - # Convert currency: - # First we use the country-sepcific defltors to change the base year, and then we - # divide by 0.8041, a fixed value sourced from the World Bank's WDI - # (1 US$2005 = 0.8041 €2005) to get the values in 2005 US$MER. - # In a second step we convert from US$MER to Int$PPP - # The reason we can't do the conversin at once here is that x is not in LCU but in €. - x <- GDPuc::convertGDP(x, "constant 2010 LCU", "constant 2005 LCU") / 0.8041 - x <- GDPuc::convertGDP(x, "constant 2005 US$MER", "constant 2005 Int$PPP") - - # Rename - getNames(x) <- "GDP|PPP (million US$2005/yr)" - } - - x <- toolGeneralConvert(x, note = FALSE, countryFillWith = NA, substituteNAsWith = NA) - # fill smaller EU-countries with 0s to allow for aggregation of EU-region - x[c("ALA", "FRO", "GIB", "GGY", "IMN", "JEY"), , ] <- 0 - - return(x) -} diff --git a/R/readEurostatPopGDP.R b/R/readEurostatPopGDP.R index 2c9ad49..047d332 100644 --- a/R/readEurostatPopGDP.R +++ b/R/readEurostatPopGDP.R @@ -7,7 +7,8 @@ #' \item "population": Population, ref demo_gind #' \item "population_projections": Population projections, ref proj_19np #' \item "GDP": GDP, ref nama_10_gdp -#' \item "GDPgr_projections": Projected GDP growth rates, 2023 forecast. +#' \item "GDPgr_projections_short": Projected GDP growth rates, 2023 forecast. +#' \item "GDPgr_projections_long": #' } #' @inherit madrat::readSource return #' @seealso [madrat::readSource()] and [madrat::downloadSource()] @@ -18,12 +19,19 @@ readEurostatPopGDP <- function(subtype) { x <- switch( subtype, - "population" = readr::read_rds("demo_gind_num_code_FF.rds"), - "population_projections" = readr::read_rds("proj_19np_num_code_FF.rds"), - "GDP" = readr::read_rds("nama_10_gdp_num_code_FF.rds"), - "GDPgr_projections" = readr::read_csv("Economic-Forecast---Winter-2023.csv", col_types = "cddd") %>% + "population" = readr::read_rds("population.rds"), + "population_projections" = readr::read_rds("population_projections.rds"), + "GDP" = readr::read_rds("GDP.rds"), + "GDPgr_projections_short" = readr::read_csv("Economic-Forecast---Winter-2024.csv", col_types = "cddd") %>% tidyr::pivot_longer(2:4, names_to = "time") %>% dplyr::rename("geo" = "Category"), + "GDPgr_projections_long" = purrr::map(readxl::excel_sheets("Ageing_Report_2024-Country_fiches_1.xlsx"), + ~readxl::read_xlsx("Ageing_Report_2024-Country_fiches_1.xlsx", + sheet = .x, + range = "E22:BA23") %>% + dplyr::mutate("geo" = .x) %>% + tidyr::pivot_longer(tidyselect::starts_with("2"), names_to = "time")) %>% + purrr::list_rbind(), stop("Bad input for readEurostatPopGDP. Invalid 'subtype' argument.") ) as.magpie(x, spatial = "geo", temporal = "time") @@ -35,10 +43,11 @@ readEurostatPopGDP <- function(subtype) { convertEurostatPopGDP <- function(x, subtype) { switch( subtype, - "population" = convEurostatPopulation(x), - "population_projections" = convEurostatPopulation(x), - "GDP" = convEurostatGDP(x), - "GDPgr_projections" = convEurostatGDPgrProjections(x) + "population" = convEurostatPopulation(x), + "population_projections" = convEurostatPopulation(x), + "GDP" = convEurostatGDP(x), + "GDPgr_projections_short" = convEurostatGDPgrProjectionsShort(x), + "GDPgr_projections_long" = convEurostatGDPgrProjectionsLong(x) ) } @@ -59,19 +68,19 @@ convEurostatPopulation <- function(x) { } convEurostatGDP <- function(x) { + # Drop EA, EA12, EA19, EA20, EU15, EU27_2020, EU28, XK + x <- x[getItems(x, 1)[!getItems(x, 1) %in% c("EA", "EA12", "EA19", "EA20", "EU15", "EU27_2020", "EU28", "XK")], , ] # Convert the eurostat countrycodes to iso3c codes - getItems(x, 1) <- countrycode::countrycode(getItems(x, 1), "eurostat", "iso3c", warn = FALSE) - # ABOVE warning that is being ignored: - # Some values were not matched unambiguously: EA, EA12, EA19, EA20, EU15, EU27_2020, EU28, XK + getItems(x, 1) <- countrycode::countrycode(getItems(x, 1), "eurostat", "iso3c") x <- toolGeneralConvert(x, note = FALSE) - # Convert from constant 2005 LCU to constant 2005 Int$PPP. + # Convert from constant 2015 LCU to constant 2015 Int$PPP. getNames(x) <- "GDP" - GDPuc::convertGDP(x, "constant 2005 LCU", "constant 2005 Int$PPP", replace_NAs = c("linear", "no_conversion")) + GDPuc::convertGDP(x, "constant 2015 LCU", "constant 2015 Int$PPP", replace_NAs = c("linear", "no_conversion")) } -convEurostatGDPgrProjections <- function(x) { +convEurostatGDPgrProjectionsShort <- function(x) { # Drop EA and EU country aggregates x <- x[getItems(x, 1)[!getItems(x, 1) %in% c("EA", "EU")], , ] # Convert the eurostat countrycodes to iso3c codes @@ -79,6 +88,14 @@ convEurostatGDPgrProjections <- function(x) { toolGeneralConvert(x, note = FALSE) } +convEurostatGDPgrProjectionsLong <- function(x) { + # Drop EA and EU country aggregates + x <- x[getItems(x, 1)[!getItems(x, 1) %in% c("EA", "EU27")], , ] + # Convert the eurostat countrycodes to iso3c codes + getItems(x, 1) <- countrycode::countrycode(getItems(x, 1), "eurostat", "iso3c") + toolGeneralConvert(x, note = FALSE) +} + #' @rdname readEurostatPopGDP #' @order 1 downloadEurostatPopGDP <- function(subtype) { @@ -87,21 +104,20 @@ downloadEurostatPopGDP <- function(subtype) { switch( subtype, # Filter for "AVG" = average population. - "population" = eurostat::get_eurostat("demo_gind", - filters = list(indic_de = "AVG"), - time_format = "num", - cache_dir = "."), + "population" = eurostat::get_eurostat("demo_gind", filters = list(indic_de = "AVG"), time_format = "num") %>% + readr::write_rds("population.rds"), # Filter for baseline projection of total population. "population_projections" = eurostat::get_eurostat("proj_19np", filters = list(sex = "T", projection = "BSL", age = "TOTAL"), - time_format = "num", - cache_dir = "."), - # Filter for GDP at market prices (=B1GQ) in Chained-Linked Volumes in 2005 mil. National Currencies (= CLV05_MNAC) + time_format = "num") %>% + readr::write_rds("population_projections.rds"), + # Filter for GDP at market prices (=B1GQ) in Chained-Linked Volumes in 2015 mil. National Currencies (= CLV15_MNAC) "GDP" = eurostat::get_eurostat("nama_10_gdp", - filters = list(freq = "A", na_item = "B1GQ", unit = "CLV05_MNAC"), - time_format = "num", - cache_dir = "."), - "GDPgr_projections" = stop("Download EUROSTAT GDP growth rate projections manually.") + filters = list(freq = "A", na_item = "B1GQ", unit = "CLV15_MNAC"), + time_format = "num") %>% + readr::write_rds("GDP.rds"), + "GDPgr_projections_short" = stop("Download EUROSTAT GDP growth rate projections manually."), + "GDPgr_projections_long" = stop("Download EUROSTAT GDP growth rate projections manually.") ) switch( diff --git a/R/readIMF.R b/R/readIMF.R index c8df095..79039e5 100644 --- a/R/readIMF.R +++ b/R/readIMF.R @@ -1,9 +1,8 @@ #' Read IMF #' -#' Read-in IMF data +#' Read-in data from the IMF's World Economic Outlook. +#' Currently reading GDP per capita and current account balance data. #' -#' @param subtype Either "current_account" or "GDPpc" -#' @param subset Either "WEOOct2022all.xls" or "WEOallOct2019.xls" #' @inherit madrat::readSource return #' @seealso [madrat::readSource()] and [madrat::downloadSource()] #' @examples @@ -11,77 +10,56 @@ #' readSource("IMF") #' } #' @order 2 -readIMF <- function(subtype = "current_account", subset = "WEOOct2022all.xls") { - # Check function input - if (!subtype %in% c("current_account", "GDPpc")) { - stop("Bad input for readIMF. Invalid 'subtype' argument.") - } - - # Define source file - sourceFile <- subset +readIMF <- function() { + # Define what data, i.e.which "WEO subject codes", to keep: here GDPpc and current account balance + myWEOCodes <- c("NGDPRPPPPC", "BCA") - # Define what data, i.e.which "WEO subject codes", to keep - myWEOCodes <- if (subtype == "GDPpc") "NGDPRPPPPC" else "BCA" + my_locale <- readr::default_locale() + my_locale$encoding <- "UTF-16LE" - weoData <- readr::read_tsv(sourceFile, col_types = c(.default = "c")) %>% + weoData <- readr::read_tsv("WEOApr2024all.ashx", + col_types = c(.default = "c"), + locale = my_locale, + na = c("", "n/a", "--"), + progress = FALSE) %>% dplyr::filter(.data$`WEO Subject Code` %in% myWEOCodes) %>% tidyr::unite("tmp", c("Scale", "Units"), sep = " ") %>% dplyr::mutate(tmp = sub("NA ", "", .data$tmp), tmp = paste0("[", .data$tmp, "]")) %>% tidyr::unite("Subject Descriptor", c("Subject Descriptor", "tmp"), sep = " ") %>% dplyr::select("iso3c" = "ISO", "Subject Descriptor", tidyselect::starts_with(c("1", "2"))) %>% - tidyr::pivot_longer(tidyselect::starts_with(c("1", "2")), names_to = "year") %>% - dplyr::mutate(value = gsub(",", "", .data$value), - dplyr::across(.cols = c("year", "value"), - ~ suppressWarnings(as.double(.x))), - # The warnings that are being suppressed above, come from - # character strings that can't be converted to numeric, and - # are thus returned as NA. - value = tidyr::replace_na(.data$value, 0)) %>% + tidyr::pivot_longer(tidyselect::starts_with(c("1", "2")), + names_to = "year", + names_transform = as.numeric, + values_transform = as.numeric) %>% + tidyr::replace_na(list(value = 0)) %>% tidyr::pivot_wider(names_from = "Subject Descriptor") # Transform to magpie - out <- as.magpie(weoData) - - # TMP! Give names - if (subtype == "current_account") { - getNames(out) <- "current account [billion U.S. dollar]" - } - - return(out) + as.magpie(weoData) } #' @rdname readIMF #' @param x MAgPIE object returned by readIMF +#' @param subtype Use to filter the IMF data #' @order 3 -convertIMF <- function(x, subtype = "current_account") { - if (subtype == "current_account") { - # delete "World" - x <- x["World", , , invert = TRUE] - # delete Kosovo - x <- x["KOS", , , invert = TRUE] - - ### allocate global current account to the countries - # calculate global sum which is not 0 - xSum <- -dimSums(x, dim = 1, na.rm = TRUE) - # calculate global absolute share of current account - xAbs <- abs(x) - xAbsSum <- dimSums(xAbs, dim = 1, na.rm = TRUE) - # calculate additional value for each country - xRest <- xAbs / xAbsSum * xSum - # add global rest to the countries - x <- x + xRest +convertIMF <- function(x, subtype = "all") { + # Use convert function to filter + if (subtype == "GDPpc") { + h <-"Gross domestic product per capita, constant prices [Units Purchasing power parity; 2017 international dollar]" + x <- x[, , h] } + if (subtype == "BCA") x <- x[, , "Current account balance [Billions U.S. dollars]"] - toolGeneralConvert(x, no_remove_warning = c("UVK", "WBG"), warn = FALSE, note = FALSE) + toolGeneralConvert(x, no_remove_warning = c("UVK", "WBG"), note = FALSE) } #' @rdname readIMF #' @order 1 downloadIMF <- function() { - url <- "https://www.imf.org/-/media/Files/Publications/WEO/WEO-Database/2022/WEOOct2022all.ashx" - utils::download.file(url, sub("\\.ashx$", ".xls", basename(url)), quiet = TRUE) + url <- "https://www.imf.org/-/media/Files/Publications/WEO/WEO-Database/2024/April/WEOApr2024all.ashx" + utils::download.file(url, basename(url), quiet = TRUE) # Compose meta data list(url = url, @@ -90,7 +68,7 @@ downloadIMF <- function() { description = "World Economic Outlook database of the International Monetary Fund", unit = "-", author = "International Monetary Fund", - release_date = "October 2022", + release_date = "April 2024", license = "-", comment = "-") } diff --git a/R/readOECD.R b/R/readOECD.R deleted file mode 100644 index 6eacef9..0000000 --- a/R/readOECD.R +++ /dev/null @@ -1,90 +0,0 @@ -#' Read OECD GDP or PPP2MER ratio -#' -#' Read-in GDP or PPP2MER ratio data xlsx file from OECD as magclass object -#' -#' @param subtype data subtype. Either "gdp" or "ratioPM" -#' @return magpie object of the GDP data -#' @seealso [madrat::readSource()] -#' @examples \dontrun{ -#' readSource("OECD", "gdp") -#' readSource("OECD", "ratioPM") -#' } -#' @order 1 -readOECD <- function(subtype) { - files <- c(gdp = "OECD_v9_25-3-13-3.xlsx", - ratioPM = "OECD-WB_PPP-MER2005_conversion_rates.xlsx", - riskClass = "cre-crc-current-english.xlsx") - - file <- toolSubtypeSelect(subtype, files) - - data <- as.data.frame(readxl::read_excel(file)) - - if (subtype == "gdp") { - data <- data[data$Variable == "GDP|PPP", ] - data <- data[, c(-1, -4, -5)] # should be done better? - x <- as.magpie(data) - getNames(x) <- paste("gdp_", gsub("_v[[:alnum:],[:punct:]]*", "", getNames(x)), sep = "") - } else if (subtype == "ratioPM") { - data <- data[, c(1, 2)] - data[, 2] <- as.numeric(data[, 2]) - colnames(data) <- c("Region", "y2005") # automated? - x <- as.magpie(data) - } else if (subtype == "riskClass") { - # delete data for Kosovo - data <- data[-which(data$`Country Name (1)` == "Kosovo"), ] - # delete not needed information - data$`Country Name (1)` <- NULL # nolint - data$Notes <- NULL # nolint - data$`Previous Classification` <- NULL # nolint - # transform into numeric - data$`Current Prevailing Classification` <- as.numeric(data$`Current Prevailing Classification`) # nolint - # transfer into a magpie object - x <- as.magpie(data) - getSets(x)[1] <- "country" - } - - x -} - -#' @rdname readOECD -#' @order 2 -#' @param x MAgPIE object returned from readOECD -convertOECD <- function(x, subtype) { - - if (subtype == "gdp") { - # disaggregate "ROW" - l <- c("ATG", "DMA", "FSM", "GRD", "IMN", "KNA", "PLW", "SYC") # "TLS" - # ... time and scenario dependent? - w <- calcOutput("Population", aggregate = FALSE)[l, 2010, "pop_SSP2"] - xROW <- toolAggregate(x[c("ROW"), , ], "OECD_ROW2ISO.csv", weight = w) - # delete ROW entry - deleteROW <- setdiff(getItems(x, 1), c("ROW")) - x <- x[deleteROW, , ] - # add data for ROW - x <- mbind(x[, getYears(xROW), ], xROW) - # fill all the rest with 0 - x <- toolCountryFill(x, fill = 0) - - } else if (subtype == "ratioPM") { - # disaggregate "ROW" - l <- c("ATG", "DMA", "FSM", "GRD", "IMN", "KNA", "PLW", "SYC") # "TLS" - lROW <- rep(c("ROW"), times = length(l)) - xROW <- x[lROW, , ] - getCells(xROW) <- l - # delete ROW entry - deleteROW <- setdiff(getItems(x, 1), c("ROW")) - x <- x[deleteROW, , ] - # add data for ROW - x <- mbind(x[, getYears(xROW), ], xROW) - # fill all the rest with 1 - x <- toolCountryFill(x, fill = 1) - - } else if (subtype == "riskClass") { - # fill NA with 1 - x[is.na(x)] <- 0 - # fill all the rest with 1 - x <- toolCountryFill(x, fill = 0) - } - - x -} diff --git a/R/readPEAP.R b/R/readPEAP.R index c038f99..79ddc61 100644 --- a/R/readPEAP.R +++ b/R/readPEAP.R @@ -8,7 +8,7 @@ #' @seealso [madrat::readSource()] and [madrat::downloadSource()] #' @order 2 readPEAP <- function() { - file <- "Data_Extract_From_Population_estimates_and_projections_13_02_2023.csv" + file <- "Data_Extract_From_Population_estimates_and_projections_15_04_2024.csv" myColTypes <- readr::cols(.default = "d", "Country Name" = "_", "Country Code" = "c", @@ -44,7 +44,7 @@ downloadPEAP <- function() { description = "Population Estimates and Projections by the World Bank 1960-2050", unit = "-", author = "World Bank", - release_date = "2023", + release_date = "2024", license = "-", - comment = "Manual download required! Accessed on the 13.02.2023.") + comment = "Manual download required! Accessed on the 15.04.2024.") } diff --git a/R/readPWT.R b/R/readPWT.R deleted file mode 100644 index b9795db..0000000 --- a/R/readPWT.R +++ /dev/null @@ -1,24 +0,0 @@ -#' Read PWT -#' -#' Read-in PWT data as magclass object -#' -#' @inherit madrat::readSource return -#' @seealso [madrat::readSource()] -#' @order 1 -#' @examples \dontrun{ -#' readSource("PWT") -#' } -readPWT <- function() { - pwt <- readxl::read_excel("pwt80.xlsx", sheet = "Data") - # Remove "country", "currency_unit" and indicator ("i_") columns - pwt <- pwt[, !grepl("(^country$|^currency_unit$|^i_)", names(pwt))] - # Transform to magpie - as.magpie(pwt) -} - -#' @rdname readPWT -#' @order 2 -#' @param x MAgPIE object returned by readPWT -convertPWT <- function(x) { - toolGeneralConvert(x, note = FALSE) -} diff --git a/R/readPopulationTWN.R b/R/readPopulationTWN.R deleted file mode 100644 index 8b5b1bb..0000000 --- a/R/readPopulationTWN.R +++ /dev/null @@ -1,47 +0,0 @@ -#' Read TWN Population projections. -#' -#' @description -#' `r lifecycle::badge('deprecated')` -#' -#' Read-in Population projections for Taiwan -#' -#' @param subtype A string. Variant of population projection. -#' -#' @return A magpie object of the TWN population data -#' -#' @seealso [madrat::readSource()] -#' -#' @examples \dontrun{ -#' readSource("PopulationTWN", subtype = "medium") -#' } -#' @keywords internal -readPopulationTWN <- function(subtype) { - files <- c(medium = "A1. Population Projections - Medium Variant.xlsx", - high = "A2. Population Projections - High Variant.xlsx", - low = "A3. Population Projections - Low Variant.xlsx") - - file <- toolSubtypeSelect(subtype, files) - - twn <- as.data.frame(suppressMessages(readxl::read_excel(file, sheet = "M3", skip = 1))) - twn <- twn[!is.na(twn[[2]]),] - - names(twn)[3] <- paste(names(twn)[2], twn[1, 3], sep="_") - names(twn)[4] <- paste(names(twn)[2], twn[1, 4], sep="_") - names(twn)[2] <- paste(names(twn)[2], twn[1, 2], sep="_") - names(twn)[6] <- paste(names(twn)[5], twn[1, 6], sep="_") - names(twn)[7] <- paste(names(twn)[5], twn[1, 7], sep="_") - names(twn)[5] <- paste(names(twn)[5], twn[1, 5], sep="_") - names(twn)[9] <- paste(names(twn)[8], twn[1, 9], sep="_") - names(twn)[10] <- paste(names(twn)[8], twn[1, 10], sep="_") - names(twn)[8] <- paste(names(twn)[8], twn[1, 8], sep="_") - - twn <- twn[-1, ] - twn <- tidyr::pivot_longer(twn, -"Year", names_to = "variable") - twn$value <- as.numeric(twn$value) - twn$variable <- gsub(" +", "_", twn$variable) - twn$variable <- gsub("_years", " years", twn$variable, fixed = TRUE) - - - x <- as.magpie(twn) - x -} diff --git a/R/readSSP.R b/R/readSSP.R index ec605f2..201742c 100644 --- a/R/readSSP.R +++ b/R/readSSP.R @@ -1,173 +1,113 @@ #' Read SSP #' -#' Read-in an SSP data csv.zip file as magclass object +#' Read-in an SSP data as magclass object #' -#' @param subtype A string, either "all", "gdp", "pop", "urb", "pop2018Update", -#' "lab2018Update" or "ratioPM". #' @inherit madrat::readSource return #' @seealso [madrat::readSource()] #' @examples \dontrun{ #' readSource("SSP", subtype = "gdp") #' } #' @order 1 -readSSP <- function(subtype) { - if (!subtype %in% c("all", "gdp", "pop", "urb", "pop2018Update", "lab2018Update")) { - stop("Bad input for readSSP. Invalid 'subtype' argument.") - } - - if (subtype %in% c("all", "gdp", "pop", "urb")) { - - x <- readr::read_csv("SspDb_country_data_2013-06-12.csv.zip", - col_types = list(.default = readr::col_character()), - progress = FALSE) %>% - tidyr::unite("mod.variable", c("MODEL", "SCENARIO", "VARIABLE", "UNIT"), sep = ".") %>% - dplyr::rename("iso3c" = "REGION") %>% - # Drop columns with only NAs - dplyr::select(tidyselect::vars_select_helpers$where(~ !all(is.na(.x)))) %>% - tidyr::pivot_longer(cols = tidyselect::starts_with("2"), names_to = "year") %>% - dplyr::mutate(value = as.double(.data$value)) %>% - as.magpie(spatial = "iso3c", temporal = "year", tidy = TRUE, filter = FALSE) - - if (subtype == "gdp") x <- x[, , "GDP|PPP"][, , "OECD Env-Growth"] - if (subtype == "pop") x <- x[, , "Population"][, , "IIASA-WiC POP"] - if (subtype == "urb") x <- x[, , "Population|Urban|Share"][, , "NCAR"] - - } else if (subtype %in% c("pop2018Update", "lab2018Update")) { - - # Specifying the col_types quickens the read process - myColTypes <- readr::cols(.default = "d", scenario = "c", vers = "_", sex = "c", agegrp = "c") - x <- readr::read_csv("Population in 000 by Age and Sex, countries, SSPs 2018vers wide.csv", +readSSP <- function() { + myColTypes <- c(rep.int("text", 5), rep.int("numeric", 31)) + x <- readxl::read_xlsx("ssp_basic_drivers_release_3.0.1_full.xlsx", + sheet = "data", col_types = myColTypes, progress = FALSE) %>% - tidyr::pivot_longer(5:205, names_to = "iso3c") %>% - dplyr::rename("variable" = "scenario") %>% - as.magpie(spatial = "iso3c", temporal = "year", tidy = TRUE) - - } - x + tidyr::unite("Model.Scenario.Variable.Unit", c("Model", "Scenario", "Variable", "Unit"), sep = ".") %>% + tidyr::pivot_longer(cols = where(is.numeric), names_to = "year") + + # The above SSP release 3.0.1 does not have any data on urban population share. So we get that from an older release. + myColTypes <- paste(c(rep.int("c", 5), rep.int("d", 41)), collapse = "") + x_urb <- readr::read_csv("SspDb_country_data_2013-06-12.csv.zip", col_types = myColTypes, progress = FALSE) %>% + dplyr::filter(.data$MODEL == "NCAR", .data$VARIABLE == "Population|Urban|Share") %>% + tidyr::unite("Model.Scenario.Variable.Unit", c("MODEL", "SCENARIO", "VARIABLE", "UNIT"), sep = ".") %>% + # Drop columns (years) with only NAs + dplyr::select(tidyselect::vars_select_helpers$where(~ !all(is.na(.x)))) %>% + tidyr::pivot_longer(cols = where(is.numeric), names_to = "year") %>% + # Convert Region codes now (normally only done in convert) to harmonize with region codes in x. Use custom match + # with the country.names in x to make sure that when the country.names are converted to iso3c in convert, + # no duplicates appear. + dplyr::mutate("Region" = countrycode::countrycode(.data$REGION, + "iso3c", + "country.name", + custom_match = c("BIH" = "Bosnia and Herzegovina", + "COG" = "Congo", + "COD" = "Democratic Republic of the Congo", + "CIV" = "C\u00f4te d'Ivoire", + "MMR" = "Myanmar", + "PSE" = "Palestine", + "RUS" = "Russian Federation", + "TTO" = "Trinidad and Tobago", + "VNM" = "Viet Nam")), + .keep = "unused") + + dplyr::bind_rows(x, x_urb) %>% + as.magpie(spatial = "Region", temporal = "year", tidy = TRUE, filter = FALSE) } #' @rdname readSSP #' @order 2 #' @param x MAgPIE object returned from readSSP -convertSSP <- function(x, subtype) { - if (subtype %in% c("all", "pop", "gdp", "urb")) { - if (subtype == "all") x <- x %>% addTWNpop() %>% addTWNlab() - if (subtype == "pop") x <- addTWNpop(x) - x <- toolGeneralConvert(x) - - } else if (subtype == "pop2018Update") { - - # Sum over sex and agegrp - x <- dimSums(x, dim = c(3.2, 3.3)) - # Add the Channel Islands (GB_CHA) to Great Britain (GBR) - x["GBR", , ] <- x["GBR", , ] + x["GB_CHA", , ] - x <- x["GB_CHA", , invert = TRUE] - x <- toolGeneralConvert(x) - - } else if (subtype == "lab2018Update") { - - # Sum over sex and agegrp - agegrps <- getNames(x)[grepl("\\.(15|2|3|4|50|55|60)", getNames(x))] - x <- dimSums(x[, , agegrps], dim = c(3.2, 3.3)) - # Add the Channel Islands (GB_CHA) to Great Britain (GBR) - x["GBR", , ] <- x["GBR", , ] + x["GB_CHA", , ] - x <- x["GB_CHA", , invert = TRUE] - x <- toolGeneralConvert(x) - +#' @param subtype A string, either "all", "gdp", "pop", "lab", "urb" +convertSSP <- function(x, subtype = "all") { + # Filter for subtype in the convert Function to use common read cache + if (subtype == "gdp") { + x <- mselect(x, + Model = "OECD ENV-Growth 2023", + Scenario = c("SSP1", "SSP2", "SSP3", "SSP4", "SSP5"), + Variable = "GDP|PPP", + Unit = "billion USD_2017/yr") } - x -} - - -addTWNpop <- function(x) { - twnPop <- new.magpie("TWN", - getYears(x[, , "Population"][, , "IIASA-WiC POP"]), - getNames(x[, , "Population"][, , "IIASA-WiC POP"])) - - # Read in population data - twnPopMedium <- readSource("PopulationTWN", subtype = "medium", convert = FALSE) %>% - `[`(, , "Year-end_Population", pmatch = TRUE) %>% - `[`(, , c("0-14 years", "15-64 years", "65+ years"), pmatch = TRUE) %>% - dimSums(3) - twnPopHigh <- readSource("PopulationTWN", subtype = "high", convert = FALSE) %>% - `[`(, , "Year-end_Population", pmatch = TRUE) %>% - `[`(, , c("0-14 years", "15-64 years", "65+ years"), pmatch = TRUE) %>% - dimSums(3) - twnPopLow <- readSource("PopulationTWN", subtype = "low", convert = FALSE) %>% - `[`(, , "Year-end_Population", pmatch = TRUE) %>% - `[`(, , c("0-14 years", "15-64 years", "65+ years"), pmatch = TRUE) %>% - dimSums(3) - - # Transfer data until 2060 - years <- intersect(getYears(twnPop), getYears(twnPopMedium)) - twnPop[, years, "SSP1", pmatch = TRUE] <- twnPopLow[, years, ] - twnPop[, years, "SSP2", pmatch = TRUE] <- twnPopMedium[, years, ] - twnPop[, years, "SSP3", pmatch = TRUE] <- twnPopHigh[, years, ] - twnPop[, years, "SSP4d", pmatch = TRUE] <- twnPopMedium[, years, ] - twnPop[, years, "SSP5", pmatch = TRUE] <- twnPopLow[, years, ] - - ####### projecting population and labour until 2150 - grTWN <- new.magpie("TWN", getYears(twnPop), getNames(twnPop)) - # calculate growht rate from 2055 to 2060 - grTWN[, 2060, ] <- (twnPop[, 2060, ] / setYears(twnPop[, 2055, ], NULL)) - 1 - # calculating grow rates from 2060 to 2150 assuming zero growth in 2200 - for (t in seq(2065, 2100, 5)) { - grTWN[, t, ] <- setYears(grTWN[, 2060, ], NULL) - (t - 2060) / 5 * setYears(grTWN[, 2060, ], NULL) / 28 + if (subtype == "pop") { + x <- mselect(x, + Model = "IIASA-WiC POP 2023", + Scenario = c("SSP1", "SSP2", "SSP3", "SSP4", "SSP5"), + Variable = "Population", + Unit = "million") } - # applying assumed growth rates to population and labour matrices - for (t in seq(2065, 2100, 5)) { - twnPop[, t, ] <- setYears(twnPop[, t - 5, ], t) * (1 + grTWN[, t, ]) + if (subtype == "lab") { + # Choose only age groups between 15 and 64 + agegrps <- getNames(x, dim = "Variable")[grepl("^Population(.*)(19|24|29|34|39|44|49|54|59|64)$", + getNames(x, dim = "Variable"))] + x <- mselect(x, + Model = "IIASA-WiC POP 2023", + Variable = agegrps, + Unit = "million") + } + if (subtype == "urb") { + x <- mselect(x, + Model = "NCAR", + Variable = "Population|Urban|Share", + Unit = "%") + # Clean up Scenario names + getNames(x, dim = "Scenario") <- sub("_.*", "", getNames(x, dim = "Scenario")) } - twnPop[, 2010, ] <- 23162.123 - x[, , "IIASA-WiC POP"]["TWN", , "Population"] <- twnPop / 1000 - x -} - -addTWNlab <- function(x) { - aged <- tidyr::expand_grid("Population", - c("Male", "Female"), - c("Aged15-19", "Aged20-24", "Aged25-29", "Aged30-34", "Aged35-39", - "Aged40-44", "Aged45-49", "Aged50-54", "Aged55-59", "Aged60-64")) %>% - purrr::pmap_chr(paste, sep = "|") - twnLab <- new.magpie("TWN", - getYears(x[, , aged][, , "IIASA-WiC POP"]), - getNames(x[, , aged][, , "IIASA-WiC POP"])) - # read in labour data - twnLabMedium <- readSource("PopulationTWN", subtype = "medium", convert = FALSE) %>% - `[`(, , "Year-end_Population", pmatch = TRUE) %>% - `[`(, , "15-64 years", pmatch = TRUE) - twnLabHigh <- readSource("PopulationTWN", subtype = "high", convert = FALSE) %>% - `[`(, , "Year-end_Population", pmatch = TRUE) %>% - `[`(, , "15-64 years", pmatch = TRUE) - twnLabLow <- readSource("PopulationTWN", subtype = "low", convert = FALSE) %>% - `[`(, , "Year-end_Population", pmatch = TRUE) %>% - `[`(, , "15-64 years", pmatch = TRUE) + # Reduce dimension by summation when possible + if (subtype != "all") x <- dimSums(x, dim = c("Model", "Variable", "Unit")) + # Drop regions (anything with brackets, or called "World") and convert to iso3c. Use custom match for the Federal + # States of Micronesia (= FSM) + x <- x[!grepl("\\(|World", getCells(x)), , ] + getCells(x) <- countrycode::countrycode(getCells(x), "country.name", "iso3c", custom_match = c("Micronesia" = "FSM")) - # Transfer data until 2060 - years <- intersect(getYears(twnLab), getYears(twnLabMedium)) - twnLab[, years, "SSP1", pmatch = TRUE] <- twnLabLow[, years, ] / length(aged) - twnLab[, years, "SSP2", pmatch = TRUE] <- twnLabMedium[, years, ] / length(aged) - twnLab[, years, "SSP3", pmatch = TRUE] <- twnLabHigh[, years, ] / length(aged) - twnLab[, years, "SSP4d", pmatch = TRUE] <- twnLabMedium[, years, ] / length(aged) - twnLab[, years, "SSP5", pmatch = TRUE] <- twnLabLow[, years, ] / length(aged) - ####### projecting population and labour until 2150 - grTWN <- new.magpie("TWN", getYears(twnLab), getNames(twnLab)) - # calculate growht rate from 2055 to 2060 - grTWN[, 2060, ] <- (twnLab[, 2060, ] / setYears(twnLab[, 2055, ], NULL)) - 1 - # calculating grow rates from 2060 to 2150 assuming zero growth in 2200 - for (t in seq(2065, 2100, 5)) { - grTWN[, t, ] <- setYears(grTWN[, 2060, ], NULL) - (t - 2060) / 5 * setYears(grTWN[, 2060, ], NULL) / 28 - } - # applying assumed growth rates to population and labour matrices - for (t in seq(2065, 2100, 5)) { - twnLab[, t, ] <- setYears(twnLab[, t - 5, ], t) * (1 + grTWN[, t, ]) - } - twnLab[, 2010, ] <- 16774.919195 / length(aged) - x[, , "IIASA-WiC POP"]["TWN", , aged] <- twnLab / 1000 - x + toolGeneralConvert(x, useDefaultSetNames = subtype != "all") } +#' @rdname readSSP +#' @order 1 +downloadSSP <- function() { + stop("Manual download of SSP data required!") + # Compose meta data + list(url = "https://data.ece.iiasa.ac.at/ssp/#/downloads", + doi = "-", + title = "SSP projections", + description = "SSP projections Release 3.0.1, March2024", + unit = "-", + author = "IIASA, OECD, Wittgenstein Center", + release_date = "2024", + license = "-", + comment = "Manual download required! Accessed on the 15.04.2024.") +} diff --git a/R/readWDI.R b/R/readWDI.R index bc67ef4..6e18621 100644 --- a/R/readWDI.R +++ b/R/readWDI.R @@ -14,6 +14,12 @@ #' \item \code{"AG.SRF.TOTL.K2"}: Surface area (in square kms) #' } #' +#' @details +#' The workflow to update the WDI data is the following: call the download function manually, and rename the new +#' WDI.rds file including the download date. Then change the file_name that is read by readWDI. This ensures, that +#' the past data isn't changed between users. +#' +#' #' @inherit madrat::readSource return #' @seealso [madrat::readSource()] and [madrat::downloadSource()] #' @@ -22,7 +28,7 @@ #' } #' @order 2 readWDI <- function(subtype) { - x <- readr::read_rds("WDI_20_02_2023.Rds") + x <- readr::read_rds("WDI_15_04_2024.Rds") possibleSubtypes <- colnames(x)[!colnames(x) %in% c("iso3c", "iso2c", "country", "year")] if (!subtype %in% possibleSubtypes) { @@ -91,5 +97,5 @@ downloadWDI <- function() { author = "World Bank", release_date = "-", license = "-", - comment = "see also ") + comment = "see also https://databank.worldbank.org/source/world-development-indicators") } diff --git a/R/toolCheckUserInput.R b/R/toolCheckUserInput.R index 13f7107..5d5fd48 100644 --- a/R/toolCheckUserInput.R +++ b/R/toolCheckUserInput.R @@ -1,4 +1,11 @@ toolCheckUserInput <- function(driver, args) { + permitted_args <- c("pastData", "futureData", "harmonization", "scenario", "unit", "yearEnd", "extension2150", + "average2020", "naming", "popAsWeight", "asShare", "GDPPast", "GDPFuture", "GDPpcPast", "GDPpcFuture", + "PopulationPast", "PopulationFuture", "LabourPast", "LabourFuture", "UrbanPast", "UrbanFuture") + if (!all(names(args) %in% permitted_args)) { + stop(glue("Bad argument to calc{driver}: '{names(args)[! names(args) %in% permitted_args]}'.")) + } + # Check existence of pastData, futureData and harmonization if (!all(c("pastData", "futureData", "harmonization") %in% names(args)) && any(c("pastData", "futureData", "harmonization") %in% names(args))) { @@ -25,12 +32,12 @@ toolCheckUserInput <- function(driver, args) { } # Check 'unit' argument - if ("unit" %in% names(args) && length(args$unit) != 1 && !grepl("^constant (2005|2017) ", args$unit)) { - stop(glue("Bad argument to calc{driver}. Currently, only constant 2005 or 2017 dollars are accepted.")) + if ("unit" %in% names(args) && (length(args$unit) != 1 || !grepl("^constant (2017|2005) ", args$unit))) { + stop(glue("Bad argument to calc{driver}. Currently, only constant 2017 dollars are accepted.")) } # Check 'average2020' argument - if ("average2020" %in% names(args) && length(args$average2020) != 1 && !is.logical(args$average2020)) { + if ("average2020" %in% names(args) && (length(args$average2020) != 1 || !is.logical(args$average2020))) { stop(glue("Bad argument to calc{driver}. 'average2020' has to be TRUE of FALSE.")) } diff --git a/R/toolGeneralConvert.R b/R/toolGeneralConvert.R index ed2399d..1f667ca 100644 --- a/R/toolGeneralConvert.R +++ b/R/toolGeneralConvert.R @@ -55,5 +55,5 @@ toolGeneralConvert <- function(x, } # Sort by year - x <- x[, sort(getYears(x)), ] + x[, sort(getYears(x)), ] } diff --git a/R/toolGetEUcountries.R b/R/toolGetEUcountries.R index 27afc84..5db8aa3 100644 --- a/R/toolGetEUcountries.R +++ b/R/toolGetEUcountries.R @@ -1,13 +1,10 @@ # These countries are the ones (essential the EU-27) that receive special treatment # in the SSP2EU scenario. -toolGetEUcountries <- function(onlyWithARIADNEgdpData = FALSE) { +toolGetEUcountries <- function() { x <- toolGetMapping("regionmappingH12.csv", type = "regional", where = "mappingfolder") %>% tibble::as_tibble() %>% dplyr::filter(.data$RegionCode == "EUR", .data$CountryCode != "GBR") %>% dplyr::pull(.data$CountryCode) - if (onlyWithARIADNEgdpData) { - x <- x[x %in% where(readSource("ARIADNE", "gdp_corona") != 0)$true$regions] - } x } diff --git a/R/toolGetScenarioDefinition.R b/R/toolGetScenarioDefinition.R index 0222bf7..ca7a352 100644 --- a/R/toolGetScenarioDefinition.R +++ b/R/toolGetScenarioDefinition.R @@ -34,41 +34,33 @@ toolGetScenarioDefinition <- function(driver = NULL, scen = NULL, aslist = FALSE scenarios <- tibble::tribble( ~driver, ~scenario, ~pastData, ~futureData, ~harmonization, # GDPpc scenarios - "GDPpc", "SSPs", "WDI-MI", "SSPsOld-MI", "calibSSPs", + "GDPpc", "SSPs", "WDI-MI", "SSPs-MI", "calibSSPs", + "GDPpc", "SSP2", "WDI-MI", "SSP2-MI", "calibSSPs", "GDPpc", "SDPs", "-", "-", "calibSDPs", "GDPpc", "SSP2EU", "-", "-", "GDPoverPop", - "GDPpc", "ISIMIP", "WDI-MI", "SSPsOld-MI", "calibSSPs", - "GDPpc", "noCovid", "WDI-MI", "SSPsOld-MI", "calibNoCovid", - "GDPpc", "longCovid", "-", "-", "calibLongCovid", - "GDPpc", "shortCovid", "-", "-", "calibShortCovid", - "GDPpc", "SSPsOld", "-", "-", "GDPoverPop", + "GDPpc", "ISIMIP", "WDI-MI", "SSPs-MI", "calibSSPs", # GDP scenarios "GDP", "SSPs", "-", "-", "GDPpcWithPop", + "GDP", "SSP2", "-", "-", "GDPpcWithPop", "GDP", "SDPs", "-", "-", "GDPpcWithPop", "GDP", "ISIMIP", "-", "-", "GDPpcWithPop", "GDP", "SSP2EU", "Eurostat-WDI-MI", "SSP2EU-MI", "calibSSP2EU", - "GDP", "noCovid", "-", "-", "GDPpcWithPop", - "GDP", "longCovid", "-", "-", "GDPpcWithPop", - "GDP", "shortCovid", "-", "-", "GDPpcWithPop", - "GDP", "SSPsOld", "IHME_USD05_PPP_pc-MI", "SSPs-MI", "past_transition", # Population Scenarios "Population", "SSPs", "WDI-UN_PopDiv-MI", "SSPs-UN_PopDiv-MI", "withPEAPandFuture", + "Population", "SSP2", "WDI-UN_PopDiv-MI", "SSP2-UN_PopDiv-MI", "withPEAPandFuture", "Population", "SDPs", "WDI-UN_PopDiv-MI", "SDPs-UN_PopDiv-MI", "withPEAPandFuture", "Population", "SSP2EU", "Eurostat-WDI-UN_PopDiv-MI", "SSP2EU-UN_PopDiv-MI", "calibSSP2EU", "Population", "ISIMIP", "UN_PopDiv-MI", "SSPs-UN_PopDiv-MI", "calibISIMIP", - "Population", "SSPsOld", "WDI-MI", "SSPsOld-MI", "past_transition", - "Population", "noCovid", "WDI-UN_PopDiv-MI", "SSPs-UN_PopDiv-MI", "withPEAPandFuture", - "Population", "longCovid", "WDI-UN_PopDiv-MI", "SSPs-UN_PopDiv-MI", "withPEAPandFuture", - "Population", "shortCovid", "WDI-UN_PopDiv-MI", "SSPs-UN_PopDiv-MI", "withPEAPandFuture", # Labour Scenarios - "Labour", "SSPs", "-", "SSPs", "-", - "Labour", "SDPs", "-", "SDPs", "-", - "Labour", "SSP2EU", "-", "SSP2EU", "-", - "Labour", "SSPsOld", "-", "SSPsOld", "-", + "Labour", "SSPs", "WDI", "SSPs", "pastAndLevel", + "Labour", "SSP2", "WDI", "SSP2", "pastAndLevel", + "Labour", "SDPs", "WDI", "SDPs", "pastAndLevel", + "Labour", "SSP2EU", "WDI", "SSP2EU", "pastAndLevel", # Urban population scenarios - "Urban", "SSPs", "WDI", "SSPs", "past", - "Urban", "SDPs", "WDI", "SDPs", "past", - "Urban", "SSP2EU", "WDI", "SSP2EU", "past" + "Urban", "SSPs", "WDI", "SSPs", "pastAndGrowth", + "Urban", "SSP2", "WDI", "SSP2", "pastAndGrowth", + "Urban", "SDPs", "WDI", "SDPs", "pastAndGrowth", + "Urban", "SSP2EU", "WDI", "SSP2EU", "pastAndGrowth" ) # End of scenario-design section diff --git a/R/toolHarmonizeFuture.R b/R/toolHarmonizeFuture.R deleted file mode 100644 index eadb772..0000000 --- a/R/toolHarmonizeFuture.R +++ /dev/null @@ -1,14 +0,0 @@ -toolHarmonizeFuture <- function(past, future) { - firstyear <- min(getYears(future, as.integer = TRUE)) - tmp <- dimSums(past / setYears(past[, firstyear, ], NULL) * setYears(future[, firstyear, ], NULL), - dim = 3.2) - tmp[is.nan(tmp)] <- 0 - if (firstyear > min(getYears(past, as.integer = TRUE))) { - yearsPast <- getYears(past)[which(getYears(past, as.integer = TRUE) < firstyear)] - tmp2 <- setNames(tmp[, yearsPast, rep(1, ndata(future))], getNames(future)) - combined <- mbind(tmp2, future) - } else { - combined <- future - } - combined -} diff --git a/R/toolHarmonizePast.R b/R/toolHarmonizePast.R index a1f8f77..d8c5c62 100644 --- a/R/toolHarmonizePast.R +++ b/R/toolHarmonizePast.R @@ -1,15 +1,81 @@ -toolHarmonizePast <- function(past, future) { - firstyear <- min(getYears(future, as.integer = TRUE)) - tmp <- dimSums(future / setYears(future[, firstyear, ], NULL) * setYears(past[, firstyear, ], NULL), - dim = 3.2) - tmp[is.nan(tmp)] <- 0 - if (firstyear > min(getYears(past, as.integer = TRUE))) { - yearsPast <- getYears(past)[which(getYears(past, as.integer = TRUE) < firstyear)] - tmp2 <- setNames(past[, yearsPast, rep(1, ndata(future))], getNames(future)) - combined <- mbind(tmp2, tmp) - } else { - combined <- tmp - } - - combined +#' Harmonization tool Past +#' +#' Like all harmonization tools in mrdrivers, toolHarmonizePast takes two magpie objects, 'past' and 'future', +#' and returns a single magpie object, i.e. the harmonized time-series. In this case, the harmonized time-series is +#' always equal to 'past', in the years of 'past'. After that the harmonized time-series depends on the 'method' +#' argument chosen. +#' +#' @details # Dimensions of 'past' and 'future' +#' If the 'future' object has multiple scenarios/datatypes, i.e. the length of the third dimension is larger than 1, +#' then the a harmonized time-series is created for every scenario/datatype in future. The same 'past' object is used +#' in every case - hence the requirement that 'past' only have one scenario/datatype. +#' +#' @param past A magpie object with only one scenario/datatype, i.e. the length of the third dimension should be 1. +#' @param future A magpie object. +#' @param method A string defining the harmonization method: +#' \itemize{ +#' \item "level": the harmonized time-series is exactly equal to 'future' in the years after the last year +#' of 'past'. +#' \item "growth": the harmonized time-series follows the same growth rates as 'future', in the years +#' after the last year of 'past'. +#' \item "transition": the harmonized time-series transitions to 'future' by the year 'yEnd'. After yEnd, the +#' harmonized time-series is equal to 'future'. In the transition phase, a share of the absolute difference +#' between past' and future' in the last year of past' is added to future. This share starts at 1 in the +#' last year of 'past' and decreases linearly to 0 by yEnd. +#' } +#' @param yEnd Additional input for "transition" method. Year by which the transition period is completed. +#' +#' @return A magpie object with the same dimensions as 'future'. +toolHarmonizePast <- function(past, future, method = "level", yEnd = 2100) { + # Check dimensions of past + if (dim(past)[3] != 1) { + stop("The past data may only have one datatype, i.e. dim(past)[3] needs to be 1.") + } + + # Check time overlap + lastPastYear <- max(getYears(past, as.integer = TRUE)) + firstFutureYear <- min(getYears(future, as.integer = TRUE)) + if (lastPastYear < firstFutureYear) { + stop("The past and future data need to have some overlap.") + } + + # If lastPastYear is not in future data, then create future data for lastPastYear + # by linear interpolation. That way the return object really has all the past data. + if (!lastPastYear %in% getYears(future, as.integer = TRUE)) { + future <- magclass::time_interpolate(future, lastPastYear, integrate_interpolated_years = TRUE) + } + + # Create past data for all future scenarios + tmpPast <- past[, , rep(1, ndata(future))] + tmpPast <- setNames(tmpPast, getNames(future)) + tmpPast[is.nan(tmpPast)] <- 0 + + # Create magpie object for all future scenarios + yearsFuture <- getYears(future, as.integer = TRUE)[which(getYears(future, as.integer = TRUE) > lastPastYear)] + tmpFuture <- new.magpie(getItems(future, 1), yearsFuture, getNames(future), fill = 0) + + if (method == "level") { + tmpFuture[, , ] <- future[, yearsFuture, ] + } + if (method == "growth") { + tmpFuture[, , ] <- tmpPast[, lastPastYear, ] * future[, yearsFuture, ] / future[, lastPastYear, ] + tmpFuture[is.nan(tmpFuture)] <- 0 + } + if (method == "transition") { + yearsTrans <- yearsFuture[which(yearsFuture <= yEnd)] + tmpTrans <- new.magpie(getItems(future, 1), yearsTrans, getNames(future), fill = 0) + + diffInlastPastYear <- tmpPast[, lastPastYear, ] - future[, lastPastYear, ] + + for (y in yearsTrans) { + tmpTrans[, y, ] <- future[, y, ] + setYears(diffInlastPastYear, y) * ((yEnd - y) / (yEnd - lastPastYear)) + } + + yearsPostTrans <- yearsFuture[which(yearsFuture > yEnd)] + tmpFuture <- mbind(tmpTrans, future[, yearsPostTrans, ]) %>% suppressWarnings() + # Above warning sometimes appears: You are trying to mbind an empty magclass object. Is that really intended? + # Answer is yes! + } + + mbind(tmpPast, tmpFuture) } diff --git a/R/toolHarmonizePastGrFuture.R b/R/toolHarmonizePastGrFuture.R deleted file mode 100644 index 65fbf2f..0000000 --- a/R/toolHarmonizePastGrFuture.R +++ /dev/null @@ -1,50 +0,0 @@ -#' Harmonization tool PastGrFuture -#' -#' Like all harmonization tools in mrdrivers, toolHarmonizePastGrFuture takes two magpie objects, 'past' and 'future', -#' and returns a single magpie object, i.e. the harmonized time-series. In this case, the harmonized time-series is -#' equal to 'past', in the years of 'past', and then follows the same growth rates as 'future', for the years of -#' 'future'. -#' -#' @details # Dimensions of 'past' and 'future' -#' If the 'future' object has multiple scenarios/datatypes, i.e. the length of the third dimension is larger than 1, -#' then the a harmonized time-series is created for every scenario/datatype in future. The same 'past' object is used -#' in every case - hence the requirement that 'past' only have one scenario/datatype. -#' -#' @param past A magpie object with only one scenario/datatype, i.e. the length of the third dimension should be 1. -#' @param future A magpie object. -#' -#' @return A magpie object with the same dimensions as 'future'. -toolHarmonizePastGrFuture <- function(past, future) { - # Check dimensions of past - if (dim(past)[3] != 1) { - stop("The past data may only have one datatype, i.e. dim(past)[3] needs to be 1.") - } - - # Check time overlap - lastPastYear <- max(getYears(past, as.integer = TRUE)) - firstFutureYear <- min(getYears(future, as.integer = TRUE)) - if (lastPastYear < firstFutureYear) { - stop("The past and future data need to have some overlap.") - } - - # If lastPastYear is not in future data, then create future data for lastPastYear - # by linear interpolation. That way the return object really has all the past data. - if (!lastPastYear %in% getYears(future, as.integer = TRUE)) { - future <- magclass::time_interpolate(future, lastPastYear, integrate_interpolated_years = TRUE) - } - - # Create past data for all future scenarios - tmpPast <- past[, , rep(1, ndata(future))] - tmpPast <- setNames(tmpPast, getNames(future)) - tmpPast[is.nan(tmpPast)] <- 0 - - # Create magpie object for all future scenarios - yearsFuture <- getYears(future)[which(getYears(future, as.integer = TRUE) > lastPastYear)] - tmpFuture <- new.magpie(getItems(future, 1), yearsFuture, getNames(future), fill = 0) - - # Use growth rates of future object - tmpFuture[, , ] <- tmpPast[, lastPastYear, ] * future[, yearsFuture, ] / future[, lastPastYear, ] - tmpFuture[is.nan(tmpFuture)] <- 0 - - mbind(tmpPast, tmpFuture) -} diff --git a/R/toolHarmonizePastTransition.R b/R/toolHarmonizePastTransition.R deleted file mode 100644 index 63870d0..0000000 --- a/R/toolHarmonizePastTransition.R +++ /dev/null @@ -1,61 +0,0 @@ -#' Harmonization tool PastTransition -#' -#' Like all harmonization tools in mrdrivers, toolHarmonizePastTransition takes two magpie objects, 'past' and 'future', -#' and returns a single magpie object, i.e. the harmonized time-series. In this case, the harmonized time-series is -#' equal to 'past', in the years of 'past', and then transitions to 'future' by the year 'yEnd'. After yEnd, the -#' harmonized time-series is equal to 'future'. In the transition phase, a share of the absolute difference between -#' 'past' and future' in the last year of past' is added to future. This share starts at 1 in the last year of 'past' -#' and decreases linearly to 0 by yEnd. -#' -#' @param yEnd An integer designating the last year of the transition period. -#' @param aslist If TRUE (default is FALSE) then a list with the data object and a description field is returned, -#' insead of only the data object. -#' -#' @inheritParams toolHarmonizePastGrFuture -#' @inherit toolHarmonizePastGrFuture return -#' @inheritSection toolHarmonizePastGrFuture Dimensions of 'past' and 'future' -toolHarmonizePastTransition <- function(past, future, yEnd, aslist = FALSE) { - - lastPastYear <- max(getYears(past, as.integer = TRUE)) - firstFutureYear <- min(getYears(future, as.integer = TRUE)) - if (lastPastYear < firstFutureYear) { - stop("The past and future data need to have some overlap") - } - - # If lastPastYear is not in future data, then create future data for lastPastYear - # by linear interpolation. That way the return object really has all the past data. - if (!lastPastYear %in% getYears(future, as.integer = TRUE)) { - future <- magclass::time_interpolate(future, lastPastYear, integrate_interpolated_years = TRUE) - } - - # Create past data for all future scenarios - tmpPast <- past[, , rep(1, ndata(future))] - tmpPast <- setNames(tmpPast, getNames(future)) - tmpPast[is.nan(tmpPast)] <- 0 - - # Defined transition period and create combined scenario - yearsFuture <- getYears(future, as.integer = TRUE) - yearsTrans <- yearsFuture[which(yearsFuture >= lastPastYear & yearsFuture <= yEnd)] - diffInlastPastYear <- tmpPast[, lastPastYear, ] - future[, lastPastYear, ] - - tmpTrans <- new.magpie(getItems(future, 1), yearsTrans, getNames(future), fill = 0) - - for (y in yearsTrans) { - tmpTrans[, y, ] <- future[, y, ] + - setYears(diffInlastPastYear, y) * ((max(yearsTrans) - y) / (max(yearsTrans) - min(yearsTrans))) - } - - # The final harmonized object, is the combination of the tmpPast, the tmpTrans and future objects. - combined <- mbind(tmpPast, - tmpTrans[, which(yearsTrans > lastPastYear), ], - future[, which(yearsFuture > yEnd), ]) %>% - suppressWarnings() - # Above warning sometimes appears: You are trying to mbind an empty magclass object. Is that really intended? - # Answer is yes! - - if (aslist) { - return(list(x = combined, description = glue("use past data and transition to future data until {yEnd}"))) - } else { - return(combined) - } -} diff --git a/R/toolInterpolateAndExtrapolate.R b/R/toolInterpolateAndExtrapolate.R index 2749021..7de9bce 100644 --- a/R/toolInterpolateAndExtrapolate.R +++ b/R/toolInterpolateAndExtrapolate.R @@ -8,12 +8,12 @@ toolInterpolateAndExtrapolate <- function(data, extrapolate = TRUE) { if (!extrapolate) { # If not extrapolating, then confine the years to those between years with non 0 values missingyears <- missingyears[missingyears > min(where(data[i, , ] != 0)$true$years) & - missingyears < max(where(data[i, , ] != 0)$true$years)] + missingyears < max(where(data[i, , ] != 0)$true$years)] if (rlang::is_empty(missingyears)) next } data[i, missingyears, ] <- time_interpolate(dataset = data[i, , ][, missingyears, , invert = TRUE], - interpolated_year = missingyears, - extrapolation_type = "constant") + interpolated_year = missingyears, + extrapolation_type = "constant") } data } diff --git a/README.md b/README.md index 96a89c2..5957669 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Create GDP and Population Scenarios -R package **mrdrivers**, version **1.5.2** +R package **mrdrivers**, version **2.0.0** [![CRAN status](https://www.r-pkg.org/badges/version/mrdrivers)](https://cran.r-project.org/package=mrdrivers) [![R build status](https://pik-piam.github.io/mrdrivers/workflows/check/badge.svg)](https://pik-piam.github.io/mrdrivers/actions) [![codecov](https://codecov.io/gh/mrdrivers/branch/master/graph/badge.svg)](https://app.codecov.io/gh/mrdrivers) [![r-universe](https://pik-piam.r-universe.dev/badges/mrdrivers)](https://pik-piam.r-universe.dev/builds) @@ -103,7 +103,7 @@ In case of questions / problems please contact Johannes Koch . +Koch J, Soergel B, Leip D, Benke F, Dietrich J (2024). _mrdrivers: Create GDP and Population Scenarios_. R package version 2.0.0, . A BibTeX entry for LaTeX users is @@ -111,8 +111,8 @@ A BibTeX entry for LaTeX users is @Manual{, title = {mrdrivers: Create GDP and Population Scenarios}, author = {Johannes Koch and Bjoern Soergel and Deborra Leip and Falk Benke and Jan Philipp Dietrich}, - year = {2023}, - note = {R package version 1.5.2}, + year = {2024}, + note = {R package version 2.0.0}, url = {https://pik-piam.github.io/mrdrivers}, url = {https://github.com/pik-piam/mrdrivers}, } diff --git a/man/calcGDP.Rd b/man/calcGDP.Rd index ed1186f..4edd2a3 100644 --- a/man/calcGDP.Rd +++ b/man/calcGDP.Rd @@ -25,10 +25,7 @@ calcGDPpc( \item{unit}{A string specifying the unit of GDP. Can be either: \itemize{ -\item "constant 2005 Int$PPP" (default): Scenarios are constructed in constant 2005 Int$PPP. -\item "constant 2005 US$MER": Scenarios are constructed in constant 2005 Int$PPP and then converted with -\code{\link[GDPuc:convertGDP]{GDPuc::convertGDP()}}. -\item "constant 2017 Int$PPP": Scenarios are constructed in constant 2017 Int$PPP. +\item "constant 2017 Int$PPP" (default): Scenarios are constructed in constant 2017 Int$PPP. \item "constant 2017 US$MER": Scenarios are constructed in constant 2017 Int$PPP and then converted with \code{\link[GDPuc:convertGDP]{GDPuc::convertGDP()}}. } @@ -67,9 +64,6 @@ calcOutput("GDPpc") # Return only the SSP2EU GDP scenario calcOutput("GDP", scenario = "SSP2EU") - -# Return the now-outdated GDP scenarios used before summer 2021, -calcOutput("GDP", scenario = "SSPsOld", extension2150 = "constant", average2020 = FALSE) } \dontrun{ diff --git a/man/calcGDPHarmonized.Rd b/man/calcGDPHarmonized.Rd index 2a25d93..7b4ab05 100644 --- a/man/calcGDPHarmonized.Rd +++ b/man/calcGDPHarmonized.Rd @@ -4,10 +4,18 @@ \alias{calcGDPHarmonized} \title{Get Harmonized GDP Data} \usage{ -calcGDPHarmonized(args) +calcGDPHarmonized(harmonization, past, future, scenario, unit, ...) } \arguments{ -\item{args}{Arguments passed on to harmonization functions} +\item{harmonization}{description} + +\item{past}{description} + +\item{future}{description} + +\item{scenario}{description} + +\item{unit}{description} } \value{ magpie object with the requested output data either on country or on diff --git a/man/calcGDPPast.Rd b/man/calcGDPPast.Rd index 72de2ef..3542a48 100644 --- a/man/calcGDPPast.Rd +++ b/man/calcGDPPast.Rd @@ -8,13 +8,13 @@ \alias{calcGDPpcPast} \title{Get GDP and GDPpc scenario building blocks} \usage{ -calcGDPFuture(GDPFuture = "SSPs-MI", unit = "constant 2005 Int$PPP") +calcGDPFuture(GDPFuture, unit) calcGDPPast(GDPPast = "WDI-MI", unit = "constant 2005 Int$PPP") -calcGDPpcFuture(GDPpcFuture = "SSPsOld-MI", unit = "constant 2005 Int$PPP") +calcGDPpcFuture(GDPpcFuture, unit) -calcGDPpcPast(GDPpcPast = "WDI-MI", unit = "constant 2005 Int$PPP") +calcGDPpcPast(GDPpcPast = "WDI-MI", unit = "constant 2017 Int$PPP") } \arguments{ \item{GDPFuture}{A string designating the source for the future GDP data. Available sources are: @@ -23,16 +23,12 @@ calcGDPpcPast(GDPpcPast = "WDI-MI", unit = "constant 2005 Int$PPP") \item "SSP2EU": Combined SSP2 and Eurostat (for the EU countries) source \item "SDPs": \item "MI": Missing island dataset -\item "OECD": OECD } See the "Combining data sources with '-'" section below for how to combine data sources.} \item{unit}{A string specifying the unit of GDP. Can be either: \itemize{ -\item "constant 2005 Int$PPP" (default): Scenarios are constructed in constant 2005 Int$PPP. -\item "constant 2005 US$MER": Scenarios are constructed in constant 2005 Int$PPP and then converted with -\code{\link[GDPuc:convertGDP]{GDPuc::convertGDP()}}. -\item "constant 2017 Int$PPP": Scenarios are constructed in constant 2017 Int$PPP. +\item "constant 2017 Int$PPP" (default): Scenarios are constructed in constant 2017 Int$PPP. \item "constant 2017 US$MER": Scenarios are constructed in constant 2017 Int$PPP and then converted with \code{\link[GDPuc:convertGDP]{GDPuc::convertGDP()}}. } @@ -43,13 +39,6 @@ In all cases, GDP is returned in millions.} \item "WDI": World development indicators from the World Bank \item "MI": Missing island dataset \item "Eurostat": Eurostat -\item The IHME/James data set. This data set is tertiary source that uses available secondary sources (e.g. WDI -and PWT) to create completed GDP per capita time series -(See \href{https://pophealthmetrics.biomedcentral.com/articles/10.1186/1478-7954-10-12}{paper}). -To access, pass one of the subtypes -"IHME_USD05_PPP_pc", "IHME_USD05_MER_pc", "IMF_USD05_PPP_pc", "PENN_USD05_PPP_pc", "WB_USD05_PPP_pc", -"MADDISON_USD05_PPP_pc", "WB_USD05_MER_pc", "IMF_USD05_MER_pc", "UN_USD05_MER_pc". In all cases, the per -capita GDP will be multiplied by WDI population data to get absolute GDP data. }} \item{GDPpcFuture}{A string designating the source for the future GDP data. Available sources are: diff --git a/man/calcGDPpcHarmonized.Rd b/man/calcGDPpcHarmonized.Rd index 9a8adfd..3be63c8 100644 --- a/man/calcGDPpcHarmonized.Rd +++ b/man/calcGDPpcHarmonized.Rd @@ -4,10 +4,18 @@ \alias{calcGDPpcHarmonized} \title{Get Harmonized GDPpc Data} \usage{ -calcGDPpcHarmonized(args) +calcGDPpcHarmonized(harmonization, past, future, scenario, unit, ...) } \arguments{ -\item{args}{Arguments passed on to harmonization functions} +\item{harmonization}{description} + +\item{past}{description} + +\item{future}{description} + +\item{scenario}{description} + +\item{unit}{description} } \value{ magpie object with the requested output data either on country or on diff --git a/man/calcLabourHarmonized.Rd b/man/calcLabourHarmonized.Rd index 42a4e0d..8162a01 100644 --- a/man/calcLabourHarmonized.Rd +++ b/man/calcLabourHarmonized.Rd @@ -4,10 +4,14 @@ \alias{calcLabourHarmonized} \title{Get Harmonized Labour Data} \usage{ -calcLabourHarmonized(args) +calcLabourHarmonized(harmonization, past, future, ...) } \arguments{ -\item{args}{Arguments passed on to harmonization functions} +\item{harmonization}{description} + +\item{past}{description} + +\item{future}{description} } \value{ magpie object with the requested output data either on country or on diff --git a/man/calcPopulationHarmonized.Rd b/man/calcPopulationHarmonized.Rd index ec4a3a4..18dd1f2 100644 --- a/man/calcPopulationHarmonized.Rd +++ b/man/calcPopulationHarmonized.Rd @@ -4,10 +4,14 @@ \alias{calcPopulationHarmonized} \title{Get Harmonized Population Data} \usage{ -calcPopulationHarmonized(args) +calcPopulationHarmonized(harmonization, past, future, yEnd, ...) } \arguments{ -\item{args}{Arguments passed on to harmonization functions} +\item{harmonization}{description} + +\item{past}{description} + +\item{future}{description} } \value{ magpie object with the requested output data either on country or on diff --git a/man/calcPopulationPast.Rd b/man/calcPopulationPast.Rd index e51fc42..e36a379 100644 --- a/man/calcPopulationPast.Rd +++ b/man/calcPopulationPast.Rd @@ -45,7 +45,6 @@ Available sources are: \item "SDPs": \item "UN_PopDiv": United Nations \item "MI": Missing island dataset -\item "SSPsOld": Old SSPs from the IIASA database }} \item{PopulationPast}{A string designating the source for the historical population data. diff --git a/man/calcRatioPPP2MER.Rd b/man/calcRatioPPP2MER.Rd index a6de2be..e5dd8c8 100644 --- a/man/calcRatioPPP2MER.Rd +++ b/man/calcRatioPPP2MER.Rd @@ -4,12 +4,10 @@ \alias{calcRatioPPP2MER} \title{MER over PPP ratio} \usage{ -calcRatioPPP2MER(from = "WDI", when = 2005) +calcRatioPPP2MER(when = 2017) } \arguments{ -\item{from}{A string indicating the source. Can be either "WDI" (default) or "OECD".} - -\item{when}{An integer (defaults to 2005) specifying the year of the PPP2MER factor.} +\item{when}{An integer (defaults to 2017) specifying the year of the PPP2MER factor.} } \value{ magpie object with the requested output data either on country or on @@ -17,8 +15,8 @@ regional level depending on the choice of argument "aggregate" or a list of info if supplementary is set to TRUE. } \description{ -Get a conversion factor to convert GDP in constant 2005 Int$PPP into constant 2005 US$MER. -Use the when argument to switch the year of the conversion factor. +Get a conversion factor to convert GDP in constant 2017 Int$PPP into constant 2017 US$MER. +Use the when argument to switch the year of the conversion factor. Source = WDI. } \examples{ \dontrun{ diff --git a/man/calcUrbanHarmonized.Rd b/man/calcUrbanHarmonized.Rd index 31b5161..438fcb0 100644 --- a/man/calcUrbanHarmonized.Rd +++ b/man/calcUrbanHarmonized.Rd @@ -4,10 +4,14 @@ \alias{calcUrbanHarmonized} \title{Get Harmonized Urban Data} \usage{ -calcUrbanHarmonized(args) +calcUrbanHarmonized(harmonization, past, future, ...) } \arguments{ -\item{args}{Arguments passed on to harmonization functions} +\item{harmonization}{description} + +\item{past}{description} + +\item{future}{description} } \value{ magpie object with the requested output data either on country or on diff --git a/man/mrdrivers-package.Rd b/man/mrdrivers-package.Rd index b3a1764..d9585a4 100644 --- a/man/mrdrivers-package.Rd +++ b/man/mrdrivers-package.Rd @@ -9,5 +9,26 @@ \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#stable}{\figure{lifecycle-stable.svg}{options: alt='[Stable]'}}}{\strong{[Stable]}} Create scenarios of GDP, Population, GDP per capita, and Urban population share +} +\seealso{ +Useful links: +\itemize{ + \item \url{https://pik-piam.github.io/mrdrivers} + \item \url{https://github.com/pik-piam/mrdrivers} + \item Report bugs at \url{https://github.com/pik-piam/mrdrivers/issues} +} + +} +\author{ +\strong{Maintainer}: Johannes Koch \email{jokoch@pik-potsdam.de} + +Authors: +\itemize{ + \item Bjoern Soergel + \item Deborra Leip + \item Falk Benke + \item Jan Philipp Dietrich +} + } \keyword{internal} diff --git a/man/readARIADNE.Rd b/man/readARIADNE.Rd deleted file mode 100644 index 43c7bf5..0000000 --- a/man/readARIADNE.Rd +++ /dev/null @@ -1,25 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readARIADNE.R -\name{readARIADNE} -\alias{readARIADNE} -\alias{convertARIADNE} -\title{Read ARIADNE Reference Scenario} -\usage{ -readARIADNE(subtype) - -convertARIADNE(x, subtype) -} -\arguments{ -\item{subtype}{data subtype. Either "population", "gdp", or "gdp_corona"} - -\item{x}{MAgPIE object returned by readARIADNE} -} -\value{ -magpie object of ARIADNE reference scenario data by country -} -\description{ -Read ARIADNE Reference Scenario data from various .xls files as magpie object -} -\seealso{ -\code{\link[madrat:readSource]{madrat::readSource()}} -} diff --git a/man/readEurostatPopGDP.Rd b/man/readEurostatPopGDP.Rd index b37a226..3259bf8 100644 --- a/man/readEurostatPopGDP.Rd +++ b/man/readEurostatPopGDP.Rd @@ -18,7 +18,8 @@ convertEurostatPopGDP(x, subtype) \item "population": Population, ref demo_gind \item "population_projections": Population projections, ref proj_19np \item "GDP": GDP, ref nama_10_gdp -\item "GDPgr_projections": Projected GDP growth rates, 2023 forecast. +\item "GDPgr_projections_short": Projected GDP growth rates, 2023 forecast. +\item "GDPgr_projections_long": }} \item{x}{MAgPIE object returned by readEurostatPopGDP} diff --git a/man/readIMF.Rd b/man/readIMF.Rd index 943ca96..d0ecbc2 100644 --- a/man/readIMF.Rd +++ b/man/readIMF.Rd @@ -8,16 +8,14 @@ \usage{ downloadIMF() -readIMF(subtype = "current_account", subset = "WEOOct2022all.xls") +readIMF() -convertIMF(x, subtype = "current_account") +convertIMF(x, subtype = "all") } \arguments{ -\item{subtype}{Either "current_account" or "GDPpc"} - -\item{subset}{Either "WEOOct2022all.xls" or "WEOallOct2019.xls"} - \item{x}{MAgPIE object returned by readIMF} + +\item{subtype}{Use to filter the IMF data} } \value{ The read-in data, usually a magpie object. If supplementary is TRUE a list including @@ -26,7 +24,8 @@ should match the source data. The spatial dimension should either match the sour if the convert argument is set to TRUE, should be on ISO code country level. } \description{ -Read-in IMF data +Read-in data from the IMF's World Economic Outlook. +Currently reading GDP per capita and current account balance data. } \examples{ \dontrun{ diff --git a/man/readOECD.Rd b/man/readOECD.Rd deleted file mode 100644 index 2e33b11..0000000 --- a/man/readOECD.Rd +++ /dev/null @@ -1,31 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readOECD.R -\name{readOECD} -\alias{readOECD} -\alias{convertOECD} -\title{Read OECD GDP or PPP2MER ratio} -\usage{ -readOECD(subtype) - -convertOECD(x, subtype) -} -\arguments{ -\item{subtype}{data subtype. Either "gdp" or "ratioPM"} - -\item{x}{MAgPIE object returned from readOECD} -} -\value{ -magpie object of the GDP data -} -\description{ -Read-in GDP or PPP2MER ratio data xlsx file from OECD as magclass object -} -\examples{ -\dontrun{ -readSource("OECD", "gdp") -readSource("OECD", "ratioPM") -} -} -\seealso{ -\code{\link[madrat:readSource]{madrat::readSource()}} -} diff --git a/man/readPWT.Rd b/man/readPWT.Rd deleted file mode 100644 index 6000bd1..0000000 --- a/man/readPWT.Rd +++ /dev/null @@ -1,31 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readPWT.R -\name{readPWT} -\alias{readPWT} -\alias{convertPWT} -\title{Read PWT} -\usage{ -readPWT() - -convertPWT(x) -} -\arguments{ -\item{x}{MAgPIE object returned by readPWT} -} -\value{ -The read-in data, usually a magpie object. If supplementary is TRUE a list including -the data and metadata is returned instead. The temporal and data dimensionality -should match the source data. The spatial dimension should either match the source data or, -if the convert argument is set to TRUE, should be on ISO code country level. -} -\description{ -Read-in PWT data as magclass object -} -\examples{ -\dontrun{ -readSource("PWT") -} -} -\seealso{ -\code{\link[madrat:readSource]{madrat::readSource()}} -} diff --git a/man/readPopulationTWN.Rd b/man/readPopulationTWN.Rd deleted file mode 100644 index 84d0a19..0000000 --- a/man/readPopulationTWN.Rd +++ /dev/null @@ -1,28 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readPopulationTWN.R -\name{readPopulationTWN} -\alias{readPopulationTWN} -\title{Read TWN Population projections.} -\usage{ -readPopulationTWN(subtype) -} -\arguments{ -\item{subtype}{A string. Variant of population projection.} -} -\value{ -A magpie object of the TWN population data -} -\description{ -\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} - -Read-in Population projections for Taiwan -} -\examples{ -\dontrun{ -readSource("PopulationTWN", subtype = "medium") -} -} -\seealso{ -\code{\link[madrat:readSource]{madrat::readSource()}} -} -\keyword{internal} diff --git a/man/readSSP.Rd b/man/readSSP.Rd index f705227..96fcd7f 100644 --- a/man/readSSP.Rd +++ b/man/readSSP.Rd @@ -2,18 +2,20 @@ % Please edit documentation in R/readSSP.R \name{readSSP} \alias{readSSP} +\alias{downloadSSP} \alias{convertSSP} \title{Read SSP} \usage{ -readSSP(subtype) +readSSP() -convertSSP(x, subtype) +downloadSSP() + +convertSSP(x, subtype = "all") } \arguments{ -\item{subtype}{A string, either "all", "gdp", "pop", "urb", "pop2018Update", -"lab2018Update" or "ratioPM".} - \item{x}{MAgPIE object returned from readSSP} + +\item{subtype}{A string, either "all", "gdp", "pop", "lab", "urb"} } \value{ The read-in data, usually a magpie object. If supplementary is TRUE a list including @@ -22,7 +24,7 @@ should match the source data. The spatial dimension should either match the sour if the convert argument is set to TRUE, should be on ISO code country level. } \description{ -Read-in an SSP data csv.zip file as magclass object +Read-in an SSP data as magclass object } \examples{ \dontrun{ diff --git a/man/readWDI.Rd b/man/readWDI.Rd index 1fbf4c0..8ec5325 100644 --- a/man/readWDI.Rd +++ b/man/readWDI.Rd @@ -36,6 +36,11 @@ if the convert argument is set to TRUE, should be on ISO code country level. \description{ Download, read and convert WDI (World development indicators) data. } +\details{ +The workflow to update the WDI data is the following: call the download function manually, and rename the new +WDI.rds file including the download date. Then change the file_name that is read by readWDI. This ensures, that +the past data isn't changed between users. +} \examples{ \dontrun{ readSource("WDI", subtype = "SP.POP.TOTL") diff --git a/man/toolHarmonizePast.Rd b/man/toolHarmonizePast.Rd new file mode 100644 index 0000000..7fc1238 --- /dev/null +++ b/man/toolHarmonizePast.Rd @@ -0,0 +1,42 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/toolHarmonizePast.R +\name{toolHarmonizePast} +\alias{toolHarmonizePast} +\title{Harmonization tool Past} +\usage{ +toolHarmonizePast(past, future, method = "level", yEnd = 2100) +} +\arguments{ +\item{past}{A magpie object with only one scenario/datatype, i.e. the length of the third dimension should be 1.} + +\item{future}{A magpie object.} + +\item{method}{A string defining the harmonization method: +\itemize{ +\item "level": the harmonized time-series is exactly equal to 'future' in the years after the last year +of 'past'. +\item "growth": the harmonized time-series follows the same growth rates as 'future', in the years +after the last year of 'past'. +\item "transition": the harmonized time-series transitions to 'future' by the year 'yEnd'. After yEnd, the +harmonized time-series is equal to 'future'. In the transition phase, a share of the absolute difference +between past' and future' in the last year of past' is added to future. This share starts at 1 in the +last year of 'past' and decreases linearly to 0 by yEnd. +}} + +\item{yEnd}{Additional input for "transition" method. Year by which the transition period is completed.} +} +\value{ +A magpie object with the same dimensions as 'future'. +} +\description{ +Like all harmonization tools in mrdrivers, toolHarmonizePast takes two magpie objects, 'past' and 'future', +and returns a single magpie object, i.e. the harmonized time-series. In this case, the harmonized time-series is +always equal to 'past', in the years of 'past'. After that the harmonized time-series depends on the 'method' +argument chosen. +} +\section{Dimensions of 'past' and 'future'}{ +If the 'future' object has multiple scenarios/datatypes, i.e. the length of the third dimension is larger than 1, +then the a harmonized time-series is created for every scenario/datatype in future. The same 'past' object is used +in every case - hence the requirement that 'past' only have one scenario/datatype. +} + diff --git a/man/toolHarmonizePastGrFuture.Rd b/man/toolHarmonizePastGrFuture.Rd deleted file mode 100644 index 64e916b..0000000 --- a/man/toolHarmonizePastGrFuture.Rd +++ /dev/null @@ -1,28 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/toolHarmonizePastGrFuture.R -\name{toolHarmonizePastGrFuture} -\alias{toolHarmonizePastGrFuture} -\title{Harmonization tool PastGrFuture} -\usage{ -toolHarmonizePastGrFuture(past, future) -} -\arguments{ -\item{past}{A magpie object with only one scenario/datatype, i.e. the length of the third dimension should be 1.} - -\item{future}{A magpie object.} -} -\value{ -A magpie object with the same dimensions as 'future'. -} -\description{ -Like all harmonization tools in mrdrivers, toolHarmonizePastGrFuture takes two magpie objects, 'past' and 'future', -and returns a single magpie object, i.e. the harmonized time-series. In this case, the harmonized time-series is -equal to 'past', in the years of 'past', and then follows the same growth rates as 'future', for the years of -'future'. -} -\section{Dimensions of 'past' and 'future'}{ -If the 'future' object has multiple scenarios/datatypes, i.e. the length of the third dimension is larger than 1, -then the a harmonized time-series is created for every scenario/datatype in future. The same 'past' object is used -in every case - hence the requirement that 'past' only have one scenario/datatype. -} - diff --git a/man/toolHarmonizePastTransition.Rd b/man/toolHarmonizePastTransition.Rd deleted file mode 100644 index fb9f49e..0000000 --- a/man/toolHarmonizePastTransition.Rd +++ /dev/null @@ -1,35 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/toolHarmonizePastTransition.R -\name{toolHarmonizePastTransition} -\alias{toolHarmonizePastTransition} -\title{Harmonization tool PastTransition} -\usage{ -toolHarmonizePastTransition(past, future, yEnd, aslist = FALSE) -} -\arguments{ -\item{past}{A magpie object with only one scenario/datatype, i.e. the length of the third dimension should be 1.} - -\item{future}{A magpie object.} - -\item{yEnd}{An integer designating the last year of the transition period.} - -\item{aslist}{If TRUE (default is FALSE) then a list with the data object and a description field is returned, -insead of only the data object.} -} -\value{ -A magpie object with the same dimensions as 'future'. -} -\description{ -Like all harmonization tools in mrdrivers, toolHarmonizePastTransition takes two magpie objects, 'past' and 'future', -and returns a single magpie object, i.e. the harmonized time-series. In this case, the harmonized time-series is -equal to 'past', in the years of 'past', and then transitions to 'future' by the year 'yEnd'. After yEnd, the -harmonized time-series is equal to 'future'. In the transition phase, a share of the absolute difference between -'past' and future' in the last year of past' is added to future. This share starts at 1 in the last year of 'past' -and decreases linearly to 0 by yEnd. -} -\section{Dimensions of 'past' and 'future'}{ -If the 'future' object has multiple scenarios/datatypes, i.e. the length of the third dimension is larger than 1, -then the a harmonized time-series is created for every scenario/datatype in future. The same 'past' object is used -in every case - hence the requirement that 'past' only have one scenario/datatype. -} - diff --git a/tests/testthat/test-calcOutput_sets.R b/tests/testthat/test-calcOutput_sets.R index 768f85c..8432261 100644 --- a/tests/testthat/test-calcOutput_sets.R +++ b/tests/testthat/test-calcOutput_sets.R @@ -24,9 +24,9 @@ test_that("set names", { expectCorrectSetNames(calcOutput("Urban", extension2150 = "none")) expectCorrectSetNames(calcOutput("Labour", extension2150 = "none")) expectCorrectSetNames(calcOutput("GDPPast")) - expectCorrectSetNames(calcOutput("GDPFuture")) + expectCorrectSetNames(calcOutput("GDPFuture", GDPFuture = "SSPs", unit = "constant 2017 Int$PPP")) expectCorrectSetNames(calcOutput("GDPpcPast")) - expectCorrectSetNames(calcOutput("GDPpcFuture")) + expectCorrectSetNames(calcOutput("GDPpcFuture", GDPpcFuture = "SSPs", unit = "constant 2017 Int$PPP")) expectCorrectSetNames(calcOutput("GDP", extension2150 = "none")) expectCorrectSetNames(calcOutput("GDPpc", extension2150 = "none")) # I want to keep; expectCorrectSetNames(calcOutput("RatioPPP2MER")) @@ -41,10 +41,10 @@ calcs <- list("PopulationPast" = calcOutput("PopulationPast"), "UrbanFuture" = calcOutput("UrbanFuture"), "Urban" = calcOutput("Urban", extension2150 = "none"), "GDPpcPast" = calcOutput("GDPpcPast"), - "GDPpcFuture" = calcOutput("GDPpcFuture"), + "GDPpcFuture" = calcOutput("GDPpcFuture", GDPpcFuture = "SSPs", unit = "constant 2017 Int$PPP"), "GDPpc" = calcOutput("GDPpc", extension2150 = "none"), "GDPPast" = calcOutput("GDPPast"), - "GDPFuture" = calcOutput("GDPFuture"), + "GDPFuture" = calcOutput("GDPFuture", GDPFuture = "SSPs", unit = "constant 2017 Int$PPP"), "GDP" = calcOutput("GDP", extension2150 = "none"), "Labour" = calcOutput("Labour", extension2150 = "none")) @@ -55,10 +55,10 @@ calcs2 <- list("PopulationPast" = calcOutput("PopulationPast", aggregate = FALSE "UrbanFuture" = calcOutput("UrbanFuture", aggregate = FALSE), "Urban" = calcOutput("Urban", extension2150 = "none", aggregate = FALSE), "GDPpcPast" = calcOutput("GDPpcPast", aggregate = FALSE), - "GDPpcFuture" = calcOutput("GDPpcFuture", aggregate = FALSE), + "GDPpcFuture" = calcOutput("GDPpcFuture", GDPpcFuture = "SSPs", unit = "constant 2017 Int$PPP", aggregate = FALSE), "GDPpc" = calcOutput("GDPpc", extension2150 = "none", aggregate = FALSE), "GDPPast" = calcOutput("GDPPast", aggregate = FALSE), - "GDPFuture" = calcOutput("GDPFuture", aggregate = FALSE), + "GDPFuture" = calcOutput("GDPFuture", GDPFuture = "SSPs", unit = "constant 2017 Int$PPP", aggregate = FALSE), "GDP" = calcOutput("GDP", extension2150 = "none", aggregate = FALSE), "Labour" = calcOutput("Labour", extension2150 = "none", aggregate = FALSE)) @@ -69,10 +69,10 @@ calcs3 <- list("PopulationPast" = calcOutput("PopulationPast", supplementary = T "UrbanFuture" = calcOutput("UrbanFuture", supplementary = TRUE), "Urban" = calcOutput("Urban", extension2150 = "none", supplementary = TRUE), "GDPpcPast" = calcOutput("GDPpcPast", supplementary = TRUE), - "GDPpcFuture" = calcOutput("GDPpcFuture", supplementary = TRUE), + "GDPpcFuture" = calcOutput("GDPpcFuture", GDPpcFuture = "SSPs", unit = "constant 2017 Int$PPP", supplementary = TRUE), "GDPpc" = calcOutput("GDPpc", extension2150 = "none", supplementary = TRUE), "GDPPast" = calcOutput("GDPPast", supplementary = TRUE), - "GDPFuture" = calcOutput("GDPFuture", supplementary = TRUE), + "GDPFuture" = calcOutput("GDPFuture", GDPFuture = "SSPs", unit = "constant 2017 Int$PPP", supplementary = TRUE), "GDP" = calcOutput("GDP", extension2150 = "none", supplementary = TRUE), "Labour" = calcOutput("Labour", extension2150 = "none", supplementary = TRUE)) diff --git a/tests/testthat/test-scenarios.R b/tests/testthat/test-scenarios.R index 10043bc..611f49a 100644 --- a/tests/testthat/test-scenarios.R +++ b/tests/testthat/test-scenarios.R @@ -18,13 +18,14 @@ expectCorrectOutput <- function(x) { } test_that("All scenarios work", { - # SSPsOld is not compatible with average2020 (throws warning) - purrr::map(toolGetScenarioDefinition("GDPpc")$scenario[toolGetScenarioDefinition("GDPpc")$scenario != "SSPsOld"], + purrr::map(toolGetScenarioDefinition("GDPpc")$scenario, ~expectCorrectOutput(calcOutput("GDPpc", scenario = .x, extension2150 = "none"))) - expectCorrectOutput(calcOutput("GDPpc", scenario = "SSPsOld", average2020 = FALSE, extension2150 = "none")) purrr::map(toolGetScenarioDefinition("Population")$scenario, ~expectCorrectOutput(calcOutput("Population", scenario = .x, extension2150 = "none"))) - purrr::map(toolGetScenarioDefinition("GDP")$scenario[toolGetScenarioDefinition("GDP")$scenario != "SSPsOld"], + purrr::map(toolGetScenarioDefinition("GDP")$scenario, ~expectCorrectOutput(calcOutput("GDP", scenario = .x, extension2150 = "none"))) - expectCorrectOutput(calcOutput("GDP", scenario = "SSPsOld", average2020 = FALSE, extension2150 = "none")) + purrr::map(toolGetScenarioDefinition("Labour")$scenario, + ~expectCorrectOutput(calcOutput("Labour", scenario = .x, extension2150 = "none"))) + purrr::map(toolGetScenarioDefinition("Urban")$scenario, + ~expectCorrectOutput(calcOutput("Urban", scenario = .x, extension2150 = "none"))) }) diff --git a/tests/testthat/test-snaphsots.R b/tests/testthat/test-snaphsots.R index cf2d353..8414363 100644 --- a/tests/testthat/test-snaphsots.R +++ b/tests/testthat/test-snaphsots.R @@ -18,13 +18,11 @@ test_that("Default calcOutput Population calls", { }) test_that("Default calcOutput GDP calls", { - expect_snapshot_value(calcOutput("GDPFuture"), style = "json2") expect_snapshot_value(calcOutput("GDPPast"), style = "json2") expect_snapshot_value(calcOutput("GDP"), style = "json2") }) test_that("Default calcOutput GDPpc calls", { - expect_snapshot_value(calcOutput("GDPpcFuture"), style = "json2") expect_snapshot_value(calcOutput("GDPpcPast"), style = "json2") expect_snapshot_value(calcOutput("GDPpc"), style = "json2") }) diff --git a/vignettes/scenarios.Rmd b/vignettes/scenarios.Rmd index 045935c..eb14f78 100644 --- a/vignettes/scenarios.Rmd +++ b/vignettes/scenarios.Rmd @@ -29,7 +29,7 @@ library(magrittr) toolGetScenarioDefinition() %>% print(n = 200) ``` -So for example, there are currently 7 GDPpc scenarios available: the SSPs, the SDPs, SSP2EU, noCovid, longCovid, shortCovid and SSPsOld scenarios. +So for example, there are currently 4 GDPpc scenarios available: the SSPs, SSP2, the SDPs, and SSP2EU. # User Defined Scenarios @@ -38,7 +38,7 @@ The user can create custom scenarios by creating a tibble called "mrdivers_scena ```{r} mrdrivers_scenarios <- tibble::tribble( ~driver, ~scenario, ~pastData, ~futureData, ~harmonization, - "GDPpc", "nomi", "WDI", "SSPsOld", "calibSSPs", + "GDPpc", "nomi", "WDI", "SSPs", "calibSSPs", "Population", "nomi", "WDI", "SSPs", "withPEAPandFuture", "GDP", "nomi", "-", "-", "GDPpcWithPop" ) @@ -77,7 +77,7 @@ SSP2EU is based off of SSP2, but for EUR-Region countries, Eurostat data and sho Suggested overall references: K. C., S, 2020 ([link](https://pure.iiasa.ac.at/16710)) and Lutz et al., 2018 ([link](https://pure.iiasa.ac.at/id/eprint/15226/1/lutz_et_al_2018_demographic_and_human_capital.pdf)). -Detailed references: the SSP population scenarios are constructed using past WDI data ([link](https://databank.worldbank.org/source/world-development-indicators)) (filled in with UN_PopDiv data ([link](https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/EXCEL_FILES/2_Population/WPP2022_POP_F01_1_POPULATION_SINGLE_AGE_BOTH_SEXES.xlsx)) and MI data ([link](https://zenodo.org/record/4421504/files/MissingIslands.zip)), and SSP population projections (filled in with UN_PopDiv data ([link](https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/EXCEL_FILES/2_Population/WPP2022_POP_F01_1_POPULATION_SINGLE_AGE_BOTH_SEXES.xlsx)) and MI data ([link](https://zenodo.org/record/4421504/files/MissingIslands.zip)). The harmonization makes use of short term growth rates from UN_PopDiv ([link](https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/EXCEL_FILES/2_Population/WPP2022_POP_F01_1_POPULATION_SINGLE_AGE_BOTH_SEXES.xlsx)). For more details on the harmonization, see Koch and Leimbach 2023 ([link](https://doi.org/10.1016/j.ecolecon.2023.107751)). +Detailed references: the SSP population scenarios are constructed using past WDI data ([link](https://databank.worldbank.org/source/world-development-indicators)) (filled in with UN_PopDiv data ([link](https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/EXCEL_FILES/2_Population/WPP2022_POP_F01_1_POPULATION_SINGLE_AGE_BOTH_SEXES.xlsx)) and MI data ([link](https://zenodo.org/record/4421504/files/MissingIslands.zip)), and SSP population projections (filled in with UN_PopDiv data ([link](https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/EXCEL_FILES/2_Population/WPP2022_POP_F01_1_POPULATION_SINGLE_AGE_BOTH_SEXES.xlsx)) and MI data ([link](https://zenodo.org/record/4421504/files/MissingIslands.zip)). The harmonization makes use of short term growth rates from the World Bank's Population and Projections database ([link](https://databank.worldbank.org/source/population-estimates-and-projections#)). For more details on the harmonization, see Koch and Leimbach 2023 ([link](https://doi.org/10.1016/j.ecolecon.2023.107751)). ### SDPs