Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 2024 #83

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
ValidationKey: '2996376'
ValidationKey: '3971400'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
AcceptedNotes: ~
allowLinterWarnings: yes
AddInReadme: inst/README.md
enforceVersionUpdate: no
10 changes: 7 additions & 3 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -23,7 +23,6 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
gamstransfer=?ignore
any::lucode2
any::covr
any::madrat
Expand All @@ -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

Expand All @@ -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: |
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]",
Expand All @@ -22,7 +22,7 @@ Imports:
countrycode,
dplyr,
lifecycle,
GDPuc (>= 0.8.0),
GDPuc (>= 1.0.0),
glue,
magrittr,
purrr,
Expand All @@ -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
71 changes: 49 additions & 22 deletions R/calcDriver.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
}

Expand Down Expand Up @@ -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,
...)
)
}

Expand Down
33 changes: 12 additions & 21 deletions R/calcGDP.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()].
#' }
Expand All @@ -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"),
Expand All @@ -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
Expand All @@ -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)
}
Loading
Loading