Skip to content
This repository has been archived by the owner on Sep 30, 2022. It is now read-only.

Commit

Permalink
Merge e234d93 into 8efbdeb
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolepore committed Feb 7, 2020
2 parents 8efbdeb + e234d93 commit f452530
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions R/match_name.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ match_name <- function(loanbook,
return()
}

preferred <- prefer_perfect_match_by(matched, .data$id)
preferred <- prefer_perfect_match_by(matched, .data$id_2dii)

preferred %>%
restore_cols_sector_name_from_ald(prep_ald, by_sector = by_sector) %>%
Expand Down Expand Up @@ -193,7 +193,7 @@ intersect_names_as_in <- function(data, reference) {

names_added_by_match_name <- function() {
c(
"id",
"id_2dii",
"level",
"sector",
"sector_ald",
Expand Down
6 changes: 3 additions & 3 deletions R/restructure_loanbook_for_matching.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ may_overwrite_name_and_sector <- function(data, overwrite) {

overwrite_name_and_sector <- function(data, overwrite) {
data %>%
left_join(overwrite, by = c("id", "level")) %>%
left_join(overwrite, by = c("id_2dii", "level")) %>%
mutate(
source = if_else(is.na(.data$source.y), .data$source.x, "manual"),
sector = if_else(is.na(.data$sector.y), .data$sector.x, .data$sector.y),
Expand Down Expand Up @@ -164,7 +164,7 @@ check_prepare_loanbook_overwrite <- function(overwrite) {
output_cols_for_prepare_loanbook <- function() {
c(
"level",
"id",
"id_2dii",
"name",
"sector",
"source"
Expand All @@ -177,7 +177,7 @@ identify_loans_by_level <- function(data) {
cols = tidyr::starts_with("id_"),
names_to = "level",
names_prefix = "id_",
values_to = "id"
values_to = "id_2dii"
)
}

Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ matched <- read_csv("matched_edited.csv")
The `matched` dataset may have multiple matches per loan. To get the best match only, use `priorityze()` -- it picks rows where `score` is 1 and `level` per loan is of highest `priority()`.

```{r}
some_interesting_columns <- vars(id, level, starts_with("alias"), score)
some_interesting_columns <- vars(id_2dii, level, starts_with("alias"), score)
matched %>%
prioritize() %>%
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/helper-fake_lbk.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ fake_ald <- function(name_company = NULL,
#' See `fake_lbk()`
#' @noRd
fake_matched <- function(id_loan = NULL,
id = NULL,
id_2dii = NULL,
level = NULL,
score = NULL,
sector = NULL,
sector_ald = NULL,
...) {
tibble::tibble(
id_loan = id_loan %||% "L162",
id = id %||% "UP1",
id_2dii = id_2dii %||% "UP1",
level = level %||% "ultimate_parent",
score = score %||% 1,
sector = sector %||% "automotive",
Expand Down
9 changes: 5 additions & 4 deletions tests/testthat/test-match_name.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ test_that("w/ row 1 of loanbook and crucial cols yields expected", {
name_direct_loantaker = "Yuamen Xinneng Thermal Power Co Ltd",
sector_classification_system = "NACE",
sector_classification_direct_loantaker = 3511,
id = "UP1",
id_2dii = "UP1",
level = "ultimate_parent",
sector = "power",
sector_ald = "power",
Expand Down Expand Up @@ -168,7 +168,7 @@ expected_names_of_match_name_with_loanbook_demo <- c(
"lei_direct_loantaker",
"isin_direct_loantaker",

"id",
"id_2dii",
"level",
"sector",
"sector_ald",
Expand Down Expand Up @@ -258,10 +258,11 @@ test_that("takes `p`", {
})

test_that("takes `overwrite`", {
overwrite <- overwrite_demo
expect_false(
identical(
match_name(slice(loanbook_demo, 4:25), ald_demo, overwrite = NULL),
match_name(slice(loanbook_demo, 4:25), ald_demo, overwrite = overwrite_demo)
match_name(slice(loanbook_demo, 4:25), ald_demo, overwrite = overwrite)
)
)
})
Expand Down Expand Up @@ -346,7 +347,7 @@ test_that("preserves groups", {
test_that("outputs id consistent with level", {
out <- slice(loanbook_demo, 5) %>% match_name(ald_demo)
expect_equal(out$level, c("ultimate_parent", "direct_loantaker"))
expect_equal(out$id, c("UP1", "DL1"))
expect_equal(out$id_2dii, c("UP1", "DL1"))
})

test_that("no longer yiels all NAs in lbk columns (#85 @jdhoffa)", {
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-prioritize.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ test_that("w/ id_loan at level direct* & ultimate* picks only direct* (#106)", {
test_that("output is independent from the row-order of the input (#113)", {
# styler: off
matched_direct <- tibble::tribble(
~id_loan, ~id, ~level, ~score, ~sector, ~sector_ald,
"A", "D", "direct_loantaker", 1, "automotive", "automotive",
"A", "U", "ultimate_parent", 1, "automotive", "automotive",
"B", "U", "ultimate_parent", 1, "automotive", "automotive",
~id_loan, ~id_2dii, ~level, ~score, ~sector, ~sector_ald,
"A", "D", "direct_loantaker", 1, "automotive", "automotive",
"A", "U", "ultimate_parent", 1, "automotive", "automotive",
"B", "U", "ultimate_parent", 1, "automotive", "automotive",
)
# styler: on

Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-restructure_loanbook_for_matching.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,31 @@ test_that("restructure_loanbook_for_matching outputs a tibble with expected name
expect_is(out, "tbl_df")
expect_named(
out,
c("rowid", "level", "id", "name", "sector", "source", "alias")
c("rowid", "level", "id_2dii", "name", "sector", "source", "alias")
)

out2 <- restructure_loanbook_for_matching(loanbook_rowid, overwrite_demo)
expect_is(out2, "tbl_df")
expect_named(
out2,
c("rowid", "level", "id", "name", "sector", "source", "alias")
c("rowid", "level", "id_2dii", "name", "sector", "source", "alias")
)
})

test_that("restructure_loanbook_for_matching correctly overwrites name", {
overwrite <- overwrite_demo
out <- restructure_loanbook_for_matching(loanbook_rowid, overwrite_demo) %>%
filter(id %in% overwrite$id & level %in% overwrite$level) %>%
left_join(overwrite, by = c("id", "level"))
filter(id_2dii %in% overwrite$id_2dii & level %in% overwrite$level) %>%
left_join(overwrite, by = c("id_2dii", "level"))

expect_equal(out$name.x, out$name.y)
})

test_that("restructure_loanbook_for_matching correctly overwrites sector", {
overwrite <- overwrite_demo
out <- restructure_loanbook_for_matching(loanbook_rowid, overwrite_demo) %>%
filter(id %in% overwrite$id & level %in% overwrite$level) %>%
left_join(overwrite, by = c("id", "level"))
filter(id_2dii %in% overwrite$id_2dii & level %in% overwrite$level) %>%
left_join(overwrite, by = c("id_2dii", "level"))

expect_equal(out$sector.x, out$sector.y)
})
Expand Down

0 comments on commit f452530

Please sign in to comment.