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

Remove green_or_brown in favour of increasing_or_decreasing #438

Merged
merged 11 commits into from
Jul 19, 2023
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# r2dii.analysis (development version)

# `target_market_share` gains argument `green_or_brown` (#426).
# `target_market_share` gains argument `increasing_or_decreasing` (#426).

# r2dii.analysis 0.2.1

Expand Down
12 changes: 6 additions & 6 deletions R/join_abcd_scenario.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ check_portfolio_abcd_scenario <- function(valid_matches, abcd, scenario) {
}

add_green_technologies_to_abcd <- function(data, scenario) {
green_techs <- r2dii.data::green_or_brown %>%
filter(.data$green_or_brown == "green") %>%
select(-all_of("green_or_brown"))
increasing_techs <- r2dii.data::increasing_or_decreasing %>%
filter(.data$increasing_or_decreasing == "increasing") %>%
select(-all_of("increasing_or_decreasing"))

green_techs_in_scenario <- scenario %>%
increasing_techs_in_scenario <- scenario %>%
select(all_of(c("sector", "technology"))) %>%
unique() %>%
inner_join(green_techs, by = c("sector", "technology"))
inner_join(increasing_techs, by = c("sector", "technology"))

green_rows_to_add <- data %>%
group_by(
Expand All @@ -120,7 +120,7 @@ add_green_technologies_to_abcd <- function(data, scenario) {
) %>%
summarize() %>%
left_join(
green_techs_in_scenario,
increasing_techs_in_scenario,
by = "sector",
relationship = "many-to-many"
) %>%
Expand Down
12 changes: 6 additions & 6 deletions R/summarize_weighted_production.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ add_percent_change <- function(data) {

check_zero_initial_production(data)

green_or_brown <- r2dii.data::green_or_brown
increasing_or_decreasing <- r2dii.data::increasing_or_decreasing

data %>%
inner_join(green_or_brown, by = c(
inner_join(increasing_or_decreasing, by = c(
sector_abcd = "sector",
technology = "technology"
)) %>%
Expand All @@ -266,15 +266,15 @@ add_percent_change <- function(data) {
group_by(.data$sector_abcd, .data$name_abcd) %>%
arrange(.data$name_abcd, .data$year) %>%
mutate(
brown_percent_change =
decreasing_percent_change =
(.data$production - first(.data$production)) /
first(.data$production) * 100,
green_percent_change = (.data$production - first(.data$production)) /
increasing_percent_change = (.data$production - first(.data$production)) /
first(.data$sector_production) * 100
) %>%
mutate(percent_change = dplyr::case_when(
green_or_brown == "green" ~ green_percent_change,
green_or_brown == "brown" ~ brown_percent_change
increasing_or_decreasing == "increasing" ~ increasing_percent_change,
increasing_or_decreasing == "decreasing" ~ decreasing_percent_change
)) %>%
select(one_of(c(names(data), "percent_change"))) %>%
ungroup()
Expand Down
33 changes: 17 additions & 16 deletions R/target_market_share.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
#' @param weight_production Logical vector of length 1. `TRUE` defaults to
#' outputting production, weighted by relative loan-size. Set to `FALSE` to
#' output the unweighted production values.
#' @param increasing_or_decreasing A data frame like
#' [r2dii.data::increasing_or_decreasing].
#' @param ald `r lifecycle::badge('superseded')` `ald` has been superseded by
#' `abcd`.
#' @param green_or_brown A data frame like [r2dii.data::green_or_brown].
#'
#' @return A tibble including the summarized columns `metric`, `production`,
#' `technology_share`, `percentage_of_initial_production_by_scope` and
Expand Down Expand Up @@ -80,7 +81,7 @@ target_market_share <- function(data,
use_credit_limit = FALSE,
by_company = FALSE,
weight_production = TRUE,
green_or_brown = r2dii.data::green_or_brown,
increasing_or_decreasing = r2dii.data::increasing_or_decreasing,
ald = deprecated()) {
stopifnot(
is.data.frame(data),
Expand Down Expand Up @@ -160,7 +161,7 @@ target_market_share <- function(data,

tmsr_or_smsp <- tmsr_or_smsp()

data <- pick_sms_or_tms_target(data, green_or_brown, tmsr_or_smsp)
data <- pick_sms_or_tms_target(data, increasing_or_decreasing, tmsr_or_smsp)

if (nrow(data) == 0) {
return(empty_target_market_share_output())
Expand Down Expand Up @@ -223,21 +224,21 @@ target_market_share <- function(data,
data <- data %>%
dplyr::bind_rows(relevant_corporate_economy)

data <- add_percentage_of_initial_production_by_scope(data, green_or_brown, tmsr_or_smsp, by_company)
data <- add_percentage_of_initial_production_by_scope(data, increasing_or_decreasing, tmsr_or_smsp, by_company)

data %>%
ungroup()
}

add_percentage_of_initial_production_by_scope <- function(data,
green_or_brown,
increasing_or_decreasing,
tmsr_or_smsp,
by_company) {
data <- data %>%
left_join(green_or_brown, by = c("sector", "technology")) %>%
left_join(tmsr_or_smsp, by = "green_or_brown") %>%
left_join(increasing_or_decreasing, by = c("sector", "technology")) %>%
left_join(tmsr_or_smsp, by = "increasing_or_decreasing") %>%
rename(target_name = "which_metric") %>%
select(-all_of("green_or_brown"))
select(-all_of("increasing_or_decreasing"))

percent_by_sector_groups <- add_name_abcd_if_by_company(
c("sector", "region", "scenario_source", "metric"),
Expand Down Expand Up @@ -371,26 +372,26 @@ add_name_abcd_if_by_company <- function(list, by_company = FALSE) {
list
}

pick_sms_or_tms_target <- function(data, green_or_brown, tmsr_or_smsp) {
pick_sms_or_tms_target <- function(data, increasing_or_decreasing, tmsr_or_smsp) {
data %>%
left_join(tmsr_or_smsp, by = c(target_name = "which_metric")) %>%
inner_join(
green_or_brown,
increasing_or_decreasing,
by = c(
sector_abcd = "sector",
technology = "technology",
green_or_brown = "green_or_brown"
increasing_or_decreasing = "increasing_or_decreasing"
)
) %>%
warn_if_has_zero_rows("Joining `r2dii.data::green_or_brown` outputs 0 rows") %>%
select(-all_of(c("target_name", "green_or_brown")))
warn_if_has_zero_rows("Joining `r2dii.data::increasing_or_decreasing` outputs 0 rows") %>%
select(-all_of(c("target_name", "increasing_or_decreasing")))
}

tmsr_or_smsp <- function() {
dplyr::tribble(
~which_metric, ~green_or_brown,
"tmsr_target_production", "brown",
"smsp_target_production", "green"
~which_metric, ~increasing_or_decreasing,
"tmsr_target_production", "decreasing",
"smsp_target_production", "increasing"
)
}

Expand Down
5 changes: 3 additions & 2 deletions man/target_market_share.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions tests/testthat/test-target_market_share.R
Original file line number Diff line number Diff line change
Expand Up @@ -1045,11 +1045,11 @@ test_that("input with only green technologies, outputs only green technologies
test_that("technology_share is calculated per region (#315)", {
matched <- fake_matched(
id_loan = c("L1", "L2"),
name_abcd = c("green", "brown")
name_abcd = c("increasing", "decreasing")
)

abcd <- fake_abcd(
name_company = c("brown", "green"),
name_company = c("decreasing", "increasing"),
technology = c("ice", "electric"),
plant_location = c("US", "FR"),
)
Expand Down Expand Up @@ -1085,7 +1085,7 @@ test_that("technology_share is calculated per region (#315)", {
)
})

test_that("input with only brown technologies, outputs both green and brown
test_that("input with only decreasing technologies, outputs both increasing and decreasing
technologies (#318)", {
scenario <- fake_scenario(
year = rep(c(2020, 2025), 2),
Expand All @@ -1094,14 +1094,14 @@ test_that("input with only brown technologies, outputs both green and brown
technology = rep(c("ice", "electric"), each = 2),
)

abcd_brown <- fake_abcd(
abcd_decreasing <- fake_abcd(
technology = "ice"
)

# testing that input with only brown techs outputs both green and brown techs
# testing that input with only decreasing techs outputs both increasing and decreasing techs
out <- target_market_share(
fake_matched(),
abcd_brown,
abcd_decreasing,
scenario,
region_isos_demo
)
Expand Down
Loading