Skip to content

Instantly share code, notes, and snippets.

@maurolepore
Created May 3, 2021 12:14
Show Gist options
  • Save maurolepore/ef73cff4d3389791706e934d3fa4703b to your computer and use it in GitHub Desktop.
Save maurolepore/ef73cff4d3389791706e934d3fa4703b to your computer and use it in GitHub Desktop.
library(dplyr, warn.conflicts = FALSE)
library(testthat, warn.conflicts = FALSE)
library(r2dii.match)
library(r2dii.analysis)

.production <- c(1, 10)
.year <- 2022
.company <- "toyota motor corp"
.sector <- "automotive"
.technology <- c("hybrid", "ice")

ald <- tibble(
  production = .production,
  name_company = .company,
  technology = .technology,
  sector = .sector,
  year = .year,
  plant_location = c("US"),
  emission_factor = 1,
  is_ultimate_owner = TRUE
)

matched <- tibble(
  sector = .sector,
  sector_ald = .sector,
  name_ald = .company,
  id_loan = "L1",
  loan_size_outstanding = 1,
  loan_size_outstanding_currency = "XYZ",
  loan_size_credit_limit = 1,
  loan_size_credit_limit_currency = "XYZ",
  id_2dii = "DL1",
  level = "direct_loantaker",
  score = 1
)

scenario <- tibble(
    sector = .sector,
    scenario = "cps",
    technology = .technology,
    region = "global",
    year = .year,
    tmsr = 1,
    smsp = c(0.100, 0.101),
    scenario_source = "demo_2020"
  )

region <- tibble(region = "global", isos = "us", source = "demo_2020")

result <- matched %>%
  target_market_share(
    ald = ald,
    scenario = scenario,
    region_isos = region,
    by_company = TRUE,
    weight_production = FALSE

  )
out <- result %>% filter(metric == "projected")

expect_equal(
  out$technology_share,
  out$production / sum(out$production)
)
#> Error: out$technology_share not equal to out$production/sum(out$production).
#> 2/2 mismatches (average diff: 0.409)
#> [1] 0.5 - 0.0909 ==  0.409
#> [2] 0.5 - 0.9091 == -0.409

Created on 2021-05-03 by the reprex package (v2.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment