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

Creates Transition Risk Score at product level and company level #152

Merged
merged 9 commits into from
Feb 13, 2024

Conversation

kalashsinghal
Copy link
Collaborator

@kalashsinghal kalashsinghal commented Feb 5, 2024

closes #149
closes #151

Dear @maurolepore @AnneSchoenauer

This PR creates the Transition Risk Score at product and company levels. Here is the reprex of the outputs:

library(readr)
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
devtools::load_all(".")
#> ℹ Loading tiltIndicatorAfter
options(width = 500)

emissions_profile_at_product_level <- tiltIndicatorAfter:::example_emissions_profile_at_product_level() |>
  filter(companies_id %in% c("antimonarchy_canine", "nonphilosophical_llama"))
sector_profile_at_product_level <- tiltIndicatorAfter:::example_sector_profile_at_product_level() |>
  filter(companies_id %in% c("celestial_lovebird", "nonphilosophical_llama"))

trs_product <- tiltIndicatorAfter:::score_transition_risk(emissions_profile_at_product_level, sector_profile_at_product_level) |>
  unnest_product()
trs_product |> 
  print(n = Inf)
#> # A tibble: 53 × 24
#>    companies_id           company_name           country benchmark_tr_score             transition_risk_score profile_ranking reduction_targets ep_product                  activity_uuid_product_uuid           matched_activity_name                                         matched_reference_product                          unit  multi_match matching_certainty matching_certainty_company…¹ company_city postcode address main_activity tilt_sector tilt_subsector isic_4digit isic_4digit_name ei_geography
#>    <chr>                  <chr>                  <chr>   <chr>                                          <dbl>           <dbl>             <dbl> <chr>                       <chr>                                <chr>                                                         <chr>                                              <chr> <lgl>       <chr>              <chr>                        <chr>        <chr>    <chr>   <chr>         <chr>       <chr>          <chr>       <chr>            <chr>       
#>  1 antimonarchy_canine    antimonarchy_canine    germany <NA>                                          NA               1                 NA    tent                        76269c17-78d6-420b-991a-aa38c51b45b7 market for shed, large, wood, non-insulated, fire-unprotected shed, large, wood, non-insulated, fire-unprotected m2    FALSE       low                low                          hamburg      22525    schnac… distributor   constructi… construction … '4100'      Construction of… tilt_world  
#>  2 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_all                              0.212           0.333              0.09 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#>  3 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_all                              0.642           0.333              0.95 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#>  4 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_all                               0.277           0.333              0.22 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#>  5 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_all                               0.647           0.333              0.96 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#>  6 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_isic_4digit                      0.545           1                  0.09 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#>  7 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_isic_4digit                      0.975           1                  0.95 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#>  8 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_isic_4digit                       0.61            1                  0.22 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#>  9 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_isic_4digit                       0.98            1                  0.96 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 10 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_tilt_sector                      0.295           0.5                0.09 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 11 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_tilt_sector                      0.725           0.5                0.95 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 12 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_tilt_sector                       0.36            0.5                0.22 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 13 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_tilt_sector                       0.73            0.5                0.96 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 14 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_unit                             0.295           0.5                0.09 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 15 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_unit                             0.725           0.5                0.95 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 16 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_unit                              0.36            0.5                0.22 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 17 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_unit                              0.73            0.5                0.96 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 18 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_unit_isic_4digit                 0.545           1                  0.09 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 19 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_unit_isic_4digit                 0.975           1                  0.95 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 20 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_unit_isic_4digit                  0.61            1                  0.22 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 21 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_unit_isic_4digit                  0.98            1                  0.96 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 22 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_unit_tilt_sector                 0.295           0.5                0.09 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 23 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_unit_tilt_sector                 0.725           0.5                0.95 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 24 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_unit_tilt_sector                  0.36            0.5                0.22 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 25 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_unit_tilt_sector                  0.73            0.5                0.96 surface finishing, galvanic 833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 26 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_all                              0.212           0.333              0.09 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 27 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_all                              0.642           0.333              0.95 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 28 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_all                               0.277           0.333              0.22 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 29 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_all                               0.647           0.333              0.96 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 30 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_isic_4digit                      0.545           1                  0.09 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 31 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_isic_4digit                      0.975           1                  0.95 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 32 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_isic_4digit                       0.61            1                  0.22 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 33 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_isic_4digit                       0.98            1                  0.96 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 34 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_tilt_sector                      0.295           0.5                0.09 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 35 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_tilt_sector                      0.725           0.5                0.95 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 36 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_tilt_sector                       0.36            0.5                0.22 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 37 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_tilt_sector                       0.73            0.5                0.96 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 38 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_unit                             0.295           0.5                0.09 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 39 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_unit                             0.725           0.5                0.95 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 40 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_unit                              0.36            0.5                0.22 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 41 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_unit                              0.73            0.5                0.96 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 42 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_unit_isic_4digit                 0.545           1                  0.09 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 43 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_unit_isic_4digit                 0.975           1                  0.95 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 44 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_unit_isic_4digit                  0.61            1                  0.22 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 45 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_unit_isic_4digit                  0.98            1                  0.96 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 46 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_unit_tilt_sector                 0.295           0.5                0.09 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 47 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_unit_tilt_sector                 0.725           0.5                0.95 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 48 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_unit_tilt_sector                  0.36            0.5                0.22 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 49 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_unit_tilt_sector                  0.73            0.5                0.96 surface engineering         833caa78-30df-4374-900f-7f88ab44075b market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    FALSE       low                low                          solms        35606    indust… distributor   metals      other metals   '2591'      Forging, pressi… tilt_world  
#> 50 celestial_lovebird     celestial_lovebird     spain   <NA>                                          NA              NA                  0.18 table hire for parties      76269c17-78d6-420b-991a-aa38c51b45b7 market for shed, large, wood, non-insulated, fire-unprotected shed, large, wood, non-insulated, fire-unprotected m2    FALSE       low                low                          madrid       28946    c/ lit… wholesaler    constructi… construction … '4100'      Construction of… tilt_world  
#> 51 celestial_lovebird     celestial_lovebird     spain   <NA>                                          NA              NA                  0.98 table hire for parties      76269c17-78d6-420b-991a-aa38c51b45b7 market for shed, large, wood, non-insulated, fire-unprotected shed, large, wood, non-insulated, fire-unprotected m2    FALSE       low                low                          madrid       28946    c/ lit… wholesaler    constructi… construction … '4100'      Construction of… tilt_world  
#> 52 celestial_lovebird     celestial_lovebird     spain   <NA>                                          NA              NA                  0.4  table hire for parties      76269c17-78d6-420b-991a-aa38c51b45b7 market for shed, large, wood, non-insulated, fire-unprotected shed, large, wood, non-insulated, fire-unprotected m2    FALSE       low                low                          madrid       28946    c/ lit… wholesaler    constructi… construction … '4100'      Construction of… tilt_world  
#> 53 celestial_lovebird     celestial_lovebird     spain   <NA>                                          NA              NA                  0.97 table hire for parties      76269c17-78d6-420b-991a-aa38c51b45b7 market for shed, large, wood, non-insulated, fire-unprotected shed, large, wood, non-insulated, fire-unprotected m2    FALSE       low                low                          madrid       28946    c/ lit… wholesaler    constructi… construction … '4100'      Construction of… tilt_world  
#> # ℹ abbreviated name: ¹​matching_certainty_company_average

