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

target_market_share function generates wrong "projected" scenario #288

Closed
KapitanKombajn opened this issue Apr 21, 2021 · 8 comments · Fixed by #293
Closed

target_market_share function generates wrong "projected" scenario #288

KapitanKombajn opened this issue Apr 21, 2021 · 8 comments · Fixed by #293
Assignees
Labels
bug an unexpected problem or unintended behavior

Comments

@KapitanKombajn
Copy link

Hi,
I believe, function target_market_share generates wrong technology_share values for „projected” metric, when the following conditions are met:

  • when only one company is in the matched loanbook,
  • flag by_company is set to TRUE
  • and weight_production is set to FALSE.
    In such situation, the technology_share for each technology is the same (for example, if in ALD dataset, a company has some capacity in 4 technologies within a given sector in year t, its capacity in year t+1 will be 25% for each technology, no matter what the starting share was in year t).

Example code (based on documentation), where each out of 3 technologies has 0.33 share:

your_loanbook <- loanbook_demo
your_ald <- ald_demo
matched <- match_name(your_loanbook, your_ald, min_score = 0.9) %>% prioritize()

#example company
matched <- matched[matched$name_direct_loantaker=='Toyota Motor Corporation',]

market_share_targets_company <- matched %>%
  target_market_share(
    ald = your_ald,
    scenario = scenario,
    by_company = TRUE,
    weight_production=FALSE
    
  )

tech_mix_data <- market_share_targets_company %>%
  filter(
    sector == "automotive",
    region == "global", 
    year == 2021,
    scenario_source =='etp_2017'
)


ggplot(
  data = tech_mix_data, 
  mapping = aes(
    x = metric, 
    y = technology_share,
    fill = technology
  )
) +
  geom_col(position='fill')+
  geom_text(aes(label=round(technology_share,2)), position="stack", size = ifelse(tech_mix_data$technology_share<0.02,0,7)) + 
  labs(x = "Metric", y = "Weighted Capacity [%]")
@cjyetman
Copy link
Member

I'm guessing this was intended for https://github.com/2DegreesInvesting/r2dii.analysis?

Maybe @maurolepore can transfer it there?

@maurolepore maurolepore transferred this issue from RMI-PACTA/PACTA_analysis Apr 21, 2021
@maurolepore
Copy link
Contributor

Thanks @KapitanKombajn and @cjyetman
We'll follow up here.

@maurolepore maurolepore added the bug an unexpected problem or unintended behavior label Apr 21, 2021
maurolepore added a commit that referenced this issue Apr 28, 2021
@maurolepore
Copy link
Contributor

@KapitanKombajn thanks a lot for reporting this issue. The main maintainer of this package (@jdhoffa) is likely busy but I could help if you are kind enough to reword your comment to clarify (a) what output you expected, and (b) what output you got instead. I would also be super helpful if you could convert your example in a reprex.

