
Apply the WOE transformation or the scorecard to new data
Source:R/apply.R, R/ead.R, R/lgd.R, and 2 more
scr_apply.RdMaterializes in R exactly what the production SQL does: the frozen Stage
1 pre-processing (training median, special-population flags,
"MISSING") followed by the frozen Stage 2 binning and, for a scorecard,
by the points. Nothing is refitted. The two paths, R and SQL, produce the
same numbers, and a test guarantees it.
Usage
scr_apply(x, newdata, ...)
# S3 method for class 'scr_result'
scr_apply(
x,
newdata,
features = scr_selected(x),
what = c("woe", "bin", "both"),
...
)
# S3 method for class 'scr_scorecard'
scr_apply(x, newdata, what = c("score", "points", "woe", "all"), ...)
# S3 method for class 'scr_ead'
scr_apply(x, newdata, what = c("all", "ead", "pool"), ...)
# S3 method for class 'scr_lgd'
scr_apply(x, newdata, what = c("pool", "lgd", "all"), ...)
# S3 method for class 'scr_pd'
scr_apply(x, newdata, ...)
# S3 method for class 'scr_study'
scr_apply(x, newdata, score = "score", numbered = TRUE, ...)Arguments
- x
An object from
scr_select()(returns WOE/bin of the approved variables), fromscr_scorecard()(returns score and points), or a score study fromscr_bands()orscr_tiers()(returns the band or tier of a score).- newdata
New table with the source columns of the requested variables. The target column is not needed.
- ...
Passed on to the methods.
- features
For
scr_result: which variables to transform. Defaults to the approved ones.- what
For
scr_result:"woe","bin"or"both". Forscr_scorecard:"score","points","woe"or"all". Forscr_lgd:"pool"(pool and pool LGDs),"lgd"(adds the predicted LGD) or"all"(adds the cure probability and the severity). Forscr_ead:"all"(default),"ead"(pool, measure, applied CCF, predicted EAD and the floor flag) or"pool"(pool and measure).- score
For
scr_study: name of the score column ofnewdata.newdatamay also be a numeric vector of scores.- numbered
For a tiers study:
TRUE(default) returns the tier labels with their order in front ("01.very high"),FALSEthe plain labels. Band labels are intervals and never get a prefix.
Output columns
For scr_result: <f>_woe and/or <f>_bin per variable. For
scr_scorecard, "score" gives link (logit), prob (model
probability), score (exact, a + b * logit) and score_points (base
plus the whole points per bin); "points" gives score, score_points
and <f>_points; "woe" gives link, score and <f>_woe; "all"
gives everything.
IRB models
scr_pd returns score, score_points, grade, pd (calibrated
individual PD), pd_be and pd_final of the grade. scr_lgd returns
pool, lgd_lra, lgd_dt, lgd_final and, with what, p_cure,
severity and lgd_pred. scr_ead returns pool, measure,
utilisation, undrawn, ccf_applied, ead_model, ead_floor,
ead_predicted and ead_floor_binding; the predicted EAD is never below
the drawn amount. scr_capital() reads pd_final, lgd_final and
ead_predicted from these outputs in its list form.
Score studies
For a score study (scr_bands(), scr_tiers()), newdata is returned
(as a copy) with tier, the band or tier number, and tier_label. The
intervals are left-closed: score >= cut is the upper side, and a
missing score gives a missing tier.
The labels of a tiers study carry their order, "01." for the tier with
the highest event rate (the first row of the tiers table) down to the
tier with the lowest, so they sort from the event-richest tier under any
objective and direction; tier is unchanged and still rises with the
event rate. The result joins to the tier_label column of the tiers
table. numbered = FALSE returns the plain labels (its label column).
See also
Other production:
predict.scr_align(),
scr_export(),
scr_monitor(),
scr_monitoring_plan(),
scr_reasons(),
scr_sql()
Examples
cfg <- scr_config(verbose = FALSE, nthread = 1, use_ranger = FALSE,
xgb_rounds = 60, n_boot = 20)
res <- scr_select(scr_demo, "default", config = cfg, drop = "id",
date_col = "ref_date")
new <- head(scr_demo, 50)
str(scr_apply(res, new)[, 1:3])
#> Classes ‘data.table’ and 'data.frame': 50 obs. of 3 variables:
#> $ vl_score_01_woe: num 0.7039 -0.6581 0.0398 0.0398 0.0398 ...
#> $ vl_score_02_woe: num 0.572 -0.77 -0.824 0.382 0.572 ...
#> $ vl_score_04_woe: num -0.8932 0.304 -0.0558 -0.0558 -0.0558 ...
#> - attr(*, ".internal.selfref")=<pointer: 0x5568e450ca30>
sc <- scr_scorecard(res)
head(scr_apply(sc, new))
#> link prob score score_points
#> <num> <num> <num> <num>
#> 1: -2.102534 0.10885077 546.5330 546
#> 2: -2.702712 0.06281350 562.3290 562
#> 3: -2.634040 0.06697956 560.5217 559
#> 4: -0.602874 0.35368644 507.0636 507
#> 5: -1.731277 0.15042432 536.7619 536
#> 6: -4.297105 0.01342521 604.2917 604
head(scr_apply(sc, new, what = "points"))
#> score score_points vl_score_01_points vl_score_02_points
#> <num> <num> <num> <num>
#> 1: 546.5330 546 -21 -16
#> 2: 562.3290 562 20 21
#> 3: 560.5217 559 -1 22
#> 4: 507.0636 507 -1 -10
#> 5: 536.7619 536 -1 -16
#> 6: 604.2917 604 16 6
#> vl_score_04_points ds_band_points vl_late_points ds_region_points
#> <num> <num> <num> <num>
#> 1: 24 2 5 4
#> 2: -8 0 -5 0
#> 3: 1 2 -13 10
#> 4: 1 -11 -5 -13
#> 5: 1 0 5 0
#> 6: 9 13 5 4
#> vl_score_06_points vl_score_07_points vl_score_05_points ds_channel_points
#> <num> <num> <num> <num>
#> 1: -3 14 -9 9
#> 2: 4 1 -3 -5
#> 3: 4 -7 -3 -5
#> 4: -3 6 -3 9
#> 5: 9 6 -9 4
#> 6: -15 14 6 9
#> vl_hist_04_points vl_score_10_points
#> <num> <num>
#> 1: -3 2
#> 2: -3 2
#> 3: 9 2
#> 4: -3 2
#> 5: -3 2
#> 6: -3 2