Skip to content

Commit

Permalink
Aggregate all loans to the same company in target_market_share (#263)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
3 people authored Jan 6, 2021
1 parent 98fcd4f commit 279459d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# r2dii.analysis (development version)

* `target_market_share()` now correctly outputs `technology_share` with
multiple loans to the same company (@georgeharris2deg #262).

# r2dii.analysis 0.1.4

* `target_market_share()` now correctly outputs unweighted production by
Expand Down
15 changes: 15 additions & 0 deletions R/target_market_share.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ target_market_share <- function(data,

data <- ungroup(warn_grouped(data, "Ungrouping input data."))

data <- aggregate_by_loan_id(data)

crucial_scenario <- c("scenario", "tmsr", "smsp")
check_crucial_names(scenario, crucial_scenario)
check_crucial_names(ald, "is_ultimate_owner")
Expand Down Expand Up @@ -386,3 +388,16 @@ reweight_technology_share <- function(data, ...) {
) %>%
ungroup()
}

aggregate_by_loan_id <- function(data) {
aggregate_columns <- c("id_loan", "loan_size_outstanding", "loan_size_credit_limit")

data %>%
dplyr::group_by_at(setdiff(names(data), aggregate_columns)) %>%
summarize(
id_loan = first(.data$id_loan),
loan_size_outstanding = sum(.data$loan_size_outstanding),
loan_size_credit_limit = sum(.data$loan_size_credit_limit)
) %>%
ungroup()
}
72 changes: 71 additions & 1 deletion tests/testthat/test-target_market_share.R
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ test_that("outputs same names regardless of the value of `weight_production` (#1
expect_equal(diff_names, character(0))
})

test_that("with known input outputs `technology_share` as expected (#184)", {
test_that("with known input outputs `technology_share` as expected (#184, #262)", {
matched <- fake_matched(
id_loan = c("L1", "L2"),
loan_size_outstanding = c(1, 3),
Expand Down Expand Up @@ -445,6 +445,76 @@ test_that("with known input outputs `technology_share` as expected (#184)", {
c(0.923, 0.076),
tolerance = 1e-3
)

################ More extensive test (#262)

matched <- fake_matched(
id_loan = c("L1", "L2", "L3"),
loan_size_outstanding = c(1000, 15000, 1200),
name_ald = c("a", "b", "a"),
id_2dii = c("DL1", "DL2", "DL1"),
sector = "power",
sector_ald = "power"
)

ald <- fake_ald(
name_company = rep(c("a", "b"), each = 6),
sector = "power",
technology = rep(
c(
"coalcap",
"gascap",
"hydrocap",
"nuclearcap",
"oilcap",
"renewablescap"
),
2
),
production = c(100, 200, 300, 100, 100, 200, 500, 0, 0, 300, 100, 100),
year = 2020
)

scenario <- fake_scenario(
sector = "power",
technology = c(
"coalcap",
"gascap",
"hydrocap",
"nuclearcap",
"oilcap",
"renewablescap"
),
year = 2020,
tmsr = 1,
smsp = 0
)

out <- target_market_share(
matched,
ald,
scenario,
region_isos_stable
) %>%
split(.$metric)

expect_equal(
out$projected$technology_share,
c(0.4488, 0.0255, 0.0383, 0.2744, 0.1, 0.1128),
tolerance = 1e-3
)

expect_equal(
out$target_sds$technology_share,
c(0.4488, 0.0255, 0.0383, 0.2744, 0.1, 0.1128),
tolerance = 1e-3
)

expect_equal(
out$corporate_economy$technology_share,
c(0.3, 0.1, 0.15, 0.2, 0.1, 0.15),
tolerance = 1e-3
)
})

test_that("w/ some region missing some scenario outputs expected `production`
Expand Down

0 comments on commit 279459d

Please sign in to comment.