forked from pik-piam/mrremind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove documentation warnings and cleanup DESCRIPTION
- Loading branch information
1 parent
a3f2939
commit 4a81f6d
Showing
11 changed files
with
94 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
|
@@ -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,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, | ||
|
@@ -82,4 +82,3 @@ VignetteBuilder: | |
Encoding: UTF-8 | ||
LazyData: no | ||
RoxygenNote: 7.3.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.