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

Add correct technology_mix calculation #184

Closed
jdhoffa opened this issue Aug 21, 2020 · 0 comments · Fixed by #192
Closed

Add correct technology_mix calculation #184

jdhoffa opened this issue Aug 21, 2020 · 0 comments · Fixed by #192
Assignees

Comments

@jdhoffa
Copy link
Member

jdhoffa commented Aug 21, 2020

I had a large misconception in my understanding of how we should calculate the "technology mix" metric.
It was my understanding that we simply take the relative portfolio-weight of each company (the loan size to that company divided by the total loans to that sector), and multiply that by the actual production in each technology. This is correct for the Volume Trajectory, but the Technology Mix is a little more subtle.

For the technology mix, we should actually multiply the technology share of each company by the portfolio-weight. It's a subtle difference but affects the results significantly.

I will prioritize this immediately post-vacation and will aim to have the feature added before a second CRAN release that week.

This result CAN actually be achieved with the current code and a bit of massaging. See the following reprex:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(r2dii.data)
library(r2dii.match)
library(r2dii.analysis)

# This is just a demo, but you should use your actual portfolio "match_result" file
match_result <- loanbook_demo %>%
  match_name(ald_demo) %>%
  prioritize()

# First we will use the match results to calculate the sectoral portfolio-weight
# of each company in your loanbook. Note: we use loan_size_outstanding here.
# Use credit limit if you wish.
portfolio_weights <- match_result %>%
  select(id_loan, loan_size_outstanding, name_ald, sector_ald) %>%
  group_by(sector_ald) %>%
  mutate(
    sector_total = sum(loan_size_outstanding),
    portfolio_weight = loan_size_outstanding / sector_total
  ) %>%
  select(name_ald, sector_ald, portfolio_weight)
ungroup()
#> Error in UseMethod("ungroup"): no applicable method for 'ungroup' applied to an object of class "NULL"

# Then we will calculate the RAW technology mix of each company (using their
# FULL production), ie. `company_technology_mix`. We will then join in the
# portfolio weights, and weight the technology mix, ie.
# `portfolio_technology_share`.
company_level_results <-
  match_result %>%
  target_market_share(
    ald_demo,
    scenario_demo_2020,
    region_isos_demo,
    by_company = TRUE,
    weight_production = FALSE
  ) %>%
  select(-c(tmsr, smsp)) %>%
  filter(production_metric == "projected") %>%
  group_by(name_ald, year) %>%
  mutate(
    sector_total = sum(production_value),
    company_technology_share = production_value / sector_total
  ) %>%
  left_join(
    portfolio_weights, by = c(
      name_ald = "name_ald",
      sector = "sector_ald"
      )
    ) %>%
  mutate(
    portfolio_technology_share = portfolio_weight * company_technology_share
    )
#> Adding missing grouping variables: `tmsr`, `smsp`

# We will then aggregate and re-normalize this "weighted mix of technologies" to
# get the portfolio's actual technology mix
portfolio_level_results <- company_level_results %>%
  group_by(sector, technology, year, region) %>%
  summarize(portfolio_technology_mix = sum(company_technology_share)) %>%
  group_by(sector, year, region) %>%
  mutate(
    x = portfolio_technology_mix,
    portfolio_technology_mix = x / sum(x),
    x = NULL
    )
#> `summarise()` regrouping output by 'sector', 'technology', 'year' (override with `.groups` argument)

Created on 2020-08-21 by the reprex package (v0.3.0)

@jdhoffa jdhoffa self-assigned this Aug 21, 2020
@jdhoffa jdhoffa added this to To do in r2dii via automation Aug 21, 2020
@jdhoffa jdhoffa moved this from To do to In progress in r2dii Sep 1, 2020
r2dii automation moved this from In progress to Done Sep 2, 2020
@AlexAxthelm AlexAxthelm removed this from Done in r2dii Sep 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant