-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathHomework1_script.R
375 lines (268 loc) · 10.6 KB
/
Homework1_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
# Homework #1 ISYE6501 ------------------------------------------------------------------------
# code from the first homework is below
# document is meant to supplement pdf report
# please refer to the pdf report for offical answers, descriptions, and commentary
# this document is provided to make replicating code easier for homework grader
# http://html2pdf.com
# workflow ------------------------------------------------------------------------------------
# set up directory and packages
setwd('~/Desktop/GTX/Homework 1/')
# load packages
pacman::p_load(tidyverse, kernlab, caret, kknn, modelr)
# get data ------------------------------------------------------------------------------------
# get data and format
credit_df <- read.table('2.2credit_card_data-headersSummer2018.txt',header = T) %>%
as_tibble() %>%
dplyr::rename(., 'response_y' = R1)
# data summary
str(credit_df)
# get data and format
credit_df <- read.table('2.2credit_card_data-headersSummer2018.txt',header = T) %>%
as_tibble() %>%
dplyr::rename(., 'response_y' = R1)
# data summary
str(credit_df)
# linear svm ----------------------------------------------------------------------------------
# set seed
set.seed(2)
# data frame of model parameters
model_param <- data.frame(
type = 'C-svc',
kernal = 'vanilladot',
cross = 5,
scaled = T,
stringsAsFactors = F
)
# set up a list of possible C values: choose C from .5 to 100 by .05
# - will build a model for each C and determine the 'best' C based on accuracy
cost_list <- as.list(seq(from = 0.5, to = 100, by = .05))
# set up a list to put the error metrics of each model into
error_list <- list()
# cost loop - for each value of cost - fit a model - extract accuracy - put into error list
for (i in seq_along(cost_list)) {
c <- cost_list[[i]]
set.seed(2)
# fit ksvm model based on model parameters
ksvm_fit <- ksvm(
response_y ~ .,
data = credit_df,
scaled = model_param$scaled ,
type = model_param$type,
kernal = model_param$kernal,
C = c,
cross = model_param$cross
)
error_list[[i]] = ksvm_fit@error
}
# reduce list of errors and cost parameters into a column in a data frame
error_df <- reduce(error_list, rbind.data.frame)
cost_df <- reduce(cost_list, rbind.data.frame)
# performance measures - put list of errors and costs next to each other to find highest accuracy
performance_train <- cbind(error_df, cost_df) %>%
rename(
'svm_error' = !!names(.[1]),
'svm_cost' = !!names(.[2])
) %>%
mutate(svm_accuracy = 1-svm_error) %>%
filter(svm_error == min(svm_error)) %>%
arrange(., svm_cost) %>%
.[1,]
# fit model with our best C determined by training error
svm_bestfit <- ksvm_fit <- ksvm(
response_y ~ .,
data = credit_df,
scaled = model_param$scaled ,
type = model_param$type,
kernal = model_param$kernal,
C = performance_train$svm_cost,
cross = model_param$cross
)
## extracting the model formula
# calculate a1...am the coefficients for each predictor!
svm_formula <- colSums(svm_bestfit@xmatrix[[1]] * svm_bestfit@coef[[1]]) %>%
as.data.frame() %>%
rownames_to_column('predictor') %>%
rbind(., data.frame(predictor ='z.Intercept', . = svm_bestfit@b)) %>%
spread(., key = predictor, value = .) %>%
as_tibble()
# print results
performance_train
svm_bestfit
svm_formula
# flexible svm --------------------------------------------------------------------------------
# set up list of possible cost values
cost_list <- as.list(seq(from = 0.5, to = 200, by = .05))
# set up empty list to put error results for each cost into
error_list <- list()
# fit a radial svm for every value of cost...extract error to compare
for (i in seq_along(cost_list)) {
c <- cost_list[[i]]
set.seed(2)
# fit ksvm model
ksvm_fit <- ksvm(
response_y ~ .,
data = credit_df,
scaled = T,
type = 'C-svc',
kernal = 'rbfdot',
C = c,
cross = 10,
kpar = 'automatic')
error_list[[i]] = ksvm_fit@error
}
# collapse lists to data frames
error_df <- reduce(error_list, rbind.data.frame)
cost_df <- reduce(cost_list, rbind.data.frame)
# find the optimal value of Cost - cost value with lowest error
performance_train <- cbind(error_df, cost_df) %>%
rename(
'svm_error' = !!names(.[1]),
'svm_cost' = !!names(.[2])
) %>%
filter(svm_error == min(svm_error)) %>%
arrange(., svm_cost) %>%
.[1,]
# fit model with our best C determined by training error
svm_bestfit <- ksvm_fit <- ksvm(
response_y ~ .,
data = credit_df,
scaled = T,
type = 'C-svc',
kernal = 'rbfdot',
C = performance_train$svm_cost,
cross = 10,
kpar = 'automatic'
)
# look at the results of our model
svm_bestfit@error
svm_bestfit@cross
# knn first attempt ---------------------------------------------------------------------------
set.seed(2)
# set up train.kknn to find the optimal k using leave one out classification
(knn_fit_LOOCV <- train.kknn(
response_y ~ .,
data = credit_df,
# test = credit_df,
kmax = 100,
# kcv = 10,
scale = T)
)
# apply optimal K to the original training dataset
knn_fit <- kknn(
response_y ~ .,
train = credit_df,
test = credit_df,
k = 58,
scale = T)
# accuracy measures
fitted <- fitted(knn_fit) %>%
as_tibble() %>%
mutate(value = ifelse(value > .5, 1, 0)) %>% # round to 1 if prob > .5, 0 otherwise
cbind(., credit_df$response_y) %>%
mutate(acc = value == credit_df$response_y) # if prediction == actual then True, else False
# percetage accuracy of the model
(test_accuracy <- mean(fitted$acc))
# knn cross validation ------------------------------------------------------------------------
set.seed(4)
# from documentation - k fold cross validation needs kcv arguement
# kcv = Number of partitions for k-fold cross validation.
# find best value of K by cross validation - what is the estimated test accuracy?
knn_fit <- train.kknn(response_y ~ .,
data = credit_df,
kmax = 60, # search K values up to 600
kcv = 10, # base accuarcy on 10-fold cross validation
scale = T)
summary(knn_fit)
# fit a model with less that the optimal K and compare accuracy
# K lower than our best fit should give inferior results
knn_fit2 <- train.kknn(response_y ~ .,
data = credit_df,
kmax = 30,
kcv = 10,
scale = T)
# mean squared error
summary(knn_fit2)
# fit a model with less that the optimal K and compare accuracy
# K lower than our best fit should give inferior results
knn_fit3 <- train.kknn(response_y ~ .,
data = credit_df,
kmax = 20,
kcv = 10,
scale = T)
# mean squared error
summary(knn_fit3)
# fit a model with less that the optimal K and compare accuracy
# K lower than our best fit should give inferior results
knn_fit4 <- train.kknn(response_y ~ .,
data = credit_df,
kmax = 10,
kcv = 10,
scale = T)
# mean squared error
summary(knn_fit4)
# knn train, test, validation -----------------------------------------------------------------
set.seed(2)
# develop the training and holdout partition
partition <- createDataPartition(credit_df$response_y, p = .6,
list = F)
# develop the training set
train <- credit_df[partition,]
# develop the holdout set
holdout <- credit_df[-partition,]
# develop the split of the holdout data into test and validation
partition_valid <- createDataPartition(holdout$response_y, p = .5,
list = F)
# split holdout into test
test <- holdout[partition_valid, ]
# split holdout in validation
validation <- holdout[-partition_valid, ]
# check splits
dim(train);dim(test);dim(validation)
# set up list of possible K values from 1 to 100 by 3
possible_k <- as.list(seq(from = 1, to = 100, by = 3))
# set up a blank list to put accuracy values into
test_accuracy <- list()
# fit a model for each possible value of K and extract the accuracy from each model
for (i in seq_along(possible_k)) {
k = possible_k[[i]]
knn_fit <- kknn(
response_y ~ .,
train = train,
test = test,
# valid = validation$response_y,
k = k,
scale = T)
fitted <- fitted(knn_fit) %>%
as_tibble() %>%
mutate(value = ifelse(value > .5, 1, 0)) %>%
cbind(., test$response_y) %>%
mutate(acc = value == test$response_y)
test_accuracy[[i]] <- mean(fitted$acc)
}
# put the K and test accuracy lists into dataframes
k_df <- reduce(possible_k, rbind.data.frame)
test_acc_df <- reduce(test_accuracy, rbind.data.frame)
# find the best K associated with the highest accuracy
(performance_test <- cbind(k_df, test_acc_df) %>%
rename(
'knn_error' = !!names(.[2]),
'knn_k' = !!names(.[1])
) %>%
filter(knn_error == max(knn_error)) %>%
arrange(., knn_k) %>%
.[1,])
# fit the best model on the training + testing data - find accuracy on the validation set
knn_best_fit <- kknn(
response_y ~ .,
train = rbind(train, test), # combine train to be train + test datasets
test = validation,
k = performance_test$knn_k, # best value of K found from test set accuracy
scale = T)
# view the validation set accuracy
best_fitted <- fitted(knn_best_fit) %>%
as_tibble() %>%
mutate(value = ifelse(value > .5, 1, 0)) %>% # kknn outputs a score for each value
cbind(., validation$response_y) %>%
mutate(acc = value == validation$response_y)
# accuracy of our best fit knn model on the validation dataset
(valid_accuracy <- mean(best_fitted$acc))