Skip to content

Commit

Permalink
add price anticipation deviation criterion to convergence plot
Browse files Browse the repository at this point in the history
  • Loading branch information
fbenke-pik committed Jan 19, 2024
1 parent 6aa13d3 commit e1dc041
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '222856062'
ValidationKey: '222943560'
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: 'remind2: The REMIND R package (2nd generation)'
version: 1.129.3
date-released: '2024-01-12'
version: 1.129.4
date-released: '2024-01-18'
abstract: Contains the REMIND-specific routines for data and model output manipulation.
authors:
- family-names: Rodrigues
Expand Down
7 changes: 3 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: remind2
Title: The REMIND R package (2nd generation)
Version: 1.129.3
Date: 2024-01-12
Version: 1.129.4
Date: 2024-01-18
Authors@R: c(
person("Renato", "Rodrigues", , "[email protected]", role = c("aut", "cre")),
person("Lavinia", "Baumstark", role = "aut"),
Expand Down Expand Up @@ -51,7 +51,6 @@ Imports:
gdxrrw,
ggplot2,
gms,
gridExtra,
htmltools,
iamc,
knitr,
Expand Down Expand Up @@ -89,6 +88,6 @@ Suggests:
VignetteBuilder:
knitr
Encoding: UTF-8
RoxygenNote: 7.2.3
RoxygenNote: 7.3.0
Config/testthat/parallel: true
Config/testthat/edition: 3
193 changes: 113 additions & 80 deletions R/plotNashConvergence.R
Original file line number Diff line number Diff line change
Expand Up @@ -296,102 +296,40 @@ plotNashConvergence <- function(gdx) { # nolint cyclocomp_linter
surplusSummaryPlotly <- ggplotly(surplusSummary, tooltip = c("text"))
subplots <- append(subplots, list(surplusSummaryPlotly))

# Price anticipation ----
# Deviation due to price anticipation ----

cmMaxFadeoutPriceAnticip <- as.vector(readGDX(gdx, name = "cm_maxFadeoutPriceAnticip", react = "error"))
p80FadeoutPriceAnticipIter <- readGDX(gdx, name = "p80_fadeoutPriceAnticip_iter",
restore_zeros = FALSE, react = "error") %>%
as.quitte() %>%
select("iteration", "fadeoutPriceAnticip" = "value")
maxTolerance <- readGDX(gdx,
name = "p80_surplusMaxTolerance",
restore_zeros = FALSE, react = "error"
)[, , "good"] %>%
as.numeric()

data <- p80FadeoutPriceAnticipIter %>%
data <- readGDX(gdx, name = "p80_DevPriceAnticipGlobAllMax2100Iter",
restore_zeros = FALSE, react = "error") %>%
as.quitte() %>%
select("iteration", "value") %>%
mutate(
"iteration" := as.numeric(.data$iteration),
"converged" = ifelse(.data$fadeoutPriceAnticip > cmMaxFadeoutPriceAnticip, "no", "yes"),
"tooltip" = ifelse(
.data$converged == "yes",
paste0(
"Converged<br>Price Anticipation fade out is low enough<br>",
round(.data$fadeoutPriceAnticip, 5), " <= ", cmMaxFadeoutPriceAnticip
),
paste0(
"Not converged<br>Price Anticipation fade out is not low enough<br>",
round(.data$fadeoutPriceAnticip, 5), " > ", cmMaxFadeoutPriceAnticip
)
)
"converged" = ifelse(.data$value > 0.1 * maxTolerance, "no", "yes"),
"text" = "hallo"
)

priceAnticipation <- ggplot(data, aes_(x = ~iteration)) +
geom_line(aes_(y = ~fadeoutPriceAnticip), alpha = 0.3, linewidth = aestethics$line$size) +

priceAnticipationDeviation <- ggplot(data, aes_(x = ~iteration)) +
suppressWarnings(geom_point(
size = 2,
aes_(y = 0.0001, fill = ~converged, text = ~tooltip),
alpha = aestethics$alpha
)) +
theme_minimal() +
scale_fill_manual(values = booleanColor) +
scale_y_continuous(breaks = c(0.0001), labels = c("Price\nAnticipation")) +
scale_y_continuous(breaks = c(0.0001), labels = c("Price Anticipation\nDeviation")) +
scale_x_continuous(breaks = c(data$iteration)) +
labs(x = NULL, y = NULL) +
coord_cartesian(ylim = c(-0.2, 1))

priceAnticipationPlotly <- ggplotly(priceAnticipation, tooltip = c("text"))
subplots <- append(subplots, list(priceAnticipationPlotly))

# Tax Convergence (optional) ----

cmTaxConvCheck <- as.vector(readGDX(gdx, name = "cm_TaxConvCheck", react = "error"))

p80ConvNashTaxrevIter <- readGDX(gdx, name = "p80_convNashTaxrev_iter", restore_zeros = FALSE, react = "error") %>%
as.quitte() %>%
select("region", "period", "iteration", "value") %>%
mutate("failed" = abs(.data$value) > 1e-4)

data <- p80ConvNashTaxrevIter %>%
group_by(.data$iteration) %>%
summarise(converged = ifelse(any(.data$failed == TRUE), "no", "yes")) %>%
mutate("tooltip" = "Converged")

for (i in unique(p80ConvNashTaxrevIter$iteration)) {
if (data[data$iteration == i, "converged"] == "no") {
tmp <- filter(p80ConvNashTaxrevIter, .data$iteration == i, .data$failed == TRUE) %>%
mutate("item" = paste0(.data$region, " ", .data$period)) %>%
select("region", "period", "item") %>%
distinct()

if (nrow(tmp) > 10) {
data[data$iteration == i, "tooltip"] <- paste0(
"Iteration ", i, " ",
"not converged:<br>",
paste0(unique(tmp$region), collapse = ", "),
"<br>",
paste0(unique(tmp$period), collapse = ", ")
)
} else {
data[data$iteration == i, "tooltip"] <- paste0(
"Iteration ", i, " ",
"not converged:<br>",
paste0(unique(tmp$item), collapse = ", ")
)
}
}
}

yLabel <- ifelse(cmTaxConvCheck == 0, "Tax Convergence\n(inactive)", "Tax Convergence")

taxConvergence <- suppressWarnings(ggplot(data, aes_(
x = ~iteration, y = yLabel,
fill = ~converged, text = ~tooltip
))) +
geom_hline(yintercept = 0) +
theme_minimal() +
geom_point(size = 2, alpha = aestethics$alpha) +
scale_fill_manual(values = booleanColor) +
scale_y_discrete(breaks = c(yLabel), drop = FALSE) +
labs(x = NULL, y = NULL)

taxConvergencePlotly <- ggplotly(taxConvergence, tooltip = c("text"))
subplots <- append(subplots, list(taxConvergencePlotly))
priceAnticipationDeviation <- ggplotly(priceAnticipationDeviation, tooltip = c("text"))
subplots <- append(subplots, list(priceAnticipationDeviation))

# Emission Market Deviation (optional) ----

Expand Down Expand Up @@ -580,9 +518,104 @@ plotNashConvergence <- function(gdx) { # nolint cyclocomp_linter

damageInternalizationPlotly <- ggplotly(damageInternalization, tooltip = c("text"))
subplots <- append(subplots, list(damageInternalizationPlotly))
}

# Tax Convergence (optional) ----

cmTaxConvCheck <- as.vector(readGDX(gdx, name = "cm_TaxConvCheck", react = "error"))

p80ConvNashTaxrevIter <- readGDX(gdx, name = "p80_convNashTaxrev_iter", restore_zeros = FALSE, react = "error") %>%
as.quitte() %>%
select("region", "period", "iteration", "value") %>%
mutate("failed" = abs(.data$value) > 1e-4)

data <- p80ConvNashTaxrevIter %>%
group_by(.data$iteration) %>%
summarise(converged = ifelse(any(.data$failed == TRUE), "no", "yes")) %>%
mutate("tooltip" = "Converged")

for (i in unique(p80ConvNashTaxrevIter$iteration)) {
if (data[data$iteration == i, "converged"] == "no") {
tmp <- filter(p80ConvNashTaxrevIter, .data$iteration == i, .data$failed == TRUE) %>%
mutate("item" = paste0(.data$region, " ", .data$period)) %>%
select("region", "period", "item") %>%
distinct()

if (nrow(tmp) > 10) {
data[data$iteration == i, "tooltip"] <- paste0(
"Iteration ", i, " ",
"not converged:<br>",
paste0(unique(tmp$region), collapse = ", "),
"<br>",
paste0(unique(tmp$period), collapse = ", ")
)
} else {
data[data$iteration == i, "tooltip"] <- paste0(
"Iteration ", i, " ",
"not converged:<br>",
paste0(unique(tmp$item), collapse = ", ")
)
}
}
}

yLabel <- ifelse(cmTaxConvCheck == 0, "Tax Convergence\n(inactive)", "Tax Convergence")

taxConvergence <- suppressWarnings(ggplot(data, aes_(
x = ~iteration, y = yLabel,
fill = ~converged, text = ~tooltip
))) +
geom_hline(yintercept = 0) +
theme_minimal() +
geom_point(size = 2, alpha = aestethics$alpha) +
scale_fill_manual(values = booleanColor) +
scale_y_discrete(breaks = c(yLabel), drop = FALSE) +
labs(x = NULL, y = NULL)

taxConvergencePlotly <- ggplotly(taxConvergence, tooltip = c("text"))
subplots <- append(subplots, list(taxConvergencePlotly))

# Price anticipation (optional) ----

cmMaxFadeoutPriceAnticip <- as.vector(readGDX(gdx, name = "cm_maxFadeoutPriceAnticip", react = "error"))
p80FadeoutPriceAnticipIter <- readGDX(gdx, name = "p80_fadeoutPriceAnticip_iter",
restore_zeros = FALSE, react = "error") %>%
as.quitte() %>%
select("iteration", "fadeoutPriceAnticip" = "value")

data <- p80FadeoutPriceAnticipIter %>%
mutate(
"iteration" := as.numeric(.data$iteration),
"converged" = ifelse(.data$fadeoutPriceAnticip > cmMaxFadeoutPriceAnticip, "no", "yes"),
"tooltip" = ifelse(
.data$converged == "yes",
paste0(
"Converged<br>Price Anticipation fade out is low enough<br>",
round(.data$fadeoutPriceAnticip, 5), " <= ", cmMaxFadeoutPriceAnticip
),
paste0(
"Not converged<br>Price Anticipation fade out is not low enough<br>",
round(.data$fadeoutPriceAnticip, 5), " > ", cmMaxFadeoutPriceAnticip
)
)
)

priceAnticipation <- ggplot(data, aes_(x = ~iteration)) +
geom_line(aes_(y = ~fadeoutPriceAnticip), alpha = 0.3, linewidth = aestethics$line$size) +
suppressWarnings(geom_point(
size = 2,
aes_(y = 0.0001, fill = ~converged, text = ~tooltip),
alpha = aestethics$alpha
)) +
theme_minimal() +
scale_fill_manual(values = booleanColor) +
scale_y_continuous(breaks = c(0.0001), labels = c("Price\nAnticipation (inactive)")) +
scale_x_continuous(breaks = c(data$iteration)) +
labs(x = NULL, y = NULL) +
coord_cartesian(ylim = c(-0.2, 1))

priceAnticipationPlotly <- ggplotly(priceAnticipation, tooltip = c("text"))
subplots <- append(subplots, list(priceAnticipationPlotly))

# Summary plot ----

Expand All @@ -594,7 +627,7 @@ plotNashConvergence <- function(gdx) { # nolint cyclocomp_linter
out$plot <- subplot(
subplots,
nrows = n,
heights = c(3 / (n + 3), rep(1 / (n + 3), 2), 2 / (n + 3), 1 / (n + 3), rep(1 / (n + 3), n - 5)),
heights = c(2 / (n + 1), rep(1 / (n + 1), n - 1)),
shareX = TRUE,
titleX = FALSE
) %>%
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The REMIND R package (2nd generation)

R package **remind2**, version **1.129.3**
R package **remind2**, version **1.129.4**

[![CRAN status](https://www.r-pkg.org/badges/version/remind2)](https://cran.r-project.org/package=remind2) [![R build status](https://github.com/pik-piam/remind2/workflows/check/badge.svg)](https://github.com/pik-piam/remind2/actions) [![codecov](https://codecov.io/gh/pik-piam/remind2/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/remind2) [![r-universe](https://pik-piam.r-universe.dev/badges/remind2)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -49,7 +49,7 @@ In case of questions / problems please contact Renato Rodrigues <renato.rodrigue

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

Rodrigues R, Baumstark L, Benke F, Dietrich J, Dirnaichner A, Führlich P, Giannousakis A, Hasse R, Hilaire J, Klein D, Koch J, Kowalczyk K, Levesque A, Malik A, Merfort A, Merfort L, Morena-Leiva S, Pehl M, Pietzcker R, Rauner S, Richters O, Rottoli M, Schötz C, Schreyer F, Siala K, Sörgel B, Spahr M, Strefler J, Verpoort P, Weigmann P (2024). _remind2: The REMIND R package (2nd generation)_. R package version 1.129.3, <URL: https://github.com/pik-piam/remind2>.
Rodrigues R, Baumstark L, Benke F, Dietrich J, Dirnaichner A, Führlich P, Giannousakis A, Hasse R, Hilaire J, Klein D, Koch J, Kowalczyk K, Levesque A, Malik A, Merfort A, Merfort L, Morena-Leiva S, Pehl M, Pietzcker R, Rauner S, Richters O, Rottoli M, Schötz C, Schreyer F, Siala K, Sörgel B, Spahr M, Strefler J, Verpoort P, Weigmann P (2024). _remind2: The REMIND R package (2nd generation)_. R package version 1.129.4, <https://github.com/pik-piam/remind2>.

A BibTeX entry for LaTeX users is

Expand All @@ -58,7 +58,7 @@ A BibTeX entry for LaTeX users is
title = {remind2: The REMIND R package (2nd generation)},
author = {Renato Rodrigues and Lavinia Baumstark and Falk Benke and Jan Philipp Dietrich and Alois Dirnaichner and Pascal Führlich and Anastasis Giannousakis and Robin Hasse and Jérome Hilaire and David Klein and Johannes Koch and Katarzyna Kowalczyk and Antoine Levesque and Aman Malik and Anne Merfort and Leon Merfort and Simón Morena-Leiva and Michaja Pehl and Robert Pietzcker and Sebastian Rauner and Oliver Richters and Marianna Rottoli and Christof Schötz and Felix Schreyer and Kais Siala and Björn Sörgel and Mike Spahr and Jessica Strefler and Philipp Verpoort and Pascal Weigmann},
year = {2024},
note = {R package version 1.129.3},
note = {R package version 1.129.4},
url = {https://github.com/pik-piam/remind2},
}
```
21 changes: 13 additions & 8 deletions inst/markdown/nashAnalysis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,34 @@ output:
toc_float: true
code_folding: hide
params:
gdx: "fulldata.gdx"
gdx: "/home/benke/dev/pik-piam/miptemplate/SSP2EU-EU21-NDC_2023-11-01_19.58.36/fulldata.gdx"
warning: false
message: false
figWidth: 8
---


```{r loading_libraries, include=FALSE}
library(knitr)
library(dplyr)
library(remind2)
library(plotly)
library(ggplot2)
library(gridExtra)
library(scales)
library(knitr)
library(mip)
library(plotly)
library(remind2)
library(scales)
library(svglite)
knitr::opts_chunk$set(
echo = FALSE,
error = TRUE,
fig.width = params$figWidth,
message = params$message,
warning = params$warning
warning = params$warning,
dev = "svglite",
fix.ext = ".svg",
dpi = 100
)
```

Expand Down Expand Up @@ -430,8 +435,8 @@ for (v in setdiff(unique(prices_and_surplus_scaled$all_enty), "good")) {
```

## Convergence Plots
```{r results = "asis"}
diag <- remind2::plotNashConvergence(gdx = params$gdx)
```{r results = "asis", fig.width=12, fig.height=6}
diag <- plotNashConvergence(gdx = params$gdx)
htmltools::tagList(diag$plot)
htmltools::tagList(diag$tradeDetailPlot)
```
Expand Down
Loading

0 comments on commit e1dc041

Please sign in to comment.