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

feat: target_market_share gains extended time-horizon for target_* outputs, to entire time-horizon of input scenario #481

Merged
merged 28 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8c87380
add failing test
jdhoffa Mar 11, 2024
8c6dbb3
add failing test to join_abcd_scenario
jdhoffa Mar 11, 2024
bd4be8d
pass full scenario dates from join_abcd_scenario
jdhoffa Mar 15, 2024
e75b916
Merge branch 'main' into 157-extend_market_share_timeline
jdhoffa Mar 15, 2024
edbaf80
fix test with bad expectation
jdhoffa Mar 15, 2024
c787de6
fix failing tests
jdhoffa Mar 15, 2024
12265b1
summarize like production
jdhoffa Mar 15, 2024
273d0a1
Merge branch 'main' into 157-extend_market_share_timeline
jdhoffa Mar 15, 2024
b8e8a6c
move summarize to after input check
jdhoffa Mar 15, 2024
f3a3cdd
filter scenario years less than min ABCD
jdhoffa Mar 15, 2024
9dfb7d7
optionally remove scenario prod col prior to joining
jdhoffa Mar 15, 2024
d520caa
join shares min year between abcd and scenario
jdhoffa Mar 15, 2024
b05f33d
allow NAs in production column
jdhoffa Mar 15, 2024
771e0e1
fix tidyeval warning
jdhoffa Mar 15, 2024
7c3f908
accept NAs in production column
jdhoffa Mar 15, 2024
ae5c146
fix global var bindings
jdhoffa Mar 18, 2024
9b7ea2e
add failing test
jdhoffa Mar 18, 2024
6f53081
fix failing test
jdhoffa Mar 18, 2024
8caf788
fix random bug in check
jdhoffa Mar 18, 2024
9c536f4
add failing test
jdhoffa Mar 18, 2024
4785a72
dont add green techs in percent change vignette
jdhoffa Mar 18, 2024
bca5f3f
fix failing test
jdhoffa Mar 18, 2024
570e389
ensure all years greater than start years
jdhoffa Mar 18, 2024
725a4f8
add failing test
jdhoffa Mar 20, 2024
2aee477
dont fill 0 production
jdhoffa Mar 20, 2024
b1d5662
only skip missing val check when necessary
jdhoffa Mar 25, 2024
f5d6791
rename test
jdhoffa Mar 25, 2024
1902587
add percent_change to missing val exception
jdhoffa Mar 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 49 additions & 10 deletions R/join_abcd_scenario.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,46 @@ join_abcd_scenario <- function(data,
abcd <- add_green_technologies_to_abcd(abcd, scenario)
}

abcd <- dplyr::filter(abcd, .data[["year"]] >= min(scenario[["year"]]))
scenario <- dplyr::filter(scenario, .data[["year"]] >= min(abcd[["year"]]))

abcd_wide <- tidyr::pivot_wider(
abcd,
names_from = "year",
names_sep = "~",
values_from = c("production", "emission_factor"),
values_fill = list(production = 0)
)

scenario_wide <- tidyr::pivot_wider(
scenario,
names_from = "year",
names_sep = "~",
values_from = c("tmsr", "smsp"),
values_fill = list(production = 0)
)

data <- data %>%
left_join(abcd_wide, by = abcd_columns(), relationship = "many-to-many") %>%
left_join(scenario_wide, by = scenario_columns(), relationship = "many-to-many") %>%
warn_if_has_zero_rows("Joining `scenario` outputs 0 rows.")

data <- tidyr::pivot_longer(
data,
cols = tidyr::contains("~"),
names_sep = "~",
names_to = c("metric", "year")
)

data <- dplyr::mutate(data, year = as.integer(.data[["year"]]))

data <- tidyr::pivot_wider(
data,
names_from = "metric",
values_from = "value"
)

