-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPH125_9_CYO_Report.tex
1493 lines (1274 loc) · 85.3 KB
/
PH125_9_CYO_Report.tex
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
\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\else % if luatex or xelatex
\ifxetex
\usepackage{mathspec}
\else
\usepackage{fontspec}
\fi
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\usepackage[margin=1in]{geometry}
\usepackage{hyperref}
\hypersetup{unicode=true,
pdftitle={Surface Detection by Robot Movements - Research Project},
pdfauthor={Marian Dumitrascu},
pdfborder={0 0 0},
breaklinks=true}
\urlstyle{same} % don't use monospace font for urls
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{longtable,booktabs}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
%%% Use protect on footnotes to avoid problems with footnotes in titles
\let\rmarkdownfootnote\footnote%
\def\footnote{\protect\rmarkdownfootnote}
%%% Change title format to be more compact
\usepackage{titling}
% Create subtitle command for use in maketitle
\newcommand{\subtitle}[1]{
\posttitle{
\begin{center}\large#1\end{center}
}
}
\setlength{\droptitle}{-2em}
\title{Surface Detection by Robot Movements - Research Project}
\pretitle{\vspace{\droptitle}\centering\huge}
\posttitle{\par}
\author{Marian Dumitrascu}
\preauthor{\centering\large\emph}
\postauthor{\par}
\predate{\centering\large\emph}
\postdate{\par}
\date{April 5, 2019}
\usepackage{float}
\floatplacement{figure}{H}
\begin{document}
\maketitle
\hypertarget{introduction}{%
\section{Introduction}\label{introduction}}
This document is the final report for the Capstone project of
\emph{``HarvardX: PH125 Data Science - Professional Certification''}.
For this \emph{Choose Your Own} project I choose a Kaggle.com open
competition challenge. This is
\href{https://www.kaggle.com/c/career-con-2019}{\emph{CareerCon 2019 -
Help Navigate Robots}}. Here is the description from Kaggle:
\begin{quote}
\emph{In this competition, you'll help robots recognize the floor
surface they're standing on using data collected from Inertial
Measurement Units (IMU sensors).} \emph{We've collected IMU sensor data
while driving a small mobile robot over different floor surfaces on the
university premises. The task is to predict which one of the nine floor
types (carpet, tiles, concrete) the robot is on using sensor data such
as acceleration and velocity. Succeed and you'll help improve the
navigation of robots without assistance across many different surfaces,
so they won't fall down on the job.}
\end{quote}
The task is challenging, but I believe I can obtain a decent result
using techniques and tools learned in this seet of courses. Please note
that some code chunks are not showing in the PDF for aesthetic reasons.
You can find all code behind in the .Rmd version.
\hypertarget{report-structure}{%
\subsection{Report Structure}\label{report-structure}}
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
First I will describe data provided and the output expected
\item
then, will perform data analysis, visualization and get insights
\item
pre-process and transform data,
\item
analyse features and selectmost impartant ones
\item
decide the model to use, try KNN, rpart and two flavosrs of
randomForest
\item
perform the final data prediction and show the results
\item
draw some conclusions
\end{enumerate}
\hypertarget{report-and-data-location}{%
\subsection{Report and Data Location}\label{report-and-data-location}}
I keep this project on GitHub at this Url:
\url{https://github.com/mariandumitrascu/ph125_9_HelpRobotsNavigate}
Data loaded by the R scripts is kept on an AWS public S3 bucket, to be
easily loaded. This will be available for the duration of the grading.
\hypertarget{the-data}{%
\section{The Data}\label{the-data}}
\hypertarget{data-description}{%
\subsection{Data Description}\label{data-description}}
Input data from Kaggle consists in 4 files:
\begin{itemize}
\item
\emph{X\_trian.csv} and \emph{X\_test.csv} - the input data, covering
10 sensor channels and 128 measurements per time series plus three ID
columns:
\begin{itemize}
\tightlist
\item
\emph{row\_id}: The ID for this row.
\item
\emph{series\_id}: ID number for the measurement series. Foreign key
to y\_train/sample\_submission.
\item
\emph{measurement\_number}: Measurement number within the series.
\end{itemize}
The orientation channels encode the current angles of how the robot is
oriented as a quaternion (see Wikipedia). Angular velocity describes
the angle and speed of motion, and linear acceleration components
describe how the speed is changing at different times. The 10 sensor
channels are:
\begin{itemize}
\tightlist
\item
\emph{orientation\_X}
\item
\emph{orientation\_Y}
\item
\emph{orientation\_Z}
\item
\emph{orientation\_W}
\item
\emph{angular\_velocity\_X}
\item
\emph{angular\_velocity\_Y}
\item
\emph{angular\_velocity\_Z}
\item
\emph{linear\_acceleration\_X}
\item
\emph{linear\_acceleration\_Y}
\item
\emph{linear\_acceleration\_Z}
\end{itemize}
\item
\emph{y\_train.csv} - the surfaces for training set.
\begin{itemize}
\tightlist
\item
\emph{series\_id}: ID number for the measurement series.
\item
\emph{group\_id}: ID number for all of the measurements taken in a
recording session. Provided for the training set only, to enable
more cross validation strategies.
\item
\emph{surface}: labels or classes of the training data. this is the
element that need to be predicted
\end{itemize}
\item
\emph{sample\_submission.csv} - a sample submission file in the
correct format.
\end{itemize}
In this report I will split the training data into two partitions, will
fit a model on the first one, and measure it's accuracy on the second. I
will use a small part of data to make it run faster. The R script for
generating the final results will use all data.
\hypertarget{data-analysis}{%
\subsection{Data Analysis}\label{data-analysis}}
I will make the following assumptions about observations:
\begin{itemize}
\tightlist
\item
all observations are made using the same robot
\item
the interval between the 128 observations for each series is always
the same.
\item
the surface is a plane, no stairs, hills or valleys
\end{itemize}
From a physicist perspective there are three forces involved:
gravitation force, robot propulsion force, and friction force.
Gravitation force is constant. Friction force depends on the surface by
a coefficient and propulsion is an unknown variable. We need to
basically determine the friction coefficient based on a movement
pattern. Moving objects will travel longer if the surface has a lower
friction than on a surface with higher friction. On the other side,
changing direction can be slower on a surface with higher friction such
as carpet.
Quick look at the first 5 rows of the training data:
\begin{longtable}[]{@{}lrrrrrrrrrrrrrl@{}}
\toprule
row\_id & series\_id & measurement\_number & orientation\_X &
orientation\_Y & orientation\_Z & orientation\_W & angular\_velocity\_X
& angular\_velocity\_Y & angular\_velocity\_Z & linear\_acceleration\_X
& linear\_acceleration\_Y & linear\_acceleration\_Z & group\_id &
surface\tabularnewline
\midrule
\endhead
0\_0 & 0 & 0 & -0.75853 & -0.63435 & -0.10488 & -0.10597 & 0.1076500 &
0.0175610 & 0.0007674 & -0.74857 & 2.1030 & -9.7532 & 13 &
fine\_concrete\tabularnewline
0\_1 & 0 & 1 & -0.75853 & -0.63434 & -0.10490 & -0.10600 & 0.0678510 &
0.0299390 & 0.0033855 & 0.33995 & 1.5064 & -9.4128 & 13 &
fine\_concrete\tabularnewline
0\_2 & 0 & 2 & -0.75853 & -0.63435 & -0.10492 & -0.10597 & 0.0072747 &
0.0289340 & -0.0059783 & -0.26429 & 1.5922 & -8.7267 & 13 &
fine\_concrete\tabularnewline
0\_3 & 0 & 3 & -0.75852 & -0.63436 & -0.10495 & -0.10597 & -0.0130530 &
0.0194480 & -0.0089735 & 0.42684 & 1.0993 & -10.0960 & 13 &
fine\_concrete\tabularnewline
0\_4 & 0 & 4 & -0.75852 & -0.63435 & -0.10495 & -0.10596 & 0.0051349 &
0.0076517 & 0.0052452 & -0.50969 & 1.4689 & -10.4410 & 13 &
fine\_concrete\tabularnewline
\bottomrule
\end{longtable}
\hypertarget{data-distribution}{%
\subsection{Data Distribution}\label{data-distribution}}
Here is a distribution of measurements by surface in the training set:
\begin{longtable}[]{@{}lr@{}}
\toprule
surface & measurements\tabularnewline
\midrule
\endhead
carpet & 189\tabularnewline
concrete & 779\tabularnewline
fine\_concrete & 363\tabularnewline
hard\_tiles & 21\tabularnewline
hard\_tiles\_large\_space & 308\tabularnewline
soft\_pvc & 732\tabularnewline
soft\_tiles & 297\tabularnewline
tiled & 514\tabularnewline
wood & 607\tabularnewline
\bottomrule
\end{longtable}
We can see that we deal with an unbalanced data set. \emph{hard\_tiles}
have only 21 records while \emph{concrete} 779
Angular velocity data is produced by a magnetostatic sensor, it
indicates the angular speed the robot is moving in reference with earth
orientation. Here is the distribution of angular velocity by surface:
\includegraphics{PH125_9_CYO_Report_files/figure-latex/angular velocities distrib-1.pdf}
Linear acceleration data is produced by an inertial sensor. We can
approximate this later with linear distance if we consider the unit of
time to be 1. Here is the distribution of linear acceleration by
surface:
\includegraphics{PH125_9_CYO_Report_files/figure-latex/linear acc distrib-1.pdf}
We can observe noticeable differences in distribution of these variables
by surface.
Orientation data comes from a gyroscope sensor. To draw the distribution
of orientation, we'll convert quaternion values to euler angles which
are more intuitive and easier to interpret. Euler angles provide a way
to represent the 3D orientation of an object using a combination of
three rotations about different axes:
\begin{itemize}
\tightlist
\item
roll - rotation around x, noted \emph{phi},
\item
pitch - rotation around y, noted \emph{theta},
\item
yaw - rotation around z, noted \emph{psi}
\end{itemize}
See the image below (from:
\url{http://www.chrobotics.com/library/understanding-euler-angles})
\begin{figure}
\includegraphics[width=0.6\linewidth]{image_euler_angles} \caption{Euler Angles}\label{fig:euler angles image}
\end{figure}
To convert quaternions to euler angles I used \emph{Q2EA} function from
\emph{orientlib} package.(See:
\url{https://www.rdocumentation.org/packages/orientlib/versions/0.10.3})
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# define a function to convert quaternion values to euler angles. }
\NormalTok{convert_quaternions_to_euler <-}\StringTok{ }\ControlFlowTok{function}\NormalTok{(a_dataset)\{}
\CommentTok{# use Q2EA from RSpincalc to convert quaternions to euler angles}
\NormalTok{ Q <-}\StringTok{ }\NormalTok{a_dataset }\OperatorTok{%>%}\StringTok{ }\KeywordTok{select}\NormalTok{(}
\NormalTok{ orientation_X, }
\NormalTok{ orientation_Y, }
\NormalTok{ orientation_Z, }
\NormalTok{ orientation_W) }\OperatorTok{%>%}\StringTok{ }
\StringTok{ }\KeywordTok{as.matrix}\NormalTok{()}
\NormalTok{ euler_matrix <-}\StringTok{ }\KeywordTok{Q2EA}\NormalTok{(Q, }
\DataTypeTok{EulerOrder=}\StringTok{'xyz'}\NormalTok{, }
\DataTypeTok{tol =} \DecValTok{10} \OperatorTok{*}\StringTok{ }\NormalTok{.Machine}\OperatorTok{$}\NormalTok{double.eps, }
\DataTypeTok{ichk =} \OtherTok{FALSE}\NormalTok{, }
\DataTypeTok{ignoreAllChk =} \OtherTok{FALSE}\NormalTok{)}
\CommentTok{# add the new columns to the dataset}
\NormalTok{ a_dataset <-}\StringTok{ }\NormalTok{a_dataset }\OperatorTok{%>%}\StringTok{ }\KeywordTok{mutate}\NormalTok{(}
\DataTypeTok{phi =}\NormalTok{ euler_matrix[,}\DecValTok{1}\NormalTok{],}
\DataTypeTok{theta =}\NormalTok{ euler_matrix[,}\DecValTok{2}\NormalTok{], }
\DataTypeTok{psi =}\NormalTok{ euler_matrix[,}\DecValTok{3}\NormalTok{])}
\CommentTok{# remove quaternion columns}
\NormalTok{ a_dataset <-}\StringTok{ }\NormalTok{a_dataset }\OperatorTok{%>%}\StringTok{ }\KeywordTok{select}\NormalTok{(}
\OperatorTok{-}\NormalTok{orientation_X, }
\OperatorTok{-}\NormalTok{orientation_Y, }
\OperatorTok{-}\NormalTok{orientation_Z, }
\OperatorTok{-}\NormalTok{orientation_W)}
\CommentTok{# return the new dataset}
\NormalTok{ a_dataset}
\NormalTok{\}}
\NormalTok{x_train <-}\StringTok{ }\KeywordTok{convert_quaternions_to_euler}\NormalTok{(x_train)}
\NormalTok{x_test <-}\StringTok{ }\KeywordTok{convert_quaternions_to_euler}\NormalTok{(x_test)}
\end{Highlighting}
\end{Shaded}
Distribution of orientation angles by surface:
\includegraphics{PH125_9_CYO_Report_files/figure-latex/euler distrib-1.pdf}
It looks like the most noticeable differences between surfaces can be
observed in the orientation distribution.
If we consider unit of time to be 1, we can draw the path of the robot
movement. In the following figures I draw the path for several cases,
faceted by surface. I expect that the surface would influence the shape
of these paths.
\includegraphics{PH125_9_CYO_Report_files/figure-latex/draw path-1.pdf}
\includegraphics{PH125_9_CYO_Report_files/figure-latex/draw path-2.pdf}
I cannot observe obvious differences between paths on carpet vs
concrete, but maybe a machine can. An idea would be to convert all paths
to images and do an image analysis, but I will stick with a more
classical approach for this project. I'll use several models from caret
package: \emph{KNN}, \emph{rpart} and \emph{randomForest.}
\hypertarget{data-pre-processing}{%
\subsection{Data Pre-Processing}\label{data-pre-processing}}
An important step in fitting a good model is pre-processing the data. In
our case we have series of observation of 128 measurements. In this
section I will create aggregations around these measurements. These are:
mean, standard deviation, distances of segments from one point to the
next. total distance, total angle change both from magnetostatic and
gyro sensors. All of these are encapsulated in the following function:
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# function for pre-processing }
\CommentTok{# n_of_rows defaults to total number ofseries}
\CommentTok{# is_train indicates that the data is training, thus will do an extra action}
\NormalTok{pre_process <-}\StringTok{ }\ControlFlowTok{function}\NormalTok{(a_dataframe, }\DataTypeTok{n_of_rows =} \KeywordTok{nrow}\NormalTok{(a_dataframe)}\OperatorTok{/}\DecValTok{128}\NormalTok{ ) \{}
\CommentTok{# get data grouped by seeries_id and compute some means }
\NormalTok{ processed_data_df <-}\StringTok{ }\NormalTok{a_dataframe }\OperatorTok{%>%}\StringTok{ }
\StringTok{ }\KeywordTok{group_by}\NormalTok{(series_id) }\OperatorTok{%>%}\StringTok{ }
\StringTok{ }\KeywordTok{summarize}\NormalTok{(}
\DataTypeTok{phi_mean_all =} \KeywordTok{mean}\NormalTok{(phi),}
\DataTypeTok{phi_sd_all =} \KeywordTok{sd}\NormalTok{(phi),}
\DataTypeTok{phi_mean_to_sd_all =} \KeywordTok{mean}\NormalTok{(phi)}\OperatorTok{/}\KeywordTok{sd}\NormalTok{(phi),}
\DataTypeTok{theta_mean_all =} \KeywordTok{mean}\NormalTok{(theta),}
\DataTypeTok{theta_sd_all =} \KeywordTok{sd}\NormalTok{(theta),}
\DataTypeTok{theta_mean_to_sd_all =} \KeywordTok{mean}\NormalTok{(theta)}\OperatorTok{/}\KeywordTok{sd}\NormalTok{(theta),}
\DataTypeTok{psi_mean_all =} \KeywordTok{mean}\NormalTok{(psi),}
\DataTypeTok{psi_sd_all =} \KeywordTok{sd}\NormalTok{(psi),}
\DataTypeTok{psi_mean_to_sd_all =} \KeywordTok{mean}\NormalTok{(psi)}\OperatorTok{/}\KeywordTok{sd}\NormalTok{(psi),}
\CommentTok{# this is the rectangular area that surounds the path of linear movement}
\DataTypeTok{dist_area =}\NormalTok{ (}\KeywordTok{max}\NormalTok{(linear_acceleration_X) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(linear_acceleration_X)) }\OperatorTok{*}\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(linear_acceleration_Y) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(linear_acceleration_Y)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(linear_acceleration_X) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(linear_acceleration_X)) }\OperatorTok{*}\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(linear_acceleration_Z) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(linear_acceleration_Z)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(linear_acceleration_Y) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(linear_acceleration_Y)) }\OperatorTok{*}\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(linear_acceleration_Z) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(linear_acceleration_Z)),}
\CommentTok{# this is the rectangular area that surounds the path of angular movement}
\DataTypeTok{omega_area =}\NormalTok{ (}\KeywordTok{max}\NormalTok{(angular_velocity_X) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(angular_velocity_X)) }\OperatorTok{*}\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(angular_velocity_Y) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(angular_velocity_Y)) }\OperatorTok{+}
\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(angular_velocity_X) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(angular_velocity_X)) }\OperatorTok{*}\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(angular_velocity_Z) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(angular_velocity_Z)) }\OperatorTok{+}
\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(angular_velocity_Y) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(angular_velocity_Y)) }\OperatorTok{*}\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(angular_velocity_Z) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(angular_velocity_Z)),}
\DataTypeTok{euler_area =}\NormalTok{ (}\KeywordTok{max}\NormalTok{(phi) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(phi)) }\OperatorTok{*}\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(theta) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(theta)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(phi) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(phi)) }\OperatorTok{*}\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(psi) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(psi)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(theta) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(theta)) }\OperatorTok{*}\StringTok{ }\NormalTok{(}\KeywordTok{max}\NormalTok{(psi) }\OperatorTok{-}\StringTok{ }\KeywordTok{min}\NormalTok{(psi)),}
\DataTypeTok{dist_mean_x =} \KeywordTok{mean}\NormalTok{(linear_acceleration_X),}
\DataTypeTok{dist_mean_y =} \KeywordTok{mean}\NormalTok{(linear_acceleration_Y),}
\DataTypeTok{dist_mean_z =} \KeywordTok{mean}\NormalTok{(linear_acceleration_Z),}
\DataTypeTok{omega_mean_x =} \KeywordTok{mean}\NormalTok{(angular_velocity_X),}
\DataTypeTok{omega_mean_y =} \KeywordTok{mean}\NormalTok{(angular_velocity_Y),}
\DataTypeTok{omega_mean_Z =} \KeywordTok{mean}\NormalTok{(angular_velocity_Z),}
\DataTypeTok{dist_sd_x =} \KeywordTok{sd}\NormalTok{(linear_acceleration_X),}
\DataTypeTok{dist_sd_y =} \KeywordTok{sd}\NormalTok{(linear_acceleration_Y),}
\DataTypeTok{dist_sd_z =} \KeywordTok{sd}\NormalTok{(linear_acceleration_Z),}
\DataTypeTok{omega_sd_x =} \KeywordTok{sd}\NormalTok{(angular_velocity_X),}
\DataTypeTok{omega_sd_y =} \KeywordTok{sd}\NormalTok{(angular_velocity_Y),}
\DataTypeTok{omega_sd_Z =} \KeywordTok{sd}\NormalTok{(angular_velocity_Z) }
\NormalTok{ ) }\OperatorTok{%>%}\StringTok{ }
\StringTok{ }\KeywordTok{slice}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\NormalTok{n_of_rows)}
\CommentTok{# define an empty data frame with summary metrics that we'll use for each set of 128 observations}
\NormalTok{ metrics <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"dist_total"}\NormalTok{,}\StringTok{"dist_max"}\NormalTok{,}\StringTok{"dist_min"}\NormalTok{,}\StringTok{"dist_max_to_min"}\NormalTok{,}\StringTok{"dist_mean"}\NormalTok{,}\StringTok{"dist_sd"}\NormalTok{,}\StringTok{"dist_mean_to_sd"}\NormalTok{,}
\StringTok{"omega_total"}\NormalTok{,}\StringTok{"omega_max"}\NormalTok{,}\StringTok{"omega_min"}\NormalTok{,}\StringTok{"omega_max_to_min"}\NormalTok{,}\StringTok{"omega_mean"}\NormalTok{,}\StringTok{"omega_sd"}\NormalTok{,}\StringTok{"omega_mean_to_sd"}\NormalTok{,}
\StringTok{"phi_total"}\NormalTok{,}\StringTok{"phi_max"}\NormalTok{,}\StringTok{"phi_min"}\NormalTok{,}\StringTok{"phi_mean"}\NormalTok{,}\StringTok{"phi_sd"}\NormalTok{,}\StringTok{"phi_mean_to_sd"}\NormalTok{,}
\StringTok{"theta_total"}\NormalTok{,}\StringTok{"theta_max"}\NormalTok{,}\StringTok{"theta_min"}\NormalTok{,}\StringTok{"theta_mean"}\NormalTok{,}\StringTok{"theta_sd"}\NormalTok{,}\StringTok{"theta_mean_to_sd"}\NormalTok{,}
\StringTok{"psi_total"}\NormalTok{,}\StringTok{"psi_max"}\NormalTok{,}\StringTok{"psi_min"}\NormalTok{,}\StringTok{"psi_mean"}\NormalTok{,}\StringTok{"psi_sd"}\NormalTok{,}\StringTok{"psi_mean_to_sd"}\NormalTok{,}
\StringTok{"euler_total"}\NormalTok{,}\StringTok{"euler_max"}\NormalTok{,}\StringTok{"euler_min"}\NormalTok{,}\StringTok{"euler_mean"}\NormalTok{,}\StringTok{"euler_sd"}\NormalTok{,}\StringTok{"euler_mean_to_sd"}\NormalTok{)}
\NormalTok{ tmp_df <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\KeywordTok{matrix}\NormalTok{(}\DataTypeTok{ncol =} \KeywordTok{length}\NormalTok{(metrics), }\DataTypeTok{nrow =} \DecValTok{0}\NormalTok{) )}
\KeywordTok{colnames}\NormalTok{(tmp_df) <-}\StringTok{ }\NormalTok{metrics}
\CommentTok{# loop over each series and compute aggegations}
\CommentTok{# should use apply type of function here, but I use "for" until I master the apply}
\ControlFlowTok{for}\NormalTok{ (s_id }\ControlFlowTok{in}\NormalTok{ processed_data_df}\OperatorTok{$}\NormalTok{series_id)}
\NormalTok{ \{}
\CommentTok{# get current measurement set}
\NormalTok{ this_chunk_df <-}\StringTok{ }\NormalTok{a_dataframe }\OperatorTok{%>%}\StringTok{ }\KeywordTok{filter}\NormalTok{(series_id }\OperatorTok{==}\StringTok{ }\NormalTok{s_id)}
\CommentTok{# select only columns we are interested in }
\NormalTok{ this_chunk_df <-}\StringTok{ }\NormalTok{this_chunk_df }\OperatorTok{%>%}\StringTok{ }
\StringTok{ }\KeywordTok{select}\NormalTok{(}
\NormalTok{ phi, theta, psi, }
\NormalTok{ angular_velocity_X, angular_velocity_Y, angular_velocity_Z,}
\NormalTok{ linear_acceleration_X, linear_acceleration_Y, linear_acceleration_Z)}
\CommentTok{# this is a vector with euclidian distances from one point to the next}
\NormalTok{ dist_v <-}\StringTok{ }\KeywordTok{sqrt}\NormalTok{(}\KeywordTok{diff}\NormalTok{(this_chunk_df}\OperatorTok{$}\NormalTok{linear_acceleration_X)}\OperatorTok{^}\DecValTok{2} \OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{diff}\NormalTok{(this_chunk_df}\OperatorTok{$}\NormalTok{linear_acceleration_Y)}\OperatorTok{^}\DecValTok{2} \OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{diff}\NormalTok{(this_chunk_df}\OperatorTok{$}\NormalTok{linear_acceleration_Z)}\OperatorTok{^}\DecValTok{2}\NormalTok{)}
\NormalTok{ omega_v <-}\StringTok{ }\KeywordTok{sqrt}\NormalTok{(}\KeywordTok{diff}\NormalTok{(this_chunk_df}\OperatorTok{$}\NormalTok{angular_velocity_X)}\OperatorTok{^}\DecValTok{2} \OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{diff}\NormalTok{(this_chunk_df}\OperatorTok{$}\NormalTok{angular_velocity_Y)}\OperatorTok{^}\DecValTok{2} \OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{diff}\NormalTok{(this_chunk_df}\OperatorTok{$}\NormalTok{angular_velocity_Z)}\OperatorTok{^}\DecValTok{2}\NormalTok{)}
\NormalTok{ phi_v <-}\StringTok{ }\KeywordTok{abs}\NormalTok{(}\KeywordTok{diff}\NormalTok{(this_chunk_df}\OperatorTok{$}\NormalTok{phi))}
\NormalTok{ theta_v <-}\StringTok{ }\KeywordTok{abs}\NormalTok{(}\KeywordTok{diff}\NormalTok{(this_chunk_df}\OperatorTok{$}\NormalTok{theta))}
\NormalTok{ psi_v <-}\StringTok{ }\KeywordTok{abs}\NormalTok{(}\KeywordTok{diff}\NormalTok{(this_chunk_df}\OperatorTok{$}\NormalTok{psi))}
\CommentTok{# fill or temp data frame with summary computations for our 128 measurement set}
\NormalTok{ tmp_df <-}\StringTok{ }\KeywordTok{bind_rows}\NormalTok{(tmp_df, }\KeywordTok{data_frame}\NormalTok{(}
\CommentTok{# all features starting with "dist_" refers to linear movement}
\DataTypeTok{dist_total =} \KeywordTok{sum}\NormalTok{(dist_v),}
\DataTypeTok{dist_max =} \KeywordTok{max}\NormalTok{(dist_v),}
\DataTypeTok{dist_min =} \KeywordTok{min}\NormalTok{(dist_v),}
\DataTypeTok{dist_max_to_min =} \KeywordTok{max}\NormalTok{(dist_v)}\OperatorTok{/}\KeywordTok{min}\NormalTok{(dist_v),}
\DataTypeTok{dist_mean =} \KeywordTok{mean}\NormalTok{(dist_v),}
\DataTypeTok{dist_sd =} \KeywordTok{sd}\NormalTok{(dist_v),}
\DataTypeTok{dist_mean_to_sd =} \KeywordTok{mean}\NormalTok{(dist_v)}\OperatorTok{/}\KeywordTok{sd}\NormalTok{(dist_v), }\CommentTok{# reciprocal coef of variation}
\CommentTok{# all features starting with "omega_" refers to angle velocity measurments}
\DataTypeTok{omega_total =} \KeywordTok{sum}\NormalTok{(omega_v),}
\DataTypeTok{omega_max =} \KeywordTok{max}\NormalTok{(omega_v),}
\DataTypeTok{omega_min =} \KeywordTok{min}\NormalTok{(omega_v),}
\DataTypeTok{omega_max_to_min =} \KeywordTok{max}\NormalTok{(omega_v)}\OperatorTok{/}\KeywordTok{min}\NormalTok{(omega_v),}
\DataTypeTok{omega_mean =} \KeywordTok{mean}\NormalTok{(omega_v),}
\DataTypeTok{omega_sd =} \KeywordTok{sd}\NormalTok{(omega_v),}
\DataTypeTok{omega_mean_to_sd =} \KeywordTok{mean}\NormalTok{(omega_v)}\OperatorTok{/}\KeywordTok{sd}\NormalTok{(omega_v), }\CommentTok{# reciprocal coef of variation}
\CommentTok{# phi, theta and psi reffers to roll, pitch and yaw rotations}
\DataTypeTok{phi_total =} \KeywordTok{sum}\NormalTok{(phi_v),}
\DataTypeTok{phi_max =} \KeywordTok{max}\NormalTok{(phi_v),}
\DataTypeTok{phi_min =} \KeywordTok{min}\NormalTok{(phi_v),}
\DataTypeTok{phi_mean =} \KeywordTok{mean}\NormalTok{(phi_v),}
\DataTypeTok{phi_sd =} \KeywordTok{sd}\NormalTok{(phi_v),}
\DataTypeTok{phi_mean_to_sd =} \KeywordTok{mean}\NormalTok{(phi_v)}\OperatorTok{/}\KeywordTok{sd}\NormalTok{(phi_v),}
\DataTypeTok{theta_total =} \KeywordTok{sum}\NormalTok{(theta_v),}
\DataTypeTok{theta_max =} \KeywordTok{max}\NormalTok{(theta_v),}
\DataTypeTok{theta_min =} \KeywordTok{min}\NormalTok{(theta_v),}
\DataTypeTok{theta_mean =} \KeywordTok{mean}\NormalTok{(theta_v),}
\DataTypeTok{theta_sd =} \KeywordTok{sd}\NormalTok{(theta_v),}
\DataTypeTok{theta_mean_to_sd =} \KeywordTok{mean}\NormalTok{(theta_v)}\OperatorTok{/}\KeywordTok{sd}\NormalTok{(theta_v),}
\DataTypeTok{psi_total =} \KeywordTok{sum}\NormalTok{(psi_v),}
\DataTypeTok{psi_max =} \KeywordTok{max}\NormalTok{(psi_v),}
\DataTypeTok{psi_min =} \KeywordTok{min}\NormalTok{(psi_v),}
\DataTypeTok{psi_mean =} \KeywordTok{mean}\NormalTok{(psi_v),}
\DataTypeTok{psi_sd =} \KeywordTok{sd}\NormalTok{(psi_v),}
\DataTypeTok{psi_mean_to_sd =} \KeywordTok{mean}\NormalTok{(psi_v)}\OperatorTok{/}\KeywordTok{sd}\NormalTok{(psi_v),}
\CommentTok{# features starting with "euler_" refers to }
\CommentTok{# agragation of all phi, theta and psi rotations}
\DataTypeTok{euler_total =} \KeywordTok{sum}\NormalTok{(phi_v }\OperatorTok{+}\StringTok{ }\NormalTok{theta_v }\OperatorTok{+}\StringTok{ }\NormalTok{psi_v), }
\DataTypeTok{euler_max =} \KeywordTok{max}\NormalTok{(phi_v }\OperatorTok{+}\StringTok{ }\NormalTok{theta_v }\OperatorTok{+}\StringTok{ }\NormalTok{psi_v),}
\DataTypeTok{euler_min =} \KeywordTok{min}\NormalTok{(phi_v }\OperatorTok{+}\StringTok{ }\NormalTok{theta_v }\OperatorTok{+}\StringTok{ }\NormalTok{psi_v),}
\DataTypeTok{euler_mean =} \KeywordTok{mean}\NormalTok{(phi_v }\OperatorTok{+}\StringTok{ }\NormalTok{theta_v }\OperatorTok{+}\StringTok{ }\NormalTok{psi_v),}
\DataTypeTok{euler_sd =} \KeywordTok{sd}\NormalTok{(phi_v }\OperatorTok{+}\StringTok{ }\NormalTok{theta_v }\OperatorTok{+}\StringTok{ }\NormalTok{psi_v),}
\DataTypeTok{euler_mean_to_sd =} \KeywordTok{mean}\NormalTok{(phi_v }\OperatorTok{+}\StringTok{ }\NormalTok{theta_v }\OperatorTok{+}\StringTok{ }\NormalTok{psi_v)}\OperatorTok{/}\KeywordTok{sd}\NormalTok{(phi_v }\OperatorTok{+}\StringTok{ }\NormalTok{theta_v }\OperatorTok{+}\StringTok{ }\NormalTok{psi_v)}
\NormalTok{ ))}
\NormalTok{ \} }\CommentTok{# end of for over series}
\CommentTok{# add the summary computations to the data set of series}
\NormalTok{ processed_data_df <-}\StringTok{ }\KeywordTok{bind_cols}\NormalTok{(processed_data_df, tmp_df)}
\CommentTok{# more features}
\CommentTok{# I added thesee as an experimentation after observing the dependencies graphs}
\CommentTok{# will explain later}
\NormalTok{ processed_data_df <-}\StringTok{ }\NormalTok{processed_data_df }\OperatorTok{%>%}\StringTok{ }\KeywordTok{mutate}\NormalTok{(}
\CommentTok{# this is an agular momentum }
\DataTypeTok{f1 =} \KeywordTok{log}\NormalTok{(dist_mean_to_sd}\OperatorTok{*}\NormalTok{omega_mean_to_sd),}
\CommentTok{# this is an angular momentum }
\DataTypeTok{f2 =} \KeywordTok{log}\NormalTok{(dist_total}\OperatorTok{*}\NormalTok{omega_total),}
\CommentTok{# this is the angle between theta and psi vectors}
\DataTypeTok{f3 =} \KeywordTok{abs}\NormalTok{(}\KeywordTok{atan}\NormalTok{(theta_mean_all}\OperatorTok{/}\NormalTok{psi_mean_all))) }
\CommentTok{# return the proceessed data}
\NormalTok{ processed_data_df}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}
Pre-process the data and save it to a local file. This is to save time
for further analysis by skipping the pre-processing procedure. I'm using
\emph{tic()} \emph{toc()} functions from \emph{tictoc} package to
display processing time.
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# pre-process train and test data sets}
\KeywordTok{tic}\NormalTok{(}\StringTok{"process train data"}\NormalTok{)}
\NormalTok{x_train_processed <-}\StringTok{ }\KeywordTok{pre_process}\NormalTok{(x_train)}
\KeywordTok{toc}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## process train data: 50.44 sec elapsed
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{tic}\NormalTok{(}\StringTok{"process test data"}\NormalTok{)}
\NormalTok{x_test_processed <-}\StringTok{ }\KeywordTok{pre_process}\NormalTok{(x_test)}
\KeywordTok{toc}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## process test data: 49.1 sec elapsed
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# rejoin train data with the labels data set}
\NormalTok{x_train_processed <-}\StringTok{ }\NormalTok{x_train_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{left_join}\NormalTok{(y_train, }\DataTypeTok{by =} \StringTok{"series_id"}\NormalTok{)}
\CommentTok{# create a folder "data" if doesnt exist}
\ControlFlowTok{if}\NormalTok{ (}\OperatorTok{!}\KeywordTok{dir.exists}\NormalTok{(}\StringTok{"data"}\NormalTok{)) }\KeywordTok{dir.create}\NormalTok{(}\StringTok{"data"}\NormalTok{)}
\CommentTok{# save processed data, and use these files from now on}
\KeywordTok{write_csv}\NormalTok{(x_train_processed, }\StringTok{"data/x_train_processed.csv"}\NormalTok{)}
\KeywordTok{write_csv}\NormalTok{(x_test_processed, }\StringTok{"data/x_test_processed.csv"}\NormalTok{)}
\CommentTok{# clean some variables and the environment}
\KeywordTok{rm}\NormalTok{(x_train, x_test, y_train)}
\KeywordTok{rm}\NormalTok{(x_train_processed, x_test_processed)}
\end{Highlighting}
\end{Shaded}
Load pre-processed data from hard-disk and use them from here. In this
report I will use a smaller data set from the training data, to make
things work faster. Full data set will be used in the final script
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x_train_processed_from_file <-}\StringTok{ }\KeywordTok{read_csv}\NormalTok{(}\StringTok{"data/x_train_processed.csv"}\NormalTok{)}
\NormalTok{x_test_processed_from_file <-}\StringTok{ }\KeywordTok{read_csv}\NormalTok{(}\StringTok{"data/x_test_processed.csv"}\NormalTok{)}
\CommentTok{# if we load data from a file, convert surface to factor}
\NormalTok{x_train_processed_from_file <-}\StringTok{ }\NormalTok{x_train_processed_from_file }\OperatorTok{%>%}\StringTok{ }\KeywordTok{mutate}\NormalTok{(}\DataTypeTok{surface =} \KeywordTok{as.factor}\NormalTok{(surface))}
\CommentTok{# use a smaller set of datab to save time}
\NormalTok{x_train_processed_from_file <-}\StringTok{ }\NormalTok{x_train_processed_from_file }\OperatorTok{%>%}\StringTok{ }\KeywordTok{slice}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{1200}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\hypertarget{correlated-predictors}{%
\subsection{Correlated Predictors}\label{correlated-predictors}}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# from now we can load pre-processed data from csv files without the need to pre-process again}
\NormalTok{x_test_processed <-}\StringTok{ }\NormalTok{x_test_processed_from_file}
\NormalTok{x_train_processed <-}\StringTok{ }\NormalTok{x_train_processed_from_file }
\end{Highlighting}
\end{Shaded}
We created 65 features, lets trim some of them. First, I will identify
those that are strongly correlated. Lets plot the correlation matrix:
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(RColorBrewer)}
\KeywordTok{library}\NormalTok{(corrplot)}
\NormalTok{x_matrix <-}\StringTok{ }\NormalTok{x_train_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{select}\NormalTok{(}\OperatorTok{-}\NormalTok{series_id, }\OperatorTok{-}\NormalTok{group_id, }\OperatorTok{-}\NormalTok{surface) }\OperatorTok{%>%}\StringTok{ }
\StringTok{ }\KeywordTok{as.matrix}\NormalTok{()}
\NormalTok{x_cor <-}\StringTok{ }\KeywordTok{cor}\NormalTok{(x_matrix, }\DataTypeTok{use =} \StringTok{"pairwise.complete"}\NormalTok{)}
\KeywordTok{corrplot}\NormalTok{(x_cor,}\DataTypeTok{method =} \StringTok{"square"}\NormalTok{, }\DataTypeTok{number.cex =} \FloatTok{.5}\NormalTok{, }\DataTypeTok{tl.cex =} \FloatTok{0.6}\NormalTok{ ,}\DataTypeTok{order =} \StringTok{"hclust"}\NormalTok{, }\DataTypeTok{title =} \StringTok{"Correlation Matrix"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{PH125_9_CYO_Report_files/figure-latex/correlation matrix before-1.pdf}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{rm}\NormalTok{(x_matrix, x_cor)}
\end{Highlighting}
\end{Shaded}
Using function \emph{findCorrelation} from \emph{caret} we can remove
features that are highly correlated
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# convert both test and train data to matrix in order to analyse feature corelation}
\NormalTok{x_train_matrix <-}\StringTok{ }\NormalTok{x_train_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{select}\NormalTok{(}\OperatorTok{-}\NormalTok{surface, }\OperatorTok{-}\NormalTok{series_id) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{as.matrix}\NormalTok{()}
\NormalTok{x_test_matrix <-}\StringTok{ }\NormalTok{x_test_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{select}\NormalTok{(}\OperatorTok{-}\NormalTok{series_id) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{as.matrix}\NormalTok{()}
\CommentTok{# find features that are high correlated }
\CommentTok{# find linear dependencies and eliminate them}
\NormalTok{names_to_remove_test <-}\StringTok{ }\KeywordTok{findCorrelation}\NormalTok{(}\KeywordTok{cor}\NormalTok{(x_test_matrix), }\DataTypeTok{cutoff =} \FloatTok{0.95}\NormalTok{, }\DataTypeTok{names =} \OtherTok{TRUE}\NormalTok{, }\DataTypeTok{verbose =} \OtherTok{FALSE}\NormalTok{, }\DataTypeTok{exact=}\OtherTok{TRUE}\NormalTok{)}
\CommentTok{# remove correlated features from both train and test sets}
\NormalTok{x_train_processed <-}\StringTok{ }\NormalTok{x_train_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{select}\NormalTok{(}\OperatorTok{-}\NormalTok{names_to_remove_test) }
\NormalTok{x_test_processed <-}\StringTok{ }\NormalTok{x_test_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{select}\NormalTok{(}\OperatorTok{-}\NormalTok{names_to_remove_test) }
\CommentTok{# draw again the correlation matrix}
\NormalTok{x_matrix <-}\StringTok{ }\NormalTok{x_train_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{select}\NormalTok{(}\OperatorTok{-}\NormalTok{series_id, }\OperatorTok{-}\NormalTok{group_id, }\OperatorTok{-}\NormalTok{surface) }\OperatorTok{%>%}\StringTok{ }
\StringTok{ }\KeywordTok{as.matrix}\NormalTok{()}
\NormalTok{x_cor <-}\StringTok{ }\KeywordTok{cor}\NormalTok{(x_matrix, }\DataTypeTok{use =} \StringTok{"pairwise.complete"}\NormalTok{)}
\KeywordTok{corrplot}\NormalTok{(x_cor,}\DataTypeTok{method =} \StringTok{"square"}\NormalTok{, }\DataTypeTok{number.cex =} \FloatTok{.5}\NormalTok{, }\DataTypeTok{tl.cex =} \FloatTok{0.6}\NormalTok{ ,}\DataTypeTok{order =} \StringTok{"hclust"}\NormalTok{, }\DataTypeTok{title =} \StringTok{"Correlation Matrix"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{PH125_9_CYO_Report_files/figure-latex/correlation matrix after-1.pdf}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{rm}\NormalTok{(x_train_matrix, x_test_matrix, x_matrix, x_cor)}
\end{Highlighting}
\end{Shaded}
The number of predictors decreased to 42
Another useful technique in pre-processing is centering around means and
scaling the predictors:
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# pre-process the data, center and scale the values across all predictors}
\NormalTok{pre_process <-}\StringTok{ }\NormalTok{x_train_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{select}\NormalTok{(}\OperatorTok{-}\NormalTok{series_id, }\OperatorTok{-}\NormalTok{group_id) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{preProcess}\NormalTok{(}\DataTypeTok{method =} \KeywordTok{c}\NormalTok{(}\StringTok{"center"}\NormalTok{, }\StringTok{"scale"}\NormalTok{))}
\NormalTok{x_train_processed <-}\StringTok{ }\KeywordTok{predict}\NormalTok{(pre_process, x_train_processed)}
\NormalTok{x_test_processed <-}\StringTok{ }\KeywordTok{predict}\NormalTok{(pre_process, x_test_processed)}
\KeywordTok{rm}\NormalTok{(pre_process)}
\end{Highlighting}
\end{Shaded}
\hypertarget{data-visualization}{%
\subsection{Data Visualization}\label{data-visualization}}
Before moving forward with more feature selection, I will plot some
interesting visualizations of the feature dependencies. These are plots
of orientation angles as sums over all 128 points. \emph{phi},
\emph{theta} and \emph{psi} are euler orientation angles.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x_train_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(}\KeywordTok{aes}\NormalTok{(phi_mean_all, psi_mean_all, }\DataTypeTok{fill =}\NormalTok{ surface)) }\OperatorTok{+}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{color =}\NormalTok{ surface)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{cex=}\DecValTok{6}\NormalTok{, }\DataTypeTok{pch=}\DecValTok{21}\NormalTok{) }\OperatorTok{+}
\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"phi_mean vs. psi_mean"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{PH125_9_CYO_Report_files/figure-latex/euler visualizations-1.pdf}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x_train_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(}\KeywordTok{aes}\NormalTok{(theta_mean_all, psi_mean_all, }\DataTypeTok{fill =}\NormalTok{ surface)) }\OperatorTok{+}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{color =}\NormalTok{ surface)) }\OperatorTok{+}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{cex=}\DecValTok{6}\NormalTok{, }\DataTypeTok{pch=}\DecValTok{21}\NormalTok{) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"theta_mean vs. psi_mean"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{PH125_9_CYO_Report_files/figure-latex/euler visualizations-2.pdf}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x_train_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(}\KeywordTok{aes}\NormalTok{(theta_mean_all, phi_mean_all, }\DataTypeTok{fill =}\NormalTok{ surface)) }\OperatorTok{+}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{color =}\NormalTok{ surface)) }\OperatorTok{+}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{cex=}\DecValTok{6}\NormalTok{, }\DataTypeTok{pch=}\DecValTok{21}\NormalTok{) }\OperatorTok{+}
\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"theta_mean vs. phi_mean"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{PH125_9_CYO_Report_files/figure-latex/euler visualizations-3.pdf}
I would speculate that all points that are not on the global pattern are
actually outlines, but I did not explore that hypothesis.
The following is the angle between \emph{theta} - \emph{psi} vectors and
the angular momentum:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x_train_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(}\KeywordTok{aes}\NormalTok{(f2, f3, }\DataTypeTok{fill =}\NormalTok{ surface)) }\OperatorTok{+}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{color =}\NormalTok{ surface)) }\OperatorTok{+}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{cex=}\DecValTok{6}\NormalTok{, }\DataTypeTok{pch=}\DecValTok{21}\NormalTok{) }\OperatorTok{+}
\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"theta-psi angle vs. angular momentum"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{PH125_9_CYO_Report_files/figure-latex/theta psi and momentum-1.pdf}
We can see some separations by surfaces that may allow a KNN model to
work. In fact if we run KNN for just \emph{surface \textasciitilde{} f2
+ f3} we will get an accuracy of 0.56 which is promising.
\hypertarget{more-feature-selection}{%
\subsection{More Feature Selection}\label{more-feature-selection}}
I will continue now removing more features based on their importance in
a randomForest model. I will split the training set into two partitions:
\emph{x\_train\_for\_train} used for training, and
\emph{x\_train\_for\_test} to measure the accuracy. Then will plot
variable importance. Will not use here any tuning, since this is not the
final model.
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# partition x_train_processed data for training and testing }
\NormalTok{test_index <-}\StringTok{ }\KeywordTok{createDataPartition}\NormalTok{(}\DataTypeTok{y =}\NormalTok{ x_train_processed}\OperatorTok{$}\NormalTok{surface, }\DataTypeTok{times =} \DecValTok{1}\NormalTok{, }\DataTypeTok{p =} \FloatTok{0.5}\NormalTok{, }\DataTypeTok{list =} \OtherTok{FALSE}\NormalTok{)}
\NormalTok{x_train_for_train <-}\StringTok{ }\NormalTok{x_train_processed[}\OperatorTok{-}\NormalTok{test_index, ]}
\NormalTok{x_train_for_test <-}\StringTok{ }\NormalTok{x_train_processed[test_index, ]}
\KeywordTok{rm}\NormalTok{(test_index)}
\NormalTok{model_fit <-}\StringTok{ }\KeywordTok{randomForest}\NormalTok{(}
\NormalTok{ surface }\OperatorTok{~}\StringTok{ }\NormalTok{., }
\DataTypeTok{metric =} \StringTok{"Accuracy"}\NormalTok{, }
\CommentTok{# remove series_id, group_id and select only 500 points}
\DataTypeTok{data =} \KeywordTok{slice}\NormalTok{(}\KeywordTok{select}\NormalTok{(x_train_for_train, }\OperatorTok{-}\NormalTok{series_id, }\OperatorTok{-}\NormalTok{group_id), }\DecValTok{1}\OperatorTok{:}\DecValTok{500}\NormalTok{)}
\NormalTok{ )}
\NormalTok{y_hat <-}\StringTok{ }\KeywordTok{predict}\NormalTok{(model_fit, }\KeywordTok{select}\NormalTok{(x_train_for_test, }\OperatorTok{-}\NormalTok{series_id))}
\NormalTok{y_test <-}\StringTok{ }\NormalTok{x_train_for_test}\OperatorTok{$}\NormalTok{surface}
\CommentTok{# # we dont show accuracy and confusion table for now}
\CommentTok{# conf_matrix <- confusionMatrix(y_hat, x_train_for_test$surface)}
\CommentTok{# conf_matrix$overall["Accuracy"]}
\CommentTok{# conf_matrix$table %>% knitr::kable()}
\NormalTok{importance <-}\StringTok{ }\KeywordTok{importance}\NormalTok{(model_fit)}
\NormalTok{importance <-}\StringTok{ }\NormalTok{importance[}\KeywordTok{order}\NormalTok{(importance[,}\DecValTok{1}\NormalTok{], }\DataTypeTok{decreasing =} \OtherTok{TRUE}\NormalTok{), ]}
\CommentTok{# plot variable importance}
\KeywordTok{varImpPlot}\NormalTok{(model_fit)}
\end{Highlighting}
\end{Shaded}
\includegraphics{PH125_9_CYO_Report_files/figure-latex/randomForest-1.pdf}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# this is the list of variables in order of importance, decreasing}
\NormalTok{features <-}\StringTok{ }\KeywordTok{names}\NormalTok{(importance)}
\end{Highlighting}
\end{Shaded}
We observe following variables does not help prediction too much:
\emph{theta\_min}, \emph{omega\_max\_to\_min}, \emph{dist\_mean\_y},
\emph{omega\_mean\_x}, \emph{dist\_mean\_x}, \emph{dist\_mean\_z} So I
will remove them:
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# remove columns do not contribute to classification}
\NormalTok{x_train_processed <-}\StringTok{ }\NormalTok{x_train_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{select}\NormalTok{(}\OperatorTok{-}\NormalTok{theta_min, }\OperatorTok{-}\NormalTok{omega_max_to_min, }\OperatorTok{-}\NormalTok{dist_mean_y, }\OperatorTok{-}\NormalTok{omega_mean_x, }\OperatorTok{-}\NormalTok{dist_mean_x, }\OperatorTok{-}\NormalTok{dist_mean_z)}
\NormalTok{x_test_processed <-}\StringTok{ }\NormalTok{x_test_processed }\OperatorTok{%>%}\StringTok{ }\KeywordTok{select}\NormalTok{(}\OperatorTok{-}\NormalTok{theta_min, }\OperatorTok{-}\NormalTok{omega_max_to_min, }\OperatorTok{-}\NormalTok{dist_mean_y, }\OperatorTok{-}\NormalTok{omega_mean_x, }\OperatorTok{-}\NormalTok{dist_mean_x, }\OperatorTok{-}\NormalTok{dist_mean_z)}
\end{Highlighting}
\end{Shaded}
\hypertarget{the-model}{%
\section{The Model}\label{the-model}}
It is time now to fit some models. I tried many models on the side but I
will present in this report only
\begin{itemize}
\tightlist
\item
KNN,
\item
rpart,
\item
randomForest,
\item
randomForest assembly
\end{itemize}
Please note that the code in this report is using only 1000 records from
training, that is further partitioned in train and test with 0.5 ratio
I've done this to make it run faster. I am using full data set in the R
script and also I will present the final results from Kaggle (i think I
said this before).
\hypertarget{knn-model}{%
\subsection{KNN Model}\label{knn-model}}
KNN is getting incrementally slower with the number of features. So for
KNN I used only the most important variables as part of the formula.
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# use most important variables to fit KNN}
\NormalTok{model_fit <-}\StringTok{ }\KeywordTok{train}\NormalTok{(surface }\OperatorTok{~}\StringTok{ }\NormalTok{f3 }\OperatorTok{+}\StringTok{ }\NormalTok{f2 }\OperatorTok{+}\StringTok{ }\NormalTok{phi_mean_all }\OperatorTok{+}\StringTok{ }\NormalTok{theta_mean_all }\OperatorTok{+}\StringTok{ }\NormalTok{psi_mean_all, }\DataTypeTok{method =} \StringTok{"knn"}\NormalTok{, }
\DataTypeTok{tuneGrid =} \KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{k =} \KeywordTok{seq}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{100}\NormalTok{, }\DecValTok{2}\NormalTok{)), }
\DataTypeTok{data =} \KeywordTok{select}\NormalTok{(x_train_for_train, }\OperatorTok{-}\NormalTok{series_id, }\OperatorTok{-}\NormalTok{group_id))}
\KeywordTok{ggplot}\NormalTok{(model_fit) }
\end{Highlighting}
\end{Shaded}
\includegraphics{PH125_9_CYO_Report_files/figure-latex/KNN-1.pdf}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# get confusion matrix and display it together with the results}
\NormalTok{y_hat <-}\StringTok{ }\KeywordTok{predict}\NormalTok{(model_fit, }\KeywordTok{select}\NormalTok{(x_train_for_test, }\OperatorTok{-}\NormalTok{series_id, }\OperatorTok{-}\NormalTok{group_id), }\DataTypeTok{type =} \StringTok{"raw"}\NormalTok{)}
\NormalTok{conf_matrix <-}\StringTok{ }\KeywordTok{confusionMatrix}\NormalTok{(y_hat, x_train_for_test}\OperatorTok{$}\NormalTok{surface)}
\CommentTok{# display confusion matrix}
\NormalTok{conf_matrix}\OperatorTok{$}\NormalTok{table }\OperatorTok{%>%}\StringTok{ }\NormalTok{knitr}\OperatorTok{::}\KeywordTok{kable}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\begin{longtable}[]{@{}lrrrrrrrrr@{}}
\toprule
& carpet & concrete & fine\_concrete & hard\_tiles &
hard\_tiles\_large\_space & soft\_pvc & soft\_tiles & tiled &
wood\tabularnewline
\midrule
\endhead
carpet & 20 & 4 & 0 & 0 & 0 & 5 & 0 & 2 & 2\tabularnewline
concrete & 5 & 84 & 0 & 0 & 0 & 5 & 0 & 6 & 5\tabularnewline
fine\_concrete & 0 & 0 & 33 & 0 & 5 & 1 & 1 & 2 & 3\tabularnewline
hard\_tiles & 1 & 0 & 0 & 7 & 0 & 0 & 0 & 0 & 0\tabularnewline
hard\_tiles\_large\_space & 0 & 2 & 0 & 0 & 23 & 2 & 0 & 1 &
3\tabularnewline
soft\_pvc & 4 & 4 & 4 & 0 & 2 & 114 & 7 & 5 & 3\tabularnewline
soft\_tiles & 1 & 1 & 4 & 0 & 6 & 8 & 91 & 1 & 0\tabularnewline
tiled & 7 & 5 & 0 & 1 & 0 & 0 & 2 & 42 & 5\tabularnewline
wood & 0 & 2 & 2 & 0 & 1 & 2 & 1 & 9 & 47\tabularnewline
\bottomrule
\end{longtable}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# create a data frame to store Accuracy results by model}
\NormalTok{model_results <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{Model =} \StringTok{"KNN"}\NormalTok{, }\DataTypeTok{Accuracy =}\NormalTok{ conf_matrix}\OperatorTok{$}\NormalTok{overall[}\StringTok{"Accuracy"}\NormalTok{])}
\NormalTok{model_results }\OperatorTok{%>%}\StringTok{ }\NormalTok{knitr}\OperatorTok{::}\KeywordTok{kable}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\begin{longtable}[]{@{}llr@{}}
\toprule
& Model & Accuracy\tabularnewline
\midrule
\endhead
Accuracy & KNN & 0.7645108\tabularnewline
\bottomrule
\end{longtable}
\hypertarget{rpart-model}{%
\subsection{rpart Model}\label{rpart-model}}
Another model I tried is rpart (Recursive Partitioning And Regression
Tree). I will tune it on the complexity coeficient
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{model_fit <-}\StringTok{ }\KeywordTok{train}\NormalTok{(surface }\OperatorTok{~}\StringTok{ }\NormalTok{., }
\DataTypeTok{method =} \StringTok{"rpart"}\NormalTok{,}
\DataTypeTok{tuneGrid =} \KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{cp =} \KeywordTok{seq}\NormalTok{(}\DecValTok{0}\NormalTok{, }\FloatTok{0.075}\NormalTok{, }\DataTypeTok{len =} \DecValTok{40}\NormalTok{)),}
\DataTypeTok{data =} \KeywordTok{select}\NormalTok{(x_train_for_train, }\OperatorTok{-}\NormalTok{series_id, }\OperatorTok{-}\NormalTok{group_id))}
\KeywordTok{ggplot}\NormalTok{(model_fit)}
\end{Highlighting}
\end{Shaded}
\includegraphics{PH125_9_CYO_Report_files/figure-latex/rpart-1.pdf}