Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(calculate_match_success_rate)
export(crucial_lbk)
export(match_name)
export(prioritize)
export(prioritize_level)
importFrom(data.table,":=")
importFrom(data.table,setDT)
importFrom(data.table,setkey)
importFrom(dplyr,"%>%")
importFrom(dplyr,all_of)
importFrom(dplyr,distinct)
importFrom(dplyr,filter)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# r2dii.match (development version)

* `calculate_match_success_rate()` added to facilitate analyzing the success rate of the matching process

# r2dii.match 0.4.0

* `data_dictionary` dataset added to define the columns in each dataset used or exported by the functions in this package
Expand Down
47 changes: 47 additions & 0 deletions R/calculate_match_success_rate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#' Calculate the success rate of the matching process
#'
#' @param matched A data frame like the validated output of [match_name()].
#'
#' @param loanbook A data frame structured like [r2dii.data::loanbook_demo].
#'
#' @return A data frame.
#'
#' @examples
#' matched <- match_name(r2dii.data::loanbook_demo, r2dii.data::abcd_demo)
#' calculate_match_success_rate(matched, r2dii.data::loanbook_demo)
#'
#' @family main functions
#'
#' @importFrom dplyr %>%
#'
#' @export

calculate_match_success_rate <- function(matched, loanbook) {
merge_by <-
c(
sector_classification_system = "code_system",
sector_classification_direct_loantaker = "code"
)

loanbook_with_sectors <-
loanbook %>%
purrr::modify_at(names(merge_by), as.character) %>%
dplyr::left_join(r2dii.data::sector_classifications, by = merge_by)

by_cols <- names(matched)[names(matched) %in% names(loanbook_with_sectors)]

dplyr::left_join(loanbook_with_sectors, matched, by = by_cols) %>%
dplyr::mutate(
loan_size_outstanding = as.numeric(.data$loan_size_outstanding),
loan_size_credit_limit = as.numeric(.data$loan_size_credit_limit),
matched = dplyr::case_when(
score == 1 ~ "Matched",
is.na(score) ~ "Not Matched",
TRUE ~ "Not Matched"
),
sector = dplyr::case_when(
borderline == TRUE & matched == "Not Matched" ~ "not in scope",
TRUE ~ sector
)
)
}
30 changes: 30 additions & 0 deletions man/calculate_match_success_rate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/match_name.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/prioritize.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/testthat/test-calculate_match_success_rate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test_that("returns expected output", {
loanbook <- fake_lbk(
name_direct_loantaker = c("Steckel GmbH", "XXX"),
sector_classification_direct_loantaker = c("C29.1", "C29.1"),
loan_size_outstanding = c(1e6, 1e6),
loan_size_credit_limit = c(1e7, 1e7)
)
matched <- match_name(loanbook = loanbook, abcd = r2dii.data::abcd_demo)
out <- calculate_match_success_rate(matched = matched, loanbook = loanbook)

expect_s3_class(object = out, class = "data.frame")
expect_contains(object = names(out), expected = "matched")
expect_identical(object = out$matched, expected = c("Matched", "Not Matched"))
})
Loading