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

SDA: first year of scenario should determine start of analysis #439

Closed
jacobvjk opened this issue Jul 19, 2023 · 3 comments · Fixed by #440
Closed

SDA: first year of scenario should determine start of analysis #439

jacobvjk opened this issue Jul 19, 2023 · 3 comments · Fixed by #440
Assignees

Comments

@jacobvjk
Copy link
Member

jacobvjk commented Jul 19, 2023

right_join(

For the target_sda() function the linked line shows that the scenario data is right joined onto the abcd. This means that in case the abcd has values starting in a year earlier than the first scenario year, this year will not be dropped from the analysis and the scenario thus does not determine the start year. Applying an inner join should fix this.

For comparison, in target_market_share() the following join is used and thus the first year of the scenario data determines the start year:

inner_join(scenario, by = scenario_columns()) %>%

Reprex:

library(tidyverse)
library(r2dii.data)
library(r2dii.match)
library(r2dii.analysis)

lbk_raw <- r2dii.data::loanbook_demo

# let us have the production data start a year befor the initial years in the scenario files
abcd <- r2dii.data::abcd_demo %>% 
  filter(year >= 2019)

matched <- r2dii.match::match_name(lbk_raw, abcd = abcd) %>% 
  r2dii.match::prioritize()

# min year for tmsr/smsp and sda sceanrios is the same
r2dii.data::scenario_demo_2020 %>% 
  pull(year) %>% 
  min()
#> [1] 2020

r2dii.data::co2_intensity_scenario_demo %>% 
  pull(year) %>% 
  min()
#> [1] 2020

# calculation for EI sectors, using SDA  
result_sda <- matched %>% 
  r2dii.analysis::target_sda(
    abcd = abcd,
    co2_intensity_scenario = r2dii.data::co2_intensity_scenario_demo,
    region_isos = r2dii.data::region_isos_demo
  )
#> Warning: Removing rows in abcd where `emission_factor` is NA

# calculation for production pathway sectors, using target market share
result_market_share <- matched %>% 
  r2dii.analysis::target_market_share(
    abcd = abcd,
    scenario = r2dii.data::scenario_demo_2020,
    region_isos = r2dii.data::region_isos_demo
  )

# comparison of intial years in results
result_sda %>% 
  pull(year) %>% 
  min()
#> [1] 2019

result_market_share %>% 
  pull(year) %>% 
  min()
#> [1] 2020
Created on 2023-07-19 with [reprex v2.0.2](https://reprex.tidyverse.org/)
@jacobvjk jacobvjk changed the title first year of scenario should determine start of analysis SDA: first year of scenario should determine start of analysis Jul 19, 2023
@jdhoffa
Copy link
Member

jdhoffa commented Jul 19, 2023

Seems like it may be a little more complicated than just changing that line, but i am having a look now.

@jdhoffa
Copy link
Member

jdhoffa commented Jul 19, 2023

Ok actually I don't think this requires any action.

Notice that in the following reprex, all target values start at the start of the scenario, while all "measured" values (ie. corporate economy and projected values) begin at the first value available. To me this is expected behaviour:

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
library(r2dii.data)
library(r2dii.analysis)

matched <- tibble::tribble(
  ~id_loan, ~loan_size_outstanding, ~loan_size_outstanding_currency, ~loan_size_credit_limit, ~loan_size_credit_limit_currency, ~id_2dii,            ~level, ~score,      ~sector,     ~name_abcd, ~sector_abcd,
  "L162",                      1,                           "EUR",                       2,                            "EUR",    "UP1", "ultimate_parent",      1, "automotive", "shaanxi auto",     "cement"
)

abcd <- tibble::tribble(
  ~name_company,  ~sector, ~technology, ~year, ~production, ~emission_factor, ~plant_location, ~is_ultimate_owner,
  "shaanxi auto", "cement",       "ice",  2024,           1,                1,            "BF",               TRUE,
  "shaanxi auto", "cement",       "ice",  2025,           1,                1,            "BF",               TRUE
)

scenario <- tibble::tribble(
  ~scenario,  ~sector,  ~region, ~year, ~emission_factor,           ~emission_factor_unit, ~scenario_source,
  "b2ds", "cement", "global",  2025,              0.5, "tons of CO2 per ton of cement",      "demo_2020",
  "b2ds", "cement", "global",  2050,              0.2, "tons of CO2 per ton of cement",      "demo_2020"
)

out <- target_sda(
  matched,
  abcd,
  scenario,
  region_isos = r2dii.analysis:::region_isos_stable
)

out_actual_data <- filter(out, !grepl("target", emission_factor_metric))
min(out_actual_data$year)
#> [1] 2024

out_targets <- filter(out, grepl("target", emission_factor_metric))
min(out_targets$year)
#> [1] 2025

Created on 2023-07-19 with reprex v2.0.2

@jdhoffa
Copy link
Member

jdhoffa commented Jul 19, 2023

In any case, I have opened #440 to prove in a test that the expected behavior is maintained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants