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

bug: prepared scenarios must have tmsr values within reasonable range #63

Open
jacobvjk opened this issue Dec 10, 2024 · 4 comments
Open
Labels
bug an unexpected problem or unintended behavior priority

Comments

@jacobvjk
Copy link
Member

jacobvjk commented Dec 10, 2024

isf_2023 has some tmsr values = Inf for nuclear power in Turkey. This will lead to errors downstream when using the prepared scenario file in r2dii.analysis::target_market_share():

library(r2dii.data)
library(r2dii.match)
library(r2dii.analysis)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

abcd <- r2dii.data::abcd_demo
lbk <- r2dii.data::loanbook_demo
scenarios_good <- dplyr::filter(r2dii.data::scenario_demo_2020, scenario == "sps", region == "global", sector == "power", dplyr::between(year, 2020, 2025))

print(scenarios_good, n = 36)
#> # A tibble: 36 × 8
#>    scenario sector technology    region  year  tmsr      smsp scenario_source
#>    <chr>    <chr>  <chr>         <chr>  <int> <dbl>     <dbl> <chr>          
#>  1 sps      power  coalcap       global  2020 1      0        demo_2020      
#>  2 sps      power  gascap        global  2020 1      0        demo_2020      
#>  3 sps      power  hydrocap      global  2020 1      0        demo_2020      
#>  4 sps      power  nuclearcap    global  2020 1      0        demo_2020      
#>  5 sps      power  oilcap        global  2020 1      0        demo_2020      
#>  6 sps      power  renewablescap global  2020 1      0        demo_2020      
#>  7 sps      power  coalcap       global  2021 0.983 -0.00307  demo_2020      
#>  8 sps      power  gascap        global  2021 1.01   0.00446  demo_2020      
#>  9 sps      power  hydrocap      global  2021 1.01   0.000883 demo_2020      
#> 10 sps      power  nuclearcap    global  2021 0.986 -0.00132  demo_2020      
#> 11 sps      power  oilcap        global  2021 0.931 -0.00315  demo_2020      
#> 12 sps      power  renewablescap global  2021 1.06   0.0145   demo_2020      
#> 13 sps      power  coalcap       global  2022 0.965 -0.00614  demo_2020      
#> 14 sps      power  gascap        global  2022 1.03   0.00892  demo_2020      
#> 15 sps      power  hydrocap      global  2022 1.01   0.00177  demo_2020      
#> 16 sps      power  nuclearcap    global  2022 0.971 -0.00263  demo_2020      
#> 17 sps      power  oilcap        global  2022 0.863 -0.00631  demo_2020      
#> 18 sps      power  renewablescap global  2022 1.13   0.0290   demo_2020      
#> 19 sps      power  coalcap       global  2023 0.948 -0.00921  demo_2020      
#> 20 sps      power  gascap        global  2023 1.04   0.0134   demo_2020      
#> 21 sps      power  hydrocap      global  2023 1.02   0.00265  demo_2020      
#> 22 sps      power  nuclearcap    global  2023 0.957 -0.00395  demo_2020      
#> 23 sps      power  oilcap        global  2023 0.794 -0.00946  demo_2020      
#> 24 sps      power  renewablescap global  2023 1.19   0.0436   demo_2020      
#> 25 sps      power  coalcap       global  2024 0.930 -0.0123   demo_2020      
#> 26 sps      power  gascap        global  2024 1.06   0.0178   demo_2020      
#> 27 sps      power  hydrocap      global  2024 1.02   0.00353  demo_2020      
#> 28 sps      power  nuclearcap    global  2024 0.943 -0.00527  demo_2020      
#> 29 sps      power  oilcap        global  2024 0.726 -0.0126   demo_2020      
#> 30 sps      power  renewablescap global  2024 1.25   0.0581   demo_2020      
#> 31 sps      power  coalcap       global  2025 0.913 -0.0153   demo_2020      
#> 32 sps      power  gascap        global  2025 1.07   0.0223   demo_2020      
#> 33 sps      power  hydrocap      global  2025 1.03   0.00442  demo_2020      
#> 34 sps      power  nuclearcap    global  2025 0.929 -0.00659  demo_2020      
#> 35 sps      power  oilcap        global  2025 0.657 -0.0158   demo_2020      
#> 36 sps      power  renewablescap global  2025 1.32   0.0726   demo_2020

matched_lbk <- r2dii.match::match_name(lbk, abcd)
matched_lbk_prio <- r2dii.match::prioritize(matched_lbk)

results_good <- r2dii.analysis::target_market_share(
  matched_lbk_prio,
  abcd,
  scenarios_good,
  region_isos = r2dii.data::region_isos_demo
)

scenarios_bad <- dplyr::mutate(scenarios_good, tmsr = dplyr::if_else(year >= 2023 & technology == "nuclearcap", NA_real_, tmsr))

