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

Add function calcConsShare to compute subregional consumption shares #504

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '35780400'
ValidationKey: '35982800'
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.180.0
date-released: '2024-06-04'
version: 0.181.0
date-released: '2024-06-06'
abstract: The mrremind packages contains data preprocessing for the REMIND model.
authors:
- family-names: Baumstark
Expand Down
4 changes: 2 additions & 2 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.180.0
Date: 2024-06-04
Version: 0.181.0
Date: 2024-06-06
Authors@R: c(
person("Lavinia", "Baumstark", , "[email protected]", role = c("aut", "cre")),
person("Renato", "Rodrigues", role = "aut"),
Expand Down
86 changes: 86 additions & 0 deletions R/calcConsShare.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#' Calculate the consumption shares for each of the 10 deciles across different regions
#'
#' This function calculates the consumption shares for sub-regional deciles within total consumption
#' across REMIND regions. It assumes a log-normal distribution for sub-regional consumption.
#' The calculation proceeds in three steps:
#' 1. Calculate 'sigma' (the variance of the distribution) from regional Theil index.
#' 2. Generate regional distribution with 'sigma', and integrate and normalize to compute
#' the relative share of each decile group.
#' 3. Assign regional values to individual countries within each region and set the
#' aggregation rule for calcOutput to calculate the average.
#'
#' @return magpie objects of regional consumption shares for each of the 10 deciles.
#' weights = 1, such that the aggregation later takes country average
#' @author Jiarui Zhong
#' @seealso \code{\link{calcOutput}} \code{\link{convertGini},\link{readGini}.\link{calcTheil}}
#' @examples
#' \dontrun{
#' a <- calcOutput("ConsShare")
#' }
#'

calcConsShare <- function() {

# Get regional Theil index, regional mapping in getConfig() is used.
theilT <- calcOutput(type = "Theil", aggregate = TRUE)

# Calculate regional sigma from Theil
sigma <- sqrt(2 * theilT)

# helper function compute 10 income group share given sigma
decShareFromSigma <- function(sigma) {
# assume any value of mu, which doesn't affect the result
mu <- 1

# decile boundaries
deciles <- stats::qlnorm(seq(0, 1, by = 0.1), meanlog = mu, sdlog = sigma)

# function for the integrand x * f(x)
integrand <- function(x) {
x * stats::dlnorm(x, meanlog = mu, sdlog = sigma)
}

# Compute shares by integrating over each decile range
incomeShares <- numeric(length(deciles) - 1)
for (i in 2:length(deciles)) {
incomeShares[i - 1] <- stats::integrate(integrand, lower = deciles[i - 1], upper = deciles[i])$value
}

# Normalize
normalizedShares <- incomeShares / sum(incomeShares)

return(normalizedShares)
}

consShare <- array(NA, dim = c(dim(sigma), 10),
dimnames = c(dimnames(sigma), list("decile" = 1:10)))

# Applying function across each Theil
for (i in seq_len(dim(sigma)[1])) {
for (j in seq_len(dim(sigma)[2])) {
for (k in seq_len(dim(sigma)[3])) {
consShare[i, j, k, ] <- decShareFromSigma(sigma[i, j, k])
}
}
}

consShare <- as.magpie(consShare)

# assign regional value to sub-regional countries
mapping <- toolGetMapping(
type = "regional", name = getConfig()$regionmapping,
returnPathOnly = FALSE, where = "mappingfolder"
)

consShareCountry <- toolAggregate(consShare, mapping, from = "RegionCode", to = "CountryCode")

weight <- consShareCountry
weight[ , , ] <- 1

return(list(
x = consShareCountry,
weight = weight,
unit = "Dimentionless",
description = "Consumption share of 10 sub-regional income deciles for Remind regions,
assigned to subregional countries"))
}
1 change: 1 addition & 0 deletions R/fullREMIND.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ fullREMIND <- function() {
calcOutput("GDP", years = rem_years_hist, round = 8, file = "f50_gdp.cs3r", aggregate = FALSE)
calcOutput("TCdamage", subtype = "const", round = 8, file = "f50_TC_df_const.cs4r", aggregate = FALSE)
calcOutput("TCdamage", subtype = "tasK", round = 8, file = "f50_TC_df_tasK.cs4r", aggregate = FALSE)
calcOutput("ConsShare", round = 8, file = "f_consShare.cs4r")

#-------------- emission parameter ------------------------------------------------------------------
calcOutput("EconometricEmiParameter", round = 5, file = "p_emineg_econometric.cs3r")
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.180.0**
R package **mrremind**, version **0.181.0**

[![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, Koch J (2024). _mrremind: MadRat REMIND Input Data Package_. R package version 0.180.0, <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, Koch J (2024). _mrremind: MadRat REMIND Input Data Package_. R package version 0.181.0, <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 and Johannes Koch},
year = {2024},
note = {R package version 0.180.0},
note = {R package version 0.181.0},
url = {https://github.com/pik-piam/mrremind},
}
```
34 changes: 34 additions & 0 deletions man/calcConsShare.Rd

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

Loading