Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #77 from RMI-PACTA/return-meta-loanbook
Browse files Browse the repository at this point in the history
Return meta loanbook
  • Loading branch information
jacobvjk authored Mar 1, 2024
2 parents 9cf6318 + d655e1c commit 8b7c2db
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
12 changes: 12 additions & 0 deletions run_aggregate_loanbooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ if (apply_sector_split & sector_split_type_select %in% c("equal_weights", "worst
)
}

# meta loan book----
# aggregate all individual loan books into one meta loan book and add that to
# the full list of loan books
matched_prioritized_meta <- matched_prioritized %>%
dplyr::mutate(
id_loan = paste0(.data$id_loan, "_", .data$group_id),
group_id = "meta_loanbook"
)

matched_prioritized <- matched_prioritized %>%
dplyr::bind_rows(matched_prioritized_meta)

# write final version of matched_prioritized to file for use in plot_aggregate_loanbooks.R
matched_prioritized %>%
readr::write_csv(file.path(input_path_matched, "matched_prioritized_final_for_plots.csv"))
Expand Down
30 changes: 25 additions & 5 deletions run_pacta_in_bulk.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ if (file.exists(here::here(".env"))) {
# TODO: add check if all files exist, resort to test files if not

# load input data----
region_isos_complete <- r2dii.data::region_isos

region_isos_select <- region_isos_complete %>%
region_isos_select <- r2dii.data::region_isos %>%
dplyr::filter(
.data$source == .env$scenario_source_input,
.data$region %in% .env$region_select
Expand Down Expand Up @@ -138,12 +136,25 @@ if (apply_sector_split & sector_split_type_select %in% c("equal_weights", "worst
)
}

# meta loan book----
# aggregate all individual loan books into one meta loan book and add that to
# the full list of loan books
matched_prioritized_meta <- matched_prioritized %>%
dplyr::mutate(
id_loan = paste0(.data$id_loan, "_", .data$group_id),
group_id = "meta_loanbook"
)

matched_prioritized <- matched_prioritized %>%
dplyr::bind_rows(matched_prioritized_meta)

# generate all P4B outputs----
unique_loanbooks_matched <- unique(matched_prioritized$group_id)

## generate SDA outputs----
results_sda_total <- NULL

# generate SDA results for each individual loan book, including the meta loan book
for (i in unique_loanbooks_matched) {
matched_i <- matched_prioritized %>%
dplyr::filter(.data$group_id == i) %>%
Expand All @@ -161,14 +172,19 @@ for (i in unique_loanbooks_matched) {
dplyr::bind_rows(results_sda_i)
}

# write SDA results to csv
results_sda_total %>%
readr::write_csv(file.path(output_path_standard, "sda_results_all_groups.csv"))
readr::write_csv(
file.path(output_path_standard, "sda_results_all_groups.csv"),
na = ""
)


## generate TMS outputs----

results_tms_total <- NULL

# generate TMS results for each individual loan book, including the meta loan book
for (i in unique_loanbooks_matched) {
matched_i <- matched_prioritized %>%
dplyr::filter(.data$group_id == i) %>%
Expand All @@ -186,8 +202,12 @@ for (i in unique_loanbooks_matched) {
dplyr::bind_rows(results_tms_i)
}

# write TMS results to csv
results_tms_total %>%
readr::write_csv(file.path(output_path_standard, "tms_results_all_groups.csv"))
readr::write_csv(
file.path(output_path_standard, "tms_results_all_groups.csv"),
na = ""
)

# generate P4B plots----

Expand Down

0 comments on commit 8b7c2db

Please sign in to comment.