out <- data %>%
left_join(abcd, by = abcd_columns(), relationship = "many-to-many") %>%
inner_join(scenario, by = scenario_columns(), relationship = "many-to-many") %>%
warn_if_has_zero_rows("Joining `scenario` outputs 0 rows.") %>%
mutate(plant_location = tolower(.data$plant_location)) %>%
inner_join(
region_isos,
Expand All @@ -76,17 +112,21 @@ check_portfolio_abcd_scenario <- function(valid_matches, abcd, scenario) {
walk_(names(abcd_columns()), ~ check_no_value_is_missing(valid_matches, .x))

check_crucial_names(
abcd, c("name_company", "plant_location", unname(scenario_columns()))
abcd,
c("name_company", "plant_location", "year", unname(scenario_columns()))
)
walk_(
c("name_company", unname(scenario_columns())),
c("name_company", "year", unname(scenario_columns())),
~ check_no_value_is_missing(abcd, .x)
)


check_crucial_names(scenario, c(scenario_columns(), "scenario_source", "region"))
check_crucial_names(
scenario,
c(scenario_columns(), "scenario_source", "region", "year")
)
walk_(
c(scenario_columns(), "scenario_source", "region"),
c(scenario_columns(), "scenario_source", "region", "year"),
~ check_no_value_is_missing(scenario, .x)
)

Expand All @@ -105,7 +145,7 @@ add_green_technologies_to_abcd <- function(data, scenario) {

increasing_techs_not_in_abcd <- dplyr::filter(
increasing_techs_in_scenario,
!(technology %in% unique(data$technology))
!(.data[["technology"]] %in% unique(data$technology))
)

green_rows_to_add <- data %>%
Expand Down Expand Up @@ -137,7 +177,6 @@ abcd_columns <- function() {
scenario_columns <- function() {
c(
sector_abcd = "sector",
technology = "technology",
year = "year"
technology = "technology"
)
}
15 changes: 7 additions & 8 deletions R/summarize_weighted_production.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ summarize_weighted_production_ <- function(data, ..., use_credit_limit = FALSE,

old_groups <- dplyr::groups(data)

crucial <- c("production", "sector_abcd", "year", "technology")
crucial <- c("sector_abcd", "year", "technology")

if (with_targets) {
crucial <- c(crucial, "production_target")
}

check_crucial_names(data, crucial)
check_crucial_names(data, c("production", crucial))
walk_(crucial, ~ check_no_value_is_missing(data, .x))

data <- data %>%
Expand Down Expand Up @@ -187,10 +187,9 @@ summarize_unweighted_emission_factor <- function(data, ...) {
}

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))
check_crucial_names(data, c(metric, "loan_weight"))
walk_("loan_weight", ~ check_no_value_is_missing(data, .x))
jdhoffa marked this conversation as resolved.
Show resolved Hide resolved

data %>%
mutate(
Expand Down Expand Up @@ -234,9 +233,9 @@ add_loan_weight <- function(data, use_credit_limit) {
}

add_percent_change <- function(data) {
crucial <- c("production", "sector_abcd", "year", "technology", "scenario")
crucial <- c("sector_abcd", "year", "technology", "scenario")

check_crucial_names(data, crucial)
check_crucial_names(data, c("production", crucial))
walk_(crucial, ~ check_no_value_is_missing(data, .x))

check_zero_initial_production(data)
Expand Down Expand Up @@ -303,7 +302,7 @@ add_technology_share_target <- function(data) {

check_zero_initial_production <- function(data) {
companies_with_zero_initial_production <- data %>%
group_by(.data$technology, .data$name_abcd, .data$year) %>%
group_by(.data$technology, .data$name_abcd) %>%
jdhoffa marked this conversation as resolved.
Show resolved Hide resolved
arrange(.data$year) %>%
filter(.data$year == first(.data$year)) %>%
summarize(production_at_start_year = sum(.data$production)) %>%
Expand Down
25 changes: 15 additions & 10 deletions R/target_market_share.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ target_market_share <- function(data,
is.logical(weight_production)
)

abcd <- fill_and_warn_na(abcd, "production")

region_isos <- change_to_lowercase_and_warn(region_isos, "isos")

warn_if_by_company_and_weight_production(by_company, weight_production)
Expand All @@ -95,8 +93,23 @@ target_market_share <- function(data,

check_input_for_crucial_columns(data, abcd, scenario)

abcd <- fill_and_warn_na(abcd, "production")
abcd <- dplyr::summarize(
abcd,
production = sum(.data[["production"]]),
.by = -"production"
)

data <- aggregate_by_name_abcd(data)

if ("production" %in% colnames(scenario)) {
warn("The column `production` has been removed from the dataset `scenario`.
jdhoffa marked this conversation as resolved.
Show resolved Hide resolved
The columns `tmsr` and `smsp` will be used instead",
class = "scenario_production_column_removed")
scenario <- dplyr::select(scenario, -all_of("production"))
return(scenario)
}

data <- join_abcd_scenario(
data,
abcd,
Expand All @@ -109,14 +122,6 @@ target_market_share <- function(data,
return(empty_target_market_share_output())
}

if ("production" %in% colnames(scenario)) {
warn("The column `production` has been removed from the dataset `scenario`.
The columns `tmsr` and `smsp` will be used instead",
class = "scenario_production_column_removed")
scenario <- dplyr::select(scenario, -all_of("production"))
return(scenario)
}

crucial_groups <- c(
"id_loan",
"loan_size_outstanding",
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/join_abcd_scenario.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# i abbreviated name: 1: loan_size_outstanding_currency
# i 18 more variables: loan_size_credit_limit_currency <chr>, id_2dii <chr>,
# level <chr>, score <dbl>, sector <chr>, name_abcd <chr>, sector_abcd <chr>,
# technology <chr>, year <dbl>, production <dbl>, emission_factor <dbl>,
# plant_location <chr>, is_ultimate_owner <lgl>, scenario <chr>,
# region <chr>, tmsr <dbl>, smsp <dbl>, scenario_source <chr>
# technology <chr>, plant_location <chr>, is_ultimate_owner <lgl>,
# scenario <chr>, region <chr>, scenario_source <chr>, year <int>,
# production <dbl>, emission_factor <dbl>, tmsr <dbl>, smsp <dbl>

35 changes: 32 additions & 3 deletions tests/testthat/test-join_abcd_scenario.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ test_that("outputs a number of rows equal to matches by `scenario_source`", {
matching_2 <- join_abcd_scenario(
fake_matched(),
abcd = fake_abcd(plant_location = "a"),
scenario = fake_scenario(region = "b", scenario_source = c("c", "c")),
region_isos = tibble(isos = "a", region = "b", source = "c")
scenario = fake_scenario(region = "b", scenario_source = c("c", "d")),
region_isos = tibble(isos = "a", region = "b", source = c("c", "d"))
)
expect_equal(nrow(matching_2), 2L)
})
Expand Down Expand Up @@ -217,7 +217,7 @@ test_that("warns 0-rows caused by scenario or region_isos", {

test_that("include/excludes `plant_location` inside/outside a region", {
# styler: off
region_isos_toy <- tribble(
region_isos_toy <- dplyr::tribble(
~region, ~isos, ~source,
"north america", "us", "demo_2020",
"oecd", "de", "demo_2020",
Expand Down Expand Up @@ -259,3 +259,32 @@ test_that("outputs the same with upper/lower abcd$sector or abcd$technology", {
out_upper <- join_abcd_scenario(matched, upper_technology, scenario, regions)
expect_equal(out_upper, out_lower)
})

test_that("outputs full timeline of scenario #157", {

out <- join_abcd_scenario(
fake_matched(),
fake_abcd(year = 2020),
fake_scenario(scenario = "1.5c-scen", year = c(2020, 2025)),
region_isos = region_isos_stable
)

expect_equal(max(out$year), 2025L)

})

test_that("doesnt output sectors not in input data #157", {

out <- join_abcd_scenario(
fake_matched(sector_abcd = "power"),
fake_abcd(sector = "power", technology = "hydrocap"),
fake_scenario(
sector = c("power", "automotive"),
technology = c("hydrocap", "ice")
),
region_isos = region_isos_stable
)

expect_equal(unique(out$sector_abcd), "power")

})
2 changes: 0 additions & 2 deletions tests/testthat/test-summarize_weighted_production.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ test_that("with NAs in crucial columns errors with informative message", {
expect_error_crucial_NAs("id_loan")
expect_error_crucial_NAs("loan_size_outstanding")
expect_error_crucial_NAs("loan_size_credit_limit", use_credit_limit = TRUE)
expect_error_crucial_NAs("production")
expect_error_crucial_NAs("sector_abcd")
expect_error_crucial_NAs("technology")
expect_error_crucial_NAs("year")
Expand Down Expand Up @@ -260,7 +259,6 @@ test_that("with NAs in crucial columns errors with informative message", {
expect_error_crucial_NAs("id_loan")
expect_error_crucial_NAs("loan_size_outstanding")
expect_error_crucial_NAs("loan_size_credit_limit", use_credit_limit = TRUE)
expect_error_crucial_NAs("production")
expect_error_crucial_NAs("sector_abcd")
expect_error_crucial_NAs("technology")
expect_error_crucial_NAs("year")
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-target_market_share.R
Original file line number Diff line number Diff line change
Expand Up @@ -1442,3 +1442,17 @@ test_that("correctly splits scenario names with hyphen #425", {
expect_equal(unique(out$metric), "target_1.5c-scen")

})

test_that("outputs `target` for full timeline of scenario #157", {

out <- target_market_share(
fake_matched(),
fake_abcd(year = 2020),
fake_scenario(scenario = "1.5c-scen", year = c(2020, 2025)),
region_isos_stable
) %>%
filter(grepl("target", metric))

expect_equal(max(out$year), 2025L)

})
Loading