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

Adds add_thresholds_transition_risk() function which adds low and high thresholds for transition risk score #790

Closed
wants to merge 6 commits into from

Conversation

kalashsinghal
Copy link
Collaborator

@kalashsinghal kalashsinghal commented May 30, 2024

closes 2DegreesInvesting/tiltTransitionRisk#12

Dear @maurolepore @AnneSchoenauer @Tilmon

As discussed in this comment, a new dataset will output from tiltIndicator which will become input to tiltIndicatorAfter for creating the risk categories of transition risk scores (a PR will follow in tiltIndicatorAfter to show this).

This new dataset will have low and high thresholds for transition risk scores of around 13k activity_uuid_product_uuids. I haven't come up with a good name yet to describe this new dataset, so please suggest any good name for better understanding! At the moment, I have named the function trs_low_high_thresholds_for_ecoinvent_all which will output this new dataset.

As mentioned in this comment, the function trs_low_high_thresholds_for_ecoinvent_all will have a new input from tiltIndicatorBefore after merging this PR (named as sp_all_uuids_scenario_sectors) and old inputs scenarios and co2. The co2 dataset will be used for creating the profile ranking for all 13k uuids, and datasets sp_all_uuids_scenario_sectors and scenarios will be used for creating the reduction targets for all 13k uuids. The profile ranking and reduction targets will be used to create the transition risk scores. These scores will then finally creates the low and high thresholds for transition risk scores.

I didn't have a toy dataset for sp_all_uuids_scenario_sectors, hence I used the toy_sector_profile_companies dataset with some modifications to create it. However, they are not the same and we can think of creating a new toy dataset for sp_all_uuids_scenario_sectors in tiltToydata.

Here is a reprex for better understanding:

library(tiltToyData)
library(readr)
library(dplyr)
devtools::load_all(".")
#> ℹ Loading tiltIndicator
options(width = 500)

all_uuids_scenario_sectors <- read_csv(toy_sector_profile_companies()) |>
  select(-c("companies_id", "company_name", "clustered")) |>
  distinct()

# New input from `tiltIndicatorBefore`
all_uuids_scenario_sectors 
#> # A tibble: 6 × 7
#>   activity_uuid_product_uuid           isic_4digit tilt_sector  tilt_subsector           type  sector    subsector     
#>   <chr>                                <chr>       <chr>        <chr>                    <chr> <chr>     <chr>         
#> 1 76269c17-78d6-420b-991a-aa38c51b45b7 '4100'      construction construction residential ipr   buildings <NA>          
#> 2 76269c17-78d6-420b-991a-aa38c51b45b7 '4100'      construction construction residential weo   total     residential   
#> 3 833caa78-30df-4374-900f-7f88ab44075b '2591'      metals       other metals             ipr   industry  other industry
#> 4 833caa78-30df-4374-900f-7f88ab44075b '2591'      metals       other metals             weo   total     industry      
#> 5 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb '2410'      metals       iron & steel             ipr   industry  iron and steel
#> 6 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb '2410'      metals       iron & steel             weo   total     iron and steel

scenarios <- read_csv(toy_sector_profile_any_scenarios())
emissions_profile_products <- read_csv(toy_emissions_profile_products_ecoinvent())

tiltIndicator:::trs_low_high_thresholds_for_ecoinvent_all(emissions_profile_products, all_uuids_scenario_sectors, scenarios) |> 
  print(n = Inf)
