-
Notifications
You must be signed in to change notification settings - Fork 7
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
technology_share
for target_*
is now properly calculated and weighted
#294
Merged
maurolepore
merged 22 commits into
RMI-PACTA:master
from
jdhoffa:277-target_before_weight
May 19, 2021
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
02a588d
calculate targets prior to summarizing
jdhoffa f27edf8
add function to deal with tech_share_targets
jdhoffa 6fce3fe
add failing test
jdhoffa 54509b7
prevent double counting over `region`
jdhoffa 4e01902
update expected values with correct tech_share
jdhoffa 697e9b0
style
jdhoffa 876420c
remove old FIXME
jdhoffa a45838a
update NEWS.md
jdhoffa f995b66
update NEWS.md
jdhoffa 2a3aeae
fix NEWS.md
jdhoffa 16c0deb
knit readme
jdhoffa 473265d
sum with narm TRUE
jdhoffa c7a851d
check crucials at exported fn
jdhoffa 394e1d0
Revert "check crucials at exported fn"
jdhoffa 6c5214d
Revert "sum with narm TRUE"
jdhoffa 049e1c9
refactor summarize_*_targets
jdhoffa 1dcc4e8
refactor: combine summarize-like functions
jdhoffa f1647c5
check crucials at exported fn
jdhoffa 66b797a
dont change exported function
jdhoffa d1dcd69
add_targets -> with_targets
jdhoffa e6beae1
move FIXME and make more descriptive
jdhoffa 5c1cd07
Replace rbind() with dplyr::bind_rows()
maurolepore 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 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 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 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 |
---|---|---|
|
@@ -140,58 +140,59 @@ target_market_share <- function(data, | |
|
||
data <- join_ald_scenario(data, ald, scenario, region_isos) | ||
|
||
if (nrow(data) == 0) { | ||
return(empty_target_market_share_output()) | ||
} | ||
|
||
summary_groups <- c( | ||
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", | ||
"region", | ||
"scenario_source", | ||
"name_ald" | ||
"scenario_source" | ||
) | ||
|
||
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) | ||
data <- data %>% | ||
group_by(!!!rlang::syms(crucial_groups)) %>% | ||
summarize( | ||
production = sum(.data$production) | ||
) | ||
|
||
if (nrow(data) == 0) { | ||
return(empty_target_market_share_output()) | ||
} | ||
|
||
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 +205,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 +221,28 @@ 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 <- summarize_weighted_production_( | ||
data, | ||
!!!rlang::syms(summary_groups), | ||
use_credit_limit = use_credit_limit, | ||
with_targets = TRUE | ||
) | ||
} else { | ||
data <- summarize_unweighted_production( | ||
data, | ||
!!!rlang::syms(summary_groups), | ||
with_targets = TRUE | ||
) | ||
} | ||
|
||
if (!by_company) { | ||
aggregate_company_groups <- c( | ||
"sector_ald", | ||
|
@@ -235,7 +258,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 +273,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, | ||
|
@@ -284,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) %>% | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well hot diggity! |
||
ungroup() | ||
} | ||
|
||
|
@@ -306,8 +322,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 +413,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() | ||
} | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now happens AFTER adding the targets. (See line 231). I also had to expand the
summarize_weighted_production
function, as it now needs to implement something different.A lot of WET code here unfortunately.