/
personal_loanbook.R
147 lines (134 loc) · 5.58 KB
/
personal_loanbook.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
library(tidyverse)
library(wrapr)
library(stringr)
#' - Third tab: -
#' - Comparing personal loanbook and fc loan book
#' - For comparisions need to use proportions or will not be comparable - reuse graphs but with proportions if possible
#' - Sub tab PCA your data set main dataset
#' - Bad debt interest by number of loans and sample from loanbook, add option to sample based on your distribution
## Load and clean personal loanbook
loan_clean_personal_loanbook <- function(personal_loanbook_path) {
if (is.null(personal_loanbook_path)) {
if (file.exists("personal_loanbook.csv")) {
path <- "personal_loanbook.csv"
}else {
path <- NULL
}
}else {
path <- personal_loanbook_path$datapath
}
personal_loanbook <- read_csv(path)
##Change risk to ordered factor
personal_loanbook <- personal_loanbook %>%
mutate(Risk = Risk %>% factor(
levels = c("A+", "A", "B", "C", "D", "E")
))
##Munge and reformat rate so that it is ordered
personal_loanbook <- personal_loanbook %>%
mutate(Rate = str_replace_all(Rate, "%", "") %>%
as.numeric) %>%
mutate(Rate = Rate %>%
factor(levels = unique(Rate)[order(unique(Rate))],
ordered = TRUE)
)
return(personal_loanbook)
}
## Join FC and Personal Loanbooks
bind_loanbooks <- function(personal_loanbook, fc_loanbook, verbose= TRUE) {
personal_loanbook <- personal_loanbook %>%
rename(id = `Loan ID`) %>%
mutate(invested_in = "Yes")
combined_loanbook <- fc_loanbook %>%
full_join(personal_loanbook) %>%
mutate(invested_in = invested_in %>%
replace(is.na(invested_in), "No")) %>%
mutate(`Repayments made` = as.numeric(as.character(term)) - payments_remaining %>%
as.integer) %>%
mutate(`Percentage repaid` = round(`Repayments made` / as.numeric(as.character(term)) * 100))
if (verbose) {
loans_without_data <- is.na(combined_loanbook$credit_band) %>% sum
message("Loan books are bound with ", loans_without_data, " missing loan entries.")
if (loans_without_data > 1) {
message("Consider uploading an updated funding circle loan book as some of your loans are missing data")
}
}
return(combined_loanbook)
}
## Overall summary stats for boxes
p_loanbook_overall_sum_info <- function(df,
aplus_bad = 0.6,
a_bad = 1.5,
b_bad = 2.3,
c_bad = 3.3,
d_bad = 5,
e_bad = 8) {
##Transform rate for calc
df <- df %>%
mutate(rate_prog = Rate %>%
str_split(pattern = "%") %>%
map_chr(paste, collapse = "") %>%
as.numeric) %>%
mutate(bad_debt = case_when(Risk %in% "A+" ~ aplus_bad,
Risk %in% "A" ~ a_bad,
Risk %in% "B" ~ b_bad,
Risk %in% "C" ~ c_bad,
Risk %in% "D" ~ d_bad,
Risk %in% "E" ~ e_bad)) %>%
mutate(anul_adj_rate = 100 * (1 + (rate_prog - bad_debt - 1) / (100 * 12)) ^ 12 - 100) %>%
mutate(anul_rate_prog = 100 * (1 + rate_prog / (100 * 12)) ^ 12 - 100) %>%
mutate(rate_weight = `Principal remaining` / sum(`Principal remaining`))
##Summarise on sector
sector_max <- df %>%
group_by(Sector) %>%
summarise(prin_remaining = sum(`Principal remaining`)) %>%
ungroup %>%
pull(prin_remaining) %>%
max
## Total lent
total_lent <- df$`Principal remaining` %>% sum
## Build table
df %>%
mutate(crude_interest = anul_rate_prog * `Principal remaining`) %>%
mutate(adj_interest = anul_adj_rate * `Principal remaining`) %>%
summarise(
`Amount lent` = sum(`Principal remaining`) %>%
paste0("£", .),
`Amount late (%)` = `Principal remaining` %>%
replace(!`Loan status` %in% "Late", 0) %>%
sum,
`Amount defaulted (%)` = `Principal remaining` %>%
replace(!`Loan status` %in% c("Defaulted", "Bad debt", "Bad Debt"), 0) %>%
sum,
`Number of loans invested in` = n(),
`Number of loan parts` = sum(`Number of loan parts`),
`Maximum lent in a single loan (%)` = max(`Principal remaining`),
`Maximum lent to a single sector (%)` = sector_max,
`Crude interest rate` = sum(anul_rate_prog * rate_weight) %>%
round(digits = 1) %>%
paste0("%"),
`Adjusted interest rate*` = sum(anul_adj_rate * rate_weight) %>%
round(digits = 1) %>%
paste0("%")
) %>%
mutate_at(.vars = c("Amount late (%)",
"Amount defaulted (%)",
"Maximum lent in a single loan (%)",
"Maximum lent to a single sector (%)"),
.funs = funs(paste0("£", ., " (",
round(. / total_lent * 100, digits = 1),
"%)")))
}
## Summary table stratified by stratification variable
p_loanbook_sum_table <- function(df, strat) {
## Total amount lent
total_amount_lent <- sum(df$`Principal remaining`)
##Summarise loanbook
df_sum <- df %>%
group_by(.dots = strat) %>%
summarise(`Amount lent (£)` = sum(`Principal remaining`),
`Number of loan parts` = sum(`Number of loan parts`),
`Percentage of loanbook (%)` = round(`Amount lent (£)` / total_amount_lent * 100, digits = 1)
) %>%
mutate(`Amount lent (£)` = round(`Amount lent (£)`, digits = 0))
return(df_sum)
}