Advertisement

r语言 bsda包_使用R语言creditmodel包进行Vintage分析或留存率分析

阅读量:

1 什么是vintage分析?

vintage 分析(基于账龄分层的方法)在信用卡和信贷业务中得到了广泛应用。这一方法最初源于葡萄酒领域,在此背景下研究发现:不同年份生产的葡萄酒品质存在显著差异;由此可知,在不同时间段开立或发放贷款时所涉资产的质量也会有所不同。其基本原理在于:对于不同时间段产生的不同批次资产进行独立追踪管理,并按照各自的账龄周期进行同步比较分析;通过这种年龄分层管理方法的应用, 可以深入掌握各个时间段内信用卡发放或产品发行时所涉资产的质量特征。

从更广泛的视角来看,vintage分析属于同期群分析的一种,这与社会追踪调查以及人口学中的cohorts研究方法,还有互联网运营中的用户留存情况等具有相似之处,此处不再展开具体概念.接下来,我们将深入探讨如何利用R语言中的creditmodel包来进行vintage建模.


2 creditmodel包的cohort analysis模块简介

该信用模型由汉森教授创建为一个功能强大的R语言数据科学工具包,在这一领域具有重要地位。它主要包含数据预处理、变量衍生生成以及数据分析等功能模块,并且能够实现自动化建模过程。本次讲解的重点是对信用模型中进行分群分析的技术和方法进行深入探讨,在 creditmodel 包中的数据分析分支中形成了专门用于 this 分析的核心组件(即 cohort_analysis)。其中具体包含了 cohort_table 用于基本分群统计汇总 cohort_table_plot 用于绘制分群分布图 cohortPlot 用于展示分群趋势图等关键功能。


3 cohort analysis 模块简介

Description

cohort_analysis cohort_analysis is for cohort(vintage) analysis.

Usage

复制代码
 cohort_analysis(dat, obs_id = NULL, occur_time = NULL, MOB = NULL,

    
  period = "monthly", status = NULL, amount = NULL, by_out = "cnt",
    
  start_date = NULL, end_date = NULL, dead_status = 30)
    
  
    
 cohort_table(dat, obs_id = NULL, occur_time = NULL, MOB = NULL,
    
  period = "monthly", status = NULL, amount = NULL, by_out = "cnt",
    
  start_date = NULL, end_date = NULL, dead_status = 30)

Arguments

datA data.frame contained id, occur_time, mob, status ...

obs_idIdentifier for observations or key data point. Default is NULL.

occur_timeThe identifier for the variable that denotes the time of occurrence for each observation.

MOBMonth of book

periodPeriod of event to analysis. Default is "monthly"

statusStatus of observations

amountThe name of variable representing amount. Default is NULL.

by_outOutput: amount (amt) or count (cnt)

start_dateThe earliest occurrence time of observations.

end_dateThe latest occurrence time of observations.

dead_statusStatus of dead observations.


4 使用vintage分析步骤

4.1 数据准备

开展 vintage 分析时,请确保所涉及的数据至少包含贷款编号 (loan_id)、放款时点 (loan_time)、发放金额 (loan_amount) 以及还款状态 (max_overdue_days 或 age_overdue_days) 四个关键维度的信息。

复制代码
 #安装和加载creditmodel包

    
 #install.packages("creditmodel")
    
 library(creditmodel)
    
 #使用read_data读入数据。
    
 vin_dat = read_data("vin_dat.csv")
    
 #使用creditmodel包的数据清晰模块主函数对数据进行清洗,关于数据清洗模块,以后会做详细接受,在此简单描述下各个参数的含义。
    
 vin_dat = data_cleansing(vin_dat, obs_id = "loan_id",#主键
    
                     occur_time = 'loan_time',#事件发生时间
    
                     outlier_proc = FALSE,#不进行异常值处理
    
                     missing_proc = FALSE,#不进行确实值处理
    
                     remove_dup = FALSE,#不删除重复观测
    
                     merge_cat = FALSE,#不对类别变量的类别进行合并
    
  low_var = 0.9999,#删除单一值比例大于0.9999的变量
    
  missing_rate = 0.9999 # 对缺失值比例大于0.9999的变量进行二值化处理
    
                   )
    
 #可使用creditmodel包的data_exploration函数来观察数据概貌
    
 data_exploration(vin_dat)
    
 >
    
 * Observations      : 204697
    
 * Numeric_variables : 7
    
 * Category_variables: 1
    
 * Date_variables    : 1
    
 $num
    
         Feature  NMiss Miss_Rate        Max        75%     Median       25%        Min       Mean   Std
    
 1   age_overdue_days 194695    95.11%        440        122         61         30          1         90    76
    
 2 age_overdue_period     33     0.02%         15          0          0          0          0          0  0.87
    
 3           loan_age     33     0.02%         15          6          3          1          0          4   3.2
    
 4        loan_amount      0        0%     500000      60000      40000      30000       1000      47911 24279
    
 5       loan_balance    823      0.4%     500000      53186      39046      27871        920      43112 22157
    
 6          loan_time      0        0% 2017-09-30 2017-03-31 2016-12-29 2016-11-01 2016-06-01 2017-01-11    99
    
 7   max_overdue_days 160974    78.64%        440         13          2          1          1         23    52
    
 8 max_overdue_period      0        0%       15.0        0.0        0.0        0.0        0.0        0.3  0.93
    
  
    
 $char
    
  Feature  NMiss Miss_Rate                 Value1                 Value2              Value3              Value4              Value5              Value6
    
 1 loan_id 204697      100% n2016060100000030 : 48 n2016060100000010 : 30 n1605300032102 : 16 n1606010034102 : 16 n1606020032402 : 16 n1606020034202 : 16
    
        Value7
    
 1 (Other) : 204555
    
 >
    
 #使用plot_table画出数值型变量的数据概要
    
 plot_table(data_exploration(vin_dat)$num)