trs_company <- tiltIndicatorAfter:::score_transition_risk(emissions_profile_at_product_level, sector_profile_at_product_level) |>
  unnest_company()
trs_company |> 
  print(n = Inf)
#> # A tibble: 26 × 9
#>    companies_id           company_name           country benchmark_tr_score             transition_risk_score_avg company_city postcode address                                    main_activity
#>    <chr>                  <chr>                  <chr>   <chr>                                              <dbl> <chr>        <chr>    <chr>                                      <chr>        
#>  1 antimonarchy_canine    antimonarchy_canine    germany <NA>                                             NaN     hamburg      22525    schnackenburgallee 217-223 | 22525 hamburg distributor  
#>  2 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_all                                  0.212 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#>  3 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_all                                  0.642 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#>  4 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_all                                   0.277 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#>  5 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_all                                   0.647 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#>  6 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_isic_4digit                          0.545 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#>  7 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_isic_4digit                          0.975 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#>  8 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_isic_4digit                           0.61  solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#>  9 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_isic_4digit                           0.98  solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 10 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_tilt_sector                          0.295 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 11 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_tilt_sector                          0.725 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 12 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_tilt_sector                           0.36  solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 13 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_tilt_sector                           0.73  solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 14 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_unit                                 0.295 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 15 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_unit                                 0.725 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 16 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_unit                                  0.36  solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 17 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_unit                                  0.73  solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 18 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_unit_isic_4digit                     0.545 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 19 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_unit_isic_4digit                     0.975 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 20 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_unit_isic_4digit                      0.61  solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 21 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_unit_isic_4digit                      0.98  solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 22 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2030_unit_tilt_sector                     0.295 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 23 nonphilosophical_llama nonphilosophical_llama germany 1.5C RPS_2050_unit_tilt_sector                     0.725 solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 24 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2030_unit_tilt_sector                      0.36  solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 25 nonphilosophical_llama nonphilosophical_llama germany NZ 2050_2050_unit_tilt_sector                      0.73  solms        35606    industriegebiet oberbiel | 35606 solms     distributor  
#> 26 celestial_lovebird     celestial_lovebird     spain   <NA>                                             NaN     madrid       28946    c/ litio 17 | 28946 madrid                 wholesaler