#> # A tibble: 72 × 9
#>    activity_uuid_product_uuid           grouped_by       profile_ranking reductions scenario_year transition_risk_score benchmark_tr_score             trs_low_threshold trs_high_threshold
#>    <chr>                                <chr>                      <dbl>      <dbl> <chr>                         <dbl> <chr>                                      <dbl>              <dbl>
#>  1 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb all                        0.667       0.22 1.5C RPS_2030                 0.443 1.5C RPS_2030_all                          0.366              0.492
#>  2 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb all                        0.667       0.96 1.5C RPS_2050                 0.813 1.5C RPS_2050_all                          0.756              0.872
#>  3 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb all                        0.667       0.23 NZ 2050_2030                  0.448 NZ 2050_2030_all                           0.391              0.532
#>  4 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb all                        0.667       0.94 NZ 2050_2050                  0.803 NZ 2050_2050_all                           0.751              0.864
#>  5 833caa78-30df-4374-900f-7f88ab44075b all                        0.333       0.09 1.5C RPS_2030                 0.212 1.5C RPS_2030_all                          0.366              0.492
#>  6 833caa78-30df-4374-900f-7f88ab44075b all                        0.333       0.95 1.5C RPS_2050                 0.642 1.5C RPS_2050_all                          0.756              0.872
#>  7 833caa78-30df-4374-900f-7f88ab44075b all                        0.333       0.22 NZ 2050_2030                  0.277 NZ 2050_2030_all                           0.391              0.532
#>  8 833caa78-30df-4374-900f-7f88ab44075b all                        0.333       0.96 NZ 2050_2050                  0.647 NZ 2050_2050_all                           0.751              0.864
#>  9 76269c17-78d6-420b-991a-aa38c51b45b7 all                        1           0.18 1.5C RPS_2030                 0.59  1.5C RPS_2030_all                          0.366              0.492
#> 10 76269c17-78d6-420b-991a-aa38c51b45b7 all                        1           0.98 1.5C RPS_2050                 0.99  1.5C RPS_2050_all                          0.756              0.872
#> 11 76269c17-78d6-420b-991a-aa38c51b45b7 all                        1           0.4  NZ 2050_2030                  0.7   NZ 2050_2030_all                           0.391              0.532
#> 12 76269c17-78d6-420b-991a-aa38c51b45b7 all                        1           0.97 NZ 2050_2050                  0.985 NZ 2050_2050_all                           0.751              0.864
#> 13 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb isic_4digit                1           0.22 1.5C RPS_2030                 0.61  1.5C RPS_2030_isic_4digit                  0.575              0.597
#> 14 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb isic_4digit                1           0.96 1.5C RPS_2050                 0.98  1.5C RPS_2050_isic_4digit                  0.978              0.983
#> 15 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb isic_4digit                1           0.23 NZ 2050_2030                  0.615 NZ 2050_2030_isic_4digit                   0.613              0.643
#> 16 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb isic_4digit                1           0.94 NZ 2050_2050                  0.97  NZ 2050_2050_isic_4digit                   0.977              0.982
#> 17 833caa78-30df-4374-900f-7f88ab44075b isic_4digit                1           0.09 1.5C RPS_2030                 0.545 1.5C RPS_2030_isic_4digit                  0.575              0.597
#> 18 833caa78-30df-4374-900f-7f88ab44075b isic_4digit                1           0.95 1.5C RPS_2050                 0.975 1.5C RPS_2050_isic_4digit                  0.978              0.983
#> 19 833caa78-30df-4374-900f-7f88ab44075b isic_4digit                1           0.22 NZ 2050_2030                  0.61  NZ 2050_2030_isic_4digit                   0.613              0.643
#> 20 833caa78-30df-4374-900f-7f88ab44075b isic_4digit                1           0.96 NZ 2050_2050                  0.98  NZ 2050_2050_isic_4digit                   0.977              0.982
#> 21 76269c17-78d6-420b-991a-aa38c51b45b7 isic_4digit                1           0.18 1.5C RPS_2030                 0.59  1.5C RPS_2030_isic_4digit                  0.575              0.597
#> 22 76269c17-78d6-420b-991a-aa38c51b45b7 isic_4digit                1           0.98 1.5C RPS_2050                 0.99  1.5C RPS_2050_isic_4digit                  0.978              0.983
#> 23 76269c17-78d6-420b-991a-aa38c51b45b7 isic_4digit                1           0.4  NZ 2050_2030                  0.7   NZ 2050_2030_isic_4digit                   0.613              0.643
#> 24 76269c17-78d6-420b-991a-aa38c51b45b7 isic_4digit                1           0.97 NZ 2050_2050                  0.985 NZ 2050_2050_isic_4digit                   0.977              0.982
#> 25 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb tilt_sector                1           0.22 1.5C RPS_2030                 0.61  1.5C RPS_2030_tilt_sector                  0.492              0.597
#> 26 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb tilt_sector                1           0.96 1.5C RPS_2050                 0.98  1.5C RPS_2050_tilt_sector                  0.895              0.983
#> 27 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb tilt_sector                1           0.23 NZ 2050_2030                  0.615 NZ 2050_2030_tilt_sector                   0.53               0.643
#> 28 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb tilt_sector                1           0.94 NZ 2050_2050                  0.97  NZ 2050_2050_tilt_sector                   0.89               0.975
#> 29 833caa78-30df-4374-900f-7f88ab44075b tilt_sector                0.5         0.09 1.5C RPS_2030                 0.295 1.5C RPS_2030_tilt_sector                  0.492              0.597
#> 30 833caa78-30df-4374-900f-7f88ab44075b tilt_sector                0.5         0.95 1.5C RPS_2050                 0.725 1.5C RPS_2050_tilt_sector                  0.895              0.983
#> 31 833caa78-30df-4374-900f-7f88ab44075b tilt_sector                0.5         0.22 NZ 2050_2030                  0.36  NZ 2050_2030_tilt_sector                   0.53               0.643
#> 32 833caa78-30df-4374-900f-7f88ab44075b tilt_sector                0.5         0.96 NZ 2050_2050                  0.73  NZ 2050_2050_tilt_sector                   0.89               0.975
#> 33 76269c17-78d6-420b-991a-aa38c51b45b7 tilt_sector                1           0.18 1.5C RPS_2030                 0.59  1.5C RPS_2030_tilt_sector                  0.492              0.597
#> 34 76269c17-78d6-420b-991a-aa38c51b45b7 tilt_sector                1           0.98 1.5C RPS_2050                 0.99  1.5C RPS_2050_tilt_sector                  0.895              0.983
#> 35 76269c17-78d6-420b-991a-aa38c51b45b7 tilt_sector                1           0.4  NZ 2050_2030                  0.7   NZ 2050_2030_tilt_sector                   0.53               0.643
#> 36 76269c17-78d6-420b-991a-aa38c51b45b7 tilt_sector                1           0.97 NZ 2050_2050                  0.985 NZ 2050_2050_tilt_sector                   0.89               0.975
#> 37 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb unit                       1           0.22 1.5C RPS_2030                 0.61  1.5C RPS_2030_unit                         0.492              0.597
#> 38 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb unit                       1           0.96 1.5C RPS_2050                 0.98  1.5C RPS_2050_unit                         0.895              0.983
#> 39 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb unit                       1           0.23 NZ 2050_2030                  0.615 NZ 2050_2030_unit                          0.53               0.643
#> 40 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb unit                       1           0.94 NZ 2050_2050                  0.97  NZ 2050_2050_unit                          0.89               0.975
#> 41 833caa78-30df-4374-900f-7f88ab44075b unit                       0.5         0.09 1.5C RPS_2030                 0.295 1.5C RPS_2030_unit                         0.492              0.597
#> 42 833caa78-30df-4374-900f-7f88ab44075b unit                       0.5         0.95 1.5C RPS_2050                 0.725 1.5C RPS_2050_unit                         0.895              0.983
#> 43 833caa78-30df-4374-900f-7f88ab44075b unit                       0.5         0.22 NZ 2050_2030                  0.36  NZ 2050_2030_unit                          0.53               0.643
#> 44 833caa78-30df-4374-900f-7f88ab44075b unit                       0.5         0.96 NZ 2050_2050                  0.73  NZ 2050_2050_unit                          0.89               0.975
#> 45 76269c17-78d6-420b-991a-aa38c51b45b7 unit                       1           0.18 1.5C RPS_2030                 0.59  1.5C RPS_2030_unit                         0.492              0.597
#> 46 76269c17-78d6-420b-991a-aa38c51b45b7 unit                       1           0.98 1.5C RPS_2050                 0.99  1.5C RPS_2050_unit                         0.895              0.983
#> 47 76269c17-78d6-420b-991a-aa38c51b45b7 unit                       1           0.4  NZ 2050_2030                  0.7   NZ 2050_2030_unit                          0.53               0.643
#> 48 76269c17-78d6-420b-991a-aa38c51b45b7 unit                       1           0.97 NZ 2050_2050                  0.985 NZ 2050_2050_unit                          0.89               0.975
#> 49 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb unit_isic_4digit           1           0.22 1.5C RPS_2030                 0.61  1.5C RPS_2030_unit_isic_4digit             0.575              0.597
#> 50 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb unit_isic_4digit           1           0.96 1.5C RPS_2050                 0.98  1.5C RPS_2050_unit_isic_4digit             0.978              0.983
#> 51 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb unit_isic_4digit           1           0.23 NZ 2050_2030                  0.615 NZ 2050_2030_unit_isic_4digit              0.613              0.643
#> 52 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb unit_isic_4digit           1           0.94 NZ 2050_2050                  0.97  NZ 2050_2050_unit_isic_4digit              0.977              0.982
#> 53 833caa78-30df-4374-900f-7f88ab44075b unit_isic_4digit           1           0.09 1.5C RPS_2030                 0.545 1.5C RPS_2030_unit_isic_4digit             0.575              0.597
#> 54 833caa78-30df-4374-900f-7f88ab44075b unit_isic_4digit           1           0.95 1.5C RPS_2050                 0.975 1.5C RPS_2050_unit_isic_4digit             0.978              0.983
#> 55 833caa78-30df-4374-900f-7f88ab44075b unit_isic_4digit           1           0.22 NZ 2050_2030                  0.61  NZ 2050_2030_unit_isic_4digit              0.613              0.643
#> 56 833caa78-30df-4374-900f-7f88ab44075b unit_isic_4digit           1           0.96 NZ 2050_2050                  0.98  NZ 2050_2050_unit_isic_4digit              0.977              0.982
#> 57 76269c17-78d6-420b-991a-aa38c51b45b7 unit_isic_4digit           1           0.18 1.5C RPS_2030                 0.59  1.5C RPS_2030_unit_isic_4digit             0.575              0.597
#> 58 76269c17-78d6-420b-991a-aa38c51b45b7 unit_isic_4digit           1           0.98 1.5C RPS_2050                 0.99  1.5C RPS_2050_unit_isic_4digit             0.978              0.983
#> 59 76269c17-78d6-420b-991a-aa38c51b45b7 unit_isic_4digit           1           0.4  NZ 2050_2030                  0.7   NZ 2050_2030_unit_isic_4digit              0.613              0.643
#> 60 76269c17-78d6-420b-991a-aa38c51b45b7 unit_isic_4digit           1           0.97 NZ 2050_2050                  0.985 NZ 2050_2050_unit_isic_4digit              0.977              0.982
#> 61 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb unit_tilt_sector           1           0.22 1.5C RPS_2030                 0.61  1.5C RPS_2030_unit_tilt_sector             0.492              0.597
#> 62 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb unit_tilt_sector           1           0.96 1.5C RPS_2050                 0.98  1.5C RPS_2050_unit_tilt_sector             0.895              0.983
#> 63 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb unit_tilt_sector           1           0.23 NZ 2050_2030                  0.615 NZ 2050_2030_unit_tilt_sector              0.53               0.643
#> 64 bf94b5a7-b7a2-46d1-bb95-84bc560b12fb unit_tilt_sector           1           0.94 NZ 2050_2050                  0.97  NZ 2050_2050_unit_tilt_sector              0.89               0.975
#> 65 833caa78-30df-4374-900f-7f88ab44075b unit_tilt_sector           0.5         0.09 1.5C RPS_2030                 0.295 1.5C RPS_2030_unit_tilt_sector             0.492              0.597
#> 66 833caa78-30df-4374-900f-7f88ab44075b unit_tilt_sector           0.5         0.95 1.5C RPS_2050                 0.725 1.5C RPS_2050_unit_tilt_sector             0.895              0.983
#> 67 833caa78-30df-4374-900f-7f88ab44075b unit_tilt_sector           0.5         0.22 NZ 2050_2030                  0.36  NZ 2050_2030_unit_tilt_sector              0.53               0.643
#> 68 833caa78-30df-4374-900f-7f88ab44075b unit_tilt_sector           0.5         0.96 NZ 2050_2050                  0.73  NZ 2050_2050_unit_tilt_sector              0.89               0.975
#> 69 76269c17-78d6-420b-991a-aa38c51b45b7 unit_tilt_sector           1           0.18 1.5C RPS_2030                 0.59  1.5C RPS_2030_unit_tilt_sector             0.492              0.597
#> 70 76269c17-78d6-420b-991a-aa38c51b45b7 unit_tilt_sector           1           0.98 1.5C RPS_2050                 0.99  1.5C RPS_2050_unit_tilt_sector             0.895              0.983
#> 71 76269c17-78d6-420b-991a-aa38c51b45b7 unit_tilt_sector           1           0.4  NZ 2050_2030                  0.7   NZ 2050_2030_unit_tilt_sector              0.53               0.643
#> 72 76269c17-78d6-420b-991a-aa38c51b45b7 unit_tilt_sector           1           0.97 NZ 2050_2050                  0.985 NZ 2050_2050_unit_tilt_sector              0.89               0.975

