-
Notifications
You must be signed in to change notification settings - Fork 1
/
penalisedLogisticRegressions.R
459 lines (383 loc) · 17.7 KB
/
penalisedLogisticRegressions.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
rm(list=ls())
options(scipen=999)
# For keras/ tensor flow : Need to use: (added 21_09_2020)
#keras::use_python("/home/msmith/anaconda3/bin/python3.6", required = T)
library(future.apply)
library(furrr)
library(purrr)
library(dplyr)
library(tidyr)
library(xgboost)
library(xgboostExplainer)
library(Matrix)
library(caret)
library(pROC)
library(PRROC)
library(tidyquant)
library(drlib)
library(keras)
library(tensorflow)
#library(randomForest) # Note: randomForest causes conflicts with ggplot/tidyquant "margin" function
library(lightgbm)
library(e1071)
library(stargazer)
library(gtable)
library(cowplot)
library(grid)
# library(patchwork)
# library(ggtext)
library(magrittr)
library(patchwork)
library(tidyverse)
setwd("/home/msmith/chapter_1")
########################## Read in the data #####################
# files <- list.files("/home/msmith/chapter_1/data", recursive = FALSE, pattern = ".csv")
# files <- paste("/home/msmith/chapter_1/data", files, sep = "/")
files <- list.files("/home/msmith/chapter_1/data/new_ratios", recursive = FALSE, pattern = ".csv")
files <- paste("/home/msmith/chapter_1/data/new_ratios", files, sep = "/")
readdata <- function(fn){
dt_temp <- read.csv(file = fn, stringsAsFactors = FALSE)
return(dt_temp)
}
data <- future_lapply(files, readdata)
###########################################################
#################################################################
#################################################################
#################################################################
#################### Multi ML model #############################
#################################################################
#################################################################
#################################################################
# Scaled and Outlier handled data:
remove_outliers <- function(x, na.rm = TRUE, ...) {
DescTools::Trim(x, trim = 0.1, na.rm = FALSE)
qnt <- quantile(x, probs=c(0.1, 0.9), na.rm = na.rm, ...)
H <- 1.5 * IQR(x, na.rm = na.rm)
y <- x
y[x < (qnt[1] - H)] <- NA
y[x > (qnt[2] + H)] <- NA
y
}
scale2 <- function(x, na.rm = TRUE) (x - mean(x, na.rm = na.rm)) / sd(x, na.rm)
#scale2 <- function(x, na.rm = TRUE) (x - min(x, na.rm = na.rm)) / max(x, na.rm = na.rm) - min(x, na.rm = na.rm)
sapply(data[[1]], function(x) sum(is.na(x)))
###################
#################################################################
set.seed(1234)
full_data <- data %>%
future_map(., ~as_tibble(.) %>%
rename(ID = X) %>%
sample_n(nrow(.)))
train <- full_data %>%
future_map(., ~as_tibble(.) %>%
sample_frac(0.75))
test <- full_data %>%
future_map2(.x = .,
.y = train,
.f = ~anti_join(.x, .y, by = "ID"))
#################################################################
################# Logistic Regression ###########################
#################################################################
train_scaled <- train %>%
map(.,
~mutate_at(., vars(11:ncol(.)), funs(scale2(remove_outliers(.)))) %>%
mutate_if(is.integer, as.numeric) %>%
drop_na(.)
)
test_scaled <- test %>%
map(.,
~mutate_at(., vars(11:ncol(.)), funs(scale2(remove_outliers(.)))) %>%
mutate_if(is.integer, as.numeric) %>%
drop_na(.)
)
X_train_scaled <- train_scaled %>%
map(., ~dplyr::select(., -ID, -Postcode, -City, -Latitude, -Longitude, -Region.in.country,
-Major.sectors, -NACE.Rev..2.main.section, -BvD.ID.number, -status) %>%
as.matrix())
X_test_scaled <- test_scaled %>%
map(., ~dplyr::select(., -ID, -Postcode, -City, -Latitude, -Longitude, -Region.in.country,
-Major.sectors, -NACE.Rev..2.main.section, -BvD.ID.number, -status) %>%
as.matrix())
#################################################################
XY_vars <- train_scaled[[1]] %>%
dplyr::select(-ID, -Postcode, -City, -Latitude, -Longitude, -Region.in.country,
-Major.sectors, -NACE.Rev..2.main.section, -BvD.ID.number)
X_formula <- str_subset(names(XY_vars), "status", negate = TRUE)
FORMULA <- reformulate(X_formula, response = "status")
#################### Stepwise Logistic Regression ################
library(MASS)
priorToProcess <- 1 # i.e. select which list element 1, 2, 3, 4
# Full Logistic Regression
logistModel <- glm(FORMULA, data = train_scaled[[priorToProcess]], family = "binomial")
logistModel %>% coef()
# Stepwise Logistic Regression
modelStepwise <- logistModel %>%
stepAIC(trace = FALSE)
modelStepwise %>% coef()
# Chose a final model in which one variable has been removed...
# setdiff(
# names(logistModel %>% coef()),
# names(modelStepwise %>% coef())
# )
# Compare the full and stepwise model
# Make predictions
observedClasses <- test_scaled[[priorToProcess]]$status
#Logistic
logisticProbs <- predict(logistModel, test_scaled[[priorToProcess]], type = 'response')
logisticPredictedClasses <- ifelse(logisticProbs > 0.5, 1, 0)
mean(logisticPredictedClasses == observedClasses)
# Stepwise
stepwiseProbs <- predict(modelStepwise, test_scaled[[priorToProcess]], type = 'response')
stepwisePredictedClasses <- ifelse(stepwiseProbs > 0.5, 1, 0)
mean(stepwisePredictedClasses == observedClasses)
#################################################################
############## Penalised Logistic Regression ####################
####### LASSO ########
library(glmnet)
# alpha: the elasticnet mixing parameter. Allowed values include:
# “1”: for lasso regression
# “0”: for ridge regression
# a value between 0 and 1 (say 0.3) for elastic net regression.
# lamba: a numeric value defining the amount of shrinkage. Should be specify by analyst. Use cv.glmnet to find the best lambda
lassoCV <- cv.glmnet(x = X_train_scaled[[priorToProcess]], y = train_scaled[[priorToProcess]]$status, family = "binomial", alpha = 1)
plot(lassoCV)
lassoCV %>% coef(.$lambda.min) # therefore using 1se produces a more simple model but maybe the predictions are worse
lassoCV %>% coef(.$lambda.1se)
# Using lambda min
lassoModelLambdaMin <- glmnet(x = X_train_scaled[[priorToProcess]], y = train_scaled[[priorToProcess]]$status, family = "binomial", alpha = 1, lambda = lassoCV$lambda.min)
lassoModelLambdaMin %>% coef()
# Make predictions for the Lasso model using lambda min
lassoProbsLambdaMin <- predict(lassoModelLambdaMin, newx = X_test_scaled[[priorToProcess]], type = 'response')
lassoPredictedClassesLambdaMin <- ifelse(lassoProbsLambdaMin > 0.5, 1, 0)
mean(lassoPredictedClassesLambdaMin == observedClasses)
# Using lambda 1se (which is a more simple model)
lassoModelLambda1se <- glmnet(x = X_train_scaled[[priorToProcess]], y = train_scaled[[priorToProcess]]$status, family = "binomial", alpha = 1, lambda = lassoCV$lambda.1se)
lassoModelLambda1se %>% coef()
# Make predictions for the Lasso model using lambda 1se
lassoProbsLambda1se <- predict(lassoModelLambda1se, newx = X_test_scaled[[priorToProcess]], type = 'response')
lassoPredictedClassesLambda1se <- ifelse(lassoProbsLambda1se > 0.5, 1, 0)
mean(lassoPredictedClassesLambda1se == observedClasses)
####### RIDGE ########
ridgeCV <- cv.glmnet(x = X_train_scaled[[priorToProcess]], y = train_scaled[[priorToProcess]]$status, family = "binomial", alpha = 0) # Setting alpha to 0 gives a ridge regression
plot(ridgeCV)
ridgeCV %>% coef(.$lambda.min) # therefore using 1se produces a more simple model but maybe the predictions are worse
ridgeCV %>% coef(.$lambda.1se)
# Using lambda min
ridgeModelLambdaMin <- glmnet(x = X_train_scaled[[priorToProcess]], y = train_scaled[[priorToProcess]]$status, family = "binomial", alpha = 0, lambda = ridgeCV$lambda.min)
ridgeModelLambdaMin %>% coef()
# Make predictions for the Lasso model using lambda min
ridgeProbsLambdaMin <- predict(ridgeModelLambdaMin, newx = X_test_scaled[[priorToProcess]], type = 'response')
ridgePredictedClassesLambdaMin <- ifelse(ridgeProbsLambdaMin > 0.5, 1, 0)
mean(ridgePredictedClassesLambdaMin == observedClasses)
# Using lambda 1se (which is a more simple model)
ridgeModelLambda1se <- glmnet(x = X_train_scaled[[priorToProcess]], y = train_scaled[[priorToProcess]]$status, family = "binomial", alpha = 0, lambda = ridgeCV$lambda.1se)
ridgeModelLambda1se %>% coef()
# Make predictions for the Lasso model using lambda 1se
ridgeProbsLambda1se <- predict(ridgeModelLambda1se, newx = X_test_scaled[[priorToProcess]], type = 'response')
ridgePredictedClassesLambda1se <- ifelse(ridgeProbsLambda1se > 0.5, 1, 0)
mean(ridgePredictedClassesLambda1se == observedClasses)
####### ELASTIC NET ########
# alphalist <- seq(0 , 1, by = 0.1)
# elasticnet <- lapply(alphalist, function(a){
# cv.glmnet(x = X_train_scaled[[priorToProcess]], y = train_scaled[[priorToProcess]]$status, alpha = a, family = "binomial", lambda.min.ratio = .001)
# })
#
# for(i in 1:11){
# print(paste("minimum cvm: ", round(min(elasticnet[[i]]$cvm), 4), " with corresponding lambda min: ", round(elasticnet[[i]]$lambda.min, 4), " and lambda 1se: ", round(elasticnet[[i]]$lambda.1se, 4)))
# }
library(doParallel)
a <- seq(0.1, 0.9, 0.1)
search <- foreach(i = a, .combine = rbind) %dopar% {
cv <- cv.glmnet(x = X_train_scaled[[priorToProcess]], y = train_scaled[[priorToProcess]]$status, family = "binomial", nfold = 10, type.measure = "deviance", paralle = TRUE, alpha = i)
data.frame(cvm = cv$cvm[cv$lambda == cv$lambda.1se], lambda.1se = cv$lambda.1se, alpha = i)
}
elasticNetCV <- search[search$cvm == min(search$cvm), ]
elasticNetModel <- glmnet(x = X_train_scaled[[priorToProcess]], y = train_scaled[[priorToProcess]]$status, family = "binomial", lambda = elasticNetCV$lambda.1se, alpha = elasticNetCV$alpha)
coef(elasticNetModel)
# Make predictions for the optimised Elastic Net model using lambda 1se
elasticNetModelProbs <- predict(elasticNetModel, newx = X_test_scaled[[priorToProcess]], type = 'response')
elasticNetModelClasses <- ifelse(elasticNetModelProbs > 0.5, 1, 0)
mean(elasticNetModelClasses == observedClasses)
########################################################
############ Combine everything together ###############
########################################################
library(gtools)
# Process original Logistic model
logisticCoefficients <- coef(summary(logistModel)) %>%
data.frame() %>%
rownames_to_column("Variable") %>%
dplyr::select(-c('Std..Error', 'z.value')) %>%
rename(pvalue = "Pr...z..") %>%
mutate(
OrigLogisticEstimate = round(Estimate, 3),
OrigLogisticpvalue = stars.pval(pvalue)
) %>%
dplyr::select(-c(Estimate, pvalue))
# Process stepwise Logistic model
stepwiseCoefficients <- coef(summary(modelStepwise)) %>%
data.frame() %>%
rownames_to_column("Variable") %>%
dplyr::select(-c('Std..Error', 'z.value')) %>%
rename(pvalue = "Pr...z..") %>%
mutate(
StepwiseLogisticEstimate = round(Estimate, 3),
StepwiseLogisticpvalue = stars.pval(pvalue)
) %>%
dplyr::select(-c(Estimate, pvalue))
# Process LASSO lambda min model
lassoModelLambdaMinCoefficients <- coef(lassoModelLambdaMin) %>%
as.matrix() %>%
data.frame() %>%
rownames_to_column("Variable") %>%
rename(Estimate = s0) %>%
mutate(
LASSOLambaMinEstimate = round(Estimate, 3)
) %>%
dplyr::select(-c(Estimate))
# Process LASSO lambda 1se model
lassoModelLambda1seCoefficients <- coef(lassoModelLambda1se) %>%
as.matrix() %>%
data.frame() %>%
rownames_to_column("Variable") %>%
rename(Estimate = s0) %>%
mutate(
LASSO1seEstimate = round(Estimate, 3),
LASSO1seEstimate = ifelse(LASSO1seEstimate == 0.000, ".", LASSO1seEstimate)
) %>%
dplyr::select(-c(Estimate))
# Process ridge min model
ridgeModelLambdaMinCoefficients <- coef(ridgeModelLambdaMin) %>%
as.matrix() %>%
data.frame() %>%
rownames_to_column("Variable") %>%
rename(Estimate = s0) %>%
mutate(
RIDGEMinEstimate = round(Estimate, 3)
) %>%
dplyr::select(-c(Estimate))
ridgeModelLambda1seCoefficients <- coef(ridgeModelLambda1se) %>%
as.matrix() %>%
data.frame() %>%
rownames_to_column("Variable") %>%
rename(Estimate = s0) %>%
mutate(
RIDGE1seEstimate = round(Estimate, 3),
RIDGE1seEstimate = ifelse(RIDGE1seEstimate == 0.000, ".", RIDGE1seEstimate)
) %>%
dplyr::select(-c(Estimate))
# Optimised Elastic Net coefficients
elasticNetModelLambda1seCoefficients <- coef(elasticNetModel) %>%
as.matrix() %>%
data.frame() %>%
rownames_to_column("Variable") %>%
rename(Estimate = s0) %>%
mutate(
ELASTICNETEstimate = round(Estimate, 3),
ELASTICNETEstimate = ifelse(ELASTICNETEstimate == 0.000, ".", ELASTICNETEstimate)
) %>%
dplyr::select(-c(Estimate))
# logisticCoefficients %>%
# left_join(stepwiseCoefficients, by = "Variable") %>%
# left_join(lassoModelLambdaMinCoefficients, by = "Variable") %>% # Perhaps in LaTeX colour the Estimates based on pvalues and then remove the pvalues columns
# left_join(lassoModelLambda1seCoefficients, by = "Variable") %>%
# left_join(ridgeModelLambdaMinCoefficients, by = "Variable") %>%
# left_join(ridgeModelLambda1seCoefficients, by = "Variable") %>%
# left_join(elasticNetModelLambda1seCoefficients, by = "Variable")
################### Model LateX summary ###############
stargazer(
logistModel,
modelStepwise,
title = "Variable Selection Regression Results",
align = TRUE, no.space = TRUE, font.size = "footnotesize",
dep.var.labels = c("Binary Status of Bankruptcy"),
column.labels = c(
"Original Logistic", "Stepwise Logistic")
)
logisticCoefficients %>% dplyr::select(-c(OrigLogisticpvalue)) %>%
left_join(stepwiseCoefficients, by = "Variable") %>% dplyr::select(-c(StepwiseLogisticpvalue)) %>%
left_join(lassoModelLambdaMinCoefficients, by = "Variable") %>%
left_join(lassoModelLambda1seCoefficients, by = "Variable") %>%
left_join(ridgeModelLambdaMinCoefficients, by = "Variable") %>%
left_join(ridgeModelLambda1seCoefficients, by = "Variable") %>%
left_join(elasticNetModelLambda1seCoefficients, by = "Variable") %>%
mutate_all(funs(str_remove(., "\\.*"))) %>%
setNames(c("Variable", "Logistic", "Stepwise", "LASSOMin", "LASSO1se", "RIDGEMin", "RIDGE1se", "ELASTICNET1se")) %>%
stargazer(
summary = FALSE,
rownames = FALSE,
title = "Variable Selection Regression Results",
align = TRUE, no.space = TRUE, font.size = "footnotesize"
)
############# Model performances ###############
# mean(logisticPredictedClasses == observedClasses)
# mean(stepwisePredictedClasses == observedClasses)
# mean(lassoPredictedClassesLambdaMin == observedClasses)
# mean(lassoPredictedClassesLambda1se == observedClasses)
# mean(ridgePredictedClassesLambdaMin == observedClasses)
# mean(ridgePredictedClassesLambda1se == observedClasses)
# mean(elasticNetModelClasses == observedClasses)
conMat_LogisticVarSelection <- confusionMatrix(factor(logisticPredictedClasses), factor(observedClasses))
conMat_StepwiseVarSelection <- confusionMatrix(factor(stepwisePredictedClasses), factor(observedClasses))
conMat_LASSOMinVarSelection <- confusionMatrix(factor(lassoPredictedClassesLambdaMin), factor(observedClasses))
conMat_LASSO1seVarSelection <- confusionMatrix(factor(lassoPredictedClassesLambda1se), factor(observedClasses))
conMat_RIDGEMinVarSelection <- confusionMatrix(factor(ridgePredictedClassesLambdaMin), factor(observedClasses))
conMat_RIDGE1seVarSelection <- confusionMatrix(factor(ridgePredictedClassesLambda1se), factor(observedClasses))
conMat_ELASTICNETVarSelection <- confusionMatrix(factor(elasticNetModelClasses), factor(observedClasses))
data.frame(
Metric = c("Accuracy", "Sensitivity", "Specificity", "Precision", "F1"),
Logistic = c(
round(conMat_LogisticVarSelection$overall[[1]], 3),
round(conMat_LogisticVarSelection$byClass[[1]], 3),
round(conMat_LogisticVarSelection$byClass[[2]], 3),
round(conMat_LogisticVarSelection$byClass[[5]], 3),
round(conMat_LogisticVarSelection$byClass[[7]], 3)
),
Stepwise = c(
round(conMat_StepwiseVarSelection$overall[[1]], 3),
round(conMat_StepwiseVarSelection$byClass[[1]], 3),
round(conMat_StepwiseVarSelection$byClass[[2]], 3),
round(conMat_StepwiseVarSelection$byClass[[5]], 3),
round(conMat_StepwiseVarSelection$byClass[[7]], 3)
),
LASSOMin = c(
round(conMat_LASSOMinVarSelection$overall[[1]], 3),
round(conMat_LASSOMinVarSelection$byClass[[1]], 3),
round(conMat_LASSOMinVarSelection$byClass[[2]], 3),
round(conMat_LASSOMinVarSelection$byClass[[5]], 3),
round(conMat_LASSOMinVarSelection$byClass[[7]], 3)
),
LASSO1se = c(
round(conMat_LASSO1seVarSelection$overall[[1]], 3),
round(conMat_LASSO1seVarSelection$byClass[[1]], 3),
round(conMat_LASSO1seVarSelection$byClass[[2]], 3),
round(conMat_LASSO1seVarSelection$byClass[[5]], 3),
round(conMat_LASSO1seVarSelection$byClass[[7]], 3)
),
RIDGEMin = c(
round(conMat_RIDGEMinVarSelection$overall[[1]], 3),
round(conMat_RIDGEMinVarSelection$byClass[[1]], 3),
round(conMat_RIDGEMinVarSelection$byClass[[2]], 3),
round(conMat_RIDGEMinVarSelection$byClass[[5]], 3),
round(conMat_RIDGEMinVarSelection$byClass[[7]], 3)
),
RIDGE1se = c(
round(conMat_RIDGE1seVarSelection$overall[[1]], 3),
round(conMat_RIDGE1seVarSelection$byClass[[1]], 3),
round(conMat_RIDGE1seVarSelection$byClass[[2]], 3),
round(conMat_RIDGE1seVarSelection$byClass[[5]], 3),
round(conMat_RIDGE1seVarSelection$byClass[[7]], 3)
),
ElasticNet = c(
round(conMat_ELASTICNETVarSelection$overall[[1]], 3),
round(conMat_ELASTICNETVarSelection$byClass[[1]], 3),
round(conMat_ELASTICNETVarSelection$byClass[[2]], 3),
round(conMat_ELASTICNETVarSelection$byClass[[5]], 3),
round(conMat_ELASTICNETVarSelection$byClass[[7]], 3)
)
) %>%
stargazer(
summary = FALSE,
rownames = FALSE,
title = "Variable Selection Confusion Matrix Results (two years prior)", # NOTE: This needs changing depening on the year
align = TRUE, no.space = TRUE, font.size = "footnotesize"
)