Created on 2024-02-13 with reprex v2.0.2

cc' @Tilmon


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 title and description.
  • Assign a reviewer.

EXCEPTIONS

  • Slide here any item that you intentionally choose to not do.

@kalashsinghal kalashsinghal marked this pull request as draft February 5, 2024 12:13
@kalashsinghal kalashsinghal marked this pull request as ready for review February 6, 2024 09:09
@kalashsinghal
Copy link
Collaborator Author

@AnneSchoenauer @Tilmon You can see the complete sample outputs of transition risk score at product and company level through these files:
trs_company.xlsx
trs_product.xlsx

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

Here are my comments. I focused on superficial things for now. The concepts that this function implements are best captured in tests and discussed with Anne/Tilman.

@kalashsinghal kalashsinghal marked this pull request as draft February 8, 2024 08:45
@kalashsinghal kalashsinghal marked this pull request as ready for review February 8, 2024 12:12
maurolepore
maurolepore previously approved these changes Feb 8, 2024
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.

LGTM,
Thanks @kalashsinghal.
Maybe discuss with @AnneSchoenauer the more conceptual aspects of this PR, e.g. reviewing the documentation of the new exported function is complete and clear, and that the tests capture the behaviour sufficiently.

Copy link

@AnneSchoenauer AnneSchoenauer left a comment

Choose a reason for hiding this comment

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

Hi @kalashsinghal, Could you please create also some tests that test the averaging functions? Please see my comment above that the average score, the average reduction targets and the average profile ranking should be calcualted regardless of the benchmark_score grouping. I asked Chat GPT what could be nice ways of testing if the averages are correct. Here is what it said. I really like this. Also do we need tests for the correct input data that we need? Or tests for merging? Thanks a lot for your work on this!

Chat GPT:
For the Transition Risk Score Calculation verification, we can design a series of test cases that specifically target the calculation logic of the score_transition_risk function. Below, I provide concrete examples for each scenario mentioned.

Test Setup

Before we dive into the specific tests, let's establish a base setup that these tests might follow. The setup involves creating mock data that mimics the structure expected by the score_transition_risk function, including emissions profile and sector profile at the product level.

1. All Input Data Available and Correctly Formatted

Objective: Ensure the function correctly calculates transition risk scores when all required data is present and correctly formatted.

Test Steps:

  1. Create mock emissions and sector profiles with known values where the calculation of the transition risk score is straightforward (e.g., profile_ranking and reduction_targets values that result in simple averages).
  2. Call score_transition_risk with these profiles.
  3. Assert that the calculated transition_risk_score matches expected values calculated manually.

Example Mock Data:

  • Emissions Profile: profile_ranking of 70 for all products.
  • Sector Profile: reduction_targets of 30 for all products.

Expected Outcome:

  • The transition_risk_score should be (70 + 30) / 2 = 50 for all products.

2. Missing or Incomplete Data for Some Products or Companies

Objective: Test how the function handles scenarios where some data points (e.g., reduction_targets or profile_ranking) are missing.

Test Steps:

  1. Create mock data where some products have NULL or NA for either profile_ranking or reduction_targets.
  2. Call score_transition_risk with this data.
  3. Verify that the function handles these cases gracefully, either by excluding these products from the score calculation or by appropriately calculating scores with available data.

