-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.html
1701 lines (1663 loc) · 351 KB
/
main.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="Hazem Alabiad & Betül Bayrak" />
<title>cmp713-project</title>
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
// be compatible with the behavior of Pandoc < 2.8).
document.addEventListener('DOMContentLoaded', function(e) {
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
var i, h, a;
for (i = 0; i < hs.length; i++) {
h = hs[i];
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
a = h.attributes;
while (a.length > 0) h.removeAttribute(a[0].name);
}
});
</script>
<style type="text/css">
a.anchor-section {margin-left: 10px; visibility: hidden; color: inherit;}
a.anchor-section::before {content: '#';}
.hasAnchor:hover a.anchor-section {visibility: visible;}
</style>
<script>// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020.
document.addEventListener('DOMContentLoaded', function() {
// Do nothing if AnchorJS is used
if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) {
return;
}
const h = document.querySelectorAll('h1, h2, h3, h4, h5, h6');
// Do nothing if sections are already anchored
if (Array.from(h).some(x => x.classList.contains('hasAnchor'))) {
return null;
}
// Use section id when pandoc runs with --section-divs
const section_id = function(x) {
return ((x.classList.contains('section') || (x.tagName === 'SECTION'))
? x.id : '');
};
// Add anchors
h.forEach(function(x) {
const id = x.id || section_id(x.parentElement);
if (id === '') {
return null;
}
let anchor = document.createElement('a');
anchor.href = '#' + id;
anchor.classList = ['anchor-section'];
x.classList.add('hasAnchor');
x.appendChild(anchor);
});
});
</script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
for (var j = 0; j < rules.length; j++) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") continue;
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' && rule.style.backgroundColor === '') continue;
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<style type="text/css">body {
background-color: #fff;
margin: 1em auto;
max-width: 700px;
overflow: visible;
padding-left: 2em;
padding-right: 2em;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.35;
}
#TOC {
clear: both;
margin: 0 0 10px 10px;
padding: 4px;
width: 400px;
border: 1px solid #CCCCCC;
border-radius: 5px;
background-color: #f6f6f6;
font-size: 13px;
line-height: 1.3;
}
#TOC .toctitle {
font-weight: bold;
font-size: 15px;
margin-left: 5px;
}
#TOC ul {
padding-left: 40px;
margin-left: -1.5em;
margin-top: 5px;
margin-bottom: 5px;
}
#TOC ul ul {
margin-left: -2em;
}
#TOC li {
line-height: 16px;
}
table {
margin: 1em auto;
border-width: 1px;
border-color: #DDDDDD;
border-style: outset;
border-collapse: collapse;
}
table th {
border-width: 2px;
padding: 5px;
border-style: inset;
}
table td {
border-width: 1px;
border-style: inset;
line-height: 18px;
padding: 5px 5px;
}
table, table th, table td {
border-left-style: none;
border-right-style: none;
}
table thead, table tr.even {
background-color: #f7f7f7;
}
p {
margin: 0.5em 0;
}
blockquote {
background-color: #f6f6f6;
padding: 0.25em 0.75em;
}
hr {
border-style: solid;
border: none;
border-top: 1px solid #777;
margin: 28px 0;
}
dl {
margin-left: 0;
}
dl dd {
margin-bottom: 13px;
margin-left: 13px;
}
dl dt {
font-weight: bold;
}
ul {
margin-top: 0;
}
ul li {
list-style: circle outside;
}
ul ul {
margin-bottom: 0;
}
pre, code {
background-color: #f7f7f7;
border-radius: 3px;
color: #333;
white-space: pre-wrap;
}
pre {
border-radius: 3px;
margin: 5px 0px 10px 0px;
padding: 10px;
}
pre:not([class]) {
background-color: #f7f7f7;
}
code {
font-family: Consolas, Monaco, 'Courier New', monospace;
font-size: 85%;
}
p > code, li > code {
padding: 2px 0px;
}
div.figure {
text-align: center;
}
img {
background-color: #FFFFFF;
padding: 2px;
border: 1px solid #DDDDDD;
border-radius: 3px;
border: 1px solid #CCCCCC;
margin: 0 5px;
}
h1 {
margin-top: 0;
font-size: 35px;
line-height: 40px;
}
h2 {
border-bottom: 4px solid #f7f7f7;
padding-top: 10px;
padding-bottom: 2px;
font-size: 145%;
}
h3 {
border-bottom: 2px solid #f7f7f7;
padding-top: 10px;
font-size: 120%;
}
h4 {
border-bottom: 1px solid #f7f7f7;
margin-left: 8px;
font-size: 105%;
}
h5, h6 {
border-bottom: 1px solid #ccc;
font-size: 105%;
}
a {
color: #0033dd;
text-decoration: none;
}
a:hover {
color: #6666ff; }
a:visited {
color: #800080; }
a:visited:hover {
color: #BB00BB; }
a[href^="http:"] {
text-decoration: underline; }
a[href^="https:"] {
text-decoration: underline; }
code > span.kw { color: #555; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #d14; }
code > span.fl { color: #d14; }
code > span.ch { color: #d14; }
code > span.st { color: #d14; }
code > span.co { color: #888888; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #900; font-weight: bold; }
code > span.er { color: #a61717; background-color: #e3d2d2; }
</style>
</head>
<body>
<h1 class="title toc-ignore">cmp713-project</h1>
<h4 class="author">Hazem Alabiad & Betül Bayrak</h4>
<h4 class="date">25/01/2021</h4>
<div id="introduction" class="section level1">
<h1>Introduction</h1>
<p>Tipping someone is a common thing in our community, especially if this person is a taxi driver, waiter, or delivery officer; The tip amount varies depending on many conditions; In the case of a taxi driver, it might depend on the passenger count, trip distance, trip hour, et cetera. In this project, we aim to study and predict the tip percentage of the total amount paid to taxi drivers. We used the data provided by NYC Taxi & Limousine Commission (TLC) from this <a href="https://www1.nyc.gov/site/tlc/about/tlc-trip-record-data.page">link</a>. First, we explore our data to know what our data looks like, find potential outliers, obtain general and basic statistics, for instance, observations count, variables count, NAs’ count, et cetera. Second, we apply what is called data pre-processing and data engineering that include removing unnecessary or NAs’ rows, columns, convert the data type to a more suitable one and introducing new variables to get our data ready to be fed our model that is going to predict the tip amount given other variables. Third, we build our models that utilize the pre-processed, clean data to predict a variable “tip percentage in our case”. Lastly, we measure the performance or accuracy of our model using many approaches and mathematical formulas and plot the predictions vs. actual tip percentage to decide on which model did the best.</p>
</div>
<div id="loading-libraries" class="section level1">
<h1>Loading Libraries</h1>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(randomForest)</span></code></pre></div>
<pre><code>## randomForest 4.6-14</code></pre>
<pre><code>## Type rfNews() to see new features/changes/bug fixes.</code></pre>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggmap)</span></code></pre></div>
<pre><code>## Loading required package: ggplot2</code></pre>
<pre><code>##
## Attaching package: 'ggplot2'</code></pre>
<pre><code>## The following object is masked from 'package:randomForest':
##
## margin</code></pre>
<pre><code>## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.</code></pre>
<pre><code>## Please cite ggmap if you use it! See citation("ggmap") for details.</code></pre>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(h2o)</span></code></pre></div>
<pre><code>##
## ----------------------------------------------------------------------
##
## Your next step is to start H2O:
## > h2o.init()
##
## For H2O package documentation, ask for help:
## > ??h2o
##
## After starting H2O, you can use the Web UI at http://localhost:54321
## For more information visit https://docs.h2o.ai
##
## ----------------------------------------------------------------------</code></pre>
<pre><code>##
## Attaching package: 'h2o'</code></pre>
<pre><code>## The following objects are masked from 'package:stats':
##
## cor, sd, var</code></pre>
<pre><code>## The following objects are masked from 'package:base':
##
## &&, %*%, %in%, ||, apply, as.factor, as.numeric, colnames,
## colnames<-, ifelse, is.character, is.factor, is.numeric, log,
## log10, log1p, log2, round, signif, trunc</code></pre>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(gridExtra)</span></code></pre></div>
<pre><code>##
## Attaching package: 'gridExtra'</code></pre>
<pre><code>## The following object is masked from 'package:randomForest':
##
## combine</code></pre>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span></code></pre></div>
<pre><code>##
## Attaching package: 'dplyr'</code></pre>
<pre><code>## The following object is masked from 'package:gridExtra':
##
## combine</code></pre>
<pre><code>## The following object is masked from 'package:randomForest':
##
## combine</code></pre>
<pre><code>## The following objects are masked from 'package:stats':
##
## filter, lag</code></pre>
<pre><code>## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union</code></pre>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(nnet)</span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(hrbrthemes)</span></code></pre></div>
<pre><code>## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.</code></pre>
<pre><code>## Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and</code></pre>
<pre><code>## if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow</code></pre>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(rpart.plot)</span></code></pre></div>
<pre><code>## Loading required package: rpart</code></pre>
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(mlbench)</span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(e1071)</span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(rpart)</span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(DMwR2)</span></code></pre></div>
<pre><code>## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo</code></pre>
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(caret)</span></code></pre></div>
<pre><code>## Loading required package: lattice</code></pre>
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(Metrics)</span></code></pre></div>
<pre><code>##
## Attaching package: 'Metrics'</code></pre>
<pre><code>## The following objects are masked from 'package:caret':
##
## precision, recall</code></pre>
<div class="sourceCode" id="cb37"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(fpc)</span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(Hmisc)</span></code></pre></div>
<pre><code>## Loading required package: survival</code></pre>
<pre><code>##
## Attaching package: 'survival'</code></pre>
<pre><code>## The following object is masked from 'package:caret':
##
## cluster</code></pre>
<pre><code>## Loading required package: Formula</code></pre>
<pre><code>##
## Attaching package: 'Hmisc'</code></pre>
<pre><code>## The following object is masked from 'package:e1071':
##
## impute</code></pre>
<pre><code>## The following objects are masked from 'package:dplyr':
##
## src, summarize</code></pre>
<pre><code>## The following objects are masked from 'package:base':
##
## format.pval, units</code></pre>
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tibble)</span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(knitr)</span>
<span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(MLmetrics)</span></code></pre></div>
<pre><code>##
## Attaching package: 'MLmetrics'</code></pre>
<pre><code>## The following objects are masked from 'package:caret':
##
## MAE, RMSE</code></pre>
<pre><code>## The following object is masked from 'package:base':
##
## Recall</code></pre>
<div class="sourceCode" id="cb50"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(earth) <span class="co"># fit MARS models</span></span></code></pre></div>
<pre><code>## Loading required package: plotmo</code></pre>
<pre><code>## Loading required package: plotrix</code></pre>
<pre><code>## Loading required package: TeachingDemos</code></pre>
<pre><code>##
## Attaching package: 'TeachingDemos'</code></pre>
<pre><code>## The following objects are masked from 'package:Hmisc':
##
## cnvrt.coords, subplot</code></pre>
</div>
<div id="custom-functions" class="section level1">
<h1>Custom Functions</h1>
<div class="sourceCode" id="cb56"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="st">`</span><span class="at">%notin%</span><span class="st">`</span> <span class="ot"><-</span> <span class="fu">Negate</span>(<span class="st">`</span><span class="at">%in%</span><span class="st">`</span>)</span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-3"><a href="#cb56-3" aria-hidden="true" tabindex="-1"></a>pretty_df_print <span class="ot"><-</span> <span class="cf">function</span> (df) <span class="fu">kable_styling</span>(<span class="fu">kable</span>(df))</span>
<span id="cb56-4"><a href="#cb56-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-5"><a href="#cb56-5" aria-hidden="true" tabindex="-1"></a>top_least_freq_and_neg_vals <span class="ot"><-</span> <span class="cf">function</span> (col) {</span>
<span id="cb56-6"><a href="#cb56-6" aria-hidden="true" tabindex="-1"></a> t <span class="ot"><-</span> <span class="fu">table</span>(col) <span class="sc">%>%</span> <span class="fu">as.data.frame</span>() <span class="sc">%>%</span> <span class="fu">arrange</span>(<span class="fu">desc</span>(Freq))</span>
<span id="cb56-7"><a href="#cb56-7" aria-hidden="true" tabindex="-1"></a> non_pos <span class="ot"><-</span> <span class="fu">as.data.frame</span>(col)</span>
<span id="cb56-8"><a href="#cb56-8" aria-hidden="true" tabindex="-1"></a> non_pos <span class="ot"><-</span> non_pos[non_pos <span class="sc"><=</span><span class="dv">0</span>, ]</span>
<span id="cb56-9"><a href="#cb56-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span>(<span class="st">"Top frequent values:"</span>)</span>
<span id="cb56-10"><a href="#cb56-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">print.data.frame</span>(<span class="fu">head</span>(t))</span>
<span id="cb56-11"><a href="#cb56-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span>(<span class="st">"Least frequent values:"</span>)</span>
<span id="cb56-12"><a href="#cb56-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">print.data.frame</span>(<span class="fu">tail</span>(t))</span>
<span id="cb56-13"><a href="#cb56-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span>(<span class="st">"Negative values:"</span>)</span>
<span id="cb56-14"><a href="#cb56-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">print.data.frame</span>(non_pos)</span>
<span id="cb56-15"><a href="#cb56-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span>(<span class="st">"/////////////////////////////////////////////////////"</span>)</span>
<span id="cb56-16"><a href="#cb56-16" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb56-17"><a href="#cb56-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-18"><a href="#cb56-18" aria-hidden="true" tabindex="-1"></a>na_count_df <span class="ot"><-</span> <span class="cf">function</span> (df) {</span>
<span id="cb56-19"><a href="#cb56-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(<span class="fu">as.data.frame</span>(<span class="fu">t</span>(<span class="fu">data.frame</span>(<span class="fu">sapply</span>(df, <span class="cf">function</span>(col) <span class="fu">sum</span>(<span class="fu">is.na</span>(col)))))))</span>
<span id="cb56-20"><a href="#cb56-20" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb56-21"><a href="#cb56-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-22"><a href="#cb56-22" aria-hidden="true" tabindex="-1"></a>iqr_outliers_min_max <span class="ot"><-</span> <span class="cf">function</span>(df) {</span>
<span id="cb56-23"><a href="#cb56-23" aria-hidden="true" tabindex="-1"></a> iqr_df <span class="ot"><-</span> <span class="fu">as.data.frame</span>( <span class="fu">t</span>(<span class="fu">data.frame</span>(df <span class="sc">%>%</span> <span class="fu">sapply</span>(<span class="cf">function</span> (x) {<span class="fu">quantile</span>(x, <span class="fu">c</span>(<span class="fl">0.25</span>, <span class="fl">0.75</span>))}))) ) <span class="sc">%>%</span> <span class="fu">cbind</span>((<span class="fu">data.frame</span>(df <span class="sc">%>%</span> <span class="fu">sapply</span>(IQR))))</span>
<span id="cb56-24"><a href="#cb56-24" aria-hidden="true" tabindex="-1"></a> <span class="fu">colnames</span>(iqr_df) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Q1"</span>, <span class="st">"Q3"</span>, <span class="st">"IQR"</span>)</span>
<span id="cb56-25"><a href="#cb56-25" aria-hidden="true" tabindex="-1"></a> iqr_df<span class="sc">$</span>minVal <span class="ot"><-</span> iqr_df<span class="sc">$</span>Q1 <span class="sc">-</span> <span class="fl">1.5</span><span class="sc">*</span>iqr_df<span class="sc">$</span>IQR</span>
<span id="cb56-26"><a href="#cb56-26" aria-hidden="true" tabindex="-1"></a> iqr_df<span class="sc">$</span>maxVal <span class="ot"><-</span> iqr_df<span class="sc">$</span>Q3 <span class="sc">+</span> <span class="fl">1.5</span><span class="sc">*</span>iqr_df<span class="sc">$</span>IQR</span>
<span id="cb56-27"><a href="#cb56-27" aria-hidden="true" tabindex="-1"></a> <span class="fu">print.data.frame</span>(iqr_df <span class="sc">%>%</span> <span class="fu">select</span>(<span class="st">"minVal"</span>, <span class="st">"maxVal"</span>))</span>
<span id="cb56-28"><a href="#cb56-28" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb56-29"><a href="#cb56-29" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-30"><a href="#cb56-30" aria-hidden="true" tabindex="-1"></a>iqr_outliers_finder <span class="ot"><-</span> <span class="cf">function</span>(x){</span>
<span id="cb56-31"><a href="#cb56-31" aria-hidden="true" tabindex="-1"></a> Q1 <span class="ot"><-</span> <span class="fu">quantile</span>(x, <span class="fl">0.25</span>)</span>
<span id="cb56-32"><a href="#cb56-32" aria-hidden="true" tabindex="-1"></a> Q3 <span class="ot"><-</span> <span class="fu">quantile</span>(x, <span class="fl">0.75</span>)</span>
<span id="cb56-33"><a href="#cb56-33" aria-hidden="true" tabindex="-1"></a> IQR <span class="ot"><-</span> Q3 <span class="sc">-</span> Q1</span>
<span id="cb56-34"><a href="#cb56-34" aria-hidden="true" tabindex="-1"></a> Vl <span class="ot"><-</span> Q1 <span class="sc">-</span> <span class="fl">1.5</span> <span class="sc">*</span> IQR</span>
<span id="cb56-35"><a href="#cb56-35" aria-hidden="true" tabindex="-1"></a> Vr <span class="ot"><-</span> Q3 <span class="sc">+</span> <span class="fl">1.5</span> <span class="sc">*</span> IQR</span>
<span id="cb56-36"><a href="#cb56-36" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span> (x[<span class="fu">which</span>(x <span class="sc"><</span> Vl <span class="sc">|</span> x <span class="sc">></span> Vr)])</span>
<span id="cb56-37"><a href="#cb56-37" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb56-38"><a href="#cb56-38" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-39"><a href="#cb56-39" aria-hidden="true" tabindex="-1"></a>get_freq_df_from_vector <span class="ot"><-</span> <span class="cf">function</span> (vec, col.name) {</span>
<span id="cb56-40"><a href="#cb56-40" aria-hidden="true" tabindex="-1"></a> freq_df <span class="ot"><-</span> <span class="fu">as.data.frame</span>(<span class="fu">table</span>(vec))</span>
<span id="cb56-41"><a href="#cb56-41" aria-hidden="true" tabindex="-1"></a> <span class="fu">colnames</span>(freq_df)[<span class="dv">1</span>] <span class="ot"><-</span> col.name</span>
<span id="cb56-42"><a href="#cb56-42" aria-hidden="true" tabindex="-1"></a> freq_df <span class="ot"><-</span> freq_df[<span class="fu">order</span>(freq_df[,<span class="dv">1</span>], <span class="at">decreasing =</span> T),]</span>
<span id="cb56-43"><a href="#cb56-43" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">nrow</span>(freq_df) <span class="sc"><=</span> <span class="dv">10</span>) {</span>
<span id="cb56-44"><a href="#cb56-44" aria-hidden="true" tabindex="-1"></a> <span class="fu">print.data.frame</span>(freq_df)}</span>
<span id="cb56-45"><a href="#cb56-45" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span> {</span>
<span id="cb56-46"><a href="#cb56-46" aria-hidden="true" tabindex="-1"></a> <span class="fu">cat</span>(<span class="st">"Highest values :</span><span class="sc">\n</span><span class="st">"</span>)</span>
<span id="cb56-47"><a href="#cb56-47" aria-hidden="true" tabindex="-1"></a> <span class="fu">print.data.frame</span>(<span class="fu">head</span>(freq_df))</span>
<span id="cb56-48"><a href="#cb56-48" aria-hidden="true" tabindex="-1"></a> <span class="fu">cat</span>(<span class="st">"Lowest values :</span><span class="sc">\n</span><span class="st">"</span>)</span>
<span id="cb56-49"><a href="#cb56-49" aria-hidden="true" tabindex="-1"></a> <span class="fu">print.data.frame</span>(<span class="fu">tail</span>(freq_df))</span>
<span id="cb56-50"><a href="#cb56-50" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb56-51"><a href="#cb56-51" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb56-52"><a href="#cb56-52" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-53"><a href="#cb56-53" aria-hidden="true" tabindex="-1"></a>dbscan_outliers_finder <span class="ot"><-</span> <span class="cf">function</span>(data, ...) {</span>
<span id="cb56-54"><a href="#cb56-54" aria-hidden="true" tabindex="-1"></a> <span class="fu">require</span>(fpc, <span class="at">quietly=</span><span class="cn">TRUE</span>)</span>
<span id="cb56-55"><a href="#cb56-55" aria-hidden="true" tabindex="-1"></a> cl <span class="ot"><-</span> <span class="fu">dbscan</span>(data, ...)</span>
<span id="cb56-56"><a href="#cb56-56" aria-hidden="true" tabindex="-1"></a> posOuts <span class="ot"><-</span> <span class="fu">which</span>(cl<span class="sc">$</span>cluster <span class="sc">==</span> <span class="dv">0</span>)</span>
<span id="cb56-57"><a href="#cb56-57" aria-hidden="true" tabindex="-1"></a> <span class="fu">list</span>(<span class="at">positions =</span> posOuts,</span>
<span id="cb56-58"><a href="#cb56-58" aria-hidden="true" tabindex="-1"></a> <span class="at">outliers =</span> data[posOuts,],</span>
<span id="cb56-59"><a href="#cb56-59" aria-hidden="true" tabindex="-1"></a> <span class="at">dbscanResults =</span> cl)</span>
<span id="cb56-60"><a href="#cb56-60" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
</div>
<div id="data-loading" class="section level1">
<h1>Data loading</h1>
<div class="sourceCode" id="cb57"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a>st <span class="ot"><-</span> <span class="fu">proc.time</span>()</span>
<span id="cb57-2"><a href="#cb57-2" aria-hidden="true" tabindex="-1"></a>green <span class="ot"><-</span> <span class="fu">read.csv</span>(<span class="st">"data/green_tripdata_2020-06.csv"</span>)</span>
<span id="cb57-3"><a href="#cb57-3" aria-hidden="true" tabindex="-1"></a>green <span class="ot"><-</span> green <span class="sc">%>%</span> <span class="fu">rbind</span>(<span class="fu">read.csv</span>(<span class="st">"data/green_tripdata_2020-01.csv"</span>))</span>
<span id="cb57-4"><a href="#cb57-4" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(<span class="fu">proc.time</span>() <span class="sc">-</span> st)</span></code></pre></div>
<pre><code>## user system elapsed
## 5.824 0.157 6.005</code></pre>
<div class="sourceCode" id="cb59"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">'There are ['</span>, <span class="fu">nrow</span>(green), <span class="st">'] observations, and ['</span>,<span class="fu">ncol</span>(green),<span class="st">'] variables'</span>)</span></code></pre></div>
<pre><code>## There are [ 510879 ] observations, and [ 20 ] variables</code></pre>
</div>
<div id="data-exploratory-preprocessing" class="section level1">
<h1>Data exploratory & Preprocessing</h1>
<div id="dataframe-head" class="section level2">
<h2>Dataframe head</h2>
<div class="sourceCode" id="cb61"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(green)</span></code></pre></div>
<pre><code>## VendorID lpep_pickup_datetime lpep_dropoff_datetime store_and_fwd_flag
## 1 1 2020-06-01 00:22:07 2020-06-01 00:39:03 N
## 2 2 2020-06-01 00:09:05 2020-06-01 00:22:46 N
## 3 2 2020-06-01 00:20:05 2020-06-01 00:23:33 N
## 4 2 2020-06-01 00:30:50 2020-06-01 00:37:49 N
## 5 2 2020-06-01 00:03:05 2020-06-01 00:16:46 N
## 6 2 2020-06-01 00:14:05 2020-06-01 00:23:57 N
## RatecodeID PULocationID DOLocationID passenger_count trip_distance
## 1 1 255 14 1 0.00
## 2 1 166 141 1 3.43
## 3 1 75 42 1 1.03
## 4 1 74 42 1 1.22
## 5 1 152 247 1 2.59
## 6 1 244 200 1 5.95
## fare_amount extra mta_tax tip_amount tolls_amount ehail_fee
## 1 28.2 0.0 0.5 0.00 0.0 NA
## 2 13.0 0.5 0.5 3.41 0.0 NA
## 3 5.0 0.5 0.5 0.00 0.0 NA
## 4 7.0 0.5 0.5 0.00 0.0 NA
## 5 11.5 0.5 0.5 0.00 0.0 NA
## 6 17.5 0.5 0.5 6.00 2.8 NA
## improvement_surcharge total_amount payment_type trip_type
## 1 0.3 29.00 1 1
## 2 0.3 20.46 1 1
## 3 0.3 6.30 2 1
## 4 0.3 8.30 2 1
## 5 0.3 12.80 2 1
## 6 0.3 27.60 1 1
## congestion_surcharge
## 1 0.00
## 2 2.75
## 3 0.00
## 4 0.00
## 5 0.00
## 6 0.00</code></pre>
</div>
<div id="dataframe-tail" class="section level2">
<h2>Dataframe tail</h2>
<div class="sourceCode" id="cb63"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb63-1"><a href="#cb63-1" aria-hidden="true" tabindex="-1"></a><span class="fu">tail</span>(green)</span></code></pre></div>
<pre><code>## VendorID lpep_pickup_datetime lpep_dropoff_datetime store_and_fwd_flag
## 510874 NA 2020-01-31 23:08:00 2020-01-31 23:26:00
## 510875 NA 2020-01-31 23:29:00 2020-01-31 23:47:00
## 510876 NA 2020-01-31 23:57:00 2020-02-01 00:23:00
## 510877 NA 2020-01-31 23:57:00 2020-02-01 00:10:00
## 510878 NA 2020-01-31 23:27:00 2020-02-01 00:04:00
## 510879 NA 2020-01-31 23:36:00 2020-02-01 00:01:00
## RatecodeID PULocationID DOLocationID passenger_count trip_distance
## 510874 NA 14 206 NA 7.51
## 510875 NA 167 32 NA 4.58
## 510876 NA 81 69 NA 6.55
## 510877 NA 244 241 NA 3.34
## 510878 NA 68 17 NA 8.92
## 510879 NA 22 124 NA 13.51
## fare_amount extra mta_tax tip_amount tolls_amount ehail_fee
## 510874 21.46 2.75 0 0 14.99 NA
## 510875 23.21 2.75 0 0 0.00 NA
## 510876 27.27 2.75 0 0 0.00 NA
## 510877 25.95 2.75 0 0 0.00 NA
## 510878 30.39 2.75 0 0 0.00 NA
## 510879 42.20 2.75 0 0 0.00 NA
## improvement_surcharge total_amount payment_type trip_type
## 510874 0.3 39.50 NA NA
## 510875 0.3 26.26 NA NA
## 510876 0.3 30.32 NA NA
## 510877 0.3 29.00 NA NA
## 510878 0.3 33.44 NA NA
## 510879 0.3 45.25 NA NA
## congestion_surcharge
## 510874 NA
## 510875 NA
## 510876 NA
## 510877 NA
## 510878 NA
## 510879 NA</code></pre>
</div>
<div id="brief-info-on-the-data" class="section level2">
<h2>Brief info on the data</h2>
<div class="sourceCode" id="cb65"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(green)</span></code></pre></div>
<pre><code>## 'data.frame': 510879 obs. of 20 variables:
## $ VendorID : int 1 2 2 2 2 2 2 2 2 2 ...
## $ lpep_pickup_datetime : chr "2020-06-01 00:22:07" "2020-06-01 00:09:05" "2020-06-01 00:20:05" "2020-06-01 00:30:50" ...
## $ lpep_dropoff_datetime: chr "2020-06-01 00:39:03" "2020-06-01 00:22:46" "2020-06-01 00:23:33" "2020-06-01 00:37:49" ...
## $ store_and_fwd_flag : chr "N" "N" "N" "N" ...
## $ RatecodeID : int 1 1 1 1 1 1 1 1 1 1 ...
## $ PULocationID : int 255 166 75 74 152 244 93 85 82 174 ...
## $ DOLocationID : int 14 141 42 42 247 200 95 188 70 127 ...
## $ passenger_count : int 1 1 1 1 1 1 1 1 1 1 ...
## $ trip_distance : num 0 3.43 1.03 1.22 2.59 5.95 2.25 0.65 3.5 3.38 ...
## $ fare_amount : num 28.2 13 5 7 11.5 17.5 11 5 12 11.5 ...
## $ extra : num 0 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
## $ mta_tax : num 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
## $ tip_amount : num 0 3.41 0 0 0 6 0 0 2.66 0 ...
## $ tolls_amount : num 0 0 0 0 0 2.8 0 0 0 0 ...
## $ ehail_fee : logi NA NA NA NA NA NA ...
## $ improvement_surcharge: num 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 ...
## $ total_amount : num 29 20.5 6.3 8.3 12.8 ...
## $ payment_type : int 1 1 2 2 2 1 2 2 1 2 ...
## $ trip_type : int 1 1 1 1 1 1 1 1 1 1 ...
## $ congestion_surcharge : num 0 2.75 0 0 0 0 0 0 0 0 ...</code></pre>
</div>
<div id="unnecessary-variables-deletion" class="section level2">
<h2>Unnecessary Variables Deletion</h2>
<div class="sourceCode" id="cb67"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a>num.vars <span class="ot"><-</span> <span class="fu">ncol</span>(green)</span>
<span id="cb67-2"><a href="#cb67-2" aria-hidden="true" tabindex="-1"></a>green <span class="ot"><-</span> green <span class="sc">%>%</span> <span class="fu">subset</span>(<span class="at">select =</span> <span class="sc">-</span><span class="fu">c</span>( VendorID, store_and_fwd_flag))</span>
<span id="cb67-3"><a href="#cb67-3" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"["</span>, num.vars <span class="sc">-</span> <span class="fu">ncol</span>(green) , <span class="st">"] variables were delete"</span>)</span></code></pre></div>
<pre><code>## [ 2 ] variables were delete</code></pre>
</div>
<div id="summary-of-the-data" class="section level2">
<h2>Summary of the data</h2>
<div class="sourceCode" id="cb69"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb69-1"><a href="#cb69-1" aria-hidden="true" tabindex="-1"></a><span class="fu">describe</span>(green)</span></code></pre></div>
<pre><code>## green
##
## 18 Variables 510879 Observations
## --------------------------------------------------------------------------------
## lpep_pickup_datetime
## n missing distinct
## 510879 0 385504
##
## lowest : 2008-12-31 22:06:48 2008-12-31 23:11:00 2009-01-01 00:07:51 2009-01-01 00:08:09 2009-01-01 00:34:50
## highest: 2020-06-30 23:42:13 2020-06-30 23:51:12 2020-06-30 23:52:09 2020-06-30 23:57:39 2020-06-30 23:59:37
## --------------------------------------------------------------------------------
## lpep_dropoff_datetime
## n missing distinct
## 510879 0 385627
##
## lowest : 2008-12-31 23:12:08 2009-01-01 00:17:31 2009-01-01 00:45:10 2009-01-01 00:58:27 2009-01-01 01:00:23
## highest: 2020-07-01 10:24:18 2020-07-01 12:00:04 2020-07-01 13:27:36 2020-07-01 14:51:21 2020-07-01 15:59:47
## --------------------------------------------------------------------------------
## RatecodeID
## n missing distinct Info Mean Gmd
## 370150 140729 7 0.083 1.107 0.2087
##
## lowest : 1 2 3 4 5, highest: 3 4 5 6 99
##
## Value 1 2 3 4 5 6 99
## Frequency 359569 727 189 269 9390 4 2
## Proportion 0.971 0.002 0.001 0.001 0.025 0.000 0.000
## --------------------------------------------------------------------------------
## PULocationID
## n missing distinct Info Mean Gmd .05 .10
## 510879 0 256 0.999 108.1 78.84 18 33
## .25 .50 .75 .90 .95
## 52 82 166 226 244
##
## lowest : 1 3 4 5 6, highest: 261 262 263 264 265
## --------------------------------------------------------------------------------
## DOLocationID
## n missing distinct Info Mean Gmd .05 .10
## 510879 0 260 1 128.6 87.77 21 37
## .25 .50 .75 .90 .95
## 62 129 193 238 250
##
## lowest : 1 2 3 4 5, highest: 261 262 263 264 265
## --------------------------------------------------------------------------------
## passenger_count
## n missing distinct Info Mean Gmd .05 .10
## 370150 140729 10 0.353 1.298 0.5511 1 1
## .25 .50 .75 .90 .95
## 1 1 1 2 3
##
## lowest : 0 1 2 3 4, highest: 5 6 7 8 9
##
## Value 0 1 2 3 4 5 6 7 8
## Frequency 693 320143 26611 5312 1716 9976 5666 10 18
## Proportion 0.002 0.865 0.072 0.014 0.005 0.027 0.015 0.000 0.000
##
## Value 9
## Frequency 5
## Proportion 0.000
## --------------------------------------------------------------------------------
## trip_distance
## n missing distinct Info Mean Gmd .05 .10
## 510879 0 3776 1 8.818 14.21 0.23 0.60
## .25 .50 .75 .90 .95
## 1.10 2.12 4.61 9.27 13.48
##
## lowest : -33.69 -33.29 -29.17 -26.66 -26.08
## highest: 134349.12 134355.40 134359.97 148387.95 162291.50
##
## Value 0 6000 26000 34000 42000 44000 48000 50000 84000
## Frequency 510851 1 1 1 1 4 1 3 1
## Proportion 1 0 0 0 0 0 0 0 0
##
## Value 96000 132000 134000 148000 162000
## Frequency 1 1 11 1 1
## Proportion 0 0 0 0 0
##
## For the frequency table, variable is rounded to the nearest 2000
## --------------------------------------------------------------------------------
## fare_amount
## n missing distinct Info Mean Gmd .05 .10
## 510879 0 6501 1 16.3 13.08 4.50 5.00
## .25 .50 .75 .90 .95
## 7.00 12.00 21.37 33.40 44.15
##
## lowest : -210.00 -200.00 -180.00 -97.67 -83.82
## highest: 275.00 300.00 335.50 630.00 753.00
## --------------------------------------------------------------------------------
## extra
## n missing distinct Info Mean Gmd .05 .10
## 510879 0 16 0.864 0.7613 1.052 0.00 0.00
## .25 .50 .75 .90 .95
## 0.00 0.50 1.00 2.75 2.75
##
## lowest : -4.50 -1.00 -0.50 0.00 0.50, highest: 3.50 3.75 4.50 5.50 8.25
##
## Value -4.50 -1.00 -0.50 0.00 0.50 0.80 1.00 2.50 2.75
## Frequency 4 234 333 253938 88855 1 75201 28 83708
## Proportion 0.000 0.000 0.001 0.497 0.174 0.000 0.147 0.000 0.164
##
## Value 3.00 3.25 3.50 3.75 4.50 5.50 8.25
## Frequency 4 2083 4 2067 129 4019 271
## Proportion 0.000 0.004 0.000 0.004 0.000 0.008 0.001
## --------------------------------------------------------------------------------
## mta_tax
## n missing distinct Info Mean Gmd
## 510879 0 4 0.553 0.3771 0.1866
##
## Value -0.50 0.00 0.50 3.55
## Frequency 1114 123396 386364 5
## Proportion 0.002 0.242 0.756 0.000
## --------------------------------------------------------------------------------
## tip_amount
## n missing distinct Info Mean Gmd .05 .10
## 510879 0 1549 0.737 0.9838 1.493 0.00 0.00
## .25 .50 .75 .90 .95
## 0.00 0.00 1.76 2.94 4.01
##
## lowest : -2.80 -1.29 -0.99 -0.86 -0.76, highest: 300.08 333.00 449.60 450.00 480.00
## --------------------------------------------------------------------------------
## tolls_amount
## n missing distinct Info Mean Gmd .05 .10
## 510879 0 106 0.17 0.3747 0.7103 0.00 0.00
## .25 .50 .75 .90 .95
## 0.00 0.00 0.00 0.00 6.12
##
## lowest : -6.12 -2.80 0.00 0.80 1.00, highest: 35.00 39.74 45.75 48.88 96.12
## --------------------------------------------------------------------------------
## improvement_surcharge
## n missing distinct Info Mean Gmd
## 510879 0 3 0.106 0.2883 0.02258
##
## Value -0.3 0.0 0.3
## Frequency 1172 17570 492137
## Proportion 0.002 0.034 0.963
## --------------------------------------------------------------------------------
## total_amount
## n missing distinct Info Mean Gmd .05 .10
## 510879 0 7599 1 19.4 14.6 5.80 6.80
## .25 .50 .75 .90 .95
## 9.30 14.80 25.06 38.54 49.49
##
## lowest : -210.30 -200.30 -180.30 -88.50 -80.30
## highest: 454.90 462.80 498.80 636.92 753.80
## --------------------------------------------------------------------------------
## payment_type
## n missing distinct Info Mean Gmd
## 370150 140729 5 0.746 1.456 0.5126
##
## lowest : 1 2 3 4 5, highest: 1 2 3 4 5
##
## Value 1 2 3 4 5
## Frequency 204709 162727 2043 653 18
## Proportion 0.553 0.440 0.006 0.002 0.000
## --------------------------------------------------------------------------------
## trip_type
## n missing distinct Info Mean Gmd
## 370149 140730 2 0.071 1.024 0.04715
##
## Value 1 2
## Frequency 361206 8943
## Proportion 0.976 0.024
## --------------------------------------------------------------------------------
## congestion_surcharge
## n missing distinct Info Mean Gmd
## 370150 140729 5 0.429 0.4751 0.7861
##
## lowest : -2.75 0.00 0.75 2.50 2.75, highest: -2.75 0.00 0.75 2.50 2.75
##
## Value -2.75 0.00 0.75 2.50 2.75
## Frequency 5 306179 6 163 63797
## Proportion 0.000 0.827 0.000 0.000 0.172
## --------------------------------------------------------------------------------
##
## Variables with all observations missing:
##
## [1] ehail_fee</code></pre>
</div>
<div id="count-nas-in-each-column" class="section level2">
<h2>Count NAs’ in each column</h2>
<div class="sourceCode" id="cb71"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb71-1"><a href="#cb71-1" aria-hidden="true" tabindex="-1"></a><span class="fu">na_count_df</span>(green)</span></code></pre></div>
<pre><code>## lpep_pickup_datetime
## sapply.df..function.col..sum.is.na.col... 0
## lpep_dropoff_datetime RatecodeID
## sapply.df..function.col..sum.is.na.col... 0 140729
## PULocationID DOLocationID
## sapply.df..function.col..sum.is.na.col... 0 0
## passenger_count trip_distance
## sapply.df..function.col..sum.is.na.col... 140729 0
## fare_amount extra mta_tax tip_amount
## sapply.df..function.col..sum.is.na.col... 0 0 0 0
## tolls_amount ehail_fee
## sapply.df..function.col..sum.is.na.col... 0 510879
## improvement_surcharge total_amount
## sapply.df..function.col..sum.is.na.col... 0 0
## payment_type trip_type
## sapply.df..function.col..sum.is.na.col... 140729 140730
## congestion_surcharge
## sapply.df..function.col..sum.is.na.col... 140729</code></pre>
</div>
<div id="remove-nas" class="section level2">
<h2>Remove NAs’</h2>
<p>As we can see from the output above: <code>passenger_count</code>, <code>payment_type</code>, <code>trip_type</code>, <code>congestion_surcharge</code> have ~ (24 K) NAs’ <code>fare_amount</code>, <code>extra</code>, <code>mta_tax</code>, <code>tip_amount</code>, <code>tolls_amount</code>, <code>improvement_surcharge</code>, <code>total_amount</code> have negative values. Negative values: we believe have been entered mistakenly as negative values so, we need to fix this problem by obtaining the absolute values. NAs’: We need to drop rows with NAs as they critical in the classification. As well as, we need to remove <code>ehail_fee</code> variable as it’s all NAs’. For <code>tip_amount</code> we realized that there are many <code>0</code> values, we need to get rid of them.</p>
<div class="sourceCode" id="cb73"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb73-1"><a href="#cb73-1" aria-hidden="true" tabindex="-1"></a>green <span class="ot"><-</span> green <span class="sc">%>%</span> <span class="fu">within</span>(<span class="fu">rm</span>(ehail_fee))</span>
<span id="cb73-2"><a href="#cb73-2" aria-hidden="true" tabindex="-1"></a>num.rows <span class="ot"><-</span> <span class="fu">nrow</span>(green)</span>
<span id="cb73-3"><a href="#cb73-3" aria-hidden="true" tabindex="-1"></a>green <span class="ot"><-</span> green[<span class="sc">!</span><span class="fu">is.na</span>(green<span class="sc">$</span>trip_type) <span class="sc">&</span> green<span class="sc">$</span>trip_distance <span class="sc">!=</span> <span class="dv">0</span>,]</span>
<span id="cb73-4"><a href="#cb73-4" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"["</span>, num.rows <span class="sc">-</span> <span class="fu">nrow</span>(green), <span class="st">"] observations were deleted out of ["</span>, num.rows, <span class="st">"]"</span>,</span>
<span id="cb73-5"><a href="#cb73-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"which means ~ ("</span>, <span class="fu">round</span>((num.rows <span class="sc">-</span> <span class="fu">nrow</span>(green))<span class="sc">/</span>num.rows, <span class="at">digits =</span> <span class="dv">2</span>), <span class="st">")"</span>)</span></code></pre></div>
<pre><code>## [ 158626 ] observations were deleted out of [ 510879 ] which means ~ ( 0.31 )</code></pre>
<div class="sourceCode" id="cb75"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb75-1"><a href="#cb75-1" aria-hidden="true" tabindex="-1"></a><span class="fu">as.data.frame</span>(<span class="fu">t</span>(<span class="fu">data.frame</span>(<span class="fu">sapply</span>(green, <span class="cf">function</span>(col) <span class="fu">sum</span>(<span class="fu">is.na</span>(col))))))</span></code></pre></div>
<pre><code>## lpep_pickup_datetime
## sapply.green..function.col..sum.is.na.col... 0
## lpep_dropoff_datetime RatecodeID
## sapply.green..function.col..sum.is.na.col... 0 0
## PULocationID DOLocationID
## sapply.green..function.col..sum.is.na.col... 0 0
## passenger_count trip_distance
## sapply.green..function.col..sum.is.na.col... 0 0
## fare_amount extra mta_tax
## sapply.green..function.col..sum.is.na.col... 0 0 0
## tip_amount tolls_amount
## sapply.green..function.col..sum.is.na.col... 0 0
## improvement_surcharge total_amount
## sapply.green..function.col..sum.is.na.col... 0 0
## payment_type trip_type
## sapply.green..function.col..sum.is.na.col... 0 0
## congestion_surcharge
## sapply.green..function.col..sum.is.na.col... 0</code></pre>
</div>
<div id="check-top-least-frequent-and-negative-values" class="section level2">
<h2>Check top & least frequent and negative values</h2>
<div class="sourceCode" id="cb77"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb77-1"><a href="#cb77-1" aria-hidden="true" tabindex="-1"></a><span class="fu">describe</span>(green)</span></code></pre></div>
<pre><code>## green
##
## 17 Variables 352253 Observations
## --------------------------------------------------------------------------------
## lpep_pickup_datetime
## n missing distinct
## 352253 0 327696
##
## lowest : 2009-01-01 00:08:09 2009-01-01 00:34:50 2009-01-01 00:45:53 2009-01-01 00:54:13 2009-01-01 01:19:36
## highest: 2020-06-30 23:37:20 2020-06-30 23:42:13 2020-06-30 23:51:12 2020-06-30 23:52:09 2020-06-30 23:57:39
## --------------------------------------------------------------------------------
## lpep_dropoff_datetime
## n missing distinct
## 352253 0 327343
##
## lowest : 2009-01-01 00:17:31 2009-01-01 00:45:10 2009-01-01 00:58:27 2009-01-01 01:00:23 2009-01-01 01:28:23
## highest: 2020-07-01 10:24:18 2020-07-01 12:00:04 2020-07-01 13:27:36 2020-07-01 14:51:21 2020-07-01 15:59:47
## --------------------------------------------------------------------------------
## RatecodeID
## n missing distinct Info Mean Gmd
## 352253 0 6 0.067 1.085 0.1664
##
## lowest : 1 2 3 4 5, highest: 2 3 4 5 6
##
## Value 1 2 3 4 5 6
## Frequency 344198 577 143 266 7066 3
## Proportion 0.977 0.002 0.000 0.001 0.020 0.000
## --------------------------------------------------------------------------------
## PULocationID
## n missing distinct Info Mean Gmd .05 .10
## 352253 0 237 0.997 101.7 73.53 20 33
## .25 .50 .75 .90 .95
## 49 75 134 223 244
##
## lowest : 3 4 5 6 7, highest: 260 262 263 264 265
## --------------------------------------------------------------------------------
## DOLocationID
## n missing distinct Info Mean Gmd .05 .10
## 352253 0 258 1 129.5 87.92 24 41
## .25 .50 .75 .90 .95
## 65 129 193 239 255
##
## lowest : 1 3 4 5 6, highest: 261 262 263 264 265
## --------------------------------------------------------------------------------
## passenger_count
## n missing distinct Info Mean Gmd .05 .10
## 352253 0 10 0.362 1.308 0.5671 1 1
## .25 .50 .75 .90 .95
## 1 1 1 2 3
##
## lowest : 0 1 2 3 4, highest: 5 6 7 8 9
##
## Value 0 1 2 3 4 5 6 7 8
## Frequency 610 303129 26191 5248 1691 9740 5630 6 6
## Proportion 0.002 0.861 0.074 0.015 0.005 0.028 0.016 0.000 0.000
##
## Value 9
## Frequency 2
## Proportion 0.000
## --------------------------------------------------------------------------------
## trip_distance
## n missing distinct Info Mean Gmd .05 .10
## 352253 0 3006 1 3.182 3.499 0.50 0.67
## .25 .50 .75 .90 .95
## 1.02 1.73 3.20 6.09 8.46
##
## lowest : -3.05 -1.46 -1.33 -1.02 -1.01
## highest: 80.15 84.97 124.13 130.68 134121.50
##
## Value 0 134000
## Frequency 352252 1
## Proportion 1 0
##
## For the frequency table, variable is rounded to the nearest 1000
## --------------------------------------------------------------------------------
## fare_amount
## n missing distinct Info Mean Gmd .05 .10
## 352253 0 749 0.999 11.92 8.692 4.0 5.0
## .25 .50 .75 .90 .95
## 6.5 9.0 14.0 22.0 29.0
##
## lowest : -200.00 -80.00 -52.00 -50.00 -46.96
## highest: 228.50 233.50 335.50 630.00 753.00
## --------------------------------------------------------------------------------