-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.R
554 lines (428 loc) · 18.7 KB
/
script.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# =============================================================================.
# Machine Learning in Economics
# =============================================================================.
# Building an early warning system for fiscal stress. Comparing the classical
# econometric approach of using logit regression with a decision tree model,
# i.e. a random forest
# =============================================================================.
# Initialization ----
# -----------------------------------------------------------------------------.
rm(list = ls()); gc()
start_time <- Sys.time()
# set seed (please uncomment depending on your R Version)
# if using R 3.6 or later:
set.seed(1999, sample.kind = "Rounding")
# if using R 3.5 or earlier:
# set.seed(1999)
# package management ----
# -----------------------------------------------------------------------------.
if(!require(glmnet)) install.packages("glmnet")
if(!require(randomForest)) install.packages("randomForest")
if(!require(ROCR)) install.packages("ROCR")
if(!require(iml)) install.packages("iml")
if(!require(pdp)) install.packages("pdp")
if(!require(data.table)) install.packages("data.table")
library(glmnet)
library(randomForest)
library(ROCR)
library(iml)
library(pdp)
library(data.table)
# objects ----
# -----------------------------------------------------------------------------.
means.table <- data.frame(
variable = character(),
all_periods = numeric(),
tranq_periods = numeric(),
stress_periods = numeric(),
p_value = numeric(),
significant = logical()
)
results.lasso <- data.frame(
model = character(),
year = integer(),
weight = numeric(),
prop.pos = numeric(),
prop.neg = numeric(),
avg = numeric(),
auc = numeric()
)
cutoffs.lasso <- data.frame(
model = character(),
year = integer(),
weight = numeric(),
cutoff = numeric()
)
yhat.lasso <- data.frame()
results.rf <- data.frame(
model = character(),
year = integer(),
weight = numeric(),
prop.pos = numeric(),
prop.neg = numeric(),
avg = numeric(),
auc = numeric()
)
cutoffs.rf <- data.frame(
model = character(),
year = integer(),
weight = numeric(),
cutoff = numeric()
)
weights <- c(1, 1.5, 2)
list.export <- list()
shapley.values <- data.frame(
feature = character(),
phi = numeric()
)
# functions ----
# -----------------------------------------------------------------------------.
pred <- function(model, newdata){
res <- as.data.frame(predict(model, newdata, type = "prob"))
return(res[2])
}
# =============================================================================.
# 1. Data pre-processing ----
# =============================================================================.
# -----------------------------------------------------------------------------.
# 1.1 Download Data ----
# -----------------------------------------------------------------------------.
cat("\nLoad and prepare data...")
url <- "https://raw.githubusercontent.com/bt-koch/ML-in-Economics/main/data/data-ecb-wp-2408.csv"
data <- read.csv(url, check.names = F)
rm(url)
# -----------------------------------------------------------------------------.
# 1.2 Data Manipulation ----
# -----------------------------------------------------------------------------.
# factorize dummy variable(s)
data$crisis_next_period <- factor(x = data$crisis_next_period, levels = c(0,1), labels = c(0,1))
data$crisis_next_year <- factor(data$crisis_next_year, levels = c(0,1), labels = c(0,1))
data$crisis_first_year <- factor(data$crisis_first_year, levels = c(0,1), labels = c(0,1))
# rename column
names(data)[names(data) == ""] <- "country.id"
list.export[["data"]] <- data
# read csv for variable names
varnames <- read.csv("data/varnames.csv", sep = ";")
list.export[["varnames"]] <- varnames
# =============================================================================.
# 2. Exploratory Data Analysis ----
# =============================================================================.
cat("\nPerform exploratory data analysis...")
# -----------------------------------------------------------------------------.
# 2.1 Table: means of variables with significance ----
# -----------------------------------------------------------------------------.
drop <- c("country.id", "country", "year", "crisis_next_year", "crisis_next_period",
"crisis_first_year", "developed")
variables <- names(data[, -which(names(data) %in% drop)])
for(var in variables){
all_periods <- mean(data[[var]])
tranq_periods <- mean(data[data$crisis_next_period == 0,][[var]])
stress_periods <- mean(data[data$crisis_next_period == 1,][[var]])
wilcox_test <- wilcox.test(x = data[data$crisis_next_period == 0,][[var]],
y = data[data$crisis_next_period == 1,][[var]])
p_value <- wilcox_test$p.value
significant <- ifelse(p_value < 0.05, TRUE, FALSE)
temp <- data.frame(
variable = var,
all_periods = all_periods,
tranq_periods = tranq_periods,
stress_periods = stress_periods,
p_value = p_value,
significant = significant
)
means.table <- rbind(means.table, temp)
}
list.export[["means.table"]] <- means.table
# -----------------------------------------------------------------------------.
# 2.2 pairwise correlations ----
# -----------------------------------------------------------------------------.
variables.df <- data[, which(names(data) %in% variables)]
setnames(variables.df,
old = varnames$variable,
new = varnames$name)
corr.matrix <- cor(variables.df)
list.export[["corr.matrix"]] <- corr.matrix
# =============================================================================.
# 3. Train models ----
# =============================================================================.
cat("\nTrain models:")
for(i in 2007:max(data$year)){
# ---------------------------------------------------------------------------.
# 3.1 Prepare training ----
# ---------------------------------------------------------------------------.
# train and test period
train.period <- min(data$year):(i-1)
test.period <- i
# train and test set
train.set <- data[data$year %in% train.period,]
test.set <- data[data$year == test.period,]
# create set of explanatory variables
drop <- c("country.id", "country", "year", "crisis_next_year", "crisis_next_period",
"crisis_first_year")
# GDP as measure of development of country
x.train.GDP <- as.matrix(
train.set[, -which(names(train.set) %in% c(drop, "developed"))]
)
x.test.GDP <- as.matrix(
test.set[, -which(names(test.set) %in% c(drop, "developed"))]
)
# dummy as measure of development of country
x.train.DUMMY <- as.matrix(
train.set[, -which(names(train.set) %in% c(drop, "GDP_per_cap"))]
)
x.test.DUMMY <- as.matrix(
test.set[, -which(names(test.set) %in% c(drop, "GDP_per_cap"))]
)
# response
y.train <- train.set$crisis_next_period
y.test <- test.set$crisis_next_period
# clean up
rm(drop)
# ---------------------------------------------------------------------------.
# 3.2 Logit with LASSO Penalisation ----
# ---------------------------------------------------------------------------.
cat("\ntrain logit lasso for", i)
for(dev.measure in c("GDP", "DUMMY")){
# get data ----------------------------------------------------------------.
x.train <- get(paste0("x.train.", dev.measure))
x.test <- get(paste0("x.test.", dev.measure))
# drop variables for logit because of high pairwise correlation
drop <- c("dyn_prod_dol", "dyn_fix_cap_form", "overvaluation")
x.train <- x.train[, -which(colnames(x.train) %in% drop)]
x.test <- x.test[, -which(colnames(x.test) %in% drop)]
# add interaction effects if binary variable is used for economic development
if(dev.measure == "DUMMY"){
f <- as.formula( ~ developed*.)
x.train <- model.matrix(f, as.data.frame(x.train))[,-1]
x.test <- model.matrix(f, as.data.frame(x.test))[,-1]
}
# train model -------------------------------------------------------------.
# fit model
lasso.fit <- cv.glmnet(x.train, y.train, family = "binomial", nfolds = 5,
type.measure = "auc", standardize = TRUE)
lasso.lambda.min <- lasso.fit$lambda.min
# get predicted probability on train set
lasso.response.train <- predict(lasso.fit, newx = x.train, s = "lambda.1se",
type = "response", standardize = TRUE)
# get optimal threshold
lasso.pred <- prediction(lasso.response.train, y.train)
lasso.sens <- performance(lasso.pred, measure = "sens", x.measure = "cutoff")
lasso.spec <- performance(lasso.pred, measure = "spec", x.measure = "cutoff")
for(weight in weights){
sens <- lasso.sens@y.values[[1]]
spec <- lasso.spec@y.values[[1]]
max.sum <- which.max(weight*sens+spec)
lasso.cutoff <- lasso.sens@x.values[[1]][max.sum]
temp <- data.frame(
model = paste0("logit.lasso.", dev.measure),
year = i,
weight = weight,
cutoff = lasso.cutoff
)
cutoffs.lasso <- rbind(cutoffs.lasso, temp)
}
# test model --------------------------------------------------------------.
lasso.response.test <- predict(lasso.fit, newx = x.test, s = "lambda.1se",
type = "response", standardize = TRUE)
# calculate area under curve
lasso.asses <- assess.glmnet(lasso.fit, newx = x.test, newy = y.test,
family = "binomial", standardize = TRUE)
lasso.auc <- lasso.asses$auc
# calculate true positive rate and true negative rate with different weights
# on sensitivity and specificity
for(w in weights){
cutoff.temp <- cutoffs.lasso[
cutoffs.lasso$weight == w
& cutoffs.lasso$model == paste0("logit.lasso.", dev.measure)
& cutoffs.lasso$year == i,
]$cutoff
# assign class conditional on best cutoff for corresponding weight
lasso.yhat.temp <- ifelse(lasso.response.test > cutoff.temp, 1, 0)
y.test.num <- as.numeric(as.character(y.test))
# confusion matrix
lasso.tp.temp <- sum(lasso.yhat.temp == 1 & y.test.num == 1)
lasso.fp.temp <- sum(lasso.yhat.temp == 1 & y.test.num == 0)
lasso.tn.temp <- sum(lasso.yhat.temp == 0 & y.test.num == 0)
lasso.fn.temp <- sum(lasso.yhat.temp == 0 & y.test.num == 1)
# calculate rates
lasso.prop.positives <- lasso.tp.temp/sum(y.test.num == 1)
lasso.prop.negatives <- lasso.tn.temp/sum(y.test.num == 0)
lasso.avg.temp <- 0.5*(lasso.prop.positives+lasso.prop.negatives)
# save results
temp <- data.frame(
model = paste0("logit.lasso.", dev.measure),
year = i,
weight = w,
prop.pos = lasso.prop.positives,
prop.neg = lasso.prop.negatives,
avg = lasso.avg.temp,
auc = lasso.auc
)
results.lasso <- rbind(results.lasso, temp)
} # end of loop over weights
} # end of loop over dev.measure
# ---------------------------------------------------------------------------.
# 3.3 Random Forest ----
# ---------------------------------------------------------------------------.
cat("\ntrain random forest for", i)
for(dev.measure in c("GDP", "DUMMY")){
# get data ----------------------------------------------------------------.
x.train <- get(paste0("x.train.", dev.measure))
x.test <- get(paste0("x.test.", dev.measure))
# train model -------------------------------------------------------------.
# fit model
rf.fit <- randomForest(x = x.train, y = y.train, ntree = 10000)
# get predicted probability on train set
rf.response.train <- predict(rf.fit, newx = x.train, type = "prob")
rf.response.train <- rf.response.train[,2]
# get optimal threshold
rf.pred <- prediction(rf.response.train, y.train)
rf.sens <- performance(rf.pred, measure = "sens", x.measure = "cutoff")
rf.spec <- performance(rf.pred, measure = "spec", x.measure = "cutoff")
for(weight in weights){
sens <- rf.sens@y.values[[1]]
spec <- rf.spec@y.values[[1]]
max.sum <- which.max(weight*sens+spec)
rf.cutoff <- rf.sens@x.values[[1]][max.sum]
temp <- data.frame(
model = paste0("rf.", dev.measure),
year = i,
weight = weight,
cutoff = rf.cutoff
)
cutoffs.rf <- rbind(cutoffs.rf, temp)
}
# test model --------------------------------------------------------------.
rf.response.test <- predict(rf.fit, newdata = x.test, type = "prob")
rf.response.test <- rf.response.test[,2]
# calculate area under curve
rf.asses <- prediction(rf.response.test, y.test)
rf.auc <- performance(rf.asses, measure = "auc")@y.values[[1]]
# calculate true positive rate and true negative rate with different weights
# on sensitivity and specificity
for(w in weights){
cutoff.temp <- cutoffs.rf[
cutoffs.rf$weight == w
& cutoffs.rf$model == paste0("rf.", dev.measure)
& cutoffs.rf$year == i,
]$cutoff
# assign class conditional on best cutoff for corresponding weight
rf.yhat.temp <- ifelse(rf.response.test > cutoff.temp, 1, 0)
y.test.num <- as.numeric(as.character(y.test))
# confusion matrix
rf.tp.temp <- sum(rf.yhat.temp == 1 & y.test.num == 1)
rf.fp.temp <- sum(rf.yhat.temp == 1 & y.test.num == 0)
rf.tn.temp <- sum(rf.yhat.temp == 0 & y.test.num == 0)
rf.fn.temp <- sum(rf.yhat.temp == 0 & y.test.num == 1)
# calculate rates
rf.prop.positives.temp <- rf.tp.temp/sum(y.test.num == 1)
rf.prop.negatives.temp <- rf.tn.temp/sum(y.test.num == 0)
rf.avg.temp <- 0.5*(rf.prop.positives.temp+rf.prop.negatives.temp)
temp <- data.frame(
model = paste0("rf.", dev.measure),
year = i,
weight = w,
prop.pos = rf.prop.positives.temp,
prop.neg = rf.prop.negatives.temp,
avg = rf.avg.temp,
auc = rf.auc
)
results.rf <- rbind(results.rf, temp)
} # end of loop over weights
# save some objects for evaluation ----------------------------------------.
if(i == max(data$year) & dev.measure == "GDP"){
x.train.eval <- x.train
list.export[["x.train.eval"]] <- x.train.eval
rf.fit.eval <- rf.fit
list.export[["rf.fit.eval"]] <- rf.fit.eval
rf.response.train.eval <- rf.response.train
list.export[["rf.response.train.eval"]] <- rf.response.train.eval
}
} # end of loop over dev.measure
} # end of loop over years
# =============================================================================.
# 4. get relevant results and save rmd input ----
# =============================================================================.
cat("\nFinalize data and save some objects for RMD report...")
# -----------------------------------------------------------------------------.
# 4.1 calculate average prediction metrics ----
# -----------------------------------------------------------------------------.
results.avg <- rbind(results.lasso, results.rf)
results.avg <- results.avg[results.avg$weight == 1.5,]
# aggregate over years by model
results.avg <- aggregate(results.avg[c("prop.pos", "prop.neg", "auc")],
by = list(results.avg$model), FUN = mean)
names(results.avg)[names(results.avg) == "Group.1"] <- "model"
# calculate mean
results.avg$avg <- rowMeans(results.avg[c("prop.pos", "prop.neg")])
list.export[["results.avg"]] <- results.avg
list.export[["results.lasso"]] <- results.lasso
list.export[["results.rf"]] <- results.rf
# -----------------------------------------------------------------------------.
# 4.2 calculate interpretation metrics ----
# -----------------------------------------------------------------------------.
x.train.eval.df <- as.data.frame(x.train.eval)
list.export[["x.train.eval.df"]] <- x.train.eval.df
# generate predictor object
predictor.rf.eval <- Predictor$new(
model = rf.fit.eval,
data = x.train.eval.df,
y = rf.response.train.eval,
predict.function = pred,
class = "classification"
)
# 4.2.1 shapley values --------------------------------------------------------.
# calculate shapley values for each observation
for(obs in 1:nrow(x.train.eval.df)){
cat("\rcalculate shapley values for observation", obs, "of",
nrow(x.train.eval.df), "observations")
flush.console()
x.interest <- x.train.eval.df[obs,]
temp <- Shapley$new(predictor.rf.eval, x.interest = x.interest)
temp <- data.frame(
feature = temp$results$feature,
phi = temp$results$phi
)
shapley.values <- rbind(shapley.values, temp)
}
# calculate shapley values as averages of absolute values of shapley values
# of preductors in each observation
shapley.values.raw <- shapley.values
shapley.values$phi <- abs(shapley.values$phi)
shapley.values <- aggregate(shapley.values$phi,
by = list(shapley.values$feature),
FUN = mean)
list.export[["shapley.values"]] <- shapley.values
# 4.2.2 partial dependence ----------------------------------------------------.
cat("\ncalculate partial dependence")
# calculate partial dependence
partial.net_lending <- partial(rf.fit.eval, train = x.train.eval,
pred.var = "net_lending", plot = F,
type = "classification", which.class = 2)
partial.ca_balance <- partial(rf.fit.eval, train = x.train.eval,
pred.var = "ca_balance", plot = F,
type = "classification", which.class = 2)
partial.diff_unempl <- partial(rf.fit.eval, train = x.train.eval,
pred.var = "diff_unempl", plot = F,
type = "classification", which.class = 2)
# export
list.export[["partial.net_lending"]] <- partial.net_lending
list.export[["partial.ca_balance"]] <- partial.ca_balance
list.export[["partial.diff_unempl"]] <- partial.diff_unempl
# 4.2.3 accumulated local effects ---------------------------------------------.
# calculate accumulated local effects
ale.net_lending <- FeatureEffect$new(predictor.rf.eval,
feature = "net_lending")
ale.ca_balance <- FeatureEffect$new(predictor.rf.eval,
feature = "ca_balance")
ale.diff_unempl <- FeatureEffect$new(predictor.rf.eval,
feature = "diff_unempl")
list.export[["ale.net_lending"]] <- ale.net_lending
list.export[["ale.ca_balance"]] <- ale.ca_balance
list.export[["ale.diff_unempl"]] <- ale.diff_unempl
# -----------------------------------------------------------------------------.
# 4.3 save relevant data for rmd report ----
# -----------------------------------------------------------------------------.
save(list.export, file = "data/input.RData")
cat("\nRuntime:")
Sys.time() - start_time