Expected Outcome:

  • Products with complete data are scored correctly.
  • Products with missing data are handled as per the function's design (e.g., scores set to NA or calculated using available data).

3. Edge Cases Where the Input Data Might Lead to Division by Zero or Other Mathematical Errors

Objective: Ensure the function is robust against edge cases that could result in mathematical errors, such as division by zero.

Test Steps:

  1. Create mock data where profile_ranking and reduction_targets could lead to situations that typically result in mathematical errors (e.g., both are zero, leading to a potential division by zero in some calculation approaches).
  2. Execute score_transition_risk with this dataset.
  3. Confirm that the function either avoids these errors through checks and balances or handles them elegantly (e.g., setting scores to a default value or NA).

Expected Outcome:

  • The function completes execution without errors.
  • Products with problematic data are handled in a predefined manner, ensuring the integrity of the scoring process.

Implementing the Tests

These tests can be implemented using a testing framework appropriate for your development environment (e.g., testthat for R). For each test, you would craft the specific mock data sets, run the score_transition_risk function, and then assert that the outcomes match your expectations. This approach ensures that the function behaves correctly across a wide range of scenarios, enhancing the reliability and accuracy of your transition risk scoring methodology.

@kalashsinghal
Copy link
Collaborator Author

@AnneSchoenauer I have removed averaging of profile_ranking and reduction_targets at company level in this PR. A separate ticket is created to complete this task. Does it sounds good?

Regarding tests suggestions from Chat GPT:

All Input Data Available and Correctly Formatted

Tests Added

Missing or Incomplete Data for Some Products or Companies

Tests added

Edge Cases Where the Input Data Might Lead to Division by Zero or Other Mathematical Errors

This objective is not applicable for score_transition_risk function because we are just averaging two columns and there is no possibility of division by zero. Such tests are also not added by Mauro, hence I prefer not to add it. Is it ok?

@AnneSchoenauer
Copy link

Okay thanks for your comments - make sense! Is this also the case when there are only NAs so let’s assume that we couldn’t match any ep_product to the Ecoinvent product would the code behave properly?

@AnneSchoenauer
Copy link

And one last comment! I noticed that the philo… company has two products but they are matched with the same Ecoinvent activity for this it is a bit hard to check if the average on company level is taken correctly. Could you just provide another example with a company with different products and so with different sector reduction targets and profile ranking?

Does the code also behave well if from 5 products 2 have distinct sector reductions and profile ranking, 1 product was not being able to match with reduction_targets and 1 product doesn’t have a profile_ranking and 1 product was not able to be matched with Ecoinvent and doesn’t have a reduction target?

Just to check again if also some „edge cases“ or more complicated cases will work.

@kalashsinghal
Copy link
Collaborator Author

kalashsinghal commented Feb 13, 2024

@AnneSchoenauer In the sample data we have, there is not even a single company which has more than 2 products, hence to test the edge cases you suggested I formulated a test. Please have a look at it and let me know if it covers every case!

Please note that I am joining the sector and profile dataframes using columns c("companies_id", "ep_product", "activity_uuid_product_uuid"). Code for your reference:

full_join_emmissions_sector <- function(emissions, sector) {
  full_join(
    emissions,
    sector,
    by = c("companies_id", "ep_product", "activity_uuid_product_uuid"),
    relationship = "many-to-many"
  )
}

@kalashsinghal
Copy link
Collaborator Author

kalashsinghal commented Feb 13, 2024

Is this also the case when there are only NAs so let’s assume that we couldn’t match any ep_product to the Ecoinvent product would the code behave properly?

Yes the code will behave properly, I have added a test for this

Does the code also behave well if from 5 products 2 have distinct sector reductions and profile ranking, 1 product was not being able to match with reduction_targets and 1 product doesn’t have a profile_ranking and 1 product was not able to be matched with Ecoinvent and doesn’t have a reduction target?

I have tried to catch this case using the added test. Please review!

@AnneSchoenauer
Copy link

Great @kalashsinghal approved :) Good work :)

@kalashsinghal kalashsinghal merged commit f2d5a5d into main Feb 13, 2024
7 checks passed
@kalashsinghal kalashsinghal deleted the 149_151_transition_risk_score branch February 13, 2024 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants