diff --git a/.buildlibrary b/.buildlibrary index 606224f3..fa4e83b1 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '34678800' +ValidationKey: '34700316' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/CITATION.cff b/CITATION.cff index bc47b161..36eb63ee 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: 'mrremind: MadRat REMIND Input Data Package' -version: 0.175.5 -date-released: '2024-02-07' +version: 0.175.6 +date-released: '2024-02-08' abstract: The mrremind packages contains data preprocessing for the REMIND model. authors: - family-names: Baumstark diff --git a/DESCRIPTION b/DESCRIPTION index 5bd1e7fe..6fc86b76 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: mrremind Title: MadRat REMIND Input Data Package -Version: 0.175.5 -Date: 2024-02-07 +Version: 0.175.6 +Date: 2024-02-08 Authors@R: c( person("Lavinia", "Baumstark", , "lavinia@pik-potsdam.de", role = c("aut", "cre")), person("Renato", "Rodrigues", role = "aut"), @@ -37,12 +37,12 @@ Description: The mrremind packages contains data preprocessing for the License: LGPL-3 | file LICENSE URL: https://github.com/pik-piam/mrremind Depends: - R (>= 2.10.0), + edgeTransport (>= 0.17.0), madrat (>= 3.7.1), magclass (>= 6.11.3), - mrdrivers (>= 1.0.0), mrcommons (>= 1.38.0), - edgeTransport (>= 0.17.0) + mrdrivers (>= 1.0.0), + R (>= 2.10.0) Imports: assertr, assertthat, @@ -60,6 +60,7 @@ Imports: plyr, purrr, quitte (>= 0.3105.0), + R.utils, readODS, readr, readxl, @@ -67,11 +68,10 @@ Imports: reshape2, rlang, rmndt, - R.utils, tibble, tidyr, tidyselect, - zoo, + zoo Suggests: covr, knitr, @@ -82,4 +82,3 @@ VignetteBuilder: Encoding: UTF-8 LazyData: no RoxygenNote: 7.3.1 - diff --git a/R/calcTheil.R b/R/calcTheil.R index bc9ed103..b8731756 100644 --- a/R/calcTheil.R +++ b/R/calcTheil.R @@ -1,69 +1,94 @@ #' Calculate regional Theil-T index #' -#' To calculate the regional Theil-T index (= correction to welfare function for a lognormal income distribution) we do the following: -#' (1) convert country-level Gini coefficients to Theil (2) calculate contribution to Theil-T index that includes both between-countries and within-country inequality -#' (see e.g. https://en.wikipedia.org/wiki/Theil_index). The latter can then be aggregated with calcOutput(). +#' To calculate the regional Theil-T index (= correction to welfare function for +#' a lognormal income distribution) we do the following: +#' (1) convert country-level Gini coefficients to Theil (2) calculate contribution +#' to Theil-T index that includes both between-countries and within-country inequality +#' (see e.g. https://en.wikipedia.org/wiki/Theil_index). The latter can then be +#' aggregated with calcOutput(). #' -#' NB 1: the aggregation depends on the region mapping. It is implemented such that the regionmapping specified in getConfig()$regionmapping is used. +#' NB 1: the aggregation depends on the region mapping. It is implemented such +#' that the regionmapping specified in getConfig()$regionmapping is used. #' -#' NB 2: the result of calcOutput('Theil', aggregate = FALSE), is NOT the country Theil-T, but the unweighted contribution from a given country to the regional value. +#' NB 2: the result of calcOutput('Theil', aggregate = FALSE), is NOT the country +#' Theil-T, but the unweighted contribution from a given country to the regional value. #' -#' @return magpie objects of unweighted contribution to Theil, weights (= country shares of regional GDP), docstring +#' @return magpie objects of unweighted contribution to Theil, +#' weights (= country shares of regional GDP), docstring #' @author Bjoern Soergel #' @seealso \code{\link{calcOutput}} \code{\link{convertGini},\link{readGini}} #' @examples -#' -#' \dontrun{ a <- calcOutput("Theil") +#' \dontrun{ +#' a <- calcOutput("Theil") #' } #' #' @importFrom stats qnorm -calcTheil <- function(){ - #Gini & Theil - Gini <- readSource('Gini') +calcTheil <- function() { + + ## helper functions. + TheilT.from.sigma <- function(sigma) { + # Theil T coefficient for lognormal distribution + TheilT <- sigma^2 / 2. + return(TheilT) + } + + sigma.from.Gini <- function(G) { + # assuming lognormal distribution: convert Gini to sigmas + sigma <- sqrt(2) * qnorm((G + 1) / 2) + return(sigma) + } + + # Gini and Theil + Gini <- readSource("Gini") years <- getYears(Gini) TheilT <- TheilT.from.sigma(sigma.from.Gini(Gini)) # population (in 1e6) - pop <- calcOutput(type = 'Population', aggregate = FALSE) - sspnames <- c(paste0('SSP',1:5),"SDP","SSP2EU") - pop <- pop[,years,paste0('pop_',sspnames)] + pop <- calcOutput(type = "Population", aggregate = FALSE) + sspnames <- c(paste0("SSP", 1:5), "SDP", "SSP2EU") + pop <- pop[, years, paste0("pop_", sspnames)] getNames(pop) <- sspnames - getSets(pop) <- c("iso3c","year","scenario") + getSets(pop) <- c("iso3c", "year", "scenario") # GDP (in million $ PPP 2005) GDPppp <- calcOutput(type = "GDP", aggregate = FALSE) - GDPnames <- paste0('gdp_',sspnames) - GDPppp <- GDPppp[,years,GDPnames] + GDPnames <- paste0("gdp_", sspnames) + GDPppp <- GDPppp[, years, GDPnames] getNames(GDPppp) <- sspnames - getSets(GDPppp) <- c("iso3c","year","scenario") + getSets(GDPppp) <- c("iso3c", "year", "scenario") # allocate empty objects for storing Theil contribution and weights contribTheilT <- pop - contribTheilT[,,] <- NA + contribTheilT[, , ] <- NA s_i <- pop - s_i[,,] <- NA + s_i[, , ] <- NA # contribution to Theil index depends on region mapping. We always use the one specified in getConfig(). - regionmapping <- read.csv(toolGetMapping(type = 'regional', name = getConfig()$regionmapping, - returnPathOnly = TRUE, where = 'mappingfolder'), - sep = ';', colClasses = 'character') + regionmapping <- read.csv( + toolGetMapping( + type = "regional", name = getConfig()$regionmapping, + returnPathOnly = TRUE, where = "mappingfolder" + ), + sep = ";", colClasses = "character" + ) + # GDP per capita - xbar_i <- GDPppp/pop - for (rr in unique(regionmapping$RegionCode)){ + xbar_i <- GDPppp / pop + for (rr in unique(regionmapping$RegionCode)) { rrCountries <- regionmapping$CountryCode[regionmapping$RegionCode == rr] # regional GDP per capita - GDPppp_rr <- dimSums(GDPppp[rrCountries,,], dim = 1) - Ntot_rr <- dimSums(pop[rrCountries,,], dim = 1) - xbar_rr <- GDPppp_rr/Ntot_rr + GDPppp_rr <- dimSums(GDPppp[rrCountries, , ], dim = 1) + Ntot_rr <- dimSums(pop[rrCountries, , ], dim = 1) + xbar_rr <- GDPppp_rr / Ntot_rr # contribution to Theil index (unweighted) - contribTheilT[rrCountries,,] <- TheilT[rrCountries,,] + log(xbar_i[rrCountries,,]/xbar_rr) + contribTheilT[rrCountries, , ] <- TheilT[rrCountries, , ] + log(xbar_i[rrCountries, , ] / xbar_rr) # weights = income share of country i: - # s_i = N_i/N * xbar_i/xbar = GDP_i/GDP_rr - s_i[rrCountries,,] <- GDPppp[rrCountries,,]/GDPppp_rr + # s_i = N_i/N * xbar_i/xbar = GDP_i/GDP_rr # nolint + s_i[rrCountries, , ] <- GDPppp[rrCountries, , ] / GDPppp_rr # sanity check: ensure that weights for a region sum to one (within floating point precision) - assertthat::assert_that(max(abs(dimSums(s_i[rrCountries,,],dim=1) - 1)) < 1e-10) + assertthat::assert_that(max(abs(dimSums(s_i[rrCountries, , ], dim = 1) - 1)) < 1e-10) } # for easier REMIND integration use same names as GDP scenarios for Theil @@ -71,18 +96,6 @@ calcTheil <- function(){ getNames(contribTheilT) <- GDPnames getNames(s_i) <- GDPnames - return(list(x = contribTheilT, weight = s_i, unit = "-", description = 'aggregated: Theil-T index, not-aggregated: unweighted contribution to Theil-T')) -} - -## helper functions. -TheilT.from.sigma <- function(sigma){ - # Theil T coefficient for lognormal distribution - TheilT <- sigma^2/2. - return(TheilT) -} - -sigma.from.Gini <- function(G){ - # assuming lognormal distribution: convert Gini to sigmas - sigma <- sqrt(2)*qnorm((G+1)/2) - return(sigma) + return(list(x = contribTheilT, weight = s_i, unit = "-", + description = "aggregated: Theil-T index, not-aggregated: unweighted contribution to Theil-T")) } diff --git a/R/convertStrefler.R b/R/convertStrefler.R index 981293ef..497e8c57 100644 --- a/R/convertStrefler.R +++ b/R/convertStrefler.R @@ -1,10 +1,10 @@ #' @title convertStrefler #' @description Converts data on enhanced weathering #' @param x unconverted magpie object from read-script +#' @param subtype data subtype. Either "weathering_graderegi", or "weathering_costs" #' #' @return magpie object with a completed dataset #' - convertStrefler <- function(x, subtype) { if (subtype == "weathering_graderegi") { diff --git a/R/imports.R b/R/imports.R index 0230759a..97d31d31 100644 --- a/R/imports.R +++ b/R/imports.R @@ -1,4 +1,4 @@ # Generated by lucode2: do not edit by hand -#' @import madrat magclass mrdrivers mrcommons edgeTransport +#' @import edgeTransport madrat magclass mrcommons mrdrivers NULL diff --git a/R/readIEA_ETP.R b/R/readIEA_ETP.R index 252b00da..a7b8f4da 100644 --- a/R/readIEA_ETP.R +++ b/R/readIEA_ETP.R @@ -2,7 +2,6 @@ #' #' @author Falk Benke #' @param subtype data subtype. Either "industry", "buildings", "summary", or "transport" -#' @param useCorrected boolean indicating if the corrected version of the input sheet should be used #' @importFrom tibble tibble #' @importFrom dplyr bind_rows bind_cols select mutate #' @importFrom readxl read_xlsx @@ -14,6 +13,7 @@ readIEA_ETP <- function(subtype) { region <- NULL + # nolint start subtypes <- list( industry = { list( @@ -518,7 +518,7 @@ readIEA_ETP <- function(subtype) { ) } ) - + # nolint end # ---- guardians ---- if (!subtype %in% names(subtypes)) { stop( @@ -564,8 +564,8 @@ readIEA_ETP <- function(subtype) { # set all 2055 data (for RTS/OECD/Chemicals with feedstocks) to NA due to faulty data in source if (subtype == "industry") { tmp[, 2055, "RTS.Industry|Chemicals and petrochemicals - final energy consumption and chemical feedstock|", - pmatch = T - ]["OECD",,] <- NA + pmatch = TRUE + ]["OECD", , ] <- NA } return(tmp) diff --git a/README.md b/README.md index 6d3d6035..f938403b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # MadRat REMIND Input Data Package -R package **mrremind**, version **0.175.5** +R package **mrremind**, version **0.175.6** [![CRAN status](https://www.r-pkg.org/badges/version/mrremind)](https://cran.r-project.org/package=mrremind) [![R build status](https://github.com/pik-piam/mrremind/workflows/check/badge.svg)](https://github.com/pik-piam/mrremind/actions) [![codecov](https://codecov.io/gh/pik-piam/mrremind/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrremind) [![r-universe](https://pik-piam.r-universe.dev/badges/mrremind)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Lavinia Baumstark . +Baumstark L, Rodrigues R, Levesque A, Oeser J, Bertram C, Mouratiadou I, Malik A, Schreyer F, Soergel B, Rottoli M, Mishra A, Dirnaichner A, Pehl M, Giannousakis A, Klein D, Strefler J, Feldhaus L, Brecha R, Rauner S, Dietrich J, Bi S, Benke F, Weigmann P, Richters O, Hasse R, Fuchs S, Mandaroux R (2024). _mrremind: MadRat REMIND Input Data Package_. R package version 0.175.6, . A BibTeX entry for LaTeX users is @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is title = {mrremind: MadRat REMIND Input Data Package}, author = {Lavinia Baumstark and Renato Rodrigues and Antoine Levesque and Julian Oeser and Christoph Bertram and Ioanna Mouratiadou and Aman Malik and Felix Schreyer and Bjoern Soergel and Marianna Rottoli and Abhijeet Mishra and Alois Dirnaichner and Michaja Pehl and Anastasis Giannousakis and David Klein and Jessica Strefler and Lukas Feldhaus and Regina Brecha and Sebastian Rauner and Jan Philipp Dietrich and Stephen Bi and Falk Benke and Pascal Weigmann and Oliver Richters and Robin Hasse and Sophie Fuchs and Rahel Mandaroux}, year = {2024}, - note = {R package version 0.175.5}, + note = {R package version 0.175.6}, url = {https://github.com/pik-piam/mrremind}, } ``` diff --git a/man/calcTheil.Rd b/man/calcTheil.Rd index 51445d3a..4a304f05 100644 --- a/man/calcTheil.Rd +++ b/man/calcTheil.Rd @@ -7,21 +7,27 @@ calcTheil() } \value{ -magpie objects of unweighted contribution to Theil, weights (= country shares of regional GDP), docstring +magpie objects of unweighted contribution to Theil, +weights (= country shares of regional GDP), docstring } \description{ -To calculate the regional Theil-T index (= correction to welfare function for a lognormal income distribution) we do the following: -(1) convert country-level Gini coefficients to Theil (2) calculate contribution to Theil-T index that includes both between-countries and within-country inequality -(see e.g. https://en.wikipedia.org/wiki/Theil_index). The latter can then be aggregated with calcOutput(). +To calculate the regional Theil-T index (= correction to welfare function for +a lognormal income distribution) we do the following: +(1) convert country-level Gini coefficients to Theil (2) calculate contribution +to Theil-T index that includes both between-countries and within-country inequality +(see e.g. https://en.wikipedia.org/wiki/Theil_index). The latter can then be +aggregated with calcOutput(). } \details{ -NB 1: the aggregation depends on the region mapping. It is implemented such that the regionmapping specified in getConfig()$regionmapping is used. +NB 1: the aggregation depends on the region mapping. It is implemented such +that the regionmapping specified in getConfig()$regionmapping is used. -NB 2: the result of calcOutput('Theil', aggregate = FALSE), is NOT the country Theil-T, but the unweighted contribution from a given country to the regional value. +NB 2: the result of calcOutput('Theil', aggregate = FALSE), is NOT the country +Theil-T, but the unweighted contribution from a given country to the regional value. } \examples{ - -\dontrun{ a <- calcOutput("Theil") +\dontrun{ +a <- calcOutput("Theil") } } diff --git a/man/convertStrefler.Rd b/man/convertStrefler.Rd index 0363b268..c26a6d36 100644 --- a/man/convertStrefler.Rd +++ b/man/convertStrefler.Rd @@ -8,6 +8,8 @@ convertStrefler(x, subtype) } \arguments{ \item{x}{unconverted magpie object from read-script} + +\item{subtype}{data subtype. Either "weathering_graderegi", or "weathering_costs"} } \value{ magpie object with a completed dataset diff --git a/man/readIEA_ETP.Rd b/man/readIEA_ETP.Rd index 12cb029a..19f616fe 100644 --- a/man/readIEA_ETP.Rd +++ b/man/readIEA_ETP.Rd @@ -8,8 +8,6 @@ readIEA_ETP(subtype) } \arguments{ \item{subtype}{data subtype. Either "industry", "buildings", "summary", or "transport"} - -\item{useCorrected}{boolean indicating if the corrected version of the input sheet should be used} } \description{ Read IEA ETP projections