楼主: 东方祥
3513 7

[程序分享] 信用评分卡模型案例(R语言实现) [推广有奖]

学科带头人

56%

还不是VIP/贵宾

-

威望
2
论坛币
653784 个
通用积分
46542.3897
学术水平
203 点
热心指数
253 点
信用等级
195 点
经验
53956 点
帖子
520
精华
6
在线时间
2290 小时
注册时间
2015-3-25
最后登录
2024-4-24

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. library(xlsx)
  2. dat<-read.xlsx2("credit.xlsx",1,colClasses = NA)
  3. summary(dat)
  4. dat[,1:6]<-sapply(dat[,1:6],function(x) {x[x==999]<-NA;return(x)} )
  5. dat<-dat[,-11]

  6. library(smbinning)
  7. library(prettyR)

  8. dat1<-dat
  9. dat1[,4]<-dat1[,4]-dat1[,3]
  10. table(dat1[,4])
  11. dat1[,4]<-factor(dat1[,4],levels=c(1,2),labels=c("其他","已婚"))
  12. colnames(dat1)<-c("age","income","child","marital","dur_live",
  13.                   "dur_work","housetype","nation","cardtype","loan")
  14. summary(dat1)
  15. write.csv(dat1,"dat1.csv",row.names = F)
复制代码
credit.xlsx文件: credit.xlsx (156.4 KB)


  1. dat1<-read.csv("dat1.csv")

  2. ##盖帽法函数
  3. block<-function(x,lower=T,upper=T){
  4.     if(lower){
  5.         q1<-quantile(x,0.01)
  6.         x[x<=q1]<-q1
  7.     }
  8.     if(upper){
  9.         q99<-quantile(x,0.99)
  10.         x[x>q99]<-q99
  11.     }
  12.     return(x)
  13. }

  14. dat1$loan<-as.numeric(!as.logical(dat1$loan))

  15. ##age
  16. boxplot(age~loan,data=dat1,horizontal=T, frame=F,
  17.         col="lightgray",main="Distribution")
  18. age<-smbinning(dat1,"loan","age")
  19. age$ivtable
  20. par(mfrow=c(2,2))
  21. smbinning.plot(age,option="dist",sub="年龄")
  22. smbinning.plot(age,option="WoE",sub="年龄")
  23. smbinning.plot(age,option="goodrate",sub="年龄")
  24. smbinning.plot(age,option="badrate",sub="年龄")
  25. par(mfrow=c(1,1))
  26. age$iv
  27. cred_iv<-c("年龄"=age$iv)

  28. ##income
  29. boxplot(income~loan,data=dat1,horizontal=T, frame=F,
  30.         col="lightgray",main="Distribution")
  31. dat1$income<-block(dat1$income)
  32. boxplot(income~loan,data=dat1,horizontal=T, frame=F,
  33.         col="lightgray",main="Distribution")
  34. income<-smbinning(dat1,"loan","income")
  35. income$ivtable
  36. smbinning.plot(income,option="WoE",sub="收入")
  37. income$iv
  38. cred_iv<-c(cred_iv,"收入"=income$iv)

  39. ##child
  40. boxplot(child~loan,data=dat1,horizontal=T, frame=F,
  41.         col="lightgray",main="Distribution")
  42. dat1$child<-block(dat1$child)
  43. child<-smbinning(dat1,"loan","child")
  44. child$ivtable
  45. smbinning.plot(child,option="WoE",sub="孩子数量")
  46. child$iv
  47. cred_iv<-c(cred_iv,"孩子数量"=child$iv)

  48. ##marital
  49. xtab(~marital+loan,data=dat1,chisq=T)
  50. marital<-smbinning.factor(dat1,"loan","marital")
  51. marital$ivtable
  52. smbinning.plot(marital,option="WoE",sub="婚姻状态")
  53. marital$iv
  54. cred_iv<-c(cred_iv,"婚姻状态"=marital$iv)

  55. ##dur_live
  56. boxplot(dur_live~loan,data=dat1,horizontal=T,
  57.         frame=F, col="lightgray",main="Distribution")
  58. t.test(dur_live~loan,data=dat1)
  59. dur_live<-smbinning(dat1,"loan","dur_live")
  60. dur_live

  61. ##dur_work
  62. boxplot(dur_work~loan,data=dat1,horizontal=T,
  63.         frame=F, col="lightgray",main="Distribution")
  64. t.test(dur_work~loan,data=dat1)
  65. dur_work<-smbinning(dat1,"loan","dur_work")
  66. dur_work$ivtable
  67. smbinning.plot(dur_work,option="WoE",sub="在现工作时间")
  68. dur_work$iv
  69. cred_iv<-c(cred_iv,"在现工作时间"=dur_work$iv)

  70. ##housetype
  71. xtab(~housetype+loan,data=dat1,chisq=T)
  72. housetype<-smbinning.factor(dat1,"loan","housetype")
  73. housetype$ivtable
  74. smbinning.plot(housetype,option="WoE",sub="住房类型")
  75. housetype$iv
  76. cred_iv<-c(cred_iv,"住房种类"=housetype$iv)

  77. ##nation
  78. xtab(~nation+loan,data=dat1,chisq=T)
  79. nation<-smbinning.factor(dat1,"loan","nation")
  80. nation$ivtable
  81. smbinning.plot(nation,option="WoE",sub="国籍")
  82. nation$iv
  83. cred_iv<-c(cred_iv,"国籍"=nation$iv)

  84. ##cardtype
  85. xtab(~cardtype+loan,data=dat1,chisq=T)
  86. cardtype<-smbinning.factor(dat1,"loan","cardtype")
  87. cardtype$ivtable
  88. smbinning.plot(cardtype,option="WoE",sub="信用卡类型")
  89. cardtype$iv
  90. cred_iv<-c(cred_iv,"信用卡类型"=cardtype$iv)

  91. barplot(cred_iv,main="各变量信息值")

  92. dat2<-dat1
  93. dat2<-smbinning.gen(dat2,age,"glage")
  94. dat2<-smbinning.gen(dat2,income,"glincome")
  95. dat2<-smbinning.gen(dat2,child,"glchild")
  96. dat2<-smbinning.factor.gen(dat2,marital,"glmarital")
  97. dat2<-smbinning.gen(dat2,dur_work,"gldur_work")
  98. dat2<-smbinning.factor.gen(dat2,housetype,"glhousetype")
  99. dat2<-smbinning.factor.gen(dat2,nation,"glnation")
  100. dat2<-smbinning.factor.gen(dat2,cardtype,"glcardtype")

  101. dat3<-dat2[,c(11:18,10)]

  102. write.csv(dat3,"dat3.csv",row.names = F)
