-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXGBoost Models.Rmd
343 lines (301 loc) · 10.6 KB
/
XGBoost Models.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
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
---
title: "XGBoost Test"
author: "Caleb Kornfein"
date: "7/12/2020"
output: pdf_document
---
## Packages
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(readxl)
library(car)
library(glmnet)
library(xgboost)
library(magrittr)
library(Matrix)
library(dplyr)
library(caret)
```
## Loading Data
```{r loading-data}
community_train_data <- read_excel("Communities_train.xlsx")
community_predict_data <- read_excel("Communities_predict.xlsx")
#creating train and test
set.seed(1)
n = nrow(community_train_data)
split = sample(c(TRUE, FALSE), n, replace=TRUE, prob=c(0.75, 0.25))
train <- community_train_data[split, ]
test <- community_train_data[!split, ]
xgb_train <- train[ , !names(train) %in% c("LNFZ", "SNFZ", "Krad", "PKW")]
xgb_test <- test[ , !names(test) %in% c("LNFZ", "SNFZ", "Krad", "PKW")]
overall <- community_train_data[ , !names(community_train_data) %in% c("LNFZ", "SNFZ", "Krad", "PKW")]
xgb_train_matrix <- xgb.DMatrix(data = data.matrix(xgb_train))
# train objects
PKW_train_label <- train$PKW
LNFZ_train_label <- train$LNFZ
SNFZ_train_label <- train$SNFZ
KRAD_train_label <- train$Krad
PKW_train_matrix <- xgb.DMatrix(data = data.matrix(xgb_train), label = PKW_train_label)
LNFZ_train_matrix <- xgb.DMatrix(data = data.matrix(xgb_train), label = LNFZ_train_label)
SNFZ_train_matrix <- xgb.DMatrix(data = data.matrix(xgb_train), label = SNFZ_train_label)
KRAD_train_matrix <- xgb.DMatrix(data = data.matrix(xgb_train), label = KRAD_train_label)
# test objects
PKW_test_label <- test$PKW
LNFZ_test_label <- test$LNFZ
SNFZ_test_label <- test$SNFZ
KRAD_test_label <- test$Krad
PKW_test_matrix <- xgb.DMatrix(data = data.matrix(xgb_test), label = PKW_test_label)
LNFZ_test_matrix <- xgb.DMatrix(data = data.matrix(xgb_test), label = LNFZ_test_label)
SNFZ_test_matrix <- xgb.DMatrix(data = data.matrix(xgb_test), label = SNFZ_test_label)
KRAD_test_matrix <- xgb.DMatrix(data = data.matrix(xgb_test), label = KRAD_test_label)
# combined
PKW_label <- community_train_data$PKW
LNFZ_label <- community_train_data$LNFZ
SNFZ_label <- community_train_data$SNFZ
KRAD_label <- community_train_data$Krad
PKW_matrix <- xgb.DMatrix(data = data.matrix(overall), label = PKW_label)
LNFZ_matrix <- xgb.DMatrix(data = data.matrix(overall), label = LNFZ_label)
SNFZ_matrix <- xgb.DMatrix(data = data.matrix(overall), label = SNFZ_label)
KRAD_matrix <- xgb.DMatrix(data = data.matrix(overall), label = KRAD_label)
```
# Parameter grids
```{r loading-data}
# parameters
xgb_grid_params <- expand.grid(nrounds = c(50, 200, 500),
max_depth = c(2, 3, 4, 6),
colsample_bytree = c(.5, .75, 1),
eta = c(.01, .03, .05, .07, .1, .3),
gamma= c(0, 3, 6, 9),
min_child_weight = 1,
subsample = c(.5, .75, 1))
xgb_train_control = trainControl(
method = "cv",
number = 5,
allowParallel = TRUE,
verboseIter = FALSE,
returnData = FALSE)
xgb_params <- list("objective" = "reg:squarederror",
"eval_metric" = "rmse")
PKW_watchlist <- list(train = PKW_train_matrix, test = PKW_test_matrix, overall = PKW_matrix)
LNFZ_watchlist <- list(train = LNFZ_train_matrix, test = LNFZ_test_matrix, overall = LNFZ_matrix)
SNFZ_watchlist <- list(train = SNFZ_train_matrix, test = SNFZ_test_matrix, overall = SNFZ_matrix)
KRAD_watchlist <- list(train = KRAD_train_matrix, test = KRAD_test_matrix, overall = KRAD_matrix)
```
# PKW
```{r PKW-xgb}
# Caution! For each vehicle class, the XGBoost model 1's which are for parameter testing may take a long time to run. On a 2017 Macbook Air with a 1.8 GHz Intel Core I5, each parameter testing xgboost model took approximately 35 - 40 minutes. For each vehicle class, the 2nd model already is loaded with the optimal parameters based off of the 1st model.
# eXtreme Gradient BOOSTing model 1: parameter testing
set.seed(1)
PKW_xgb_model = train(
x = xgb_train_matrix,
y = PKW_train_label,
trControl = xgb_train_control,
tuneGrid = xgb_grid_params,
method = "xgbTree",
objective = "reg:squarederror"
)
# eXtreme Gradient BOOSTing model 2: finding optimal number of iterations
set.seed(1)
PKW_xgb_model_rounds = xgb.train(
data = PKW_train_matrix,
params = xgb_params,
nrounds = 1000,
eta = .1,
watchlist = PKW_watchlist,
colsample_bytree = .75,
max_depth = 2,
gamma = 3,
subsample = .5,
)
# plotting
PKW_error <-data.frame(PKW_xgb_model_rounds$evaluation_log)
plot(PKW_error$iter, PKW_error$train_rmse, col = "blue", xlab = "Iterations", ylab = "RMSE", main = "PC")
points(PKW_error$iter, PKW_error$test_rmse, col = "red")
abline(a = 97010338, b = 0, col = "black", pch = 19, lwd = 4)
legend("topright", legend= c("MLR", "XGB Train", "XGB Test"), col=c("black", "blue", "red"), lty=1:2)
PKW_error[PKW_error$test_rmse == min(PKW_error$test_rmse),]
PKW_iter <- PKW_error[PKW_error$test_rmse == min(PKW_error$test_rmse),]$iter
quartz.save(file = "PKW_iters.tiff", type = "tiff")
dev.off()
# final model and analysis
set.seed(1)
PKW_xgb_model_rounds_final = xgb.train(
data = PKW_train_matrix,
params = xgb_params,
nrounds = PKW_iter,
eta = .1,
watchlist = PKW_watchlist,
colsample_bytree = .75,
max_depth = 2,
gamma = 3,
subsample = 0.5
)
PKW_importance <- xgb.importance(colnames(PKW_train_matrix), model = PKW_xgb_model_rounds_final)
print(PKW_importance)
xgb.plot.importance(PKW_importance)
PKW_xgb_train_rmse <- PKW_error$train_rmse[PKW_iter]
PKW_xgb_test_rmse <- min(PKW_error$test_rmse)
PKW_xgb_overall_rmse <- PKW_error$overall_rmse[PKW_iter]
```
# LNFZ
```{r LNFZ-xgb}
set.seed(1)
# eXtreme Gradient BOOSTing model 1: parameter testing
LNFZ_xgb_model = train(
x = xgb_train_matrix,
y = LNFZ_train_label,
trControl = xgb_train_control,
tuneGrid = xgb_grid_params,
method = "xgbTree",
objective = "reg:squarederror"
)
# eXtreme Gradient BOOSTing model 2: finding the optimal number of iterations
set.seed(1)
LNFZ_xgb_model_rounds = xgb.train(
data = LNFZ_train_matrix,
params = xgb_params,
nrounds = 1000,
eta = .03,
watchlist = LNFZ_watchlist,
colsample_bytree = .5,
max_depth = 2,
gamma = 3,
subsample = .5
)
# plotting
LNFZ_error <-data.frame(LNFZ_xgb_model_rounds$evaluation_log)
plot(LNFZ_error$iter, LNFZ_error$train_rmse, col = "blue", xlab = "Iterations", ylab = "RMSE", main = "LDV")
points(LNFZ_error$iter, LNFZ_error$test_rmse, col = "red")
abline(a = 4490571, b = 0, col = "black", pch = 19, lwd = 4)
legend("topright", legend= c("MLR", "XGB Train", "XGB Test"), col=c("black", "blue", "red"), lty=1:2)
LNFZ_iter <- LNFZ_error[LNFZ_error$test_rmse == min(LNFZ_error$test_rmse),]$iter
quartz.save(file = "LNFZ_iters.tiff", type = "tiff")
dev.off()
# final model and analysis
set.seed(1)
LNFZ_xgb_model_rounds_final <- xgb.train(
data = LNFZ_train_matrix,
params = xgb_params,
nrounds = LNFZ_iter,
eta = .03,
watchlist = LNFZ_watchlist,
colsample_bytree = .5,
max_depth = 2,
gamma = 3,
subsample = .5
)
LNFZ_importance <- xgb.importance(colnames(LNFZ_train_matrix), model = LNFZ_xgb_model_rounds_final)
print(LNFZ_importance)
xgb.plot.importance(LNFZ_importance)
LNFZ_xgb_train_rmse <- LNFZ_error$train_rmse[LNFZ_iter]
LNFZ_xgb_test_rmse <- min(LNFZ_error$test_rmse)
LNFZ_xgb_overall_rmse <- LNFZ_error$overall_rmse[LNFZ_iter]
```
# SNFZ
```{r SNFZ-xgb}
set.seed(1)
# eXtreme Gradient BOOSTing model 1: parameter testing
SNFZ_xgb_model = train(
x = xgb_train_matrix,
y = SNFZ_train_label,
trControl = xgb_train_control,
tuneGrid = xgb_grid_params,
method = "xgbTree",
objective = "reg:squarederror"
)
# eXtreme Gradient BOOSTing model 2: finding the optimal number of iterations
set.seed(1)
SNFZ_xgb_model_rounds = xgb.train(
data = SNFZ_train_matrix,
params = xgb_params,
nrounds = 1000,
eta = .1,
watchlist = SNFZ_watchlist,
colsample_bytree = .5,
max_depth = 2,
gamma = 0,
subsample = 0.5
)
# plotting
SNFZ_error <-data.frame(SNFZ_xgb_model_rounds$evaluation_log)
plot(SNFZ_error$iter, SNFZ_error$train_rmse, col = "blue", xlab = "Iterations", ylab = "RMSE", main = "HDV")
points(SNFZ_error$iter, SNFZ_error$test_rmse, col = "red")
abline(a = 10779736, b = 0, col = "black", pch = 19, lwd = 4)
legend("topright", legend= c("MLR", "XGB Train", "XGB Test"), col=c("black", "blue", "red"), lty=1:2)
SNFZ_iter <- SNFZ_error[SNFZ_error$test_rmse == min(SNFZ_error$test_rmse),]$iter
quartz.save(file = "SNFZ_iters.tiff", type = "tiff")
dev.off()
# final model and analysis
set.seed(1)
SNFZ_xgb_model_rounds_final = xgb.train(
data = SNFZ_train_matrix,
params = xgb_params,
nrounds = SNFZ_iter,
eta = .1,
watchlist = SNFZ_watchlist,
colsample_bytree = .5,
max_depth = 2,
gamma = 0,
subsample = 0.5
)
SNFZ_importance <- xgb.importance(colnames(SNFZ_train_matrix), model = SNFZ_xgb_model_rounds_final)
print(SNFZ_importance)
xgb.plot.importance(SNFZ_importance)
SNFZ_xgb_train_rmse <- SNFZ_error$train_rmse[SNFZ_iter]
SNFZ_xgb_test_rmse <- min(SNFZ_error$test_rmse)
SNFZ_xgb_overall_rmse <- SNFZ_error$overall_rmse[SNFZ_iter]
```
# KRAD
```{r KRAD-xgb}
set.seed(1)
# eXtreme Gradient BOOSTing model 1: parameter testing
KRAD_xgb_model = train(
x = xgb_train_matrix,
y = KRAD_train_label,
trControl = xgb_train_control,
tuneGrid = xgb_grid_params,
method = "xgbTree",
objective = "reg:squarederror"
)
# eXtreme Gradient BOOSTing model 2: finding the optimal number of iterations
set.seed(1)
KRAD_xgb_model_rounds = xgb.train(
data = KRAD_train_matrix,
params = xgb_params,
nrounds = 1000,
eta = .05,
watchlist = KRAD_watchlist,
colsample_bytree = .5,
max_depth = 3,
gamma = 9,
subsample = 1
)
# plotting
KRAD_error <-data.frame(KRAD_xgb_model_rounds$evaluation_log)
plot(KRAD_error$iter, KRAD_error$train_rmse, col = "blue", xlab = "Iterations", ylab = "RMSE", main = "MC")
points(KRAD_error$iter, KRAD_error$test_rmse, col = "red")
abline(a = 1741943, b = 0, col = "black", pch = 19, lwd = 4)
legend("topright", legend= c("MLR", "XGB Train", "XGB Test"), col=c("black", "blue", "red"), lty=1:2)
KRAD_iter <- KRAD_error[KRAD_error$test_rmse == min(KRAD_error$test_rmse),]$iter
quartz.save(file = "KRAD_iters.tiff", type = "tiff")
dev.off()
# final model and analysis
set.seed(1)
KRAD_xgb_model_rounds_final = xgb.train(
data = KRAD_train_matrix,
params = xgb_params,
nrounds = KRAD_iter,
eta = .05,
watchlist = KRAD_watchlist,
colsample_bytree = .5,
max_depth = 3,
gamma = 9,
subsample = 1
)
KRAD_importance <- xgb.importance(colnames(KRAD_train_matrix), model = KRAD_xgb_model_rounds_final)
print(KRAD_importance)
xgb.plot.importance(KRAD_importance)
KRAD_xgb_train_rmse <- KRAD_error$train_rmse[KRAD_iter]
KRAD_xgb_test_rmse <- min(KRAD_error$test_rmse)
KRAD_xgb_overall_rmse <- KRAD_error$overall_rmse[KRAD_iter]
```