-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathHomework1_isye6501.Rmd
565 lines (373 loc) · 19.1 KB
/
Homework1_isye6501.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
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
554
555
556
557
558
559
560
561
562
563
564
565
---
title: 'Homework #1: ISYE6501'
author: "Zach Olivier"
date: "5/15/2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
<br>
## Question 2.1
Question:
Describe a situation or problem from your job, everyday life, current events, etc., for which a classification model would be appropriate. List some (up to 5) predictors that you might use.
<br>
Answer:
One example I am excited about is an attempt to build a classification model to determine whether a person will purchase a certain product or not, say a new car.
Based on a list of attributes, a classification model could sort customers into groups of 'most likely will purchase' and 'most likely will not purchase' with high accuracy. This would help optimize our marketing budget by only targeting customers who are the most likely to purchase.
Predictors I would use for this model:
- Household Income
- Location
- Have they purchased a car before
- Time since last vehicle purchase
- Type of vehicle last purchased
<br>
## Question 2.2.1
The files credit_card_data.txt (without headers) and credit_card_data-headers.txt (with headers) contain a data set with 654 data points, 6 continuous and 4 binary predictor variables. It has credit card applications data with a binary response variable (last column) indicating if the application was positive or negative. The data set is the “Credit Approval Data Set” from the UCI Machine Learning Repository (https://archive.ics.uci.edu/ml/data sets/Credit+Approval) without the categorical variables and without data points that have missing values.
<br>
Question:
Using the support vector machine function ksvm contained in the R package kernlab, find a good classifier for this data.
Show the equation of your classifier, and how well it classifies the data points in the full data set.
(Don’t worry about test/validation data yet; we’ll cover that topic soon.)
<br>
Answer:
First we read in the data and load the needed packages for this analysis. Then, we quickly look at
the data and see if it matches the course description.
Our problem is classifying the data based on the 11th column - which I relabeled as response_y for
clarity.
I also established a baseline accuracy that our model based classifiers should beat easily. My naive classifier will pick '1' as the answer for every value in the training set.
In this case my naive classifier will accurately classify ~45% of the observations in the training set.
Our proposed SVM and KNN models should aim to beat this baseline accuracy measure.
<br>
```{r credit svm}
# set up directory and packages
setwd('~/Desktop/GTX/Homework 1/')
# load packages
pacman::p_load(tidyverse, kernlab, caret, kknn, modelr)
# 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)
# investigate class imbalance - this is our 'baseline' accuracy
mean(credit_df$response_y == 1)
# baseline error
(baseline <- 1 - mean(credit_df$response_y == 1))
```
Answer:
The following code sets up the Support Vector Machine algorithm using the kernlab package in R.
The ksvm function is supplied with a formula to model the response_y binary variable based on all
remaining predictors in the data set. The scaled argument is set to TRUE - the function will automatically scale the predictor data. Type is 'C-svc' to indicate we are using this function for
classification. Finally, the kernal is set to 'vanilladot' which initializes a linear decision boundary.
I then set up a for loop to pass my model different regularization (C - Cost) parameters and then
extract each model's accuracy. The model with the highest training set accuracy based on values of
C will be selected as my 'best' model. **(Note: training set accuracy is not a valid measure of model performance!)**
Here are the results:
**The best tuned model with C = 95.45 achieved 95% training set accuracy. This outperforms the naive classifer baseline accuracy greatly**
However, this does not indicate we have a good model. We may have modeled our training set real and random effects accurately - but our model will likely not generalize well to new data. There is a high likelihood that we have over fit this model to the training set.
<br>
The formula for the best tuned linear decision boundary is:
y =
**(-36.5 x A2) + (-8.36 x A3) + (54.2 x A8) + (48.6 x A9) + (-17.5 x A1) + (-22.6 x A10) + (14.5 x A11) +**
**(-22.1 x A12) + (-56.4 x A14) + (50.2 x A15) + (-.778)**
<br>
``` {r linear kernal}
# 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
# originally tried values .05 to 200 - shortened to run for homework
cost_list <- as.list(seq(from = 50, 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
```
<br>
## Question 2.2.2
Question:
You are welcome, but not required, to try other (nonlinear) kernels as well; we’re not covering them in this course, but they can sometimes be useful and might provide better predictions than vanilladot.
<br>
Answer:
Here is my experimentation with a non-linear kernal within a SVM model. I applied the same steps to hone in on a value of C that minimizes training set error.
**The radial SVM model achieves a training set error of less than 4% with our best tuned Cost parameter.**
As mentioned above - we are likely to have over fit to the training data. Training data should not be used to define the performance of our models.
Cross Validation is a better measure of real test set performance. Even though our flexible radial model achieves less than 4% error on the training set, the cross validation error is almost 19%.
```{r credit svm radial}
# set up list of possible cost values
# originally tried values .05 to 200 - shortened to run for homework
cost_list <- as.list(seq(from = 130, to = 175, 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 ~ ., # formula
data = credit_df, # training data
scaled = T, # scale predictors
type = 'C-svc', # svm for classification
kernal = 'rbfdot', # radial decision boundary
C = c, # set C to the input value from for loop
cross = 10, # implement 10 fold cross validation
kpar = 'automatic') # automatically detect best parameters for radial kernal
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
```
<br>
## Question 2.2.3
Question:
Using the k-nearest-neighbors classification function kknn contained in the R kknn package, suggest a good value of k, and show how well it classifies that data points in the full data set. Don’t forget to scale the data (scale=TRUE in kknn).
<br>
Answer:
Here we apply Leave One Out Cross Validation (LOOCV) to the credit dataset in order to find the optimal K for our KNN model. To do this, I implement the train.kknn function to cycle through possible values of K and determine the best value based on LOOCV error. Variables are scaled within the algorithm by setting scale = True. The kmax arguement is upper bound of K values we would like to consider for the model.
**In this implementation we find that the optimal K is 58.**
Using this optimal K value, we fit a KNN model to the entire training set to determine accuracy.
**The accuracy of the K = 58 KNN model is ~88%**
Note: Using this model to predict back onto the training set will give misleading results. Smaller values for K will actually predict better on the training set. However, this will not generalize to an independent or new dataset. K = 1 may perform perfectly on the training set but K = 58 will likely generalize better to other datasets.
```{r credit knn}
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))
```
<br>
## Question 3.1
Question:
Using the same data set (credit_card_data.txt or credit_card_data-headers.txt) as in Question 2.2,
use the ksvm or kknn function to find a good classifier:
(a) using cross-validation (do this for the k-nearest-neighbors model; SVM is optional)
(b) splitting the data into training, validation, and test data sets (pick either KNN or SVM; the other
is optional).
<br>
Answer: (a)
The kknn package provides a function 'train.kknn' which will search for the best value of K based on cross validation error. I specified kcv = 10 to run a 10-fold cross validation on the full credit dataset.
The train.kknn function will run the 10-fold cross validation and extract the K (nearest neighbor parameter K, not cross validation fold k) with the minimial error.
Based on the results the optimal **K is 58 and results in around 10% mean squared error**
I also provide the results for K = 30, 20, and 10 to illustrate that the accuracy improvement for using K = 58 is not that much smaller than using less neighbors.
Note: Using this model to predict back onto the training set will give misleading results. Smaller values for K will actually predict better on the training set. However, this will not generalize to an independent or new dataset. K = 1 may perform perfectly on the training set but K = 58 will likely generalize better to other datasets.
Cross validation is a better estimate of test set error and we should trust those results more than accuracy or error on the training set.
<br>
```{r credit knn cv}
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)
```
Answer: (b)
I will use the caret package to split our data set into training, test, and validation data sets. **I will portion the available data 60% training, 20% test, and 20% validation.**
We train the model on the training data set, measure accuracy and tune parameters using the test set. Once we think we have a good model, we will re-train the model on the entire training and test sets, then calculate accuracy for our best model using the validation data set.
<br>
```{r credit knn splits}
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
nrow(train)/nrow(credit_df); nrow(test)/nrow(credit_df); nrow(validation)/nrow(credit_df)
```
Answer: (b)
Here we determine the best value of K by training our KNN model on the training data set, and measuring accuracy on the test data set. Based on this method the K value that produced the maximum accuracy on the test data set is **K = 40, with accuracy of ~88%**.
Now that we have our optimal value of K based on our test data set - we will apply this model to the final holdout data in the validation set. We do this to get an unbiased view of how our model will perform on new data.
**The KNN model with K = 40 achieves ~85% accuracy on the validation dataset.** This means we should expect our classifier to accurately predict 85% of new 'unseen' data into the correct category.
``` {r knn train test valid}
# 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))
```