{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":50160,"databundleVersionId":7921029,"sourceType":"competition"},{"sourceId":169286385,"sourceType":"kernelVersion"}],"dockerImageVersionId":30618,"isInternetEnabled":true,"language":"r","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"library(tidyverse) \nlibrary(mgcv)\nlibrary(ggcorrplot)\nlibrary(pROC)\nlibrary(regclass)","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","execution":{"iopub.status.busy":"2024-04-01T06:40:50.981043Z","iopub.execute_input":"2024-04-01T06:40:50.984947Z","iopub.status.idle":"2024-04-01T06:40:57.977741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ml = read_csv(\"/kaggle/input/homecredit-ml-tree/train_ml.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:40:57.985445Z","iopub.execute_input":"2024-04-01T06:40:58.056737Z","iopub.status.idle":"2024-04-01T06:41:04.301593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ml %>% dim","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:04.306164Z","iopub.execute_input":"2024-04-01T06:41:04.307945Z","iopub.status.idle":"2024-04-01T06:41:04.332643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df = read_csv(\"/kaggle/input/homecredit-ml-tree/val_df.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:04.337177Z","iopub.execute_input":"2024-04-01T06:41:04.338946Z","iopub.status.idle":"2024-04-01T06:41:21.291937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imp_df = read_csv(\"/kaggle/input/homecredit-ml-tree/importance_df.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:21.294994Z","iopub.execute_input":"2024-04-01T06:41:21.296662Z","iopub.status.idle":"2024-04-01T06:41:21.382471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"var_df <- imp_df %>% head(40) %>% select(Variable)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:21.386060Z","iopub.execute_input":"2024-04-01T06:41:21.387865Z","iopub.status.idle":"2024-04-01T06:41:21.451887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"select_x <- var_df$Variable","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:21.455018Z","iopub.execute_input":"2024-04-01T06:41:21.456653Z","iopub.status.idle":"2024-04-01T06:41:21.472610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cormat <- train_ml %>% select(select_x)  %>% select_if(is.numeric) %>% sample_n(1000) %>% cor()","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:21.475865Z","iopub.execute_input":"2024-04-01T06:41:21.477660Z","iopub.status.idle":"2024-04-01T06:41:21.726871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cormat %>% dim","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:21.730132Z","iopub.execute_input":"2024-04-01T06:41:21.731839Z","iopub.status.idle":"2024-04-01T06:41:21.751673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"options(repr.plot.width=15, repr.plot.height=15)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:21.754731Z","iopub.execute_input":"2024-04-01T06:41:21.756415Z","iopub.status.idle":"2024-04-01T06:41:21.772336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggcorrplot(cormat)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:21.775495Z","iopub.execute_input":"2024-04-01T06:41:21.777190Z","iopub.status.idle":"2024-04-01T06:41:22.753605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_x <- train_ml %>% select(select_x)  %>% select_if(is.numeric) %>% names\nfilter1 <- !(select_x %in% numeric_x)\nfactor_x <- select_x[filter1]","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:22.757579Z","iopub.execute_input":"2024-04-01T06:41:22.759701Z","iopub.status.idle":"2024-04-01T06:41:23.349591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ml <- train_ml %>% select(c(select_x, 'target'))\nval_df <- val_df %>% select(c(select_x, 'target'))","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:23.354612Z","iopub.execute_input":"2024-04-01T06:41:23.356763Z","iopub.status.idle":"2024-04-01T06:41:23.393094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"factor_x","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:23.397996Z","iopub.execute_input":"2024-04-01T06:41:23.399943Z","iopub.status.idle":"2024-04-01T06:41:23.423307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ml$lastrejectreasonclient_4145040M <- train_ml$lastrejectreasonclient_4145040M %>% as.factor()\ntrain_ml$lastrejectreason_759M <- train_ml$lastrejectreason_759M %>% as.factor()","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:23.427299Z","iopub.execute_input":"2024-04-01T06:41:23.429010Z","iopub.status.idle":"2024-04-01T06:41:23.451726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df$lastrejectreasonclient_4145040M <- val_df$lastrejectreasonclient_4145040M %>% as.factor()\nval_df$lastrejectreason_759M <- val_df$lastrejectreason_759M %>% as.factor()","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:23.454804Z","iopub.execute_input":"2024-04-01T06:41:23.456391Z","iopub.status.idle":"2024-04-01T06:41:23.501707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# GLM modeling","metadata":{}},{"cell_type":"code","source":"model_glm0 <- train_ml %>% sample_n(10000) %>% glm(formula = target ~., family = binomial)\nmodel_glm0 %>% summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:23.504734Z","iopub.execute_input":"2024-04-01T06:41:23.506388Z","iopub.status.idle":"2024-04-01T06:41:23.971005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yhat_val <- predict(object = model_glm0, newdata = val_df, type=\"response\")\nroc_object <- roc(val_df$target, yhat_val)\nroc_object","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:23.978452Z","iopub.execute_input":"2024-04-01T06:41:23.982826Z","iopub.status.idle":"2024-04-01T06:41:26.166503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## selection 1","metadata":{}},{"cell_type":"code","source":"select_x1 <- c('target','dpdmax_139P', 'overdueamountmax_155A', 'numinstlswithdpd10_728L', 'numrejects9m_859L', 'lastdelinqdate_224D', 'numinstlswithoutdpd_562L',\n              'numinstlswithoutdpd_562L', 'overdueamountmax2_14A', 'lastrejectreasonclient_4145040M', 'lastrejectreason_759M', 'firstclxcampaign_1125D', 'birth_259D')","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:26.169633Z","iopub.execute_input":"2024-04-01T06:41:26.171260Z","iopub.status.idle":"2024-04-01T06:41:26.186393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_glm1 <- train_ml %>% select(select_x1) %>% sample_n(10000) %>% glm(formula = target ~., family = binomial)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:26.191150Z","iopub.execute_input":"2024-04-01T06:41:26.192745Z","iopub.status.idle":"2024-04-01T06:41:26.461020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_glm1 %>% summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:26.468016Z","iopub.execute_input":"2024-04-01T06:41:26.471878Z","iopub.status.idle":"2024-04-01T06:41:26.527186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_glm1 %>% VIF","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:26.534704Z","iopub.execute_input":"2024-04-01T06:41:26.538845Z","iopub.status.idle":"2024-04-01T06:41:26.632933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yhat_val <- predict(object = model_glm1, newdata = val_df, type=\"response\")\nroc_object <- roc(val_df$target, yhat_val)\nroc_object","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:26.640139Z","iopub.execute_input":"2024-04-01T06:41:26.644277Z","iopub.status.idle":"2024-04-01T06:41:27.999131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## selection 2","metadata":{}},{"cell_type":"code","source":"select_x2 <- c('target','dpdmax_139P', 'numinstlswithdpd10_728L', 'numrejects9m_859L', 'lastdelinqdate_224D',\n              'numinstlswithoutdpd_562L',  'lastrejectreasonclient_4145040M',  'firstclxcampaign_1125D', 'birth_259D')","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:28.002315Z","iopub.execute_input":"2024-04-01T06:41:28.004029Z","iopub.status.idle":"2024-04-01T06:41:28.019133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ml$lastrejectreasonclient_4145040M %>% unique()","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:42:19.561797Z","iopub.execute_input":"2024-04-01T06:42:19.564868Z","iopub.status.idle":"2024-04-01T06:42:19.599821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_glm2 <- train_ml %>% select(select_x2) %>% sample_n(10000) %>% glm(formula = target ~., family = binomial)\nmodel_glm2 %>% summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:28.022135Z","iopub.execute_input":"2024-04-01T06:41:28.023754Z","iopub.status.idle":"2024-04-01T06:41:28.281568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_glm2 %>% VIF","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:28.288438Z","iopub.execute_input":"2024-04-01T06:41:28.292265Z","iopub.status.idle":"2024-04-01T06:41:28.354418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"length(select_x2)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:28.358968Z","iopub.execute_input":"2024-04-01T06:41:28.360747Z","iopub.status.idle":"2024-04-01T06:41:28.386311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yhat_val <- predict(object = model_glm2, newdata = val_df, type=\"response\")\nroc_object <- roc(val_df$target, yhat_val)\nroc_object","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:28.395692Z","iopub.execute_input":"2024-04-01T06:41:28.398287Z","iopub.status.idle":"2024-04-01T06:41:29.773365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model GAM","metadata":{}},{"cell_type":"code","source":"select_x","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:29.777690Z","iopub.execute_input":"2024-04-01T06:41:29.779317Z","iopub.status.idle":"2024-04-01T06:41:29.802562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ml_s <- train_ml %>% sample_n(5000)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:29.806978Z","iopub.execute_input":"2024-04-01T06:41:29.808632Z","iopub.status.idle":"2024-04-01T06:41:29.833534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_gam <- train_ml_s %>% gam(formula = target ~ \n                                                   #s(pctinstlsallpaidlate4d_3546849L) + \n                                                   s(numberofoverdueinstlmaxdat_641D) +\n                                                   s(dpdmax_139P) +\n                                                   s(lastrejectdate_50D) + \n                                                   #s(overdueamountmax_155A) + \n                                                   #s(pctinstlsallpaidlate1d_3546856L) + \n                                                   s(maxdbddpdtollast12m_3658940P) +\n                                                  #s(pctinstlsallpaidlate6d_3546844L) +\n                                                  #s(daysoverduetolerancedd_3976961L) +\n                                                  #s(maxdpdtolerance_577P) + \n                                                  s(days120_123L) + \n                                                  #s(numberofoverdueinstlmax_1039L) + \n                                                  s(numinstlswithdpd10_728L) + \n                                                  s(numrejects9m_859L) + \n                                                  s(overdueamountmax2date_1142D) + \n                                                  s(pctinstlsallpaidearl3d_427L) + \n                                                  s(days90_310L) + \n                                                  lastrejectreasonclient_4145040M + \n                                                  s(pctinstlsallpaidlat10d_839L) + \n                                                  #s(days180_256L) + \n                                                  #s(numinstpaidearly3d_3546850L) + \n                                                  #s(overdueamountmax2date_1002D) + \n                                                  #s(numinstlallpaidearly3d_817L) + \n                                                  s(lastdelinqdate_224D) +\n                                                  s(days360_512L) +\n                                                  s(numinstlsallpaid_934L) + # + \n                                                  #s(numinstlswithoutdpd_562L)\n                                                  #s(dpdmax_757P)\n                                                  s(overdueamountmax2_14A) + #good\n                                                  #s(numberofoverdueinstlmaxdat_148D)\n                                                  s(pmtnum_254L) + #good\n                                                  #lastrejectreason_759M +\n                                                  #s(numberofoverdueinstlmax_1151L) + \n                                                  #s(numinstlswithdpd5_4187116L)\n                                                  s(datelastunpaid_3546854D) + \n                                                  s(firstclxcampaign_1125D) + \n                                                 # s(numinstpaidearly_338L) +\n                                                  #s(lastrejectcredamount_222A) + \n                                                  s(maxdpdinstldate_3546855D) + \n                                                  s(birth_259D)\n                                                  \n                                                  \n                                                  \n                                                   , family = binomial)\n\n\n\nmodel_gam %>% summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-01T06:41:29.836821Z","iopub.execute_input":"2024-04-01T06:41:29.838456Z","iopub.status.idle":"2024-04-01T06:42:16.667052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yhat_val_gam <- predict(object = model_gam, newdata = val_df, type=\"response\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roc_object <- roc(val_df$target, yhat_val_gam)\nroc_object","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Area under the curve: 0.7123","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"options(repr.plot.width=6, repr.plot.height=6)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot.gam(model_gam)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}