RMI-PACTA / r2dii.analysis

Tools to Calculate Climate Targets for Financial Portfolios
https://rmi-pacta.github.io/r2dii.analysis
Other
12 stars 7 forks source link

New `summarize_weighted_production()` #33

Closed maurolepore closed 4 years ago

maurolepore commented 4 years ago

Here what I captured from a meeting with @jdhoffa on 2020-02-24

Issue

New add_weighted_production() adds column weighted_production -- short for sum_of_weighted_production_by_sector_by_technology_by_year.

User story

As an analyst I want to have a function add_weighted_production(). add_weighted_production() takes a matched dataset (i.e. the output of match_name(porfolio, ald)) and adds the the column weighted_production which aggregates the weighted production off all loans for each sector, tecnology, and year.

The weighted production should be calculated as the loan_size* / sum(loan_size*_by_sector), where loan_size* defaults to loan_size_outstandings but could optionally be loan_size_limit.

Draft signature

add_weighted_production(
  data,
  loan_size = c("loan_size_outstandings", "loan_size_credit_limit")
)

Draft implementation


# store old groups

data %>% 
  ungroup()
  group_by(sector) %>% 
  mutate(sum_of_loan_size_by_sector = sum(loan_size*)) %>% 
  group_by(loan_id) %>% 
  mutate(loan_size_weighted_by_sector = loan_size* / sum_of_loan_size_by_sector) %>% 
  mutate(
    weighted_production_proxy = loan_size_weighted_by_sector * production
  ) %>% 
  ungroup() %>% 
  group_by(sector, technology, year) %>% 
  mutate(
    sum_of_weighted_production_by_sector_by_technology_by_year = 
      sum(weighted_production_proxy)
  ) %>% 
  ungroup() %>% 

  # restore old groups

loan_size*: The asterisk * reflects that it could be any of the valid inputs to the loan_size argument.

Assert

Test ideas

jdhoffa commented 4 years ago

Holy shit, this is incredible! Love this layout for proposing new functionality in issues, and I will follow it from now onward.