Created on 2024-05-30 with reprex v2.0.2

This PR is still work in progress and I will write unittests in upcoming code. Thanks for the review!


TODO

  • Link related issue/PR.
  • Describe the goal of the PR. Avoid details that are clear in the diff.
  • Mark the PR as draft.
  • Include a unit test.
  • Review your own PR in "Files changed".
  • Ensure the PR branch is updated.
  • Ensure the checks pass.
  • Change the status from draft to ready.
  • Polish the PR description to reflect what it ended up doing.
  • Polish the PR title as you'd like others to read it from the git log.
  • Assign a reviewer.
  • Document user-facing changes in the changelog.

@kalashsinghal kalashsinghal marked this pull request as draft May 30, 2024 08:18
@kalashsinghal kalashsinghal requested a review from maurolepore May 30, 2024 08:18
@Tilmon
Copy link
Contributor

Tilmon commented May 30, 2024

Hi @kalashsinghal , thanks for this! Looks good to me :)

Re Dataset name:

This new dataset will have low and high thresholds for transition risk scores of around 13k activity_uuid_product_uuids. I haven't come up with a good name yet to describe this new dataset, so please suggest any good name for better understanding! At the moment, I have named the function trs_low_high_thresholds_for_ecoinvent_all which will output this new dataset.

