Skip to content

Commit

Permalink
Refactor and change default scenarios
Browse files Browse the repository at this point in the history
Replace SSP2EU with a placeholder, as it no longer a desired scenario, but required in the input preparation pipeline. Remove completely in next release.
Add ADB sources and scenarios (not returned by default yet).
Urban pop share scenarios are improved, so as to never exceed 1, even with bezier extensions.
SSP labour scenarios are now also filled in with UN_popDiv data.
SSP population scenarios now use short term growth rates from PEAP-UN_PopDiv, instead of PEAP-MI, improving the shorterm projections of the smaller countries.
Refactor function arguments that did not comply with lintr rules (fixes pik-piam#13).
Mark "scenario.indicator" naming option as deprecated and remove in next release.
Fix all linter warnings.
Improve scenario documentation and references in vignette (fixes pik-piam#56 and fixes pik-piam#57).
Improve and simplify documentation (fixes pik-piam#16).
Remove "unit" from most functions, assuming instead that the default dollar unit is used throughout the package.
Clean up use of James data set for filling and backwards extension to 1960. These two functions are now cleanly separated from each other. Also, use of the James2019 dataset is dropped (fixes pik-piam#62), due to the fact that there is no reference for this dataset. This does not affect the scenarios much since the dataset is mainly used for its growth rates, and they shouldn't differ to much.
Refactor toolReduce and toolFillWith. toolFillWith now writes to the log which countries are filled in.
Export some of the more useful tool functions.
Speed up bezier extension (fixes pik-piam#78).
Remove MI from future datasets and make it clear that we assume constant extrapolation for most of the MI countries.
Fixes pik-piam#52.
  • Loading branch information
johanneskoch94 committed Oct 16, 2024
1 parent bd20ba9 commit 84616dc
Show file tree
Hide file tree
Showing 95 changed files with 1,865 additions and 2,650 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
AcceptedNotes: ~
allowLinterWarnings: yes
allowLinterWarnings: no
AddInReadme: inst/README.md
enforceVersionUpdate: no
10 changes: 3 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Depends:
Imports:
countrycode,
dplyr,
lifecycle,
glue,
magrittr,
purrr,
Expand All @@ -31,20 +30,17 @@ Imports:
rlang,
tibble,
tidyr,
tidyselect,
zoo
tidyselect
Suggests:
covr,
crayon,
eurostat,
kableExtra,
knitr,
pander,
rmarkdown,
testthat (>= 3.0.0),
WDI,
withr (>= 2.4.2),
yaml
yaml,
zoo
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Expand Down
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Generated by roxygen2: do not edit by hand

export(toolGeneralConvert)
export(toolGetScenarioDefinition)
export(toolGetUnitDollar)
export(toolHarmonizeFuture)
export(toolHarmonizePast)
exportPattern("^((calc(GDP|GDPpc|Population|Labour|Urban)(Past|Future|$))|read|download)")
import(GDPuc)
import(madrat)
import(magclass)
importFrom(glue,glue)
importFrom(lifecycle,deprecate_soft)
importFrom(magrittr,"%>%")
importFrom(rlang,.data)
284 changes: 99 additions & 185 deletions R/calcDriver.R

Large diffs are not rendered by default.

100 changes: 86 additions & 14 deletions R/calcGDP.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#' \itemize{
#' \item the SSPs, i.e. SSP1-5
#' \item the SDPs, i.e. SDP, SDP_EI, SDP_RC, and SDP_MC
#' \item SSP2EU
#' }
#'
#' See the vignette: \code{vignette("scenarios")} for scenario options, definitions and references.
Expand All @@ -18,8 +17,9 @@
#'
#' @param unit A string specifying the unit of GDP. Can be either:
#' \itemize{
#' \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
#' \item "`r toolGetUnitDollar(inPPP = TRUE)`" (default):
#' Scenarios are constructed in `r toolGetUnitDollar(inPPP = TRUE)`.
#' \item "`r toolGetUnitDollar()`": Scenarios are constructed in `r toolGetUnitDollar()` and then converted with
#' [GDPuc::toolConvertGDP()].
#' }
#' In all cases, GDP is returned in millions.
Expand All @@ -28,7 +28,6 @@
#' the yearly resolution is decreased to 5 year intervals.
#'
#' @param ... Arguments passed on to [calcDriver()], of which "extension2150" and "naming" are most often of interest.
#' Other [calcDriver()] arguments are used for scenario fine-tuning and by package developers.
#'
#' @inherit madrat::calcOutput return
#' @seealso \itemize{
Expand All @@ -43,12 +42,12 @@
#' calcOutput("GDP")
#' calcOutput("GDPpc")
#'
#' # Return only the SSP2EU GDP scenario
#' calcOutput("GDP", scenario = "SSP2EU")
#' # Return only the SSP2 GDP scenario
#' calcOutput("GDP", scenario = "SSP2")
#' }
#'
calcGDP <- function(scenario = c("SSPs", "SDPs", "SSP2EU"),
unit = "constant 2017 Int$PPP",
unit = toolGetUnitDollar(inPPP = TRUE),
average2020 = TRUE,
...) {
# Check user input
Expand All @@ -58,7 +57,6 @@ calcGDP <- function(scenario = c("SSPs", "SDPs", "SSP2EU"),
gdp <- calcOutput("Driver",
driver = "GDP",
scenario = scenario,
unit = "constant 2017 Int$PPP",
aggregate = FALSE,
supplementary = TRUE,
...)
Expand All @@ -85,13 +83,87 @@ calcGDP <- function(scenario = c("SSPs", "SDPs", "SSP2EU"),
if (grepl("US\\$MER", unit)) {
# Convert by interpolating and extrapolating missing conversion factors when possible.
gdp$x <- GDPuc::toolConvertGDP(gdp$x,
unit_in = "constant 2017 Int$PPP",
unit_out = "constant 2017 US$MER",
replace_NAs = c("linear", "no_conversion"))
unit_in = toolGetUnitDollar(inPPP = TRUE),
unit_out = toolGetUnitDollar(inPPP = FALSE),
replace_NAs = c("linear", "no_conversion"))
}
# Shifting to non-default-base-year prices, using only the US deflator for all countries, and neglecting any changes
# in PPPs or MERs.
if (!grepl(toolGetUnitDollar(returnOnlyBase = TRUE), unit)) {
gdp$x <- gdp$x * GDPuc::toolConvertSingle(1, "USA", unit_in = toolGetUnitDollar(inPPP = TRUE), unit_out = unit)
}
# 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)
}


#' @rdname calcGDP
#' @examples \dontrun{
#' calcOutput("GDPpc")
#' }
#'
calcGDPpc <- function(scenario = c("SSPs", "SDPs", "SSP2EU"),
unit = toolGetUnitDollar(inPPP = TRUE),
average2020 = TRUE,
...) {
# Check user input
toolCheckUserInput(driver = "GDPpc", args = c(list(...), as.list(environment())))

# GDPpc scenarios are constructed in 2017 Int$PPP, and converted, if necessary, at the end.
gdppc <- calcOutput("Driver",
driver = "GDPpc",
scenario = scenario,
popAsWeight = TRUE,
aggregate = FALSE,
supplementary = TRUE,
...)

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
# 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
# GDP, it has to calculated from GDP and population. (In other words we can't just use the same formula as for GDP,
# since it would lead to inconsistency at the end.) This is a bit hacky...
gdp2020 <- calcOutput("GDP",
scenario = scenario,
naming = "scenario",
extension2150 = "none",
aggregate = FALSE,
years = 2020)
pop2020 <- calcOutput("Population",
scenario = scenario,
naming = "scenario",
extension2150 = "none",
aggregate = FALSE,
years = 2020)
gdppc2020 <- gdp2020 / pop2020
gdppc2020[is.nan(gdppc2020)] <- 0
getNames(gdppc2020) <- getNames(gdppc$x)
gdppc$x[, 2020, ] <- gdppc2020
gdppc$description <- paste(gdppc$description, "|| 2020 value averaged over 2018-2022 time period.")
# Return only 5 year time steps, since the yearly data around 2020 is not connected to the 2020 value anymore.
years5ts <- getYears(gdppc$x, as.integer = TRUE)[getYears(gdppc$x, as.integer = TRUE) %% 5 == 0 &
getYears(gdppc$x, as.integer = TRUE) != 1960]
gdppc$x <- gdppc$x[, years5ts, ]
gdppc$weight <- gdppc$weight[, years5ts, ]
gdppc$description <- paste(gdppc$description, "5 year time steps only.")
message("The 2020 value is an an avergae over the 2018-2022 time period!!")
}

# Convert to US$MER if required
if (grepl("US\\$MER", unit)) {
# Convert by interpolating and extrapolating missing conversion factors when possible.
gdppc$x <- GDPuc::toolConvertGDP(gdppc$x,
unit_in = toolGetUnitDollar(inPPP = TRUE),
unit_out = toolGetUnitDollar(inPPP = FALSE),
replace_NAs = c("linear", "no_conversion"))
}
# Shifting to non-default-base-year prices, using only the US deflator for all countries, and neglecting any changes
# in PPPs or MERs.
if (!grepl(toolGetUnitDollar(returnOnlyBase = TRUE), unit)) {
gdp$x <- gdp$x * GDPuc::toolConvertSingle(1, "USA", unit_in = toolGetUnitDollar(inPPP = TRUE), unit_out = unit)
}

list(x = gdppc$x, weight = gdppc$weight, unit = unit, description = gdppc$description)
}
Loading

0 comments on commit 84616dc

Please sign in to comment.