-
Notifications
You must be signed in to change notification settings - Fork 1
/
Models_RF_LG_KNN.Rmd
161 lines (135 loc) · 4.21 KB
/
Models_RF_LG_KNN.Rmd
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
---
title: "ADS-503 Team 4 Final Project"
author:
- Leonard Littleton
- Lina Nguyen
- Emanuel Lucban
date: "6/13/21"
output:
pdf_document: default
html_notebook: default
html_document:
df_print: paged
---
```{r}
library(readr)
library(data.table)
library(mlbench)
library(ggplot2)
library(tidyr)
library(corrplot)
library(e1071)
library(caret)
library(naniar)
library(MLmetrics)
library(dplyr)
library(MASS)
library(pROC)
library(randomForest)
# Parallel Processing
library(doParallel)
cl <- makePSOCKcluster(4)
```
# Synthetic Financial Datasets For Fraud Detection
## Data Preparation
```{r}
synth_df <- fread('./data/PS_20174392719_1491204439457_log.csv', header = TRUE)
synth_df <- subset(synth_df, select = -c(isFlaggedFraud, nameOrig, nameDest))
synth_df <- cbind(synth_df, data.frame(hours_intraday = synth_df$step %% 24,
by_day = round(synth_df$step / 24),
day_of_week = round(synth_df$step / 24) %% 7))
# Log transform (amount, oldbalanceOrg, newbalanceOrig, oldbalanceDest, newbalanceDest)
cont_vars <- c('amount', 'oldbalanceOrg', 'newbalanceOrig', 'oldbalanceDest', 'newbalanceDest')
# add small constant to prevent inf values
log_scaled <- sapply(data.frame(synth_df)[, cont_vars], function(x) log(x + 1))
colnames(log_scaled) <- lapply(cont_vars, function(x) paste('log_', x, sep=''))
synth_df <- cbind(synth_df, log_scaled)
synth_df$type <- as.factor(synth_df$type)
dmy <- dummyVars(" ~ type", data = synth_df, sep = '.', fullRank = TRUE)
synth_df <- cbind(synth_df, data.frame(predict(dmy, newdata = synth_df)))
# drop type
synth_df <- subset(synth_df, select = -c(type))
```
**Split Data into Training and Test Datasets using Stratified Random Sampling**
```{r}
set.seed(42)
# split x and y
x <- subset(synth_df, select = -c(isFraud))
y <- synth_df$isFraud
data_part <- createDataPartition(y = y, p = 0.75, list = FALSE)
x_train <- x[data_part, ]
y_train <- y[data_part]
x_test <- x[-data_part, ]
y_test <- y[-data_part]
y_train <- as.factor(y_train)
y_test <- as.factor(y_test)
levels(y_train) <- c('no', 'yes')
levels(y_test) <- c('no', 'yes')
```
##Random Forest
```{r}
#registerDoParallel(cl)
control <- trainControl(method = 'cv', number = 5, classProbs = TRUE, summaryFunction = prSummary)
gbm <- train(x_train, y_train, method = 'gbm', metric = 'AUC',
preProcess = c('center', 'scale'),
trControl = control)
saveRDS(lr, file = 'rds_files/gbm_model.rds')
```
```{r}
print(rf)
print(rf$finalModel)
#confusion matrix
set.seed(3)
rfpredict <- predict(rf, newdata = test)
confusionMatrix(rfpredict, test$y_test)
#AUC curve
rfroc <- pROC::roc(response = train$y_test, predictor = predictions[,1])
rfauc <- rfroc$auc[1]
rfauc
```
##knn
```{r}
registerDoParallel(cl)
control <- trainControl(method = 'cv', number = 5, classProbs = TRUE, summaryFunction = prSummary)
knn <- train(x_train, y_train, method = 'knn', preProcess = c('center', 'scale'), tuneLength = 5, trainControl = control, metric = 'AUC')
saveRDS(knn, file = 'rds_files/knn_model.rds')
```
```{r}
#confusion matrix
set.seed(3)
knnpredict <- predict(knn, newdata = x_test)
confusionMatrix(knnpredict, y_test)
##AUC curve
knnpredict <- predict(knn, x_train, type = 'prob')
knnroc <- pROC::roc(reponse = y_train, predictor = knnpredict[,1])
knnauc <- knnroc$auc[1]
knnauc
```
##Logistic Regression
```{r}
registerDoParallel(cl)
#registerDoSEQ()
ctrl <- trainControl(summaryFunction = prSummary, classProbs = TRUE)
lr <- train(x_train, y= y_train, method = 'glm', preProcess = c('center', 'scale'), metric = 'AUC', trControl = ctrl)
lr
saveRDS(lr, file = 'rds_files/lr_model.rds')
```
```{r}
lrModel <- readRDS('rds_files/lr_model.rds')
```
```{r}
lrPred <- predict(lrModel, newdata = x_test)
lrCFM <- confusionMatrix(lrPred, y_test, positive = 'yes')
lrProbs <- predict(lrModel, newdata = x_test, type = 'prob')
lrCFM
```
```{r}
confusionMatrix(data = lr$pred$pred, reference = lr$pred$obs)
lrROC <- roc(response = lr$pred$obs, predictor = lr$pred$successful, levels = rev(levels(lr$pred$obs)))
plot(lrROC, legacy.axes = TRUE)
auc(lrROC)
```
```{r}
lrROC <- roc(y_test, predict(lrModel, newdata = x_test, type = 'prob')$yes)
saveRDS(lrROC, file = 'rds_files/lr_roc.rds')
```