How about calling the dataset trs_thresholds_for_ecoinvent_all, similar to the function name but shorter?
cc' @AnneSchoenauer

Copy link
Contributor

@maurolepore maurolepore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kalashsinghal

If tiltIndicator exports two internal functions (for now with # @keywords intarnal) then we can develop this feature in isolation in tiltTransitionRisk. See my comment below including a sketch of the main components of our architecture

So I suggest these next steps:

  • Move this PR to tiltTransitionRisk linking to this PR.
  • Close this PR.
  • Create a new PR in tiltIndicator exporting the two *compute_profile_ranking() functions that you need.

R/trs_low_high_thresholds_for_ecoinvent_all.R Outdated Show resolved Hide resolved
@kalashsinghal kalashsinghal changed the title Adds a new dataset with 13k uuids having low and high thresholds for transition risk score Adds add_thresholds_transition_risk() function which adds low and high thresholds for transition risk score May 31, 2024
@kalashsinghal
Copy link
Collaborator Author

ml.01 Pass threshods as arguments (to quantile())

@maurolepore The low and high thresholds are new calculated columns for transition risk scores. How you imagine I can pass them as arguments to quantile()? The quantile() only receives transition_risk_score as the argument.

Comment on lines +53 to +62
#' Calulate `transition_risk_score` and `benchmark_tr_score` columns
#'
#' @param data Dataframe.
#' @param profile_ranking Dataframe column.
#' @param reduction_targets Dataframe column.
#' @keywords internal
#' @export
create_tr_benchmarks_tr_score <- function(data, profile_ranking, reduction_targets) {
mutate(
data,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maurolepore From our discussion here, I will replace this function in tiltIndicatorAfter. Should I also copy all the tests related to this function in tiltIndicator? or the tests should remain in only tiltIndicator (one package)?

@kalashsinghal kalashsinghal marked this pull request as ready for review June 3, 2024 06:39
@kalashsinghal kalashsinghal requested a review from maurolepore June 3, 2024 06:40
@maurolepore
Copy link
Contributor

/style

@maurolepore
Copy link
Contributor

maurolepore commented Jun 3, 2024

Thanks @kalashsinghal,

I had a first look and I need to think how this new indicator fits with the existing indicators. I already have a few ideas but I need to think more before I share them.

You may install this PR in your own system and use that to draft your PR in tiltIndicatorAfter. That way you don't have to wait for me.

@maurolepore
Copy link
Contributor

@kalashsinghal

I moved this PR to 2DegreesInvesting/tiltTransitionRisk#10

It'll be easier to experiment there. Since that's a new, developer-oriented package, we can merge code before we're sure of the final interfaces.

Let's continue the conversation there.

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 this pull request may close these issues.

Create low and high thresholds for transition risk scores
3 participants