复制代码
dat1.zip (21.42 KB) 本附件包括:
  • dat1.csv


  1. dat3<-read.csv("dat3.csv")

  2. cred_mod<-glm(loan~. ,data=dat3,family=binomial())
  3. summary(cred_mod)

  4. cre_scal<-smbinning.scaling(cred_mod,pdo=45,score=800,odds=50)
  5. cre_scal$logitscaled
  6. cre_scal$minmaxscore


  7. dat4<-smbinning.scoring.gen(smbscaled=cre_scal, dataset=dat3)
  8. boxplot(Score~loan,data=dat4,horizontal=T, frame=F,
  9.         col="lightgray",main="Distribution")

  10. scaledcard<-cre_scal$logitscaled[[1]][-1,c(1,2,6)]
  11. scaledcard[,1]<-c(rep("年龄",5),rep("收入",3),
  12.                   rep("孩子数量",2),rep("婚否",2),rep("在现工作时间",5),
  13.                   rep("住房类型",3),rep("国籍",8),rep("信用卡类型",7))
  14. scaledcard
  15. write.csv(scaledcard,"card.csv",row.names = F)
复制代码
知识点3.zip (12.73 KB) 本附件包括:
  • card.csv
  • creditcard.R
  • dat3.csv

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:信用评分卡 评分卡模型 信用评分 评分卡 R语言 评分卡 信用风险评分卡 信用评分卡

沙发
hifinecon 发表于 2018-10-19 22:56:47 来自手机 |只看作者 |坛友微信交流群
东方祥 发表于 2018-10-19 16:55
credit.xlsx文件:
thanks LZ for your kindness

使用道具

藤椅
hifinecon 发表于 2018-10-19 23:26:20 |只看作者 |坛友微信交流群
thanks a lot for sharing the code, but could not call library(xlsx), got this error message

Error: package or namespace load failed for ‘xlsx’:
.onLoad failed in loadNamespace() for 'rJava', details:
  call: fun(libname, pkgname)
  error: No CurrentVersion entry in Software/JavaSoft registry! Try re-installing Java and make sure R and Java have matching architectures.

any idea??

使用道具

板凳
哎呦不错哦~ 学生认证  发表于 2018-10-20 20:24:45 |只看作者 |坛友微信交流群
hifinecon 发表于 2018-10-19 23:26
thanks a lot for sharing the code, but could not call library(xlsx), got this error message

Error ...
装个java的jdk还是什么的就好了,没有java环境
已有 1 人评分经验 学术水平 收起 理由
东方祥 + 20 + 3 精彩帖子

总评分: 经验 + 20  学术水平 + 3   查看全部评分

使用道具

报纸
哎呦不错哦~ 学生认证  发表于 2018-10-20 20:27:59 |只看作者 |坛友微信交流群
家庭人口数—孩子数量  为什么等于2就是已婚,等于1就是其他呢?  不应该孩子数量不为0 就是已婚吗  孩子都有了啊,还有等于1也有可能是已婚丧偶啊。这样处理感觉不是太好啊...

使用道具

地板
hifinecon 发表于 2018-10-20 23:11:14 |只看作者 |坛友微信交流群
哎呦不错哦~ 发表于 2018-10-20 20:24
装个java的jdk还是什么的就好了,没有java环境
tried it and it still did not work, same error. I'll just use other way to read the xlsx file. thanks for your help.

使用道具

7
hifinecon 发表于 2018-10-20 23:12:27 |只看作者 |坛友微信交流群
哎呦不错哦~ 发表于 2018-10-20 20:27
家庭人口数—孩子数量  为什么等于2就是已婚,等于1就是其他呢?  不应该孩子数量不为0 就是已婚吗  孩子都 ...
yes, I also have the doubt, but this is just an example. we could modify it accordingly. cheers!

使用道具

8
哎呦不错哦~ 学生认证  发表于 2018-10-21 16:38:35 来自手机 |只看作者 |坛友微信交流群
hifinecon 发表于 2018-10-20 23:12
yes, I also have the doubt, but this is just an example. we could modify it accordingly. cheers!
可是孩子数量的值的类是小于10个啊,不能进行smbinning分箱操作啊。

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-25 07:48