]> git.vanrenterghem.biz Git - www2.vanrenterghem.biz.git/blob - source/posts/survival_analysis_in_fintech.org
Gebruik org timestamps in posts.
[www2.vanrenterghem.biz.git] / source / posts / survival_analysis_in_fintech.org
1 #+date: <2016-12-10 15:49:02 +0800>
2 #+filetags: R fintech analysis
3 #+title: Survival analysis in fintech
5 It is useful to apply the concepts from survival data analysis in a fintech environment. After all, there will
6 usually be a substantial amount of time-to-event data to choose from.
7 This can be website visitors leaving the site, loans being repaid early,
8 clients becoming delinquent - the options are abound.
10 A visual analysis of such data can easily be obtained using R.
12 #+BEGIN_SRC R
13 library(survminer) 
14 library(survival) 
15 library(KMSurv) 
16 ## Create survival curve from a survival object 
17 #' Status is 1 if the event was observed at TimeAfterStart 
18 #' It is set to 0 to mark the right-censored time 
19 vintage.survival <- survfit(Surv(TimeAfterStart,Status) ~ Vintage, data = my.dataset) 
20 ## Generate cumulative incidence plot 
21 ci.plot <- ggsurvplot(vintage.survival, 
22                       fun = function(y) 1-y, 
23                       censor = FALSE,
24                       conf.int = FALSE, 
25                       ylab = 'Ratio event observed', 
26                       xlab = 'Time after open', 
27                       break.time.by = 30, 
28                       legend = "bottom", 
29                       legend.title = “",
30                       risk.table = TRUE, 
31                       risk.table.title = 'Number of group', 
32                       risk.table.col ="black”, 
33                       risk.table.fontsize = 4, 
34                       risk.table.height = 0.35 )
35 #+END_SRC
37 This produces a plot with a survival curve per group, and also includes
38 the risk table. This table shows how many members of the group for whom
39 no event was observed are still being followed at each point in time.
40 Labelling these "at risk" stems of course from the original concept of
41 survival analysis, where the event typically is the passing of the
42 subject.
44 The =fun = function(y) 1-y= part actually reverses the curve, resulting
45 in what is known as a cumulative incidence curve.
47 #+CAPTION: Survival/incidence curve and risk table
48 #+ATTR_HTML :width 442 :class img-fluid :alt Survival/incidence curve and risk table
49 [[file:../assets/surv-curve-risk-table.png]]
51 Underneath the plot, a risk table is added with no effort by adding
52 =risk.table = TRUE= as parameter for =ggsurvplot=.
54 Checking the trajectory of these curves for different groups of
55 customers (with a different treatment plan, to stick to the terminology)
56 is an easy way to verify whether actions are having the expected result.