-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkaggle_Kenny.Rmd
1688 lines (1365 loc) · 67 KB
/
kaggle_Kenny.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Kaggle_Kenny"
output: pdf_document
editor_options:
chunk_output_type: console
---
```{r importing the data}
library(dplyr)
library(stringr)
data <- read.csv("~/Dropbox/Stats-101C-Kaggle/train.csv")
data <- data[,-c(3)] # remove ID column because won't help with predictions and # of subjects, since they're all 1.
final_test <- read.csv("~/Dropbox/Stats-101C-Kaggle/test.csv")
final_test <- final_test[,-c(3)]
# how many missing values are there?
sum(is.na(data)) / (dim(data)[1] * dim(data)[2])
# are the missing values due to not be recorded or because they don't actually exist?
# for example, some notes mention that there was a shot, however the 'NumberofShots' variable says it's 0
seed <- 123456
```
First explore the data \newline
look at how different variables affect fatality rates to get an idea of which variables are important
```{r some basic EDA}
library(dplyr)
data %>% summarise('F' = prop.table(table(Fatal))[1],
'N' = prop.table(table(Fatal))[2],
'U' = prop.table(table(Fatal))[3],
'n' = n()) %>% arrange(desc(n))
# looking at the overall fatality rates
data %>% group_by(SubjectRace) %>% summarise('F' = prop.table(table(Fatal))[1],
'N' = prop.table(table(Fatal))[2],
'U' = prop.table(table(Fatal))[3],
'n' = n()) %>% arrange(desc(n))
# not any striking differences; however B, L, and U have the largest nonfatalities
data %>% group_by(SubjectArmed) %>% summarise('F' = prop.table(table(Fatal))[1],
'N' = prop.table(table(Fatal))[2],
'U' = prop.table(table(Fatal))[3],
'n' = n()) %>% arrange(desc(n))
# fatality rates does not seem to change based on whether subject is armed or not
data %>% group_by(SubjectGender) %>% summarise('F' = prop.table(table(Fatal))[1],
'N' = prop.table(table(Fatal))[2],
'U' = prop.table(table(Fatal))[3],
'n' = n()) %>% arrange(desc(n))
# subject gender doesn't seem to affect fatality rates either ...
data %>% group_by(SubjectAge) %>% summarise('F' = prop.table(table(Fatal))[1],
'N' = prop.table(table(Fatal))[2],
'U' = prop.table(table(Fatal))[3],
n = n()) %>% arrange(desc(n))
# too many unknowns ...
data %>% group_by(NumberOfShots) %>% summarise('F' = prop.table(table(Fatal))[1],
'N' = prop.table(table(Fatal))[2],
'U' = prop.table(table(Fatal))[3],
n = n()) %>% arrange(desc(n))
# again, too many unknowns
data %>% group_by(NumberOfOfficers) %>% summarise('F' = prop.table(table(Fatal))[1],
'N' = prop.table(table(Fatal))[2],
'U' = prop.table(table(Fatal))[3],
n = n()) %>% arrange(desc(n))
# it does seem that the more officers there are, the higher the fatality rates, though n does decrease the more officers there are, so not sure if this info is reliable.
```
# Words Associated with Fatalities
Let's figure out what words are associated with fatalities by creating a simple function to tell me the proportion of fatalities associated with each word. Not perfect, since if a comma is attached to word, then it will count that as a different word. too lazy to debug
```{r fatalality propotion of words function}
fatal_words <- function(data){
# number of unique words
data$NatureOfStop <- as.character(data$NatureOfStop)
word_list <- tolower(unique(unlist(strsplit(data$NatureOfStop, ' '))))
# yes means fatal
yes_list <- rep(0, length(word_list))
no_list <- rep(0, length(word_list))
u_list <- rep(0, length(word_list))
'%ni%' <- Negate('%in%')
for(row in 1:nrow(data)){
para_temp <- strsplit(data[row,'NatureOfStop'], ' ')
temp <- c()
for(word in unlist(para_temp)){
word <- tolower(word)
word_i <- which(word_list == word)
if (word %ni% temp){
if(data[row,'Fatal'] == 'F'){
yes_list[word_i] <- yes_list[word_i] + 1
}
if(data[row,'Fatal'] == 'N')
{no_list[word_i] <- no_list[word_i] + 1
}
if(data[row,'Fatal'] == 'U'){
u_list[word_i] <- u_list[word_i] + 1
}
temp <- c(temp, word)
}
}
temp <- c()
}
data.frame(word = word_list,
fatalities = yes_list,
non_fatal = no_list)
}
fatal_prop <- fatal_words(data)
fatal_prop$non_fatal[which(fatal_prop$non_fatal==0)] <- 1
fatal_prop$prop <- unlist(fatal_prop[2]/fatal_prop[3])
fatal_prop_ordered <- fatal_prop[order(-fatal_prop$prop),]
```
```{r fatal indexes}
# index of killed
# going to change these indexes to fatal, because these words are associated with more fatalities
i <- which(!is.na(str_locate(final_test$Notes, 'kill'))[,1]==T)
i2 <- which(!is.na(str_locate(final_test$FullNarrative, 'kill'))[,1] == T)
i3 <- which(!is.na(str_locate(final_test$FullNarrative, 'decease')[,1]) == T)
i4 <- which(!is.na(str_locate(final_test$FullNarrative, 'pronounce')[,1]) == T)
i5 <- which(!is.na(str_locate(final_test$FullNarrative, 'die')[,1]) == T)
i6 <- which(!is.na(str_locate(final_test$FullNarrative, 'demise')[,1]) == T)
i7 <- which(!is.na(str_locate(final_test$FullNarrative, 'succumb')[,1]) == T)
i8 <- which(!is.na(str_locate(final_test$FullNarrative, 'toxic')[,1]) == T)
i9 <- which(!is.na(str_locate(final_test$FullNarrative, 'mortal')[,1]) == T)
i10 <- which(!is.na(str_locate(final_test$FullNarrative, 'fatal')[,1]) == T)[-c(6:9)]
i11 <- which(!is.na(str_locate(final_test$FullNarrative, 'Fatal')[,1]) == T)
i12 <- which(!is.na(str_locate(final_test$Notes, 'Death')[,1]) == T)
i13 <- which(!is.na(str_locate(final_test$NatureOfStop, 'Mental')[,1])==T)
i14 <- which(!is.na(str_locate(final_test$FullNarrative, 'mortal')[,1]) == T)
i15 <- c(1285,417,419, 423, 424, 425, 426,433,1027,131,134,
145,146,195,273,375,1219,1221,1345,1348,1349,1354,1357,1361,1333, 232,258,
510,1359,88,1334,1322,1324,1326,1328,1330,576,1190)
i16 <- which(!is.na(str_locate(final_test$Notes, 'fatal')[,1]) == T)
i_names <- c(268,206,207,202,224,267,123,112,254,124,269,187,261,115,196,241,180,132,150,
867,875,869,870,104,188,646,647,648,650,652,657,662,663)
fatal_index <- unique(c(i,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i_names))
fatal_ids <- final_test[fatal_index, 'id']
# nonfatals
non_i1 <- which(!is.na(str_locate(final_test$FullNarrative, 'Non-Fatal')[,1]) == T)
non_i2 <- which(!is.na(str_locate(final_test$FullNarrative, 'Non-fatal')[,1]) == T)
non_i3 <- which(!is.na(str_locate(final_test$Notes, 'non')[,1]) == T)[-c(2,8,10)]
non_i4 <- which(!is.na(str_locate(final_test$Notes, 'No')[,1]) == T)[-c(172,173)]
non_i5 <- which(!is.na(str_locate(final_test$Notes, 'NO')[,1]) == T)[-c(1,2,79,80,81)]
non_i6 <- which(!is.na(str_locate(final_test$Notes, 'no hits')[,1]) == T)[-5]
non_i7 <- which(!is.na(str_locate(final_test$FullNarrative, 'No hits')[,1]) == T)
non_fatal_index <- unique(c(556,63,1241,1193,1225, 1361,496,86,422,428,
429,431,432,490,1217,1218,
1222, 1224, 1225, 1226, 1227,1346, 7,10,14,274, 1076,1104,1112,
1125,1143, 1272,1380,1385, 281, 282,507,503,506,1170,1177,83,848,1325,
60,63, 1321,1194,570,550,1275,1276,1291,1271,1272,484,330,849,
829, 1198,1320,581,546, 259,317,481,485,511,513,539,559,560,563,564,574,
651,655,660,666,
non_i1, non_i2, non_i3,non_i4, non_i5, non_i6, non_i7))
non_fatal_ids <- final_test[non_fatal_index,'id']
# table(data[!is.na(str_locate(data$NatureOfStop, 'Critical')[,1]),]$Fatal)
# View(final_test[which(!is.na(str_locate(final_test$FullNarrative, 'arrest')[,1]) == T),])
#
# View(data[which(!is.na(str_locate(data$FullNarrative, 'justif')[,1]) == T),])
# View(data[which(!is.na(str_locate(data$NatureOfStop, 'health')[,1]) == T),])
# View(data[data$Fatal=='F',])
#
# View(data[which(!is.na(str_locate(data$NatureOfStop, 'Homicide')[,1]) == T),])
# View(data[which(!is.na(str_locate(data$FullNarrative, 'no hits')[,1]) == T),])
# View(data[which(!is.na(str_locate(data$Notes, 'Employee')[,1]) == T),])
#
# View(final_test[which(!is.na(str_locate(final_test$NatureOfStop, 'http')[,1]) == T),])
# View(final_test[which(!is.na(str_locate(final_test$FullNarrative, 'no hits')[,1]) == T),])
# View(final_test[which(!is.na(str_locate(final_test$Notes, 'no ')[,1]) == T),])
# reading some of the actual narratives to confirm fatality
# View(final_test[i11,])
```
I will follow this unorthodox approach, based on my logic and intuition.
Just by simply replacing these "fatal indexes" of a "just say no" method, we increased our classification rate from 67% to 80%. This suggests that most of these indexes are, in fact, fatal indexes. I think that this is very, very valuable information to take advantage of when modeling. We will now create models and test them on the actual testing dataset. **For the model to be adequate, it must have a good CV test MSE estimate and it must satisfy these 3 conditions:**
1) It must have a good CV test MSE.
2) It must classify most of these so called "fatal indexes" as "F" rather than "N"
- My logic is that we already know that these indexes are truly "F" observations. Therefore, if the model classifies most of these as "F", then it would suggest that the model is indeed correctly classifying the observations.
3) Its classification proportion must be similar to the training Fatal proportion of F/N.
# Data Preprocessing
For this preliminary analysis, I will not be considering rows where Fatal == 'U'. Maybe later I'll do some sort of imputation
```{r}
rm_cols <- which(names(data)=='FullNarrative' | names(data)=='Notes')
data2 <- data[, -rm_cols] # these 3 commands to start for cleaning training data
data_noU <- data2[which(data2$Fatal!='U'),]
data_noU$Fatal <- as.factor(as.character(data_noU$Fatal))
# data2 <- final_test[,-c(12,14)] # these 2 commands to start for cleaning the testing data
# data_noU <- data2
# Let's see if different months are associated with higher fatality rates
library(lubridate)
Month <- month(data_noU$Date)
data_noU$Month <- as.character(Month)
# data_noU %>% group_by(Month) %>% summarise('F' = prop.table(table(Fatal))[1],
# 'N' = prop.table(table(Fatal))[2],
# 'n' = n()) %>% arrange(desc(n))
# appears that July has the lowest fatality rates
# Is the day of the week associated with higher fatality rates?
Day <- wday(data_noU$Date)
data_noU$Day <- as.character(Day)
# data_noU %>% group_by(Day) %>% summarise('F' = prop.table(table(Fatal))[1],
# 'N' = prop.table(table(Fatal))[2],
# 'n' = n()) %>% arrange(desc(n))
# Nope, doesn't seem to have much correlation.
```
Let's try to incorporate some outside info
```{r}
library(readr)
city_info <- read_delim("Desktop/Untitled.txt",
"\t", escape_double = FALSE, col_names = FALSE,
trim_ws = TRUE)
names(city_info) <- c('rank2016', 'City', 'state', 'estimate2016', 'census2010',
'change', 'sq_mi', 'sq_km', 'pop_density1', 'pop_density2', 'location')
# remove whitespace and make lowercase
data_noU$City <- tolower(apply(as.matrix(data_noU$City), 2,function(x)gsub('\\s+', '',x)))
# do the same for the external data
city_info$City <- tolower(apply(as.matrix(city_info$City), 2,function(x)gsub('\\s+', '',x)))
city_info$City <- str_extract(city_info$City, '[a-z.]*')
# cities that are in the dataset but not in external data
unique(data_noU$City[!data_noU$City %in% city_info$City])
# fixing these names
data_noU[which(data_noU$City == 'charlottemecklenburg'), 'City'] <- 'charlotte'
data_noU[which(data_noU$City == 'miamidade'), 'City'] <- 'miami'
data_noU[which(data_noU$City == 'cityofmiami'), 'City'] <- 'miami'
data_noU[which(data_noU$City == 'baltimorecity'), 'City'] <- 'baltimore'
data_noU[which(data_noU$City == 'baltimorecounty'), 'City'] <- 'baltimore'
data_noU[which(data_noU$City == 'washingtondc'), 'City'] <- 'washington'
data_noU[which(data_noU$City == 'dekalbcounty'), 'City'] <- 'atlanta'
data_noU[which(data_noU$City == 'princegeorgescounty'), 'City'] <- 'washington'
data_noU[which(data_noU$City == 'fairfaxcounty'), 'City'] <- 'washington'
merged <- merge(x=data_noU, y=city_info, by='City')
merged <- merged[!duplicated(merged$id),]
# let's clean up the variables
merged$pop_density1 <- as.numeric(gsub('\\D+','', merged$pop_density1))
merged$pop_density2 <- as.numeric(gsub('\\D+','', merged$pop_density2))
merged$sq_mi <- as.numeric(gsub('\\D+','', merged$sq_mi))
merged$sq_km <- as.numeric(gsub('\\D+','', merged$sq_km))
merged$change <- as.numeric(gsub('\\D+','', merged$change))
# categorize population densities
merged$pop_density_size <- as.factor(findInterval(merged$pop_density1, c(0,4000,10000)))
```
Let's fix the age variable and month/day and also assigning NA's as 0
```{r}
# assigning 0 to NA in order to keep the observations because model.matrix() removes all rows with NA's
merged$NewAge <- as.numeric(str_extract(merged$SubjectAge, '[0-9]+'))
merged$AgeGroup <- findInterval(merged$NewAge, c(0, 20, 40, 60, 80))
merged[is.na(merged$AgeGroup), 'AgeGroup'] <- 0
merged$AgeGroup <- as.numeric(merged$AgeGroup)
merged[is.na(merged$Month), 'Month'] <- 0
merged$Month <- as.factor(merged$Month)
merged[is.na(merged$Day), 'Day'] <- 0
merged$Day <- as.factor(merged$Day)
merged$Seasons<- as.factor(findInterval(merged$Month, c(2,5,9,12)))
merged$Weekend <- ifelse(merged$Day == 1 | merged$Day == 7, 1, 0)
# assigning NA from number of officers to 1 (the mode)
merged[is.na(merged$NumberOfOfficers), 'NumberOfOfficers'] <- 1
merged$MoreThanOneOfficer <- ifelse(merged$NumberOfOfficers >1, 1, 0)
# fixing subject armed; changing NA to 'U'
merged[is.na(merged$SubjectArmed),'SubjectArmed'] <- 'U'
# fixing subject gender, since such high proportion of males, i'm gonna assign all unknowns to M; didn't work so gonna try assigning non males as females
merged[which(merged$SubjectGender=='N/A'), 'SubjectGender'] <- 'U'
merged$SubjectGender <- as.factor(as.character(merged$SubjectGender))
#merged[which(merged$SubjectGender != 'F'), 'SubjectGender'] <- 'M'
# remove factors of gender
#merged$SubjectGender <- as.character(merged$SubjectGender)
# fixing NumberOfShots
# first, assign all NA's to one
merged$NumberOfShots <- as.character(merged$NumberOfShots)
merged[is.na(merged$NumberOfShots), 'NumberOfShots'] <- 1
shots_list <- str_extract_all(merged$NumberOfShots, '[0-9]+')
shots_clean <- c()
for(i in 1:length(shots_list)){
shots_clean[i] <- sum(as.numeric(unlist(shots_list[i])))
}
merged$ShotsClean <- shots_clean
merged[which(merged$ShotsClean==0), 'ShotsClean'] <- 1
merged$MoreThanOneShot <- ifelse(merged$ShotsClean > 1, 1, 0)
# getting the mode of the officer race
Mode = function(x){
x2 <- strsplit(as.character(x), ';')
ta = table(x2)
tam = max(ta)
names(ta[which(ta ==tam)])[1]
}
main_race <- c()
merged[is.na(merged$OfficerRace), 'OfficerRace'] <- 'U'
for(i in 1:length(merged$OfficerRace)){
main_race[i] <- Mode(merged$OfficerRace[i])
}
merged$mainOfficerRace <- as.factor(substr(main_race,1,1))
# getting mode of officer gender
main_gender <- c()
for(i in 1:length(merged$OfficerGender)){
main_gender[i] <- Mode(merged$OfficerGender[i])
}
merged$mainOfficerGender<- as.factor(substr(main_gender,1,1))
# if there is a white officer or not?
not_W_index <- is.na(str_locate(merged$OfficerRace, 'W'))[,1]
merged$OfficerWhite <- 'W'
merged[not_W_index, 'OfficerWhite'] <- 'NW'
merged$OfficerWhite <- as.factor(merged$OfficerWhite)
# Look at various proportions
# merged %>% group_by(ShotsClean) %>% summarise('F' = prop.table(table(Fatal))[1],
# 'N' = prop.table(table(Fatal))[2],
# 'n' = n()) %>% arrange(desc(n))
```
Function for merging and cleaning the dataset
```{r}
clean_up <- function(data, includeNA = F){
#rm_cols <- which(names(data)=='FullNarrative' | names(data)=='Notes')
data2 <- data
data_noU <- data2
if('Fatal' %in% names(data) & includeNA == F){
data_noU <- data2[which(data2$Fatal!='U'),]
data_noU$Fatal <- as.factor(as.character(data_noU$Fatal))
}
library(lubridate)
Month <- month(data_noU$Date)
data_noU$Month <- as.character(Month)
Day <- wday(data_noU$Date)
data_noU$Day <- as.character(Day)
Year <- year(data_noU$Date)
data_noU$Year <- as.character(Year)
library(readr)
city_info <- read_delim("Desktop/Untitled.txt",
"\t", escape_double = FALSE, col_names = FALSE,
trim_ws = TRUE)
names(city_info) <- c('rank2016', 'City', 'state', 'estimate2016', 'census2010',
'change', 'sq_mi', 'sq_km', 'pop_density1', 'pop_density2', 'location')
# remove whitespace and make lowercase
data_noU$City <- tolower(apply(as.matrix(data_noU$City), 2,function(x)gsub('\\s+', '',x)))
# do the same for the external data
city_info$City <- tolower(apply(as.matrix(city_info$City), 2,function(x)gsub('\\s+', '',x)))
city_info$City <- str_extract(city_info$City, '[a-z.]*')
# cities that are in the dataset but not in external data
unique(data_noU$City[!data_noU$City %in% city_info$City])
# fixing these names
data_noU[which(data_noU$City == 'charlottemecklenburg'), 'City'] <- 'charlotte'
data_noU[which(data_noU$City == 'miamidade'), 'City'] <- 'miami'
data_noU[which(data_noU$City == 'cityofmiami'), 'City'] <- 'miami'
data_noU[which(data_noU$City == 'baltimorecity'), 'City'] <- 'baltimore'
data_noU[which(data_noU$City == 'baltimorecounty'), 'City'] <- 'baltimore'
data_noU[which(data_noU$City == 'washingtondc'), 'City'] <- 'washington'
data_noU[which(data_noU$City == 'dekalbcounty'), 'City'] <- 'atlanta'
data_noU[which(data_noU$City == 'princegeorgescounty'), 'City'] <- 'washington'
data_noU[which(data_noU$City == 'fairfaxcounty'), 'City'] <- 'washington'
library(readxl)
city_med_income <- read_excel("Desktop/city_med_income.xlsx")
merged <- merge(x=data_noU, y=city_info, by='City')
merged <- merge(x=merged, y = city_med_income, by = 'City')
merged <- merged[!duplicated(merged$id),]
# let's clean up the variables
merged$pop_density1 <- as.numeric(gsub('\\D+','', merged$pop_density1))
merged$pop_density2 <- as.numeric(gsub('\\D+','', merged$pop_density2))
merged$sq_mi <- as.numeric(gsub('\\D+','', merged$sq_mi))
merged$sq_km <- as.numeric(gsub('\\D+','', merged$sq_km))
merged$change <- as.numeric(gsub('\\D+','', merged$change))
merged$state <- tolower(apply(as.matrix(merged$state), 2,function(x)gsub('\\s+', '',x)))
# categorize population densities
merged$pop_density_size <- as.factor(findInterval(merged$pop_density1,
c(0,2000,4000,6000,8000,10000)))
east_coast <- c('maine','newhampshire', 'massachusetts', 'rhodeisland', 'connecticut', 'newyork', 'newjersey', 'delaware','maryland','virginia','northcarolina','southcarolina','georgia','florida')
west_coast <- c('california','oregon','washington')
merged$coast <- 'neither'
merged$coast[which(merged$state %in% east_coast)] <- 'east'
merged$coast[which(merged$state %in% west_coast)] <- 'west'
merged$coast <- as.factor(merged$coast)
# assigning 0 to NA in order to keep the observations because model.matrix() removes all rows with NA's
merged$NewAge <- as.numeric(str_extract(merged$SubjectAge, '[0-9]+'))
merged[is.na(merged$NewAge), 'NewAge'] <- 0
#merged$AgeGroup <- findInterval(merged$NewAge, c(0, 20, 40, 60, 80))
#merged$AgeGroup <- findInterval(merged$NewAge, c(0, 10, 20,30, 40,50, 60,70, 80))
merged$AgeGroup <- findInterval(merged$NewAge, seq(0,80,5))
merged[which(merged$NewAge == 0), 'AgeGroup'] <- 0
merged$AgeGroup <- as.numeric(as.numeric(merged$AgeGroup))
merged[is.na(merged$Month), 'Month'] <- 0
merged$Month <- as.factor(merged$Month)
merged[is.na(merged$Day), 'Day'] <- 0
merged$Day <- as.factor(merged$Day)
merged[is.na(merged$Year), 'Year'] <- 0
# change 1905 to 2016
merged[merged$Year == '1905', 'Year'] <- 2016
merged$Year <- as.factor(merged$Year)
merged$Seasons<- as.factor(findInterval(merged$Month, c(2,5,9,12)))
merged$Weekend <- ifelse(merged$Day == 1 | merged$Day == 7, 1, 0)
# assigning NA from number of officers to 1 (the mode)
merged[is.na(merged$NumberOfOfficers), 'NumberOfOfficers'] <- 1
merged$MoreThanOneOfficer <- ifelse(merged$NumberOfOfficers >1, 1, 0)
# fixing subject armed; changing NA to 'U'
merged[is.na(merged$SubjectArmed),'SubjectArmed'] <- 'U'
# fixing subject gender, since such high proportion of males, i'm gonna assign all unknowns to M; didn't work so gonna try assigning non males as females
merged[which(merged$SubjectGender=='N/A'), 'SubjectGender'] <- 'U'
merged$SubjectGender <- as.factor(substr(as.character(merged$SubjectGender),1,1))
#merged[which(merged$SubjectGender != 'F'), 'SubjectGender'] <- 'M'
# remove factors of gender
#merged$SubjectGender <- as.character(merged$SubjectGender)
merged[is.na(merged$SubjectRace), 'SubjectRace'] <- 'U'
# fixing NumberOfShots
# first, assign all NA's to one
merged$NumberOfShots <- as.character(merged$NumberOfShots)
merged[is.na(merged$NumberOfShots), 'NumberOfShots'] <- 1
shots_list <- str_extract_all(merged$NumberOfShots, '[0-9]+')
shots_clean <- c()
for(i in 1:length(shots_list)){
shots_clean[i] <- sum(as.numeric(unlist(shots_list[i])))
}
merged$ShotsClean <- shots_clean
merged[which(merged$ShotsClean==0), 'ShotsClean'] <- 1
merged$MoreThanOneShot <- ifelse(merged$ShotsClean > 1, 1, 0)
merged[which(merged$ShotsClean>100), 'ShotsClean'] <- 45
Mode = function(x){
x2 <- strsplit(as.character(x), ';')
ta = table(x2)
tam = max(ta)
names(ta[which(ta ==tam)])[1]
}
main_race <- c()
merged[is.na(merged$OfficerRace), 'OfficerRace'] <- 'U'
for(i in 1:length(merged$OfficerRace)){
main_race[i] <- Mode(merged$OfficerRace[i])
}
merged$mainOfficerRace <- as.factor(tolower(substr(main_race,1,1)))
merged$mainOfficerRace <- as.character(merged$mainOfficerRace)
# merging race n and o into u
merged[which(merged$mainOfficerRace=='m' |
merged$mainOfficerRace=='n' |
merged$mainOfficerRace=='o'), 'mainOfficerRace'] <- 'u'
merged$mainOfficerRace <- as.factor(merged$mainOfficerRace)
# getting mode of officer gender
main_gender <- c()
for(i in 1:length(merged$OfficerGender)){
main_gender[i] <- Mode(merged$OfficerGender[i])
}
merged$mainOfficerGender<- as.factor(substr(main_gender,1,1))
merged[which(merged$mainOfficerGender=='N'), 'mainOfficerGender'] <- 'M'
merged$mainOfficerGender <- droplevels(merged$mainOfficerGender)
#######################################################################################
# extra stuff not sure if useful
merged$log_new_age <- log(merged$NewAge)
merged[merged$log_new_age==-Inf, 'log_new_age'] <- 0
merged$log_income <- log(merged$income)
merged$scaled_log_new_age <- scale(merged$log_new_age)
merged$scaled_log_income <- scale(merged$log_income)
merged$scaled_pop <- scale(merged$estimate2016)
lat <- str_extract_all(merged$location, '[0-9.]+', simplify=TRUE)[,2]
long <- str_extract_all(merged$location, '[0-9.]+', simplify=TRUE)[,1]
merged$lat <- as.numeric(lat)
merged$long <- as.numeric(long)
state_fatal_prop <- read.csv('~/dropbox/stats-101c-kaggle/state_fatal_prop.csv')
city_fatal_prop <- read.csv('~/dropbox/stats-101c-kaggle/city_fatal_prop.csv')
merged <- merge(merged, state_fatal_prop, by = 'state', all.x=TRUE )
merged <- merge(merged, city_fatal_prop, by = 'City', all.x=TRUE)
merged[is.na(merged$state_fatal_prop), 'state_fatal_prop'] <- mean(merged$state_fatal_prop, na.rm=T)
merged[is.na(merged$city_fatal_prop), 'city_fatal_prop'] <- mean(merged$city_fatal_prop, na.rm=T)
# merged$subject_minority<- as.factor(ifelse(merged$SubjectRace == 'W' |
# merged$SubjectRace == 'U', 'No', 'Yes'))
# state_gini<- read.csv('~/dropbox/stats-101c-kaggle/Datasets/Gini_State.csv')[,-1]
# merged <- merge(merged, state_gini)
#
carry_permit <- read.csv('~/dropbox/stats-101c-kaggle/Datasets/CarryPermitRight.csv')
merged <- merge(merged, carry_permit)
merged$narrativeNA <- as.factor(ifelse(is.na(merged$FullNarrative), 1,0))
merged$notesNA <- as.factor(ifelse(is.na(merged$Notes),1,0))
return(merged)
}
```
# IMPUTATIONS
Function for imputing datasets
```{r imputation function}
impute_clean_up <- function(data, includeF = TRUE){
#######################################################
#### IMPUTATIONS ####
library(missForest)
if('Fatal' %in% names(data) & includeF==TRUE){
data[which(data$Fatal=='U'), 'Fatal'] <- NA
data$Fatal <- droplevels(data$Fatal)
}
# imputing Fatal does help with accuracy!
data[which(data$SubjectArmed=='U'), 'SubjectArmed'] <- NA
data$SubjectArmed <- droplevels(data$SubjectArmed)
data[which(data$SubjectRace=='U'), 'SubjectRace'] <- NA
data$SubjectRace <- droplevels(data$SubjectRace)
data[which(data$SubjectGender=='U'), 'SubjectGender'] <- NA
data$SubjectGender <- droplevels(data$SubjectGender)
data[which(data$mainOfficerRace=='u'), 'mainOfficerRace'] <- NA
data$mainOfficerRace <- droplevels(data$mainOfficerRace)
data[which(data$mainOfficerGender=='U'), 'mainOfficerGender'] <- NA
data$mainOfficerGender <- droplevels(data$mainOfficerGender)
data[which(data$NewAge == 0), 'NewAge'] <- NA
data[which(data$Month=='0'), 'Month'] <- NA
data$Month <- droplevels(data$Month)
data[which(data$Day=='0'), 'Day'] <- NA
data$Day <- droplevels(data$Day)
data$state <- as.factor(data$state)
# imputing age group doesn't help
# getting improvements if imputing fatal, subjectarmed, subjectrace, subjectgender at same time
# wow, imputing age and DRASTIC improvement in validation set classification rate (~10%); for training dataset
if('Fatal' %in% names(data) & includeF==T){
impute_variables <- c('Fatal','SubjectArmed', 'SubjectRace', 'SubjectGender', 'NewAge',
'mainOfficerRace','mainOfficerGender', 'Month', 'Day')
}
else{
impute_variables <- c('SubjectArmed', 'SubjectRace', 'SubjectGender', 'NewAge',
'mainOfficerRace','mainOfficerGender', 'Month', 'Day')
}
mforest <- missForest(xmis = data[,impute_variables],maxiter=10, ntree=100, parallelize = "no")
data[,impute_variables] <- mforest$ximp
return.obj <- list(data, mforest)
return(return.obj)
}
```
```{r trying out imputation}
# try imputing the datasets together and then separate out?
train <- clean_up(data)
test <- clean_up(final_test)
set.seed(seed)
# imputing together ########################################################################
combined <- rbind(train[,-4], test)
combined_imputed <- impute_clean_up(combined)
combined_imputed[[2]]$OOBerror
# NRMSE PFC
# 0.7225091757 0.2146977619
training <- cbind(Fatal = train$Fatal, combined_imputed[[1]][1:2811,])
###############
training <- impute_clean_up(train, includeF=T) # imputing without fatal
training[[2]]
training1 <- cbind(Fatal = train$Fatal, training[[1]])
# don't use this method below
#############################################################################################
# impute on training first, then combine with testing, then impute together, then remove testing
# imputing train alone
set.seed(seed)
training <- impute_clean_up(train)
training[2]
# $OOBerror
# NRMSE PFC
# 0.7028000678 0.1995952690 probably varies depending on seed set
# imputing test alone
testing <- impute_clean_up(test)
testing[2]
# $OOBerror
# NRMSE PFC
# 0.7374443591 0.2262075573 maybe this higher error explains why imputing only on testing is bad
# combining the imputed training with the unimputed testing and then imputing (basically using the information gained from imputing training to help impute testing)
set.seed(seed)
combined2 <- rbind(training[[1]][,-4], test)
combined2_imputed <- impute_clean_up(combined2)
combined2_imputed[2] #OOB error
# $OOBerror
# NRMSE PFC
# 0.6673021616 0.1589161743
# use this imputed test dataset to make predictions
test.imputed.with.training <- combined2_imputed[[1]][2812:4211,]
```
```{r imputing with different target values; DOES NOT WORK}
# to compare imputation accuracy with different target variables, so that see if can extend to the unlabeled testing dataset
train <- clean_up(data)
train_all_yes <- train
train_all_yes$Fatal <- 'F'
train_all_yes$Fatal <- as.factor(train_all_yes$Fatal)
train_all_no <- train
train_all_no$Fatal <- 'N'
train_all_no$Fatal <- as.factor(train_all_no$Fatal)
training <- impute_clean_up(train)
training[[2]] # increases predictive power
training.all.yes <- impute_clean_up(train_all_yes)
training.all.yes[[2]]
training.all.yes[[1]]$Fatal <- train$Fatal # now replacing with the true values to check accuracy
# not good
training.all.no <- impute_clean_up(train_all_no)
training.all.no[[2]]
training.all.no[[1]]$Fatal <- train$Fatal
# randomly assign 66% no, 34% yes
n <- nrow(train)
yes_index <- sample(1:n, .33*n)
train_mixed <- train
train_mixed[yes_index,'Fatal'] <- 'F'
train_mixed[-yes_index,'Fatal'] <- 'N'
training_mixed <- impute_clean_up(train_mixed)
training_mixed[[2]]
training_mixed[[1]]$Fatal <- train$Fatal
# try predicting the variables using trained model, and then using those labels to help imputations?
train_predicted <- train_data
train_predicted$Fatal <- rF_mod$predicted
train_predicted_impute <- impute_clean_up(train_predicted)
train_predicted_impute[[2]]
train_predicted_impute[[1]]$Fatal <- train_data$Fatal
```
This may have potential?
```{r imputing unlabeled classes with help from training data 5/26/18}
# essentially using the labeled data from training to help with imputation
train <- clean_up(data)
test <- clean_up(final_test)
test$Fatal <- NA
test$Fatal <- as.character(test$Fatal)
test[fatal_index, 'Fatal'] <- 'F'
test[c(556,63,1241,1193,1225), 'Fatal'] <- 'N'
test$Fatal <- as.factor(test$Fatal)
set.seed(seed)
# imputing together ########################################################################
combined <- rbind(train, test)
combined_imputed <- impute_clean_up(combined)
combined_imputed[[2]]$OOBerror
training <- combined_imputed[[1]][1:2811,]
testing <- combined_imputed[[1]][2812:4211,-4]
testing_w_label <- combined_imputed[[1]][2812:4211,]
```
```{r}
train <- clean_up(data)
training <- impute_clean_up(train)
```
# Model Building
```{r}
# SubjectArmed makes predictions worse... also day and month..
# AVAILABLE VARIABLES TO CHOOSE FROM:
# SubjectRace, SubjectGender, AgeGroup, NumberOfOfficers, Month, Day, SubjectArmed, MoreThanOneOfficer,
# ShotsClean, MoreThanOneShot, OfficerWhite, mainOfficerRace, mainOfficerGender
# Based on trial and error, AgeGroup, subjectrace seems to help prediction...
# NumberOfOfficers predicts better than MoreThanOneOfficer
set.seed(seed)
training <- clean_up(data, includeNA=F)
write.csv(training, '~/dropbox/stats-101c-kaggle/Datasets/cleaned_merged2.csv', row.names=F)
testing <- clean_up(final_test)
write.csv(testing, '~/dropbox/stats-101c-kaggle/Datasets/test_clean_merged.csv', row.names=F)
############################################################################################
# crime rate by state
crime.rates <- read.delim("~/Desktop/crime rates.txt", header=FALSE)
names(crime.rates) <- c('state', 'city', 'population', 'total.violent.crimes', 'murder', 'rape', 'robbery', 'assault', 'total_property', 'burglary', 'theft', 'motor.theft', 'arson')
crime.rates$state <- tolower(apply(as.matrix(crime.rates$state), 2,function(x)gsub('\\s+', '',x)))
crime.rates <- crime.rates[,-2]
merged <- merge(x=training, y=crime.rates, by = 'state')
merged <- merged[!duplicated(merged$id),]
merged$violent_crime_category <- findInterval(merged$total.violent.crimes, c(0,200,600,2000))
merged$total_property_cat <- findInterval(merged$total_property, c(0,3000,5000,6500,8000))
merged$population2 <- as.numeric(gsub('\\D+','', merged$population))
merged$ratio <- merged$total.violent.crimes/(merged$total.violent.crimes + merged$total_property)
merged$total_crime <- merged$total.violent.crimes + merged$total_property
merged$total_crime_cat <- as.factor(findInterval(merged$total_crime, c(2000,4500,6500,8000)))
write.csv(merged, '~/dropbox/stats-101c-kaggle/Datasets/crime_merged1.csv', row.names=F)
############################################################################################
# race proportions by state
race_prop <- read.csv("~/Desktop/raw_data.csv", header=T)
race_prop <- race_prop[2:52,]
race_prop$state <- tolower(apply(as.matrix(race_prop$Location), 2,function(x)gsub('\\s+', '',x)))
race_prop$Asian <- as.character(race_prop$Asian)
race_prop[which(race_prop$Asian == 'N/A'), 'Asian'] <- '0'
race_prop$Asian <- as.numeric(race_prop$Asian)
merged <- merge(x=training, y=race_prop, by='state')
merged <- merged[!duplicated(merged$id),]
############################################################################################
```
```{r}
# training the model and then using the model to predict the 'U'
# training_U <-training[training$Fatal =='U',]
# training_U_predicted <- predict(rF_mod, newdata= training_U)
# table(training_U_predicted)
#
# training_U_forMerging <- training_U %>% select(-Fatal)
# training_U_forMerging$Fatal <- training_U_predicted
#
# merged_trainTest_U <- rbind(training_w_testing, training_U_forMerging)
write.csv(training_w_testing, '~/dropbox/stats-101c-kaggle/Datasets/kenny_training_w_testing.csv', row.names=F)
training <- clean_up(data, includeNA=T)
testing <- clean_up(final_test)
testing$Fatal <- 'U'
rownames(testing) <- 1:1400
i_fatal <- as.numeric(rownames(subset(testing, id %in% fatal_ids)))
i_nonFatal <- as.numeric(rownames(subset(testing, id %in% non_fatal_ids)))
testing[i_fatal, 'Fatal'] <- 'F'
testing[i_nonFatal, 'Fatal'] <- 'N'
testing_F <- testing[testing$Fatal == 'F',]
testing_NF <- testing[testing$Fatal == 'N',]
testing_U <- testing[testing$Fatal=='U', ]
training_w_testing <- training
training_w_testing <- rbind(training, testing_F)
training_w_testing <- rbind(training_w_testing, testing_NF)
# NewAgeCategories <- ifelse(training_w_testing$NewAge >=30, 'Yes', 'No')
# NewAgeCategories[training_w_testing$NewAge==0] <- 'Unknown'
prop.table(table(predict(rF_mod, testing_F)))
prop.table(table(predict(rF_mod, testing_NF)))
p <- predict(xgb, model.matrix(Fatal ~., testing_F[,features])[,-1], type='class')
prop.table(table(ifelse(p>0.5, 'F', 'N')))
features = c('Fatal','NewAge', 'estimate2016',
'income', 'SubjectRace', 'ShotsClean', 'Year', 'Month',
'Day', 'SubjectGender', 'SubjectArmed','NumberOfOfficers', 'mainOfficerRace',
'narrativeNA', 'notesNA', 'city_fatal_prop', 'state_fatal_prop')
train_data <- na.omit(training_w_testing[, features])
# average section
testing_X = train_data_test
testing_y = train_data_test$Fatal
p1 <- predict(rF_mod, testing_X, type= 'prob')[,1]
p2 <- predict(xgb, model.matrix(Fatal ~., testing_X[,features])[,-1])
p3 <- attr(predict(svm_mod, testing_X, probability = T),'probabilities')[,2]
p_df <- data.frame(p1,p2,p3)
p_mean <- apply(p_df,1, mean)
p_mean_class <- ifelse(p_mean >0.50, 'F', 'N')
table(p_mean_class, testing_y)
# intersection method
xgb_pred <- predict(xgb, model.matrix(Fatal ~., testing_X[,features])[,-1])
xgb_pred_class <- ifelse(xgb_pred >= 0.50, 'Yes', 'No')
rF_mod_pred_test <- predict(rF_mod, newdata = testing_X)
rF_mod_pred_test <- ifelse(rF_mod_pred_test == 'F', 'Yes', 'No')
test_df_xgb <- data.frame('id'= training_w_testing[-train_i,]$id,
'Fatal' = xgb_pred_class)
test_df_rF <- data.frame('id' = training_w_testing[-train_i,]$id,
'Fatal' = rF_mod_pred_test)
xgb_yes_rows <- which(test_df_xgb$Fatal=='Yes')
rF_yes_rows <- which(test_df_rF$Fatal=='Yes')
intersect_xgb_rF <- intersect(xgb_yes_rows, rF_yes_rows)
df_intersect <- rep('N', length(xgb_pred_class))
df_intersect[intersect_xgb_rF] <- 'F'
table(df_intersect, testing_y)
seed <- 123
n <- nrow(train_data)
set.seed(seed)
train_i <- sample(1:n,n*0.7, replace=F )
train_data_train <- train_data[train_i,]
train_data_test <- train_data[-train_i,]
train_X <- model.matrix(Fatal ~ ., data = train_data_train)[,-1]
test_X <- model.matrix(Fatal ~ ., data = train_data_test)[,-1]
prop.table(table(train_data_train$Fatal))
prop.table(table(train_data_test$Fatal))
```
random forest; optimizing hyperparameters
```{r}
# RANDOM FOREST
library(caret)
rf_trcontrol_1 <- trainControl(method="cv", number=5, verboseIter = T, returnResamp = 'all',
allowParallel = T, savePredictions = T, classProbs = T)
rf_grid_1 <- expand.grid(.mtry=c(2:10))
set.seed(seed)
rf_model2<-train(Fatal~.,data=train_data_train,
method="rf",
trControl=rf_trcontrol_1,
ntree=200,
tuneGrid = rf_grid_1,
prox=TRUE)
View(rf_model2$results)
library(randomForest)
set.seed(seed)
rF_mod <- randomForest(Fatal ~., data = train_data_train,ntree=500,
mtry=4, importance=T, nodesize=20)
rF_mod
rF_mod_pred_test <- predict(rF_mod, newdata = train_data_test)
table(rF_mod_pred_test, train_data_test$Fatal)
mean(rF_mod_pred_test == train_data_test$Fatal)
prop.table(table(rF_mod_pred_test))
importance(rF_mod)
```
other unoptimized methods learned in class (don't seem as useful)
```{r}
# LOGISTIC REGRESSION
lr <- glm(Fatal ~., train_data_train, family = 'binomial')
lr_pred <- predict(lr, newdata= train_data_test, type = 'response')
lr_pred_class <- ifelse(lr_pred >=0.5, 'N', 'F')
table(lr_pred_class, train_data_test$Fatal)
mean(lr_pred_class == train_data_test$Fatal)
# KNN
library(class)
grid_search_knn <- function(train_X, test_X, train_y, test_y, k=1:20){
test_MSE_L <- c()
for(i in k){
knn.mod <- knn(train_X, test_X, train_y, k=i)
test_MSE_L[i] <- mean(knn.mod == test_y)
}
test_MSE_L
}
knn_test_MSE <- grid_search_knn(train_X, test_X, train_data_train$Fatal, train_data_test$Fatal)
best_k <- which(knn_test_MSE == max(knn_test_MSE))
knn_mod <- knn(train_X, test_X, train_data_train$Fatal, k = best_k[1])
table(knn_mod, train_data_test$Fatal)
mean(knn_mod==train_data_test$Fatal)
# SVM
library(e1071)
svm_mod <- svm(train_X, label = train_data_train$response_dummy, scale = F, cost=20, kernel='linear')
svm_pred <- predict(svm_mod, test_X)
table(svm_pred, train_data_test$Fatal)
# doesn't seem to work well
# Neural Networks
library(neuralnet)
train_X_nn <- cbind(as.data.frame(train_X), Fatal = train_data_train$response_dummy)
n <- colnames(train_X_nn)
f <- as.formula(paste("Fatal ~", paste(n[!n %in% 'Fatal'], collapse = " + ")))
nn_mod <- neuralnet(f, data =train_X_nn, hidden = c(5,3), linear.output=T)
nn_results <- neuralnet::compute(nn_mod, test_X[,1:16])
nn_results_class <- ifelse(nn_results$net.result>= 0.5, 'F', 'N')
table(nn_results_class, train_data_test$Fatal)
mean(nn_results_class==train_data_test$Fatal)
```
svm optimizing hyperparameters
```{r}
library(caret)
library(e1071)
svm_trcontrol_1 <- trainControl(method="cv", number=5, verboseIter = T, returnResamp = 'all',
allowParallel = T, savePredictions = T, classProbs = T)
svm_grid_1 <- expand.grid(C=c(0.001,0.01, 0.1, 0.5, 1))
set.seed(seed)
svm_model<-train(x = train_X,
y=as.factor(train_data_train$Fatal),
method="svmLinear",
trControl=svm_trcontrol_1,
tuneGrid = svm_grid_1,
prox=TRUE)
View(svm_model$results)
svm_mod <- svm(Fatal ~ ., data = train_data_train, cost=10, gamma = 0.01, probability = T)
svm_mod_pred_test <- predict(svm_mod, newdata = train_data_test, probability = T)
table(svm_mod_pred_test, train_data_test$Fatal)
mean(svm_mod_pred_test == train_data_test$Fatal)
#################################################################################
svm_mod_tune <- tune(svm, Fatal ~., data=train_data_train, kernel='linear',
ranges=list(cost=c(0.001, 0.01, 0.1, 1, 5, 10, 100)))
summary(svm_mod_tune)
best_svm <- svm_mod_tune$best.model
best_svm_red <- predict(best_svm, newdata=train_data_test, probability=T)
```
xgboost; optimizing the hyperparameters
```{r}
library(caret)
# first gridsearch for best params
set.seed(seed)
xgb_grid_1 = expand.grid(
nrounds = seq(1,500,10),
eta = c(0.1,0.01),
max_depth = c(3:10),
gamma = 1,
colsample_bytree=1,
min_child_weight = 1,
subsample = seq(0.5,1,0.1)
)
xgb_trcontrol_1 = trainControl(
method = "cv",
number = 5,
verboseIter = TRUE,
returnData = FALSE,
returnResamp = 'all',
classProbs = TRUE,
#summaryFunction = twoClassSummary,
allowParallel = F,
savePredictions = TRUE
)
xgb_train_2 = train(
x = train_X,
y = as.factor(train_data_train$Fatal),
trControl = xgb_trcontrol_1,