-
Notifications
You must be signed in to change notification settings - Fork 0
Creates Transition Risk Score at product level and company level
#152
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8f1bf4d
add transition risk score
kalashsinghal 93b71dc
create input examples for transition risk score
kalashsinghal 22518f9
refactor test
kalashsinghal 368168d
refactor and add two tests
kalashsinghal 925e289
address comments
kalashsinghal d03ced5
reformat code
kalashsinghal 4a0f6db
add final test
kalashsinghal 2c70f42
refactor and add tests
kalashsinghal 213b316
add test
kalashsinghal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| #' Example input datasets for Transition Risk Score | ||
| #' | ||
| #' @return A dataframe. | ||
| #' @export | ||
| #' @keywords internal | ||
| #' | ||
| #' @examples | ||
| #' example_emissions_profile_at_product_level() | ||
| #' example_sector_profile_at_product_level() | ||
| example_emissions_profile_at_product_level <- function() { | ||
| local_options(readr.show_col_types = FALSE) | ||
| toy_emissions_profile_products_ecoinvent <- | ||
| read_csv(toy_emissions_profile_products_ecoinvent()) | ||
| toy_emissions_profile_any_companies <- | ||
| read_csv(toy_emissions_profile_any_companies()) | ||
| toy_europages_companies <- read_csv(toy_europages_companies()) | ||
| toy_ecoinvent_activities <- read_csv(toy_ecoinvent_activities()) | ||
| toy_ecoinvent_europages <- read_csv(toy_ecoinvent_europages()) | ||
| toy_ecoinvent_inputs <- read_csv(toy_ecoinvent_inputs()) | ||
| toy_isic_name <- read_csv(toy_isic_name()) | ||
|
|
||
| emissions_profile_at_product_level <- profile_emissions( | ||
| companies = toy_emissions_profile_any_companies, | ||
| co2 = toy_emissions_profile_products_ecoinvent, | ||
| europages_companies = toy_europages_companies, | ||
| ecoinvent_activities = toy_ecoinvent_activities, | ||
| ecoinvent_europages = toy_ecoinvent_europages, | ||
| isic = toy_isic_name | ||
| ) |> | ||
| unnest_product() | ||
| emissions_profile_at_product_level | ||
| } | ||
|
|
||
| #' @export | ||
| #' @rdname example_emissions_profile_at_product_level | ||
| example_sector_profile_at_product_level <- function() { | ||
| local_options(readr.show_col_types = FALSE) | ||
| toy_sector_profile_any_scenarios <- | ||
| read_csv(toy_sector_profile_any_scenarios()) | ||
| toy_sector_profile_companies <- | ||
| read_csv(toy_sector_profile_companies()) | ||
| toy_europages_companies <- read_csv(toy_europages_companies()) | ||
| toy_ecoinvent_activities <- read_csv(toy_ecoinvent_activities()) | ||
| toy_ecoinvent_europages <- read_csv(toy_ecoinvent_europages()) | ||
| toy_ecoinvent_inputs <- read_csv(toy_ecoinvent_inputs()) | ||
| toy_isic_name <- read_csv(toy_isic_name()) | ||
|
|
||
| sector_profile_at_product_level <- profile_sector( | ||
| companies = toy_sector_profile_companies, | ||
| scenarios = toy_sector_profile_any_scenarios, | ||
| europages_companies = toy_europages_companies, | ||
| ecoinvent_activities = toy_ecoinvent_activities, | ||
| ecoinvent_europages = toy_ecoinvent_europages, | ||
| isic = toy_isic_name | ||
| ) |> | ||
| unnest_product() | ||
| sector_profile_at_product_level | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| #' Transition Risk Score | ||
| #' | ||
| #' Calulate Transition Risk Score at product level and company level | ||
| #' | ||
| #' @param emissions_profile_at_product_level Dataframe. Emissions profile product level output | ||
| #' @param sector_profile_at_product_level Dataframe. Sector profile product level output | ||
| #' | ||
| #' @family top-level functions | ||
| #' | ||
| #' @return A dataframe | ||
| #' @export | ||
| #' | ||
| #' @examples | ||
| #' library(dplyr) | ||
| #' library(readr, warn.conflicts = FALSE) | ||
| #' library(tiltToyData) | ||
| #' options(readr.show_col_types = FALSE) | ||
| #' | ||
| #' emissions_companies <- read_csv(toy_emissions_profile_any_companies()) | ||
| #' products <- read_csv(toy_emissions_profile_products()) | ||
| #' europages_companies <- read_csv(toy_europages_companies()) | ||
| #' ecoinvent_activities <- read_csv(toy_ecoinvent_activities()) | ||
| #' ecoinvent_europages <- read_csv(toy_ecoinvent_europages()) | ||
| #' isic_name <- read_csv(toy_isic_name()) | ||
| #' | ||
| #' emissions_profile_at_product_level <- profile_emissions( | ||
| #' companies = emissions_companies, | ||
| #' co2 = products, | ||
| #' europages_companies = europages_companies, | ||
| #' ecoinvent_activities = ecoinvent_activities, | ||
| #' ecoinvent_europages = ecoinvent_europages, | ||
| #' isic = isic_name | ||
| #' ) |> unnest_product() | ||
| #' | ||
| #' sector_companies <- read_csv(toy_sector_profile_companies()) | ||
| #' scenarios <- read_csv(toy_sector_profile_any_scenarios()) | ||
| #' | ||
| #' sector_profile_at_product_level <- profile_sector( | ||
| #' companies = sector_companies, | ||
| #' scenarios = scenarios, | ||
| #' europages_companies = europages_companies, | ||
| #' ecoinvent_activities = ecoinvent_activities, | ||
| #' ecoinvent_europages = ecoinvent_europages, | ||
| #' isic = isic_name | ||
| #' ) |> unnest_product() | ||
| #' | ||
| #' result <- score_transition_risk(emissions_profile_at_product_level, sector_profile_at_product_level) | ||
| #' | ||
| #' result |> unnest_product() | ||
| #' | ||
| #' result |> unnest_company() | ||
| score_transition_risk <- | ||
| function(emissions_profile_at_product_level, | ||
| sector_profile_at_product_level) { | ||
| union_emissions_sector_rows <- | ||
| get_rows_union_for_common_cols( | ||
| emissions_profile_at_product_level, | ||
| sector_profile_at_product_level | ||
| ) | ||
| trs_emissions <- | ||
| prepare_trs_emissions(emissions_profile_at_product_level) | ||
| trs_sector <- prepare_trs_sector(sector_profile_at_product_level) | ||
|
|
||
| trs_product <- | ||
| full_join_emmissions_sector(trs_emissions, trs_sector) |> | ||
| create_tr_benchmarks_tr_score() |> | ||
| select(-c("scenario_year", "benchmark")) |> | ||
| left_join( | ||
| union_emissions_sector_rows, | ||
| by = c("companies_id", "ep_product", "activity_uuid_product_uuid") | ||
| ) |> | ||
| relocate(relocate_trs_columns(product_level_trs_ranking_reduction_columns())) |> | ||
| distinct() | ||
|
|
||
| trs_company <- trs_product |> | ||
| select( | ||
| trs_company_columns(), | ||
| product_level_trs_ranking_reduction_columns() | ||
| ) |> | ||
| create_benchmarks_averages() |> | ||
| select(-product_level_trs_ranking_reduction_columns()) |> | ||
| relocate(relocate_trs_columns(trs_company_avg_columns())) |> | ||
| distinct() | ||
|
|
||
| nest_levels(trs_product, trs_company) | ||
| } | ||
|
|
||
| create_tr_benchmarks_tr_score <- function(data) { | ||
| mutate( | ||
| data, | ||
| transition_risk_score = ifelse( | ||
| is.na(.data$profile_ranking) | is.na(.data$reduction_targets), | ||
| NA, | ||
| (.data$profile_ranking + .data$reduction_targets) / 2 | ||
| ), | ||
| benchmark_tr_score = ifelse( | ||
| is.na(.data$scenario_year) | is.na(.data$benchmark), | ||
| NA, | ||
| paste(.data$scenario_year, .data$benchmark, sep = "_") | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| create_benchmarks_averages <- function(data) { | ||
| mutate( | ||
| data, | ||
| transition_risk_score_avg = mean(.data$transition_risk_score, na.rm = TRUE), | ||
| reduction_targets_avg = mean(.data$reduction_targets, na.rm = TRUE), | ||
| profile_ranking_avg = mean(.data$profile_ranking, na.rm = TRUE), | ||
| .by = c("companies_id", "benchmark_tr_score") | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| prepare_trs_emissions <- function(data) { | ||
| select( | ||
| data, | ||
| c( | ||
| "companies_id", | ||
| "benchmark", | ||
| "profile_ranking", | ||
| "ep_product", | ||
| "activity_uuid_product_uuid" | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| prepare_trs_sector <- function(data) { | ||
| data |> | ||
| select( | ||
| c( | ||
| "companies_id", | ||
| "scenario", | ||
| "year", | ||
| "reduction_targets", | ||
| "ep_product", | ||
| "activity_uuid_product_uuid" | ||
| ) | ||
| ) |> | ||
| mutate(scenario_year = paste(.data$scenario, .data$year, sep = "_")) |> | ||
| select(-c("scenario", "year")) | ||
| } | ||
|
|
||
| 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" | ||
| ) | ||
| } | ||
|
|
||
| get_rows_union_for_common_cols <- | ||
| function(emissions_at_product_level, | ||
| sector_at_product_level) { | ||
| emission_common_columns <- emissions_at_product_level |> | ||
| select(common_columns_emissions_sector_at_product_level()) |> | ||
| distinct() | ||
|
|
||
| sector_common_columns <- sector_at_product_level |> | ||
| select(common_columns_emissions_sector_at_product_level()) |> | ||
| distinct() | ||
|
|
||
| distinct(bind_rows(emission_common_columns, sector_common_columns)) | ||
| } | ||
|
|
||
| relocate_trs_columns <- function(columns) { | ||
| c( | ||
| "companies_id", | ||
| "company_name", | ||
| "country", | ||
| "benchmark_tr_score", | ||
| columns | ||
| ) | ||
| } | ||
|
|
||
| product_level_trs_ranking_reduction_columns <- function() { | ||
| c( | ||
| "transition_risk_score", | ||
| "profile_ranking", | ||
| "reduction_targets" | ||
| ) | ||
| } | ||
|
|
||
| trs_company_columns <- function() { | ||
| c(common_columns_emissions_sector_at_company_level(), | ||
| "benchmark_tr_score" | ||
| ) | ||
| } | ||
|
|
||
| trs_company_avg_columns <- function() { | ||
| c( | ||
| "transition_risk_score_avg", | ||
| "profile_ranking_avg", | ||
| "reduction_targets_avg" | ||
| ) | ||
| } | ||
|
|
||
| trs_company_output_columns <- function() { | ||
| c( | ||
| trs_company_columns(), | ||
| trs_company_avg_columns() | ||
| ) | ||
| } | ||
|
|
||
| trs_product_output_columns <- function() { | ||
| c( | ||
| common_columns_emissions_sector_at_product_level(), | ||
| product_level_trs_ranking_reduction_columns(), | ||
| "benchmark_tr_score" | ||
| ) | ||
| } | ||
|
|
||
| common_columns_emissions_sector_at_product_level <- function() { | ||
| c( | ||
| "companies_id", | ||
| "company_name", | ||
| "country", | ||
| "ep_product", | ||
| "matched_activity_name", | ||
| "matched_reference_product", | ||
| "unit", | ||
| "multi_match", | ||
| "matching_certainty", | ||
| "matching_certainty_company_average", | ||
| "company_city", | ||
| "postcode", | ||
| "address", | ||
| "main_activity", | ||
| "activity_uuid_product_uuid", | ||
| "tilt_sector", | ||
| "tilt_subsector", | ||
| "isic_4digit", | ||
| "isic_4digit_name", | ||
| "ei_geography" | ||
| ) | ||
| } | ||
|
|
||
| common_columns_emissions_sector_at_company_level <- function() { | ||
| c( | ||
| "companies_id", | ||
| "company_name", | ||
| "country", | ||
| "company_city", | ||
| "postcode", | ||
| "address", | ||
| "main_activity" | ||
| ) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.