From 02a588dd2f11209b0b195d5de96cf5b8992f68b0 Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Mon, 10 May 2021 13:46:07 +0200 Subject: [PATCH 01/22] calculate targets prior to summarizing --- R/target_market_share.R | 116 +++++++++++++++++++++++++--------------- 1 file changed, 72 insertions(+), 44 deletions(-) diff --git a/R/target_market_share.R b/R/target_market_share.R index c52d3c72..1af22b82 100644 --- a/R/target_market_share.R +++ b/R/target_market_share.R @@ -144,54 +144,32 @@ target_market_share <- function(data, return(empty_target_market_share_output()) } - summary_groups <- c( - "scenario", - "tmsr", - "smsp", - "region", - "scenario_source", - "name_ald" - ) - - if (weight_production) { - data <- summarize_weighted_production( - data, - !!!rlang::syms(summary_groups), - use_credit_limit = use_credit_limit - ) - } else { - data <- summarize_unweighted_production( - data, - !!!rlang::syms(summary_groups) - ) - } - target_groups <- c("sector_ald", "scenario", "region", "name_ald") data <- data %>% group_by(!!!rlang::syms(c(target_groups, "year"))) %>% - mutate(sector_weighted_production = sum(.data$weighted_production)) %>% + mutate(sector_production = sum(.data$production)) %>% arrange(.data$year) %>% group_by(!!!rlang::syms(target_groups)) %>% - mutate(initial_sector_production = first(.data$sector_weighted_production)) %>% - select(-.data$sector_weighted_production) + mutate(initial_sector_production = first(.data$sector_production)) %>% + select(-.data$sector_production) data <- data %>% group_by(!!!rlang::syms(c(target_groups, "technology", "year"))) %>% - mutate(technology_weighted_production = sum(.data$weighted_production)) %>% + mutate(technology_production = sum(.data$production)) %>% arrange(.data$year) %>% group_by(!!!rlang::syms(c(target_groups, "technology"))) %>% - mutate(initial_technology_production = first(.data$technology_weighted_production)) %>% - select(-.data$technology_weighted_production) + mutate(initial_technology_production = first(.data$technology_production)) %>% + select(-.data$technology_production) green_or_brown <- r2dii.data::green_or_brown tmsr_or_smsp <- tmsr_or_smsp() data <- data %>% mutate( - tmsr_target_weighted_production = .data$initial_technology_production * + tmsr_target_production = .data$initial_technology_production * .data$tmsr, - smsp_target_weighted_production = .data$initial_technology_production + + smsp_target_production = .data$initial_technology_production + (.data$initial_sector_production * .data$smsp) ) %>% select( @@ -204,10 +182,10 @@ target_market_share <- function(data, ) %>% pivot_longer( cols = c( - "tmsr_target_weighted_production", "smsp_target_weighted_production" + "tmsr_target_production", "smsp_target_production" ), names_to = "target_name", - values_to = "weighted_production_target" + values_to = "production_target" ) %>% left_join(tmsr_or_smsp, by = c(target_name = "which_metric")) %>% inner_join( @@ -220,6 +198,60 @@ target_market_share <- function(data, ) %>% select(-.data$target_name, -.data$green_or_brown) + summary_groups <- c( + "scenario", + "region", + "scenario_source", + "name_ald" + ) + + if (weight_production) { + data <- data %>% + ungroup() %>% + add_loan_weight(use_credit_limit = use_credit_limit) %>% + add_technology_share() %>% + add_technology_share_target() %>% + calculate_weighted_loan_metric("production") %>% + calculate_weighted_loan_metric("technology_share") %>% + calculate_weighted_loan_metric("production_target") %>% + calculate_weighted_loan_metric("technology_share_target") %>% + group_by( + .data$sector_ald, + .data$technology, + .data$year, + !!!rlang::syms(summary_groups) + ) %>% + summarize( + weighted_production = sum(.data$weighted_loan_production), + weighted_technology_share = sum(.data$weighted_loan_technology_share), + weighted_production_target = sum(.data$weighted_loan_production_target), + weighted_technology_share_target = sum(.data$weighted_loan_technology_share_target) + ) %>% + # Restore old groups + group_by(!!!dplyr::groups(data)) + } else { + data <- data %>% + select(-c( + .data$id_loan, + .data$loan_size_credit_limit, + .data$loan_size_outstanding + )) %>% + distinct() %>% + group_by(.data$sector_ald, .data$technology, .data$year, !!!rlang::syms(summary_groups)) %>% + # FIXME: Confusing: `weighted_production` holds unweighted_production? + summarize( + weighted_production = .data$production, + weighted_production_target = .data$production_target, + .groups = "keep" + ) %>% + ungroup(.data$technology) %>% + mutate( + weighted_technology_share = .data$weighted_production / sum(.data$weighted_production), + weighted_technology_share_target = .data$weighted_production_target / sum(.data$weighted_production_target) + ) %>% + group_by(!!!dplyr::groups(data)) + } + if (!by_company) { aggregate_company_groups <- c( "sector_ald", @@ -235,7 +267,8 @@ target_market_share <- function(data, summarize( weighted_production = sum(.data$weighted_production), weighted_production_target = sum(.data$weighted_production_target), - weighted_technology_share = sum(.data$weighted_technology_share) + weighted_technology_share = sum(.data$weighted_technology_share), + weighted_technology_share_target = sum(.data$weighted_technology_share_target) ) } @@ -249,14 +282,6 @@ target_market_share <- function(data, !!!rlang::syms(reweighting_groups) ) - data <- data %>% - group_by(!!!rlang::syms(reweighting_groups)) %>% - mutate( - .x = .data$weighted_production_target, - weighted_technology_share_target = .data$.x / sum(.data$.x), - .x = NULL - ) - data <- data %>% pivot_wider2( names_from = .data$scenario, @@ -306,8 +331,8 @@ unnest_list_columns <- function(data) { tmsr_or_smsp <- function() { dplyr::tribble( ~which_metric, ~green_or_brown, - "tmsr_target_weighted_production", "brown", - "smsp_target_weighted_production", "green" + "tmsr_target_production", "brown", + "smsp_target_production", "green" ) } @@ -397,8 +422,11 @@ reweight_technology_share <- function(data, ...) { group_by(...) %>% mutate( .x = .data$weighted_technology_share, + .y = .data$weighted_technology_share_target, weighted_technology_share = .data$.x / sum(.data$.x), - .x = NULL + weighted_technology_share_target = .data$.y / sum(.data$.y), + .x = NULL, + .y = NULL ) %>% ungroup() } From f27edf835cc0ac7e7e533f9e5c47483495541fed Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Mon, 10 May 2021 13:46:27 +0200 Subject: [PATCH 02/22] add function to deal with tech_share_targets --- R/summarize_weighted_production.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/R/summarize_weighted_production.R b/R/summarize_weighted_production.R index c80d1ed4..6ee92bd6 100644 --- a/R/summarize_weighted_production.R +++ b/R/summarize_weighted_production.R @@ -211,6 +211,18 @@ add_technology_share <- function(data) { group_by(!!!dplyr::groups(data)) } +add_technology_share_target <- function(data) { + crucial <- c("production_target", "sector_ald", "year", "technology") + + check_crucial_names(data, crucial) + walk_(crucial, ~ check_no_value_is_missing(data, .x)) + + data %>% + group_by(.data$sector_ald, .data$year, .data$scenario, .data$name_ald) %>% + mutate(technology_share_target = .data$production_target / sum(.data$production_target)) %>% + group_by(!!!dplyr::groups(data)) +} + check_zero_initial_production <- function(data) { companies_with_zero_initial_production <- data %>% group_by(.data$technology, .data$name_ald, .data$year) %>% From 6fce3fe0594f99e1c08b35218f6e0550ccc4dc8e Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Mon, 10 May 2021 14:00:06 +0200 Subject: [PATCH 03/22] add failing test --- tests/testthat/test-target_market_share.R | 61 +++++++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-target_market_share.R b/tests/testthat/test-target_market_share.R index c7be2dbb..209490b0 100644 --- a/tests/testthat/test-target_market_share.R +++ b/tests/testthat/test-target_market_share.R @@ -201,11 +201,12 @@ test_that("with known input outputs as expected, at company level", { production = c(10, 30, 20, 20, 90, 95, 100, 100) ) + #FIXME: duplicated entries scenario scenario <- fake_scenario( - technology = c("electric", "ice", "electric", "ice", "electric", "ice", "electric", "ice"), - year = c(2020, 2020, 2021, 2021, 2020, 2020, 2021, 2021), - tmsr = c(1, 1, 1.85, 0.6, 1, 1, 1.85, 0.6), - smsp = c(0, 0, 0.34, -0.2, 0, 0, 0.34, -0.2) + technology = c("electric", "ice", "electric", "ice"), + year = c(2020, 2020, 2021, 2021), + tmsr = c(1, 1, 1.85, 0.6), + smsp = c(0, 0, 0.34, -0.2) ) out <- target_market_share( @@ -885,3 +886,55 @@ test_that("projects technology share as 'production / total production' when .production ) }) + +test_that("Initial value of technology_share consistent between `projected` and + `target_*` (#277)", { + + matched <- fake_matched( + id_loan = c("L1", "L2"), + name_ald = c("company a", "company b") + ) + + ald <- fake_ald( + name_company = c("company a", "company b", "company a", "company b"), + technology = c("ice", "ice", "electric", "electric"), + production = c(100, 1, 100, 3), + year = 2020 + ) + + scenario <- fake_scenario( + technology = c("ice", "electric"), + year = 2020, + tmsr = 1, + smsp = 0 + ) + + out <- target_market_share( + matched, + ald, + scenario, + region_isos_stable + ) %>% + filter( + metric %in% c("projected", "target_sds"), + year == 2020 + ) %>% + select(technology, metric, technology_share) + + out_ice <- out %>% + filter(technology == "ice") %>% + split(.$metric) + + out_electric <- out %>% + filter(technology == "electric") %>% + split(.$metric) + + expect_equal( + out_ice$projected$technology_share, + out_ice$target_sds$technology_share + ) + expect_equal( + out_electric$projected$technology_share, + out_electric$target_sds$technology_share + ) + }) From 54509b7e83dfcd842f33d82d691bd9f6d4e6d4bb Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Mon, 10 May 2021 14:36:48 +0200 Subject: [PATCH 04/22] prevent double counting over `region` --- R/target_market_share.R | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/R/target_market_share.R b/R/target_market_share.R index 1af22b82..96bbe772 100644 --- a/R/target_market_share.R +++ b/R/target_market_share.R @@ -140,6 +140,29 @@ target_market_share <- function(data, data <- join_ald_scenario(data, ald, scenario, region_isos) + crucial_groups <- c( + "id_loan", + "loan_size_outstanding", + "loan_size_outstanding_currency", + "loan_size_credit_limit", + "loan_size_credit_limit_currency", + "name_ald", + "sector_ald", + "technology", + "year", + "scenario", + "region", + "tmsr", + "smsp", + "scenario_source" + ) + + data <- data %>% + group_by(!!!rlang::syms(crucial_groups)) %>% + summarize( + production = sum(.data$production) + ) + if (nrow(data) == 0) { return(empty_target_market_share_output()) } From 4e01902358779d6d1eb939824d20411f5801b31c Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Mon, 10 May 2021 14:38:47 +0200 Subject: [PATCH 05/22] update expected values with correct tech_share --- tests/testthat/test-target_market_share.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-target_market_share.R b/tests/testthat/test-target_market_share.R index 209490b0..1f982541 100644 --- a/tests/testthat/test-target_market_share.R +++ b/tests/testthat/test-target_market_share.R @@ -466,7 +466,7 @@ test_that("with known input outputs `technology_share` as expected (#184, #262)" expect_equal( out$target_sds$technology_share, - c(0.923, 0.076), + c(0.914, 0.086), tolerance = 1e-3 ) From 697e9b01cdeea0e2f209ada7f71b52aca22fc8d0 Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Mon, 10 May 2021 14:40:15 +0200 Subject: [PATCH 06/22] style --- R/target_market_share.R | 6 +- tests/testthat/test-target_market_share.R | 97 +++++++++++------------ 2 files changed, 51 insertions(+), 52 deletions(-) diff --git a/R/target_market_share.R b/R/target_market_share.R index 96bbe772..8e045ab6 100644 --- a/R/target_market_share.R +++ b/R/target_market_share.R @@ -243,7 +243,7 @@ target_market_share <- function(data, .data$technology, .data$year, !!!rlang::syms(summary_groups) - ) %>% + ) %>% summarize( weighted_production = sum(.data$weighted_loan_production), weighted_technology_share = sum(.data$weighted_loan_technology_share), @@ -266,12 +266,12 @@ target_market_share <- function(data, weighted_production = .data$production, weighted_production_target = .data$production_target, .groups = "keep" - ) %>% + ) %>% ungroup(.data$technology) %>% mutate( weighted_technology_share = .data$weighted_production / sum(.data$weighted_production), weighted_technology_share_target = .data$weighted_production_target / sum(.data$weighted_production_target) - ) %>% + ) %>% group_by(!!!dplyr::groups(data)) } diff --git a/tests/testthat/test-target_market_share.R b/tests/testthat/test-target_market_share.R index 1f982541..9c8db5f8 100644 --- a/tests/testthat/test-target_market_share.R +++ b/tests/testthat/test-target_market_share.R @@ -201,7 +201,7 @@ test_that("with known input outputs as expected, at company level", { production = c(10, 30, 20, 20, 90, 95, 100, 100) ) - #FIXME: duplicated entries scenario + # FIXME: duplicated entries scenario scenario <- fake_scenario( technology = c("electric", "ice", "electric", "ice"), year = c(2020, 2020, 2021, 2021), @@ -889,52 +889,51 @@ test_that("projects technology share as 'production / total production' when test_that("Initial value of technology_share consistent between `projected` and `target_*` (#277)", { + matched <- fake_matched( + id_loan = c("L1", "L2"), + name_ald = c("company a", "company b") + ) + + ald <- fake_ald( + name_company = c("company a", "company b", "company a", "company b"), + technology = c("ice", "ice", "electric", "electric"), + production = c(100, 1, 100, 3), + year = 2020 + ) + + scenario <- fake_scenario( + technology = c("ice", "electric"), + year = 2020, + tmsr = 1, + smsp = 0 + ) + + out <- target_market_share( + matched, + ald, + scenario, + region_isos_stable + ) %>% + filter( + metric %in% c("projected", "target_sds"), + year == 2020 + ) %>% + select(technology, metric, technology_share) + + out_ice <- out %>% + filter(technology == "ice") %>% + split(.$metric) + + out_electric <- out %>% + filter(technology == "electric") %>% + split(.$metric) - matched <- fake_matched( - id_loan = c("L1", "L2"), - name_ald = c("company a", "company b") - ) - - ald <- fake_ald( - name_company = c("company a", "company b", "company a", "company b"), - technology = c("ice", "ice", "electric", "electric"), - production = c(100, 1, 100, 3), - year = 2020 - ) - - scenario <- fake_scenario( - technology = c("ice", "electric"), - year = 2020, - tmsr = 1, - smsp = 0 - ) - - out <- target_market_share( - matched, - ald, - scenario, - region_isos_stable - ) %>% - filter( - metric %in% c("projected", "target_sds"), - year == 2020 - ) %>% - select(technology, metric, technology_share) - - out_ice <- out %>% - filter(technology == "ice") %>% - split(.$metric) - - out_electric <- out %>% - filter(technology == "electric") %>% - split(.$metric) - - expect_equal( - out_ice$projected$technology_share, - out_ice$target_sds$technology_share - ) - expect_equal( - out_electric$projected$technology_share, - out_electric$target_sds$technology_share - ) - }) + expect_equal( + out_ice$projected$technology_share, + out_ice$target_sds$technology_share + ) + expect_equal( + out_electric$projected$technology_share, + out_electric$target_sds$technology_share + ) +}) From 876420cb3977e697a8e2f0132aaaa80988745c5f Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Mon, 10 May 2021 14:40:35 +0200 Subject: [PATCH 07/22] remove old FIXME --- tests/testthat/test-target_market_share.R | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testthat/test-target_market_share.R b/tests/testthat/test-target_market_share.R index 9c8db5f8..fe62555e 100644 --- a/tests/testthat/test-target_market_share.R +++ b/tests/testthat/test-target_market_share.R @@ -201,7 +201,6 @@ test_that("with known input outputs as expected, at company level", { production = c(10, 30, 20, 20, 90, 95, 100, 100) ) - # FIXME: duplicated entries scenario scenario <- fake_scenario( technology = c("electric", "ice", "electric", "ice"), year = c(2020, 2020, 2021, 2021), From a45838aa0bbc83600a086d568804a2ff1c15931b Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Mon, 10 May 2021 15:03:07 +0200 Subject: [PATCH 08/22] update NEWS.md --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 7fa6f703..253dd0a6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # r2dii.analysis (development version) +* `target_market_share()` now correctly outputs weighted target technology share + values, in line with methodology. (@georgeharris2deg #277). + * `target_market_share()` now correctly projects technology share as 'production / total production' when computing by company, unweighted by relative loan size (@KapitanKombajn #288). From f995b66b9d10226b88f40ff1f04869b81a0496be Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Tue, 11 May 2021 13:33:57 +0200 Subject: [PATCH 09/22] update NEWS.md --- NEWS.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 253dd0a6..310e387e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,7 @@ # r2dii.analysis (development version) -* `target_market_share()` now correctly outputs weighted target technology share - values, in line with methodology. (@georgeharris2deg #277). +* `target_market_share()` now correctly outputs target `technology share`, in + line with methodology. (@georgeharris2deg #277). * `target_market_share()` now correctly projects technology share as 'production / total production' when computing by company, From 2a3aeae5d25d154daac33b50966dbc78f270dc20 Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Wed, 12 May 2021 13:17:20 +0200 Subject: [PATCH 10/22] fix NEWS.md --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 310e387e..3b7827f9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,7 @@ # r2dii.analysis (development version) * `target_market_share()` now correctly outputs target `technology share`, in - line with methodology. (@georgeharris2deg #277). + line with methodology (@georgeharris2deg #277). * `target_market_share()` now correctly projects technology share as 'production / total production' when computing by company, From 16c0debc00d3162d22d0f15716f058f541cd4150 Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Fri, 14 May 2021 15:47:24 +0200 Subject: [PATCH 11/22] knit readme --- README.md | 67 +++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 09946dc7..b46347d7 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,9 @@ to study how their capital allocation impacts the climate. Before you install r2dii.analysis you may want to: - - [Try an rstudio.cloud project with this package already +- [Try an rstudio.cloud project with this package already installed](https://rstudio.cloud/project/1424833). - - [Learn how to minimize installation +- [Learn how to minimize installation errors](https://gist.github.com/maurolepore/a0187be9d40aee95a43f20a85f4caed6#installation). When you are ready, install the released version of r2dii.analysis from @@ -50,12 +50,11 @@ issue?](https://2degreesinvesting.github.io/posts/2020-06-26-instructions-to-rai ## Example - - Use `library()` to attach the packages you need. r2dii.analysis does +- Use `library()` to attach the packages you need. r2dii.analysis does not depend on the packages r2dii.data and r2dii.match; but we - suggest you install them – with `install.packages(c("r2dii.data", - "r2dii.match"))` – so you can reproduce our examples. - - + suggest you install them – with + `install.packages(c("r2dii.data", "r2dii.match"))` – so you can + reproduce our examples. ``` r library(r2dii.data) @@ -63,11 +62,9 @@ library(r2dii.match) library(r2dii.analysis) ``` - - Use `r2dii.match::match_name()` to identify matches between your +- Use `r2dii.match::match_name()` to identify matches between your loanbook and the asset level data. - - ``` r matched <- match_name(loanbook_demo, ald_demo) %>% prioritize() @@ -75,9 +72,7 @@ matched <- match_name(loanbook_demo, ald_demo) %>% ### Add Scenario Targets - - Use `target_sda()` to calculate SDA targets of CO2 emissions. - - +- Use `target_sda()` to calculate SDA targets of CO2 emissions. ``` r matched %>% @@ -102,11 +97,9 @@ matched %>% #> # … with 498 more rows ``` - - Use `target_market_share` to calculate market-share scenario targets +- Use `target_market_share` to calculate market-share scenario targets at the portfolio level: - - ``` r matched %>% target_market_share( @@ -130,9 +123,7 @@ matched %>% #> # … with 3,632 more rows, and 1 more variable: technology_share ``` - - Or at the company level: - - +- Or at the company level: ``` r matched %>% @@ -146,22 +137,20 @@ matched %>% #> This will result in company-level results, weighted by the portfolio #> loan size, which is rarely useful. Did you mean to set one of these #> arguments to `FALSE`? -#> # A tibble: 14,754 x 11 -#> sector technology year region scenario_source name_ald sector_weighted_p… -#> -#> 1 automo… electric 2020 global demo_2020 toyota mo… 4275858. -#> 2 automo… electric 2020 global demo_2020 toyota mo… 4275858. -#> 3 automo… electric 2020 global demo_2020 toyota mo… 4275858. -#> 4 automo… electric 2020 global demo_2020 toyota mo… 4275858. -#> 5 automo… hybrid 2020 global demo_2020 toyota mo… 4275858. -#> 6 automo… hybrid 2020 global demo_2020 toyota mo… 4275858. -#> 7 automo… hybrid 2020 global demo_2020 toyota mo… 4275858. -#> 8 automo… hybrid 2020 global demo_2020 toyota mo… 4275858. -#> 9 automo… ice 2020 global demo_2020 toyota mo… 4275858. -#> 10 automo… ice 2020 global demo_2020 toyota mo… 4275858. -#> # … with 14,744 more rows, and 4 more variables: -#> # technology_weighted_production , metric , production , -#> # technology_share +#> # A tibble: 14,754 x 9 +#> sector technology year region scenario_source name_ald metric production +#> +#> 1 automo… electric 2020 global demo_2020 toyota mot… proje… 324592. +#> 2 automo… electric 2020 global demo_2020 toyota mot… targe… 324592. +#> 3 automo… electric 2020 global demo_2020 toyota mot… targe… 324592. +#> 4 automo… electric 2020 global demo_2020 toyota mot… targe… 324592. +#> 5 automo… hybrid 2020 global demo_2020 toyota mot… proje… 628681. +#> 6 automo… hybrid 2020 global demo_2020 toyota mot… targe… 628681. +#> 7 automo… hybrid 2020 global demo_2020 toyota mot… targe… 628681. +#> 8 automo… hybrid 2020 global demo_2020 toyota mot… targe… 628681. +#> 9 automo… ice 2020 global demo_2020 toyota mot… proje… 3322586. +#> 10 automo… ice 2020 global demo_2020 toyota mot… targe… 3322586. +#> # … with 14,744 more rows, and 1 more variable: technology_share ``` ### Utility Functions @@ -169,11 +158,9 @@ matched %>% The `target_*()` functions provide shortcuts for common operations. They wrap some utility functions that you may also use directly: - - Use `join_ald_scenario()` to join a matched dataset to the relevant +- Use `join_ald_scenario()` to join a matched dataset to the relevant scenario data, and to pick assets in the relevant regions. - - ``` r loanbook_joined_to_ald_scenario <- matched %>% join_ald_scenario( @@ -183,11 +170,9 @@ loanbook_joined_to_ald_scenario <- matched %>% ) ``` - - Use `summarize_weighted_production()` with different grouping +- Use `summarize_weighted_production()` with different grouping arguments to calculate scenario-targets: - - ``` r # portfolio level loanbook_joined_to_ald_scenario %>% From 473265dd2838f6e9c246d60d515660ba6c491fa1 Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Tue, 18 May 2021 13:09:48 +0200 Subject: [PATCH 12/22] sum with narm TRUE --- R/target_market_share.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/target_market_share.R b/R/target_market_share.R index 8e045ab6..ed70e0e6 100644 --- a/R/target_market_share.R +++ b/R/target_market_share.R @@ -160,7 +160,7 @@ target_market_share <- function(data, data <- data %>% group_by(!!!rlang::syms(crucial_groups)) %>% summarize( - production = sum(.data$production) + production = sum(.data$production, rm.na = TRUE) ) if (nrow(data) == 0) { @@ -288,10 +288,10 @@ target_market_share <- function(data, data <- data %>% group_by(!!!rlang::syms(aggregate_company_groups)) %>% summarize( - weighted_production = sum(.data$weighted_production), - weighted_production_target = sum(.data$weighted_production_target), - weighted_technology_share = sum(.data$weighted_technology_share), - weighted_technology_share_target = sum(.data$weighted_technology_share_target) + weighted_production = sum(.data$weighted_production, rm.na = TRUE), + weighted_production_target = sum(.data$weighted_production_target, rm.na = TRUE), + weighted_technology_share = sum(.data$weighted_technology_share, rm.na = TRUE), + weighted_technology_share_target = sum(.data$weighted_technology_share_target, rm.na = TRUE) ) } From c7a851d7c5d4b31d16abbb5b7cbed5529f9e33f3 Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Tue, 18 May 2021 13:32:28 +0200 Subject: [PATCH 13/22] check crucials at exported fn --- R/summarize_weighted_production.R | 36 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/R/summarize_weighted_production.R b/R/summarize_weighted_production.R index 6ee92bd6..5a4dd8be 100644 --- a/R/summarize_weighted_production.R +++ b/R/summarize_weighted_production.R @@ -53,7 +53,24 @@ #' #' summarize_weighted_percent_change(master, use_credit_limit = TRUE) summarize_weighted_production <- function(data, ..., use_credit_limit = FALSE) { - stopifnot(is.data.frame(data)) + stopifnot( + is.data.frame(data), + is.logical(use_credit_limit) + ) + + crucial <- c( + "id_loan", + "loan_size_outstanding", + "loan_size_outstanding_currency", + "loan_size_credit_limit", + "loan_size_credit_limit_currency", + "sector_ald", + "name_ald", + "year", + "scenario" + ) + + check_crucial_names(data, crucial) data %>% ungroup() %>% @@ -121,7 +138,6 @@ summarize_weighted_emission_factor <- function(data, ..., use_credit_limit = FAL calculate_weighted_loan_metric <- function(data, metric) { crucial <- c(metric, "loan_weight") - check_crucial_names(data, crucial) walk_(crucial, ~ check_no_value_is_missing(data, .x)) data %>% @@ -144,7 +160,6 @@ add_loan_weight <- function(data, use_credit_limit) { "id_loan", "sector_ald", "year", loan_size, currency ) - check_crucial_names(data, crucial) walk_(crucial, ~ check_no_value_is_missing(data, .x)) old_groups <- dplyr::groups(data) @@ -166,11 +181,6 @@ add_loan_weight <- function(data, use_credit_limit) { } add_percent_change <- function(data) { - crucial <- c("production", "sector_ald", "year", "technology", "scenario") - - check_crucial_names(data, crucial) - walk_(crucial, ~ check_no_value_is_missing(data, .x)) - check_zero_initial_production(data) green_or_brown <- r2dii.data::green_or_brown @@ -200,11 +210,6 @@ add_percent_change <- function(data) { } add_technology_share <- function(data) { - crucial <- c("production", "sector_ald", "year", "technology") - - check_crucial_names(data, crucial) - walk_(crucial, ~ check_no_value_is_missing(data, .x)) - data %>% group_by(.data$sector_ald, .data$year, .data$scenario, .data$name_ald) %>% mutate(technology_share = .data$production / sum(.data$production)) %>% @@ -212,11 +217,6 @@ add_technology_share <- function(data) { } add_technology_share_target <- function(data) { - crucial <- c("production_target", "sector_ald", "year", "technology") - - check_crucial_names(data, crucial) - walk_(crucial, ~ check_no_value_is_missing(data, .x)) - data %>% group_by(.data$sector_ald, .data$year, .data$scenario, .data$name_ald) %>% mutate(technology_share_target = .data$production_target / sum(.data$production_target)) %>% From 394e1d01bfef6a0704a1f06df0d03ac5b14c8401 Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Tue, 18 May 2021 14:39:57 +0200 Subject: [PATCH 14/22] Revert "check crucials at exported fn" This reverts commit c7a851d7c5d4b31d16abbb5b7cbed5529f9e33f3. --- R/summarize_weighted_production.R | 36 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/R/summarize_weighted_production.R b/R/summarize_weighted_production.R index 5a4dd8be..6ee92bd6 100644 --- a/R/summarize_weighted_production.R +++ b/R/summarize_weighted_production.R @@ -53,24 +53,7 @@ #' #' summarize_weighted_percent_change(master, use_credit_limit = TRUE) summarize_weighted_production <- function(data, ..., use_credit_limit = FALSE) { - stopifnot( - is.data.frame(data), - is.logical(use_credit_limit) - ) - - crucial <- c( - "id_loan", - "loan_size_outstanding", - "loan_size_outstanding_currency", - "loan_size_credit_limit", - "loan_size_credit_limit_currency", - "sector_ald", - "name_ald", - "year", - "scenario" - ) - - check_crucial_names(data, crucial) + stopifnot(is.data.frame(data)) data %>% ungroup() %>% @@ -138,6 +121,7 @@ summarize_weighted_emission_factor <- function(data, ..., use_credit_limit = FAL calculate_weighted_loan_metric <- function(data, metric) { crucial <- c(metric, "loan_weight") + check_crucial_names(data, crucial) walk_(crucial, ~ check_no_value_is_missing(data, .x)) data %>% @@ -160,6 +144,7 @@ add_loan_weight <- function(data, use_credit_limit) { "id_loan", "sector_ald", "year", loan_size, currency ) + check_crucial_names(data, crucial) walk_(crucial, ~ check_no_value_is_missing(data, .x)) old_groups <- dplyr::groups(data) @@ -181,6 +166,11 @@ add_loan_weight <- function(data, use_credit_limit) { } add_percent_change <- function(data) { + crucial <- c("production", "sector_ald", "year", "technology", "scenario") + + check_crucial_names(data, crucial) + walk_(crucial, ~ check_no_value_is_missing(data, .x)) + check_zero_initial_production(data) green_or_brown <- r2dii.data::green_or_brown @@ -210,6 +200,11 @@ add_percent_change <- function(data) { } add_technology_share <- function(data) { + crucial <- c("production", "sector_ald", "year", "technology") + + check_crucial_names(data, crucial) + walk_(crucial, ~ check_no_value_is_missing(data, .x)) + data %>% group_by(.data$sector_ald, .data$year, .data$scenario, .data$name_ald) %>% mutate(technology_share = .data$production / sum(.data$production)) %>% @@ -217,6 +212,11 @@ add_technology_share <- function(data) { } add_technology_share_target <- function(data) { + crucial <- c("production_target", "sector_ald", "year", "technology") + + check_crucial_names(data, crucial) + walk_(crucial, ~ check_no_value_is_missing(data, .x)) + data %>% group_by(.data$sector_ald, .data$year, .data$scenario, .data$name_ald) %>% mutate(technology_share_target = .data$production_target / sum(.data$production_target)) %>% From 6c5214db715d2cc6871ddd7539af3200f286df4d Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Tue, 18 May 2021 14:40:44 +0200 Subject: [PATCH 15/22] Revert "sum with narm TRUE" This reverts commit 473265dd2838f6e9c246d60d515660ba6c491fa1. --- R/target_market_share.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/target_market_share.R b/R/target_market_share.R index ed70e0e6..8e045ab6 100644 --- a/R/target_market_share.R +++ b/R/target_market_share.R @@ -160,7 +160,7 @@ target_market_share <- function(data, data <- data %>% group_by(!!!rlang::syms(crucial_groups)) %>% summarize( - production = sum(.data$production, rm.na = TRUE) + production = sum(.data$production) ) if (nrow(data) == 0) { @@ -288,10 +288,10 @@ target_market_share <- function(data, data <- data %>% group_by(!!!rlang::syms(aggregate_company_groups)) %>% summarize( - weighted_production = sum(.data$weighted_production, rm.na = TRUE), - weighted_production_target = sum(.data$weighted_production_target, rm.na = TRUE), - weighted_technology_share = sum(.data$weighted_technology_share, rm.na = TRUE), - weighted_technology_share_target = sum(.data$weighted_technology_share_target, rm.na = TRUE) + weighted_production = sum(.data$weighted_production), + weighted_production_target = sum(.data$weighted_production_target), + weighted_technology_share = sum(.data$weighted_technology_share), + weighted_technology_share_target = sum(.data$weighted_technology_share_target) ) } From 049e1c96d4eb18dca699299099c398040d1d627b Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Tue, 18 May 2021 14:53:21 +0200 Subject: [PATCH 16/22] refactor summarize_*_targets --- R/summarize_weighted_production.R | 52 +++++++++++++++++++++++++++++++ R/target_market_share.R | 52 ++++++------------------------- 2 files changed, 61 insertions(+), 43 deletions(-) diff --git a/R/summarize_weighted_production.R b/R/summarize_weighted_production.R index 6ee92bd6..f741b3f5 100644 --- a/R/summarize_weighted_production.R +++ b/R/summarize_weighted_production.R @@ -68,6 +68,35 @@ summarize_weighted_production <- function(data, ..., use_credit_limit = FALSE) { ) %>% # Restore old groups group_by(!!!dplyr::groups(data)) + +} + +summarize_weighted_production_and_targets <- function(data, ..., use_credit_limit = FALSE) { + stopifnot(is.data.frame(data)) + + data %>% + ungroup() %>% + add_loan_weight(use_credit_limit = use_credit_limit) %>% + add_technology_share() %>% + add_technology_share_target() %>% + calculate_weighted_loan_metric("production") %>% + calculate_weighted_loan_metric("technology_share") %>% + calculate_weighted_loan_metric("production_target") %>% + calculate_weighted_loan_metric("technology_share_target") %>% + group_by( + .data$sector_ald, + .data$technology, + .data$year, + ... + ) %>% + summarize( + weighted_production = sum(.data$weighted_loan_production), + weighted_technology_share = sum(.data$weighted_loan_technology_share), + weighted_production_target = sum(.data$weighted_loan_production_target), + weighted_technology_share_target = sum(.data$weighted_loan_technology_share_target) + ) %>% + # Restore old groups + group_by(!!!dplyr::groups(data)) } summarize_unweighted_production <- function(data, ...) { @@ -86,6 +115,29 @@ summarize_unweighted_production <- function(data, ...) { group_by(!!!dplyr::groups(data)) } +summarize_unweighted_production_and_targets <- function(data, ...) { + data %>% + select(-c( + .data$id_loan, + .data$loan_size_credit_limit, + .data$loan_size_outstanding + )) %>% + distinct() %>% + group_by(.data$sector_ald, .data$technology, .data$year, ...) %>% + # FIXME: Confusing: `weighted_production` holds unweighted_production? + summarize( + weighted_production = .data$production, + weighted_production_target = .data$production_target, + .groups = "keep" + ) %>% + ungroup(.data$technology) %>% + mutate( + weighted_technology_share = .data$weighted_production / sum(.data$weighted_production), + weighted_technology_share_target = .data$weighted_production_target / sum(.data$weighted_production_target) + ) %>% + group_by(!!!dplyr::groups(data)) +} + #' @rdname summarize_weighted_production #' @export summarize_weighted_percent_change <- function(data, ..., use_credit_limit = FALSE) { diff --git a/R/target_market_share.R b/R/target_market_share.R index 8e045ab6..8066fbfe 100644 --- a/R/target_market_share.R +++ b/R/target_market_share.R @@ -229,50 +229,16 @@ target_market_share <- function(data, ) if (weight_production) { - data <- data %>% - ungroup() %>% - add_loan_weight(use_credit_limit = use_credit_limit) %>% - add_technology_share() %>% - add_technology_share_target() %>% - calculate_weighted_loan_metric("production") %>% - calculate_weighted_loan_metric("technology_share") %>% - calculate_weighted_loan_metric("production_target") %>% - calculate_weighted_loan_metric("technology_share_target") %>% - group_by( - .data$sector_ald, - .data$technology, - .data$year, - !!!rlang::syms(summary_groups) - ) %>% - summarize( - weighted_production = sum(.data$weighted_loan_production), - weighted_technology_share = sum(.data$weighted_loan_technology_share), - weighted_production_target = sum(.data$weighted_loan_production_target), - weighted_technology_share_target = sum(.data$weighted_loan_technology_share_target) - ) %>% - # Restore old groups - group_by(!!!dplyr::groups(data)) + data <- summarize_weighted_production_and_targets( + data, + !!!rlang::syms(summary_groups), + use_credit_limit = use_credit_limit + ) } else { - data <- data %>% - select(-c( - .data$id_loan, - .data$loan_size_credit_limit, - .data$loan_size_outstanding - )) %>% - distinct() %>% - group_by(.data$sector_ald, .data$technology, .data$year, !!!rlang::syms(summary_groups)) %>% - # FIXME: Confusing: `weighted_production` holds unweighted_production? - summarize( - weighted_production = .data$production, - weighted_production_target = .data$production_target, - .groups = "keep" - ) %>% - ungroup(.data$technology) %>% - mutate( - weighted_technology_share = .data$weighted_production / sum(.data$weighted_production), - weighted_technology_share_target = .data$weighted_production_target / sum(.data$weighted_production_target) - ) %>% - group_by(!!!dplyr::groups(data)) + data <- summarize_unweighted_production_and_targets( + data, + !!!rlang::syms(summary_groups) + ) } if (!by_company) { From 1dcc4e810c5b8c8c0622983b8a355534332ff4d2 Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Tue, 18 May 2021 15:09:22 +0200 Subject: [PATCH 17/22] refactor: combine summarize-like functions --- R/summarize_weighted_production.R | 131 ++++++++++++++---------------- R/target_market_share.R | 10 ++- 2 files changed, 69 insertions(+), 72 deletions(-) diff --git a/R/summarize_weighted_production.R b/R/summarize_weighted_production.R index f741b3f5..d4722ec2 100644 --- a/R/summarize_weighted_production.R +++ b/R/summarize_weighted_production.R @@ -52,90 +52,85 @@ #' summarize_weighted_percent_change(master) #' #' summarize_weighted_percent_change(master, use_credit_limit = TRUE) -summarize_weighted_production <- function(data, ..., use_credit_limit = FALSE) { +summarize_weighted_production <- function(data, ..., use_credit_limit = FALSE, add_targets = FALSE) { stopifnot(is.data.frame(data)) - data %>% + old_groups <- dplyr::groups(data) + + data <- data %>% ungroup() %>% add_loan_weight(use_credit_limit = use_credit_limit) %>% - add_technology_share() %>% - calculate_weighted_loan_metric("production") %>% - calculate_weighted_loan_metric("technology_share") %>% - group_by(.data$sector_ald, .data$technology, .data$year, ...) %>% - summarize( - weighted_production = sum(.data$weighted_loan_production), - weighted_technology_share = sum(.data$weighted_loan_technology_share) - ) %>% - # Restore old groups - group_by(!!!dplyr::groups(data)) + add_technology_share() + + if (add_targets) { + data %>% + add_technology_share_target() %>% + calculate_weighted_loan_metric("production") %>% + calculate_weighted_loan_metric("technology_share") %>% + calculate_weighted_loan_metric("production_target") %>% + calculate_weighted_loan_metric("technology_share_target") %>% + group_by( + .data$sector_ald, + .data$technology, + .data$year, + ... + ) %>% + summarize( + weighted_production = sum(.data$weighted_loan_production), + weighted_technology_share = sum(.data$weighted_loan_technology_share), + weighted_production_target = sum(.data$weighted_loan_production_target), + weighted_technology_share_target = sum(.data$weighted_loan_technology_share_target) + ) %>% + # Restore old groups + group_by(!!!old_groups) + } else { + data %>% + calculate_weighted_loan_metric("production") %>% + calculate_weighted_loan_metric("technology_share") %>% + group_by(.data$sector_ald, .data$technology, .data$year, ...) %>% + summarize( + weighted_production = sum(.data$weighted_loan_production), + weighted_technology_share = sum(.data$weighted_loan_technology_share) + ) %>% + # Restore old groups + group_by(!!!old_groups) + } } -summarize_weighted_production_and_targets <- function(data, ..., use_credit_limit = FALSE) { - stopifnot(is.data.frame(data)) - - data %>% - ungroup() %>% - add_loan_weight(use_credit_limit = use_credit_limit) %>% - add_technology_share() %>% - add_technology_share_target() %>% - calculate_weighted_loan_metric("production") %>% - calculate_weighted_loan_metric("technology_share") %>% - calculate_weighted_loan_metric("production_target") %>% - calculate_weighted_loan_metric("technology_share_target") %>% - group_by( - .data$sector_ald, - .data$technology, - .data$year, - ... - ) %>% - summarize( - weighted_production = sum(.data$weighted_loan_production), - weighted_technology_share = sum(.data$weighted_loan_technology_share), - weighted_production_target = sum(.data$weighted_loan_production_target), - weighted_technology_share_target = sum(.data$weighted_loan_technology_share_target) - ) %>% - # Restore old groups - group_by(!!!dplyr::groups(data)) -} +summarize_unweighted_production <- function(data, ..., add_targets = FALSE) { + old_groups <- dplyr::groups(data) -summarize_unweighted_production <- function(data, ...) { - data %>% + data <- data %>% select(-c( .data$id_loan, .data$loan_size_credit_limit, .data$loan_size_outstanding )) %>% distinct() %>% - group_by(.data$sector_ald, .data$technology, .data$year, ...) %>% + group_by(.data$sector_ald, .data$technology, .data$year, ...) # FIXME: Confusing: `weighted_production` holds unweighted_production? - summarize(weighted_production = .data$production, .groups = "keep") %>% - ungroup(.data$technology, .data$tmsr, .data$smsp) %>% - mutate(weighted_technology_share = .data$weighted_production / sum(.data$weighted_production)) %>% - group_by(!!!dplyr::groups(data)) -} -summarize_unweighted_production_and_targets <- function(data, ...) { - data %>% - select(-c( - .data$id_loan, - .data$loan_size_credit_limit, - .data$loan_size_outstanding - )) %>% - distinct() %>% - group_by(.data$sector_ald, .data$technology, .data$year, ...) %>% - # FIXME: Confusing: `weighted_production` holds unweighted_production? - summarize( - weighted_production = .data$production, - weighted_production_target = .data$production_target, - .groups = "keep" - ) %>% - ungroup(.data$technology) %>% - mutate( - weighted_technology_share = .data$weighted_production / sum(.data$weighted_production), - weighted_technology_share_target = .data$weighted_production_target / sum(.data$weighted_production_target) - ) %>% - group_by(!!!dplyr::groups(data)) + if (add_targets) { + data %>% + summarize( + weighted_production = .data$production, + weighted_production_target = .data$production_target, + .groups = "keep" + ) %>% + ungroup(.data$technology) %>% + mutate( + weighted_technology_share = .data$weighted_production / sum(.data$weighted_production), + weighted_technology_share_target = .data$weighted_production_target / sum(.data$weighted_production_target) + ) %>% + group_by(!!!old_groups) + } else { + data %>% + summarize(weighted_production = .data$production, .groups = "keep") %>% + ungroup(.data$technology, .data$tmsr, .data$smsp) %>% + mutate(weighted_technology_share = .data$weighted_production / sum(.data$weighted_production)) %>% + group_by(!!!old_groups) + } } #' @rdname summarize_weighted_production diff --git a/R/target_market_share.R b/R/target_market_share.R index 8066fbfe..54bde70c 100644 --- a/R/target_market_share.R +++ b/R/target_market_share.R @@ -229,15 +229,17 @@ target_market_share <- function(data, ) if (weight_production) { - data <- summarize_weighted_production_and_targets( + data <- summarize_weighted_production( data, !!!rlang::syms(summary_groups), - use_credit_limit = use_credit_limit + use_credit_limit = use_credit_limit, + add_targets = TRUE ) } else { - data <- summarize_unweighted_production_and_targets( + data <- summarize_unweighted_production( data, - !!!rlang::syms(summary_groups) + !!!rlang::syms(summary_groups), + add_targets = TRUE ) } From f1647c5b2e0fcc02823f07d7cd445c40b4b6a25b Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Tue, 18 May 2021 15:18:58 +0200 Subject: [PATCH 18/22] check crucials at exported fn --- R/summarize_weighted_production.R | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/R/summarize_weighted_production.R b/R/summarize_weighted_production.R index d4722ec2..2aadad5f 100644 --- a/R/summarize_weighted_production.R +++ b/R/summarize_weighted_production.R @@ -57,6 +57,15 @@ summarize_weighted_production <- function(data, ..., use_credit_limit = FALSE, a old_groups <- dplyr::groups(data) + crucial <- c("production", "sector_ald", "year", "technology") + + if (add_targets) { + crucial <- c(crucial, "production_target") + } + + check_crucial_names(data, crucial) + walk_(crucial, ~ check_no_value_is_missing(data, .x)) + data <- data %>% ungroup() %>% add_loan_weight(use_credit_limit = use_credit_limit) %>% @@ -247,11 +256,6 @@ add_percent_change <- function(data) { } add_technology_share <- function(data) { - crucial <- c("production", "sector_ald", "year", "technology") - - check_crucial_names(data, crucial) - walk_(crucial, ~ check_no_value_is_missing(data, .x)) - data %>% group_by(.data$sector_ald, .data$year, .data$scenario, .data$name_ald) %>% mutate(technology_share = .data$production / sum(.data$production)) %>% @@ -259,11 +263,6 @@ add_technology_share <- function(data) { } add_technology_share_target <- function(data) { - crucial <- c("production_target", "sector_ald", "year", "technology") - - check_crucial_names(data, crucial) - walk_(crucial, ~ check_no_value_is_missing(data, .x)) - data %>% group_by(.data$sector_ald, .data$year, .data$scenario, .data$name_ald) %>% mutate(technology_share_target = .data$production_target / sum(.data$production_target)) %>% From 66b797a3780ba26645fc2b2f247b65f1f919b224 Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Tue, 18 May 2021 15:32:48 +0200 Subject: [PATCH 19/22] dont change exported function --- R/summarize_weighted_production.R | 6 +++++- R/target_market_share.R | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/R/summarize_weighted_production.R b/R/summarize_weighted_production.R index 2aadad5f..70464b10 100644 --- a/R/summarize_weighted_production.R +++ b/R/summarize_weighted_production.R @@ -52,7 +52,11 @@ #' summarize_weighted_percent_change(master) #' #' summarize_weighted_percent_change(master, use_credit_limit = TRUE) -summarize_weighted_production <- function(data, ..., use_credit_limit = FALSE, add_targets = FALSE) { +summarize_weighted_production <- function(data, ..., use_credit_limit = FALSE) { + summarize_weighted_production_(data, ..., use_credit_limit = use_credit_limit, add_targets = FALSE) +} + +summarize_weighted_production_ <- function(data, ..., use_credit_limit = FALSE, add_targets = FALSE) { stopifnot(is.data.frame(data)) old_groups <- dplyr::groups(data) diff --git a/R/target_market_share.R b/R/target_market_share.R index 54bde70c..caadf19b 100644 --- a/R/target_market_share.R +++ b/R/target_market_share.R @@ -229,7 +229,7 @@ target_market_share <- function(data, ) if (weight_production) { - data <- summarize_weighted_production( + data <- summarize_weighted_production_( data, !!!rlang::syms(summary_groups), use_credit_limit = use_credit_limit, From d1dcd69960624b72efe85543bee1a9b367cea4cb Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Wed, 19 May 2021 09:43:57 +0200 Subject: [PATCH 20/22] add_targets -> with_targets More accurate name, since this flag doesn't actually add anything just checks to see if targets are already there or not. --- R/summarize_weighted_production.R | 12 ++++++------ R/target_market_share.R | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/R/summarize_weighted_production.R b/R/summarize_weighted_production.R index 70464b10..450bc246 100644 --- a/R/summarize_weighted_production.R +++ b/R/summarize_weighted_production.R @@ -53,17 +53,17 @@ #' #' summarize_weighted_percent_change(master, use_credit_limit = TRUE) summarize_weighted_production <- function(data, ..., use_credit_limit = FALSE) { - summarize_weighted_production_(data, ..., use_credit_limit = use_credit_limit, add_targets = FALSE) + summarize_weighted_production_(data, ..., use_credit_limit = use_credit_limit, with_targets = FALSE) } -summarize_weighted_production_ <- function(data, ..., use_credit_limit = FALSE, add_targets = FALSE) { +summarize_weighted_production_ <- function(data, ..., use_credit_limit = FALSE, with_targets = FALSE) { stopifnot(is.data.frame(data)) old_groups <- dplyr::groups(data) crucial <- c("production", "sector_ald", "year", "technology") - if (add_targets) { + if (with_targets) { crucial <- c(crucial, "production_target") } @@ -75,7 +75,7 @@ summarize_weighted_production_ <- function(data, ..., use_credit_limit = FALSE, add_loan_weight(use_credit_limit = use_credit_limit) %>% add_technology_share() - if (add_targets) { + if (with_targets) { data %>% add_technology_share_target() %>% calculate_weighted_loan_metric("production") %>% @@ -111,7 +111,7 @@ summarize_weighted_production_ <- function(data, ..., use_credit_limit = FALSE, } -summarize_unweighted_production <- function(data, ..., add_targets = FALSE) { +summarize_unweighted_production <- function(data, ..., with_targets = FALSE) { old_groups <- dplyr::groups(data) data <- data %>% @@ -124,7 +124,7 @@ summarize_unweighted_production <- function(data, ..., add_targets = FALSE) { group_by(.data$sector_ald, .data$technology, .data$year, ...) # FIXME: Confusing: `weighted_production` holds unweighted_production? - if (add_targets) { + if (with_targets) { data %>% summarize( weighted_production = .data$production, diff --git a/R/target_market_share.R b/R/target_market_share.R index caadf19b..20c63c3a 100644 --- a/R/target_market_share.R +++ b/R/target_market_share.R @@ -233,13 +233,13 @@ target_market_share <- function(data, data, !!!rlang::syms(summary_groups), use_credit_limit = use_credit_limit, - add_targets = TRUE + with_targets = TRUE ) } else { data <- summarize_unweighted_production( data, !!!rlang::syms(summary_groups), - add_targets = TRUE + with_targets = TRUE ) } From e6beae14790c0355398d3fad078b567e9878551d Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Wed, 19 May 2021 09:47:44 +0200 Subject: [PATCH 21/22] move FIXME and make more descriptive --- R/summarize_weighted_production.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/summarize_weighted_production.R b/R/summarize_weighted_production.R index 450bc246..8cc9c514 100644 --- a/R/summarize_weighted_production.R +++ b/R/summarize_weighted_production.R @@ -122,8 +122,10 @@ summarize_unweighted_production <- function(data, ..., with_targets = FALSE) { )) %>% distinct() %>% group_by(.data$sector_ald, .data$technology, .data$year, ...) - # FIXME: Confusing: `weighted_production` holds unweighted_production? + # FIXME: Though production here is unweighted, we still name the variables + # `weighted_*`. This is to allow easier reshaping of the output data at the + # end of `target_market_share()`. if (with_targets) { data %>% summarize( From 5c1cd070b0bca89417ba5ed16f62331d2494c122 Mon Sep 17 00:00:00 2001 From: Mauro Lepore Date: Wed, 19 May 2021 08:27:39 -0600 Subject: [PATCH 22/22] Replace rbind() with dplyr::bind_rows() --- R/target_market_share.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/target_market_share.R b/R/target_market_share.R index 20c63c3a..af723789 100644 --- a/R/target_market_share.R +++ b/R/target_market_share.R @@ -300,7 +300,7 @@ target_market_share <- function(data, ald_with_benchmark <- calculate_ald_benchmark(ald, region_isos, by_company) data %>% - rbind(ald_with_benchmark) %>% + dplyr::bind_rows(ald_with_benchmark) %>% ungroup() }