-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelsReal.Rmd
More file actions
314 lines (205 loc) · 8.55 KB
/
modelsReal.Rmd
File metadata and controls
314 lines (205 loc) · 8.55 KB
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
---
title: "Final Project - Frequentist Models"
author: "Jack Crum"
data: "4/13/2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r Installing and Loading Packages}
# Installing and Loading Packages
setwd("C:/Users/sjcrum/Documents/Bayesian Statistics/DataSets/Visas")
#install.packages("MASS", dependencies=TRUE, repos='http://cran.rstudio.com/')
#install.packages("ggplot2", dependencies=TRUE, repos='http://cran.rstudio.com/')
#install.packages("caret", dependencies=TRUE, repos='http://cran.rstudio.com/')
#install.packages("dplyr", dependencies=TRUE, repos='http://cran.rstudio.com/')
#install.packages("car", dependencies=TRUE, repos='http://cran.rstudio.com/')
library(ggplot2)
library(caret)
library(dplyr)
library(MASS)
library(corrplot)
library(xgboost)
library(data.table)
library(car)
```
```{r Loading training and testing sets}
#Loading training and testing sets
training <- read.csv("training_dummy.csv")
testing <- read.csv("testing_dummy.csv")
#Removing first id column
training <- training[2:323]
testing <- testing[2:323]
#Converting the predicted variable 'case_status' into a factor
training$case_status <- as.factor(training$case_status)
testing$case_status <- as.factor(testing$case_status)
#Removing case_status to create predictor dataset
train_X <- training[-1]
test_X <- testing[-1]
```
```{r Full Logistic Model}
#Set seed for reproducibility
set.seed(29)
#Train model on whole training dataset
modelLogit <- glm(case_status ~ ., family=binomial(link='logit'), data=training)
summary(modelLogit)
```
```{r Full Logistic Model Prediction}
# Predict model on testing dataset with a 0.8 classification threshold
predLogit <- predict(modelLogit, test_X ,type='response')
predLogit <- ifelse(predLogit > 0.8, 1, 0)
predLogit <- as.factor(predLogit)
# Calculate and print accuracy
misClasificError <- mean(predLogit != testing$case_status)
print(paste('Accuracy:',round(1-misClasificError, 4)))
# Create confusion matrix to calculate model evaluation metrics
conf <- confusionMatrix(predLogit, testing$case_status, positive = "1")
conf
```
```{r Plot Thresholds Function}
# This functoins plots accuracy, sensitivity, and specificity of a model on the same graph across various classification thresholds from 0.5 to 0.95 at 0.05 intervals
plotThresholdScores <- function(model, test_X_data, test_y_data, model_name){
#Set empty vectors
acc <- c()
sens <- c()
spec <- c()
#Set sequence of classification thresholds
sequence <- seq(0.5, 0.95, 0.05)
#For loop to predict and calculate metrics at each classification threshold
for (i in 1:length(sequence)){
predLogit <- predict(model, test_X_data ,type='response')
pred <- ifelse(predLogit > sequence[i], 1, 0)
pred <- as.factor(pred)
conf <- confusionMatrix(pred, test_y_data, positive = "1")
print(conf)
#Append scores to empty vectors
acc[i] <- conf$overall["Accuracy"]
sens[i] <- conf$byClass["Sensitivity"]
spec[i] <- conf$byClass["Specificity"]
}
#Bind scores into one dataframe
df <- as.data.frame(cbind(sequence, acc, sens, spec))
print(df)
#Plot dataframe
ggplot(df) + geom_line(aes(x = sequence, y = acc), color = "green") + geom_line(aes(x = sequence, y = sens), color = "blue") + geom_line(aes(x = sequence, y = spec), color = "red") + labs(x = "Threshold", y = "Metric Score", title = paste("Metric Scores of", model_name))
}
#Plot threshold scores on full logistic model
plotThresholdScores(modelLogit, test_X, testing$case_status, "Full Logistic Model")
```
```{r VIF1}
#Check for multicollinearity
#Returns an error, which means there is perfect collinearity in the model
#vif(modelLogit)
```
```{r Extract Significant Variables}
#This function extracts all variable names from a model with a p-value below the set threshold to retain only significant variables
signVarCols <- function(model, threshold){
#Extract p-values of all variables in the model
signifvars <- coef(summary(model))[,4]
signifvarsDF <- as.data.frame(signifvars)
#Set the row names (variable names) to a column
signvars <- setDT(signifvarsDF, keep.rownames = TRUE)[]
#Extract only varialbes with signficant p-value
signvars <- signvars %>% filter(signifvars <= threshold)
#Extract variable names from dataframe
colNames <- signvars[,1]
#Remove intercept
colNames <- colNames[2:length(colNames)]
#Return the vector of variable names
return(colNames)
}
#Extract significant variables from full logistic model
colNames <- signVarCols(modelLogit, 0.01)
#Create new training and testing sets with only significant variables
training_sub <- training[, c(colNames)]
testing_sub <- testing[, c(colNames)]
#Bind the predicted variable to the new datasets
training_sub <- cbind(training$case_status, training_sub)
testing_sub <- cbind(testing$case_status, testing_sub)
#Move predicted variable to the front
colnames(training_sub)[1] <- "case_status"
colnames(testing_sub)[1] <- "case_status"
```
```{r SignVars Logistic Regression}
set.seed(29)
#Train model on signficiant logistic model
modelLogitSub <- glm(case_status ~ ., family=binomial(link='logit'), data=training_sub)
summary(modelLogitSub)
```
```{r VIF2}
#Test for multicollinearity
vif(modelLogitSub)
```
```{r Plot Thresholds on signvars logistic}
#Create new testing data
test_sub_X <- subset(testing_sub, select = -c(case_status))
#Plot thresholds on signvars logistic
plotThresholdScores(modelLogitSub, test_sub_X, testing_sub$case_status, "Logisitic Regression Subset")
```
```{r Stepwise Logistic}
#Train stepwise logistic regression model
stepLogit <- step(modelLogitSub, direction = "both")
```
```{r VIF3}
#Test for multicollinearity in stepwise model
vif(stepLogit)
```
```{r Plot Thresholds Stepwise}
#Plot thresholds of stepwise model
plotThresholdScores(stepLogit, test_sub_X, testing_sub$case_status, "Logisitic Regression Step")
```
```{r Prepare data for XGBoost model}
#Create labels and predictor datasets for XGBoost model
train_label <- training$case_status
test_label <- testing$case_status
data_train <- as.matrix(subset(training, select = -c(case_status)))
data_test <- as.matrix(subset(testing, select = -c(case_status)))
#Convert to numeric, subtract one to retain binary, and convert whole dataset to a matrix
train_label_max <- as.matrix(as.numeric(train_label)-1)
test_label_max <- as.matrix(as.numeric(test_label)-1)
```
```{r Create XGBoost Matrices}
#Prepare XGBoost Matrices
dtrain <- xgb.DMatrix(data = data_train, label=train_label_max)
dtest <- xgb.DMatrix(data = data_test, label=test_label_max)
```
```{r Parameters list}
#Establish parameters for XGBoost model
params <- list(booster = "gbtree", objective = "binary:logistic", eta=0.3, gamma=0, max_depth=20, min_child_weight=1, subsample=1, colsample_bytree=1)
```
```{r XGBoost1}
#First XGBoost model training
xgb1 <- xgboost(data = data_train, label = train_label_max, params = params, nrounds = 100, verbose = 1, print_every_n = 10, early_stopping_rounds = 20, save_period = 0, save_name = "xgboost.model")
```
```{r XGBoost2}
#Second XGBoost model training
xgb2 <- xgb.train (params = params, data = dtrain, nrounds = 100, print_every_n = 10, early_stop_round = 10, maximize = F , eval_metric = "error", eval_metric = "auc")
```
```{r XGBoost model prediction}
#XGBoost model prediction with 0.9 classification threshold
xgbpred <- predict(xgb1, data_test)
xgbprediction <- ifelse (xgbpred > 0.9,1,0)
#Convert labels and test predicted variable to factors for analysis
test_label_factor <- as.factor(test_label)
xgbpred_factor <- as.factor(xgbprediction)
#Create confusion matrix for evalution
confxgb1 <- confusionMatrix(xgbpred_factor, test_label, positive = "1")
confxgb1
```
```{r}
#Model prediction with 0.9 classification threshold
xgbpred2 <- predict(xgb2, data_test)
xgbprediction2 <- ifelse (xgbpred2 > 0.9,1,0)
#Convert labels and test predicted variable to factors for analysis
test_label_factor <- as.factor(test_label)
xgbpred2_factor <- as.factor(xgbprediction2)
#Confusion matrix
confxgb2 <- confusionMatrix(xgbpred2_factor, test_label, positive = "1")
confxgb2
```
```{r}
#view variable importance plot
mat <- xgb.importance(feature_names = colnames(data_train),model = xgb1)
xgb.plot.importance(importance_matrix = mat[1:20])
```