Skip to content

Commit

Permalink
Merge pull request #506 from fbenke-pik/convergence
Browse files Browse the repository at this point in the history
Enhance nash convergence plots
  • Loading branch information
fbenke-pik authored Jan 19, 2024
2 parents b42b0ba + 35f3ac4 commit ddcedbb
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 146 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '222943560'
ValidationKey: '222974595'
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.4
date-released: '2024-01-18'
version: 1.129.5
date-released: '2024-01-19'
abstract: Contains the REMIND-specific routines for data and model output manipulation.
authors:
- family-names: Rodrigues
Expand Down
6 changes: 3 additions & 3 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.4
Date: 2024-01-18
Version: 1.129.5
Date: 2024-01-19
Authors@R: c(
person("Renato", "Rodrigues", , "[email protected]", role = c("aut", "cre")),
person("Lavinia", "Baumstark", role = "aut"),
Expand Down Expand Up @@ -88,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
196 changes: 118 additions & 78 deletions R/plotNashConvergence.R
Original file line number Diff line number Diff line change
Expand Up @@ -296,102 +296,47 @@ 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",
"converged" = ifelse(.data$value > 0.1 * maxTolerance, "no", "yes"),
"tooltip" = ifelse(.data$value > 0.1 * maxTolerance,
paste0(
"Converged<br>Price Anticipation fade out is low enough<br>",
round(.data$fadeoutPriceAnticip, 5), " <= ", cmMaxFadeoutPriceAnticip
"Not converged<br>Price Anticipation deviation is not low enough<br>",
round(.data$value, 5), " > ", 0.1 * maxTolerance
),
paste0(
"Not converged<br>Price Anticipation fade out is not low enough<br>",
round(.data$fadeoutPriceAnticip, 5), " > ", cmMaxFadeoutPriceAnticip
)
)
"Converged"
),
)

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 +525,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 +634,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.4**
R package **remind2**, version **1.129.5**

[![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.4, <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.5, <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.4},
note = {R package version 1.129.5},
url = {https://github.com/pik-piam/remind2},
}
```
Loading

0 comments on commit ddcedbb

Please sign in to comment.