|
2 | 2 | ## Demo file for scoremodel; start with 'demo(scoremodel)'
|
3 | 3 | ## -----------------------------------------------------------------------------------
|
4 | 4 |
|
5 |
| -devAskNewPage(ask = FALSE) |
| 5 | +devAskNewPage(ask = FALSE) |
6 | 6 | library(scoremodel)
|
7 | 7 | data(CreditData)
|
8 | 8 | #convert character variables to factors
|
| 9 | + |
9 | 10 | mysample <- convertType(CreditData, toType = "fac", vars = -1)
|
10 | 11 | #homogeneity exclusion
|
| 12 | + |
11 | 13 | mysample <- delNArate(mysample, narate_critical = 0.9)[[3]]
|
12 | 14 | mysample <- delFewValues(mysample, minN = 5, minF = 2, exclude = "target")
|
13 | 15 | mysample <- delSinvalPercent(mysample, percent = 0.9, exclude = "target")
|
14 | 16 | #split dataset to train and test
|
| 17 | + |
15 | 18 | set.seed(21)
|
16 | 19 | splitresult <- splitData(mysample, size = 0.7, ifpercent = TRUE)
|
17 | 20 | train <- splitresult[[1]]
|
18 | 21 | test <- splitresult[[2]]
|
19 | 22 | #optimal binning and generate woe configuration list
|
| 23 | + |
20 | 24 | configlist <- dfBinningFun(train, binMethod = 1, p = 0.05, maxcat = 10, aliquots = 5)
|
21 | 25 | #woe encoding
|
| 26 | + |
22 | 27 | x_train_encoding <- woeEncodeFun_df(train[-length(train)], config_list = configlist)
|
23 | 28 | x_test_encoding <- woeEncodeFun_df(test[-length(test)], config_list = configlist)
|
24 | 29 | train_encoding <- cbind(x_train_encoding, train["target"])
|
25 | 30 | #collinear elimination
|
| 31 | + |
26 | 32 | train_encoding_red <- collElimination(train_encoding, cor_critical = 0.8)
|
27 | 33 | #model training
|
| 34 | + |
28 | 35 | fit <- LRfit(train_encoding_red, sig = 0.05)
|
29 | 36 | summary(fit)
|
30 | 37 | #prediction
|
| 38 | + |
31 | 39 | p_train <- LRpredict(fit)
|
32 | 40 | p_test <- LRpredict(fit, newdata = x_test_encoding)
|
33 | 41 | #calculate KS of model
|
| 42 | + |
34 | 43 | myks(train[,"target"], p_train)
|
35 | 44 | myks(test[,"target"], p_test)
|
36 | 45 | #calculate PSI of model
|
| 46 | + |
37 | 47 | psi(p_train, p_test, bins = 10, binMethod = "EF")
|
38 | 48 | #model performance visualization
|
| 49 | + |
39 | 50 | myCurves(train[,"target"], p_train, test[,"target"], p_test, ontest = FALSE,
|
40 | 51 | lift_bins = 10, P0 = 600, PDO = 50, color_scheme = 1, ifsave = FALSE)
|
41 | 52 | myCurves(train[,"target"], p_train, test[,"target"], p_test, ontest = TRUE,
|
42 | 53 | lift_bins = 10, P0 = 600, PDO = 50, color_scheme = 1, ifsave = FALSE)
|
43 |
| -devAskNewPage(ask = TRUE) |
| 54 | +devAskNewPage(ask = TRUE) |
0 commit comments