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

technology_share values seem off #262

Closed
jdhoffa opened this issue Jan 6, 2021 · 1 comment · Fixed by #263
Closed

technology_share values seem off #262

jdhoffa opened this issue Jan 6, 2021 · 1 comment · Fixed by #263

Comments

@jdhoffa
Copy link
Member

jdhoffa commented Jan 6, 2021

See reprex. Expected values were manually calculated in excel.


library(r2dii.data)
library(r2dii.analysis)
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

matched <- tibble::tribble(
  ~id_loan, ~loan_size_outstanding, ~loan_size_outstanding_currency, ~loan_size_credit_limit, ~loan_size_credit_limit_currency, ~id_2dii,             ~level, ~score, ~sector,   ~name_ald, ~sector_ald,
  "L1",                  1000L,                           "USD",                   1000L,                            "USD",    "DL1", "direct_loantaker",     1L, "power", "company a",     "power",
  "L2",                 15000L,                           "USD",                  15000L,                            "USD",    "DL2", "direct_loantaker",     1L, "power", "company b",     "power",
  "L3",                  1200L,                           "USD",                   1200L,                            "USD",    "DL1", "direct_loantaker",     1L, "power", "company a",     "power"
)

ald <- tibble::tribble(
  ~name_company, ~sector,     ~technology, ~year, ~production, ~emission_factor, ~plant_location, ~is_ultimate_owner,
  "company a", "power",       "coalcap", 2020L,        100L,              0.5,            "US",               TRUE,
  "company a", "power",        "gascap", 2020L,        200L,              0.2,            "US",               TRUE,
  "company a", "power",      "hydrocap", 2020L,        300L,                0,            "US",               TRUE,
  "company a", "power",    "nuclearcap", 2020L,        100L,                0,            "US",               TRUE,
  "company a", "power",        "oilcap", 2020L,        100L,              0.3,            "US",               TRUE,
  "company a", "power", "renewablescap", 2020L,        200L,                0,            "US",               TRUE,
  "company b", "power",       "coalcap", 2020L,        500L,              0.5,            "US",               TRUE,
  "company b", "power",        "gascap", 2020L,          0L,              0.2,            "US",               TRUE,
  "company b", "power",      "hydrocap", 2020L,          0L,                0,            "US",               TRUE,
  "company b", "power",    "nuclearcap", 2020L,        300L,                0,            "US",               TRUE,
  "company b", "power",        "oilcap", 2020L,        100L,              0.3,            "US",               TRUE,
  "company b", "power", "renewablescap", 2020L,        100L,                0,            "US",               TRUE
)

scenario <- tibble::tribble(
  ~scenario, ~sector,     ~technology,  ~region, ~year, ~tmsr, ~smsp, ~scenario_source,
  "sds", "power",       "coalcap", "global",  2020,     1,     0,      "demo_2020",
  "sds", "power",        "gascap", "global",  2020,     1,     0,      "demo_2020",
  "sds", "power",      "hydrocap", "global",  2020,     1,     0,      "demo_2020",
  "sds", "power",    "nuclearcap", "global",  2020,     1,     0,      "demo_2020",
  "sds", "power",        "oilcap", "global",  2020,     1,     0,      "demo_2020",
  "sds", "power", "renewablescap", "global",  2020,     1,     0,      "demo_2020"
)

out <- target_market_share(
  matched, 
  ald, 
  scenario, 
  region_isos_demo
) %>% 
  filter(metric == "projected")

expected_out <- tibble::tribble(
  ~production, ~technology_share,
  448.8372093,       0.448837209,
  25.58139535,       0.025581395,
  38.37209302,       0.038372093,
  274.4186047,       0.274418605,
  100,               0.1,
  112.7906977,       0.112790698
)

out %>% 
  select(production, technology_share)
#> # A tibble: 6 x 2
#>   production technology_share
#>        <dbl>            <dbl>
#> 1      449.            0.473 
#> 2       25.6           0.0137
#> 3       38.4           0.0205
#> 4      274.            0.286 
#> 5      100             0.10  
#> 6      113.            0.107

expected_out
#> # A tibble: 6 x 2
#>   production technology_share
#>        <dbl>            <dbl>
#> 1      449.            0.449 
#> 2       25.6           0.0256
#> 3       38.4           0.0384
#> 4      274.            0.274 
#> 5      100             0.1   
#> 6      113.            0.113

Created on 2021-01-06 by the reprex package (v0.3.0)

@jdhoffa
Copy link
Member Author

jdhoffa commented Jan 6, 2021

The issue arises because there are multiple loans given to the same company. When we calculate the technology_share, it then "double counts" the companies overall production so-to-speak, and drops it's technology_share slightly lower than it should.

I will add a function earlier in the target_* function to aggregate all loans to the same company (likely should have done this a while ago).

maurolepore added a commit that referenced this issue Jan 6, 2021
Closes #262 

As the first step of target_market_share, I now aggregate by id_loan to the same company
on the input data.

Co-authored-by: GitHub Actions <[email protected]>
Co-authored-by: Mauro Lepore <[email protected]>
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