edc70989d4504d457639f1d5560f1e32.png

4.2 vintage分析

4.2.1 cohort_dat表的构建

使用cohort_analysis函数来构建cohort_dat表。

复制代码
 cohort_dat = cohort_analysis(vin_dat,

    
                         obs_id = 'loan_id',#放款编号
    
                         occur_time = 'loan_time', #放款时间
    
                         MOB = NULL,#month on book在账月份,找个可以自己定义为一个变量,默认以自然月为月份。
    
                         period = 'monthly',#以月作为同一时期,也可按周weekly
    
                         status = "age_overdue_days",#使用账龄末逾期天数作为状态,也为自己定义的0、1变量
    
                         dead_status = 30, #逾期天数大于30天则为dead状态,若为0、1变量,此处应设为0.
    
                         amount = "loan_amount", #如果以金额统计,则必须设置,此处按放款金额计算,也可以按余额
    
                         by_out = 'amt',#如果以金额统计则为‘amt’,以笔数统计则为‘cnt’
    
                         start_date = "2016-08-01",#统计日开始时间
    
                         end_date = '2017-05-31'#统计日结束时间
    
                       )

最终表结构如下:

ec9138408ccc3b52dee09908e772020a.png

4.2.2 画出vintage图

绘制vintage图极为简便,可以通过直接调用cohort_plot函数并传递上一步骤生成的cohort_dat参数即可。

复制代码
    cohort_plot(cohort_dat)
8d485e1810a378b66a060ddd90636f36.png

通过观察图表数据可以看出

4.2.3 vintage表格

通过调用cohort_table函数生成vintage表格,在其中参与cohort_analysis 输入参数保持一致

复制代码
 vin_table = cohort_table(vin_dat, obs_id = 'loan_id', occur_time = 'loan_time', MOB = NULL,

    
  period = 'monthly', status = "max_overdue_days",
    
  dead_status = 30, amount = "loan_balance", by_out = 'amt',
    
  start_date = "2016-09-01", end_date = '2017-07-31')

最终表格如下表所示:

95b9b0bf2ca4a986c2f912d56eec9667.png

4.2.4 画出vintage表格

如何优雅地绘制vintage表格呢?实际上只需调用 cohort_table_plot 函数即可。但因汉森教授偶尔疏忽,在 R 语言 CRAN 库最新版本 1.1.8 的 creditmodel 包中发现该函数存在一些缺陷。因此我附上了修复该缺陷的源码,并建议在绘制 vintage 表格之前加载该函数。

复制代码
 #' cohort_table_plot

    
 #' code{cohort_table_plot} is for ploting cohort(vintage) analysis table.
    
 #' @param cohort_dat A data.frame generated by code{cohort_analysis}.
    
 #' @import ggplot2
    
 #' @export
    
 cohort_table_plot = function(cohort_dat) {
    
  #set global variables
    
  opt = options('warn' = -1, scipen = 200, stringsAsFactors = FALSE, digits = 6) #
    
  cohort_dat[is.na(cohort_dat)] = 0
    
  #initial parameters
    
  Cohort_Group = Cohort_Period = Events = Events_rate = Opening_Total = 
    
  Retention_Total = cohor_dat = final_Events = m_a = max_age = NULL
    
  #plot
    
  cohort_plot = ggplot(cohort_dat, aes(reorder(paste0(Cohort_Period), Cohort_Period),
    
                                   Cohort_Group, fill = Events_rate)) +
    
    geom_tile(colour = 'white') +
    
    geom_text(aes(label = as_percent(Events_rate, 4)), size = 3) +
    
    scale_fill_gradient2(limits = c(0, max(cohort_dat$Events_rate)),
    
                     low = love_color('deep_red'), mid = 'white',
    
                     high = love_color(),
    
                     midpoint = median(cohort_dat$Events_rate,
    
                                       na.rm = TRUE),
    
                     na.value = love_color('pale_grey')) +
    
    scale_y_discrete(limits = rev(unique(cohort_dat$Cohort_Group))) +
    
    scale_x_discrete(position = "top") +
    
    labs(x = "Cohort_Period", title = "Cohort Analysis") +
    
    theme(text = element_text(size = 15), rect = element_blank()) +
    
    plot_theme(legend.position = 'right', angle = 0)
    
  return(cohort_plot)
    
  options(opt) #reset global variables
    
 }

该数据可视化模块依赖于GGPLOT2包生成图表,并因而在绘制图表之前,请确保加载了GGPLOT2库

复制代码
 vin_table = cohort_table(vin_dat, obs_id = 'loan_id', occur_time = 'loan_time', MOB = NULL,

    
  period = 'monthly', status = "max_overdue_days",
    
  dead_status = 30, amount = "loan_balance", by_out = 'amt',
    
  start_date = "2016-09-01", end_date = '2017-07-31')
    
 cohort_table_plot(cohort_dat)
a0e86cbeb60555edbe18401c08584409.png

都看到这里了,双击屏幕点个赞,再走吧!

5总结

该R语言包整合了变量衍生、数据预处理、数据分析、建模以及数据可视化的功能模块,并以其强大的性能在多个领域中广泛应用。对于该包的更深入应用,请订阅汉森老师的个人公众号《hansenmode》以获取最新资讯和技术指导。

希望本文对您有所启发的朋友们,请点赞并转发给更多人哦!感谢您的支持与鼓励。

另外,以上分析过程所使用的数据均为模拟数据,没有任何实际参考价值。

全部评论 (0)

还没有任何评论哟~