Skip to content

Commit

Permalink
remove documentation warnings and cleanup DESCRIPTION
Browse files Browse the repository at this point in the history
  • Loading branch information
fbenke-pik committed Feb 8, 2024
1 parent a3f2939 commit 4a81f6d
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -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'
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: '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
Expand Down
15 changes: 7 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre")),
person("Renato", "Rodrigues", role = "aut"),
Expand Down Expand Up @@ -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,
Expand All @@ -60,18 +60,18 @@ Imports:
plyr,
purrr,
quitte (>= 0.3105.0),
R.utils,
readODS,
readr,
readxl,
reshape,
reshape2,
rlang,
rmndt,
R.utils,
tibble,
tidyr,
tidyselect,
zoo,
zoo
Suggests:
covr,
knitr,
Expand All @@ -82,4 +82,3 @@ VignetteBuilder:
Encoding: UTF-8
LazyData: no
RoxygenNote: 7.3.1

105 changes: 59 additions & 46 deletions R/calcTheil.R
Original file line number Diff line number Diff line change
@@ -1,88 +1,101 @@
#' 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
# change this if we later want to test effect of per capita income growth vs. inequality
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"))
}
2 changes: 1 addition & 1 deletion R/convertStrefler.R
Original file line number Diff line number Diff line change
@@ -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") {
Expand Down
2 changes: 1 addition & 1 deletion R/imports.R
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions R/readIEA_ETP.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,6 +13,7 @@ readIEA_ETP <- function(subtype) {

region <- NULL

# nolint start
subtypes <- list(
industry = {
list(
Expand Down Expand Up @@ -518,7 +518,7 @@ readIEA_ETP <- function(subtype) {
)
}
)

# nolint end
# ---- guardians ----
if (!subtype %in% names(subtypes)) {
stop(
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Lavinia Baumstark <lavinia@pik-po

To cite package **mrremind** in publications use:

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.5, <https://github.com/pik-piam/mrremind>.
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, <https://github.com/pik-piam/mrremind>.

A BibTeX entry for LaTeX users is

Expand All @@ -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},
}
```
22 changes: 14 additions & 8 deletions man/calcTheil.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/convertStrefler.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions man/readIEA_ETP.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4a81f6d

Please sign in to comment.