print(scenarios_bad, n = 36)
#> # A tibble: 36 × 8
#>    scenario sector technology    region  year   tmsr      smsp scenario_source
#>    <chr>    <chr>  <chr>         <chr>  <int>  <dbl>     <dbl> <chr>          
#>  1 sps      power  coalcap       global  2020  1      0        demo_2020      
#>  2 sps      power  gascap        global  2020  1      0        demo_2020      
#>  3 sps      power  hydrocap      global  2020  1      0        demo_2020      
#>  4 sps      power  nuclearcap    global  2020  1      0        demo_2020      
#>  5 sps      power  oilcap        global  2020  1      0        demo_2020      
#>  6 sps      power  renewablescap global  2020  1      0        demo_2020      
#>  7 sps      power  coalcap       global  2021  0.983 -0.00307  demo_2020      
#>  8 sps      power  gascap        global  2021  1.01   0.00446  demo_2020      
#>  9 sps      power  hydrocap      global  2021  1.01   0.000883 demo_2020      
#> 10 sps      power  nuclearcap    global  2021  0.986 -0.00132  demo_2020      
#> 11 sps      power  oilcap        global  2021  0.931 -0.00315  demo_2020      
#> 12 sps      power  renewablescap global  2021  1.06   0.0145   demo_2020      
#> 13 sps      power  coalcap       global  2022  0.965 -0.00614  demo_2020      
#> 14 sps      power  gascap        global  2022  1.03   0.00892  demo_2020      
#> 15 sps      power  hydrocap      global  2022  1.01   0.00177  demo_2020      
#> 16 sps      power  nuclearcap    global  2022  0.971 -0.00263  demo_2020      
#> 17 sps      power  oilcap        global  2022  0.863 -0.00631  demo_2020      
#> 18 sps      power  renewablescap global  2022  1.13   0.0290   demo_2020      
#> 19 sps      power  coalcap       global  2023  0.948 -0.00921  demo_2020      
#> 20 sps      power  gascap        global  2023  1.04   0.0134   demo_2020      
#> 21 sps      power  hydrocap      global  2023  1.02   0.00265  demo_2020      
#> 22 sps      power  nuclearcap    global  2023 NA     -0.00395  demo_2020      
#> 23 sps      power  oilcap        global  2023  0.794 -0.00946  demo_2020      
#> 24 sps      power  renewablescap global  2023  1.19   0.0436   demo_2020      
#> 25 sps      power  coalcap       global  2024  0.930 -0.0123   demo_2020      
#> 26 sps      power  gascap        global  2024  1.06   0.0178   demo_2020      
#> 27 sps      power  hydrocap      global  2024  1.02   0.00353  demo_2020      
#> 28 sps      power  nuclearcap    global  2024 NA     -0.00527  demo_2020      
#> 29 sps      power  oilcap        global  2024  0.726 -0.0126   demo_2020      
#> 30 sps      power  renewablescap global  2024  1.25   0.0581   demo_2020      
#> 31 sps      power  coalcap       global  2025  0.913 -0.0153   demo_2020      
#> 32 sps      power  gascap        global  2025  1.07   0.0223   demo_2020      
#> 33 sps      power  hydrocap      global  2025  1.03   0.00442  demo_2020      
#> 34 sps      power  nuclearcap    global  2025 NA     -0.00659  demo_2020      
#> 35 sps      power  oilcap        global  2025  0.657 -0.0158   demo_2020      
#> 36 sps      power  renewablescap global  2025  1.32   0.0726   demo_2020

results_bad <- r2dii.analysis::target_market_share(
  matched_lbk_prio,
  abcd,
  scenarios_bad,
  region_isos = r2dii.data::region_isos_demo
)
#> Error in `check_no_value_is_missing()`:
#> ! Column `tmsr` must not contain any `NA`s.
Created on 2024-12-10 with [reprex v2.1.0](https://reprex.tidyverse.org/)

In general it is hard to see how to calculate a reasonable PACTA result with an infinite value and more practically, the Inf is read in as an NA, when reading the tmsr column as a numerical column.

I think it would be justifiable to exclude combinations of (scenario_source X scenario X sector), if any value for TMSR or SMSP within that combination is not within expected bounds. This should probably mean that at least NA and Inf are not allowed

Open to thoughts on this topic @jdhoffa @cjyetman @Antoine-Lalechere

@jacobvjk jacobvjk added bug an unexpected problem or unintended behavior priority labels Dec 10, 2024
@cjyetman
Copy link
Member

I think there's two problems that need to be fixed here:

  1. If the intended output is a CSV (it is), Inf cannot be encoded universally in a CSV so those rows should be dropped or the Infs should be converted to NAs if that's appropriate (not sure if that is appropriate here)
  2. any code that is using the data in the CSV (namely r2dii.analysis) should handle NAs gracefully, either ignoring/skipping them or providing an informative error.

It seems like most or part of both of those is already achieved... Infs get converted to NA (or technically '') when the data is exported to CSV (whether or not that was an intentional decision), and an informative error is shown. So maybe one of the alternatives is a better choice, either drop rows with Inf/NA before the output is made or ignore/skip rows with NA in r2dii.analysis?

@jacobvjk
Copy link
Member Author

I could imagine dropping the affected combination of (scenario_source X scenario X sector) in r2dii.analysis as a solution. For all years, not only the affected ones, that is. We would probably want to add a warning, listing the affected combinations that were removed.

@jacobvjk
Copy link
Member Author

jacobvjk commented Dec 11, 2024

Nevertheless, I do think that we should consider, if we think Inf values (or resulting NA values) are something that we consider reasonable to write into the prepared scenario data sets. I personally do not see the point, but will have to spend another few minutes thinking about the underlying logic a bit

@AlexAxthelm
Copy link
Contributor

Related, is 0 (or a negative) a valid value? Is there a reasonable upper limit (100 billion)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior priority
Projects
None yet
Development

No branches or pull requests

3 participants