knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

The package r2dii.match helps you to match counterparties from a loanbook to companies in a physical-asset database. Each section below shows you how.

Setup

We use the package r2dii.match to access the most important functions you'll learn about. We also use example datasets from the package r2dii.data, and optional but convenient functions from the packages dplyr and readr.

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

Format input data loanbook and asset-based company data (abcd)

We need two datasets: a "loanbook" and an "asset-based company dataset" (abcd). These should be formatted like: loanbook_demo and abcd_demo (from the r2dii.data package).

A note on sector classification: Matches are preferred when the sector from the loanbook matches the sector from the abcd. The loanbook sector is determined internally using the sector_classification_system and sector_classification_direct_loantaker columns. Currently, we only allow a couple specific values for sector_classification_system:

sector_classifications$code_system %>%
  unique()

If you would like to use a different classification system, please raise an issue in r2dii.data and we can incorporate it.

loanbook_demo

abcd_demo

If you want to use loanbook_demo and abcd_demo as template to create your own datasets, do this:

# Writting to current working directory 
loanbook_demo %>% 
  readr::write_csv(path = "loanbook_demo.csv")

abcd_demo %>% 
  readr::write_csv(path = "abcd_demo.csv")
# Reading from current working directory 
your_loanbook <- readr::read_csv("your_loanbook.csv")
your_abcd <- readr::read_csv("your_abcd.csv")

Here we continue to use the *_demo datasets, pretending they contain the data of your own.

# WARNING: Skip this to avoid overwriting your data with our demo data
your_loanbook <- loanbook_demo
your_abcd <- abcd_demo

Score the goodness of the match between the loanbook and abcd datasets

match_name() scores the match between names in a loanbook dataset (lbk) and names in an asset-based company dataset (abcd). The names come from the columns name_direct_loantaker, name_intermediate_parent_* and name_ultimate_parent of the loanbook dataset, and from the column name_company of the a asset-based company dataset. There can be any number of name_intermediate_parent_* columns, where * indicates the level up the corporate tree from direct_loantaker.

The raw names are internally transformed applying best-practices commonly used in name matching algorithms, such as:

The similarity is then scored between the internally-transformed names of the loanbook against the abcd. (For more information on the scoring algorithm used, see: stringdist::stringsim()).

match_name(your_loanbook, your_abcd)

match_name() defaults to scoring matches between name strings that belong to the same sector. Using by_sector = FALSE removes this limitation -- increasing computation time, and the number of potentially incorrect matches to manually validate.

match_name(your_loanbook, your_abcd, by_sector = FALSE) %>%
  nrow()

# Compare
match_name(your_loanbook, your_abcd, by_sector = TRUE) %>%
  nrow()

min_score allows you to minimum threshold score.

matched <- match_name(your_loanbook, your_abcd, min_score = 0.9)
range(matched$score)

Maybe overwrite matches

If you are happy with the matching coverage achieved, proceed to the next step. Otherwise, you can manually add matches, not found automatically by match_name(). To do this, manually inspect the abcd and find a company you would like to match to your loanbook. Once a match is found, use excel to write a .csv file similar to overwrite_demo, where:

matched <- match_name(
  your_loanbook, your_abcd,
  min_score = 0.9, overwrite = overwrite_demo
)

Notice the warning.

Validate matches


Prioritize validated matches by level

The validated dataset may have multiple matches per loan. Consider the case where a loan is given to "Acme Power USA", a subsidiary of "Acme Power Co.". There may be both "Acme Power USA" and "Acme Power Co." in the abcd, and so there could be two valid matches for this loan. To get the best match only, use prioritize() -- it picks rows where score is 1 and level per loan is of highest priority():

# Pretend we validated the matched dataset
valid_matches <- matched

some_interesting_columns <- vars(id_2dii, level, score)

valid_matches %>%
  prioritize() %>%
  select(!!!some_interesting_columns)

By default, highest priority refers to the most granular match (direct_loantaker). The default priority is set internally via prioritize_levels().

prioritize_level(matched)

You may use a different priority. One way to do that is to pass a function to priority. For example, use rev to reverse the default priority.

matched %>%
  prioritize(priority = rev) %>%
  select(!!!some_interesting_columns)


2DegreesInvesting/r2dii.match documentation built on Sept. 12, 2022, 11:15 p.m.