Here is a template you could adapt (and maybe paste inside reprex::reprex({ # your code goes here}) to generate the reprex):

library(dplyr, warn.conflicts = FALSE)
library(r2dii.data)
library(r2dii.match)
library(r2dii.analysis)



# TODO: Adapt to fake the problematic input
single_company <- 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,
    "L162",                      1,                           "EUR",                       2,                            "EUR",    "UP1", "ultimate_parent",      1, "automotive", "shaanxi auto", "automotive"
)

# TODO: Adapt to fake the problematic input
ald <- tibble::tribble(
   ~name_company,      ~sector, ~technology, ~year, ~production, ~emission_factor, ~plant_location, ~is_ultimate_owner,
  "shaanxi auto", "automotive",       "ice",  2025,           1,                1,            "BF",               TRUE
)

# TODO: Adapt to fake the problematic input
scenario <- tibble::tribble(
  ~scenario,      ~sector, ~technology,  ~region, ~year, ~tmsr, ~smsp, ~scenario_source,
      "sds", "automotive",       "ice", "global",  2025,   0.5, -0.08,      "demo_2020"
)

# TODO: Adapt to fake the problematic input
regions <- tibble::tribble(
   ~region, ~isos,     ~source,
  "global",  "bf", "demo_2020"
)



out <- target_market_share(
  ald = ald,
  scenario = scenario,
  region_isos = regions,
  # With this arguments
  data = single_company,
  by_company = TRUE,
  weight_production = FALSE
)

# TODO: Complete
# I expected the output to be ...
# Instead the output is ...
out
#> # A tibble: 3 x 11
#>   sector   technology  year region scenario_source name_ald   sector_weighted_p…
#>   <chr>    <chr>      <dbl> <chr>  <chr>           <chr>                   <dbl>
#> 1 automot… ice         2025 global demo_2020       shaanxi a…                  1
#> 2 automot… ice         2025 global demo_2020       shaanxi a…                  1
#> 3 automot… ice         2025 global demo_2020       corporate…                 NA
#> # … with 4 more variables: technology_weighted_production <dbl>, metric <chr>,
#> #   production <dbl>, technology_share <dbl>

Created on 2021-04-29 by the reprex package (v2.0.0)

@KapitanKombajn
Copy link
Author

Hi @maurolepore, sorry for not being clear enough. Here's my rewritten example:

library(r2dii.data)
library(r2dii.match) 
library(r2dii.analysis)
library(dplyr, warn.conflicts = FALSE)
library(tidyverse)

#test datasets
your_loanbook <- loanbook_demo
your_ald <- ald_demo

matched <- match_name(loanbook_demo, ald_demo) %>%
  prioritize()

#select only one company to ease presentation
matched <- matched[matched$name_direct_loantaker=='Toyota Motor Corporation',]

#calculate results
market_share_targets_company<- matched %>%
  target_market_share(
    ald = ald_demo,
    scenario = scenario_demo_2020,
    region_isos = region_isos_demo,
    by_company = TRUE,
    weight_production = FALSE
    
  )

tech_mix_data <- market_share_targets_company %>%
  filter(
    sector == "automotive",
    region == "global", 
    metric == "projected",
    scenario_source =='demo_2020'
  )

#recalculation of technology_share
tmp<-aggregate(tech_mix_data$production, by=list(year=tech_mix_data$year), sum)
colnames(tmp)<-c("year", "total_production")

tech_mix_data_corrected<- merge(tech_mix_data,tmp)

tech_mix_data_corrected$technology_share_expected<- tech_mix_data_corrected$production/tech_mix_data_corrected$total_production

#graph of the original data (technology share is wrong starting from year 2021)
ggplot(
  data = tech_mix_data_corrected, 
  mapping = aes(
    x = year, 
    y = technology_share,
    fill = technology
  )
) +
  geom_col(position='fill')+
  geom_text(aes(label=round(technology_share,2)), position="stack") + 
  labs(x = "Production metric", y = "Technology Share [%]")

#graph of the corrected  data
ggplot(
  data = tech_mix_data_corrected, 
  mapping = aes(
    x = year, 
    y = technology_share_expected,
    fill = technology
  )
) +
  geom_col(position='fill')+
  geom_text(aes(label=round(technology_share_expected,2)), position="stack") + 
  labs(x = "Production metric", y = "Technology Share [%]")

Created on 2021-04-30 by the reprex package (v2.0.0)

@KapitanKombajn
Copy link
Author

Hi, I just wanted to ask if this fix is live already or not yet? I just reran the above reprex but unfortunatelly it still generates wrong results.

@jdhoffa
Copy link
Member

jdhoffa commented May 19, 2021

We have not released the update to CRAN yet, we are aiming to fix another bug before we release it.
However you can (at your own risk) install the development version of the code using:
devtools::install_github("2DegreesInvesting/r2dii.analysis")

@KapitanKombajn
Copy link
Author

Got it. Thanks!

@jdhoffa
Copy link
Member

jdhoffa commented May 28, 2021

FYI the package is now uploaded to CRAN and you should see the changes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants