-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrediction Assignment.htm
More file actions
327 lines (281 loc) · 15.5 KB
/
Prediction Assignment.htm
File metadata and controls
327 lines (281 loc) · 15.5 KB
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
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=Generator content="Microsoft Word 15 (filtered)">
<style>
<!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"Lucida Console";
panose-1:2 11 6 9 4 5 4 2 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin-top:0in;
margin-right:0in;
margin-bottom:8.0pt;
margin-left:0in;
line-height:103%;
text-autospace:ideograph-other;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{margin-top:0in;
margin-right:0in;
margin-bottom:8.0pt;
margin-left:.5in;
line-height:103%;
text-autospace:ideograph-other;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
.MsoChpDefault
{font-family:"Calibri",sans-serif;}
.MsoPapDefault
{margin-bottom:8.0pt;
line-height:105%;
text-autospace:ideograph-other;}
/* Page Definitions */
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
-->
</style>
</head>
<body lang=EN-US>
<div class=WordSection1>
<p class=MsoListParagraph style='margin-bottom:6.0pt;text-indent:-.25in'><span
style='font-size:18.0pt;line-height:103%'>1.<span style='font:7.0pt "Times New Roman"'>
</span></span><span style='font-size:18.0pt;line-height:103%'>Data loading</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:12.0pt;
margin-left:.1in'><span style='font-size:12.0pt;line-height:103%'>Load caret
and ggplot2 packages</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:6.0pt;
margin-left:.25in;background:#D9D9D9'><span style='font-size:12.0pt;line-height:
103%;font-family:"Arial",sans-serif'>library(caret);
library(ggplot2) </span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:12.0pt;
margin-left:.1in'><span style='font-size:12.0pt;line-height:103%'>The next step
is to load the train and test data sets. To be able to preprocess the input
data, the entries <b>NA</b> and <b>DIV/0!</b> are both set to be NA</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:0in;
margin-left:.25in;margin-bottom:.0001pt;background:#D9D9D9'><span
style='font-size:12.0pt;line-height:103%;font-family:"Arial",sans-serif'>input_train
<- read.csv("pml-training.csv",
na.strings=c("NA","#DIV/0!"))</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:6.0pt;
margin-left:.25in;background:#D9D9D9'><span style='font-size:12.0pt;line-height:
103%;font-family:"Arial",sans-serif'>input_test <-
read.csv("pml-testing.csv",
na.strings=c("NA","#DIV/0!")) </span></p>
<p class=MsoNormal style='margin-bottom:6.0pt'> </p>
<p class=MsoListParagraph style='margin-bottom:6.0pt;text-align:justify;
text-indent:-.25in'><span style='font-size:18.0pt;line-height:103%'>2.<span
style='font:7.0pt "Times New Roman"'> </span></span><span
style='font-size:18.0pt;line-height:103%'>Data Preprocessing</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:12.0pt;
margin-left:.1in;text-align:justify'><span style='font-size:12.0pt;line-height:
103%'>It is useful to know in advance the set of output values, which we are
going to predict. In our case it turns out that the output takes a value in the
discrete set <i>S</i> = {A, B, C, D, E}. Note here that the <b>table()</b>
command shows the number of occurrences for each discrete output. This is
useful to check, prior to building our predictor, because we can check the
appearance frequency of each elements of <i>S.</i> If there is strong bias
towards a particular value then one should be careful in using <b>accuracy</b>
as performance measure because a naïve majority-class predictor can give good
accuracy results. However as we can see below the appearance frequencies
suggest that all the components appear sufficiently often (each frequency is
around 20% which corresponds to an almost uniform spread) in our training
sample. </span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:6.0pt;
margin-left:.25in;text-align:justify;background:#D9D9D9'><span
style='font-size:12.0pt;line-height:103%;font-family:"Arial",sans-serif'>table(input_train$classe)/length(input_train$classe)</span><span
style='line-height:103%;font-family:"Arial",sans-serif'> </span></p>
<div align=center>
<table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width=488
style='width:365.9pt;border-collapse:collapse'>
<tr>
<td width=104 style='width:77.9pt;border:solid black 1.0pt;padding:0in 5.4pt 0in 5.4pt'>
<p class=MsoNormal style='margin-top:0in;margin-right:-.2in;margin-bottom:
0in;margin-left:0in;margin-bottom:.0001pt;line-height:normal'>Output</p>
</td>
<td width=77 style='width:.8in;border:solid black 1.0pt;border-left:none;
padding:0in 5.4pt 0in 5.4pt'>
<p class=MsoNormal style='margin-top:0in;margin-right:-.2in;margin-bottom:
0in;margin-left:0in;margin-bottom:.0001pt;line-height:normal'>A</p>
</td>
<td width=77 style='width:.8in;border:solid black 1.0pt;border-left:none;
padding:0in 5.4pt 0in 5.4pt'>
<p class=MsoNormal style='margin-top:0in;margin-right:-.2in;margin-bottom:
0in;margin-left:0in;margin-bottom:.0001pt;line-height:normal'>B</p>
</td>
<td width=77 style='width:.8in;border:solid black 1.0pt;border-left:none;
padding:0in 5.4pt 0in 5.4pt'>
<p class=MsoNormal style='margin-top:0in;margin-right:-.2in;margin-bottom:
0in;margin-left:0in;margin-bottom:.0001pt;line-height:normal'>C</p>
</td>
<td width=77 style='width:.8in;border:solid black 1.0pt;border-left:none;
padding:0in 5.4pt 0in 5.4pt'>
<p class=MsoNormal style='margin-top:0in;margin-right:-.2in;margin-bottom:
0in;margin-left:0in;margin-bottom:.0001pt;line-height:normal'>D </p>
</td>
<td width=77 style='width:.8in;border:solid black 1.0pt;border-left:none;
padding:0in 5.4pt 0in 5.4pt'>
<p class=MsoNormal style='margin-top:0in;margin-right:-.2in;margin-bottom:
0in;margin-left:0in;margin-bottom:.0001pt;line-height:normal'>E</p>
</td>
</tr>
<tr>
<td width=104 style='width:77.9pt;border:solid black 1.0pt;border-top:none;
padding:0in 5.4pt 0in 5.4pt'>
<p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'>Frequency</p>
</td>
<td width=77 style='width:.8in;border-top:none;border-left:none;border-bottom:
solid black 1.0pt;border-right:solid black 1.0pt;padding:0in 5.4pt 0in 5.4pt'>
<p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Lucida Console";
color:black'>0.284</span></p>
</td>
<td width=77 style='width:.8in;border-top:none;border-left:none;border-bottom:
solid black 1.0pt;border-right:solid black 1.0pt;padding:0in 5.4pt 0in 5.4pt'>
<p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Lucida Console";
color:black'>0.196</span></p>
</td>
<td width=77 style='width:.8in;border-top:none;border-left:none;border-bottom:
solid black 1.0pt;border-right:solid black 1.0pt;padding:0in 5.4pt 0in 5.4pt'>
<p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Lucida Console";
color:black'>0.174</span></p>
</td>
<td width=77 style='width:.8in;border-top:none;border-left:none;border-bottom:
solid black 1.0pt;border-right:solid black 1.0pt;padding:0in 5.4pt 0in 5.4pt'>
<p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Lucida Console";
color:black'>0.164</span></p>
</td>
<td width=77 style='width:.8in;border-top:none;border-left:none;border-bottom:
solid black 1.0pt;border-right:solid black 1.0pt;padding:0in 5.4pt 0in 5.4pt'>
<p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Lucida Console";
color:black'>0.184</span></p>
</td>
</tr>
</table>
</div>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:6.0pt;
margin-left:.25in;text-align:justify'> </p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:12.0pt;
margin-left:.1in;text-align:justify'><span style='font-size:12.0pt;line-height:
103%'>At the reprocessing stage it seems intuitive to discard the first seven
columns that are not related to the output variate <b>classe.</b></span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:6.0pt;
margin-left:.25in;background:#D9D9D9'><span style='font-size:12.0pt;line-height:
103%;font-family:"Arial",sans-serif'>input_train <- input_train[,-c(1:7)];
input_test = input_test[,-c(1:7)]</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:12.0pt;
margin-left:.1in;text-align:justify'><span style='font-size:12.0pt;line-height:
103%'>It is also interesting to check the content of the columns. A simple
inspection, using the head() or tail() functions, indicates that certain
columns have almost no components. It is possible to check how many columns are
sparse, i.e., have few input data. The following set of commands returns the
columns, whose missing or NA values, constitute more than 90% of the column
size and then drops them from the training and the testing sets, respectively. </span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:0in;
margin-left:.25in;margin-bottom:.0001pt;background:#D9D9D9'><span
style='font-size:12.0pt;line-height:103%;font-family:"Arial",sans-serif'>high_num_NAs
<- sapply(input_train, function(x){mean(is.na(x))>0.9}) </span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:0in;
margin-left:.25in;margin-bottom:.0001pt;background:#D9D9D9'><span
style='font-size:12.0pt;line-height:103%;font-family:"Arial",sans-serif'>input_train
<- input_train[,high_num_NAs == FALSE] ; </span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:6.0pt;
margin-left:.25in;background:#D9D9D9'><span style='font-size:12.0pt;line-height:
103%;font-family:"Arial",sans-serif'>input_test <- input_test[,high_num_NAs
== FALSE]</span></p>
<p class=MsoNormal style='margin-bottom:6.0pt'><span style='font-size:14.0pt;
line-height:103%'> </span></p>
<p class=MsoListParagraph style='margin-bottom:6.0pt;text-indent:-.25in'><span
style='font-size:18.0pt;line-height:103%'>3.<span style='font:7.0pt "Times New Roman"'>
</span></span><span style='font-size:18.0pt;line-height:103%'>Model Training</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:12.0pt;
margin-left:.1in'><span style='font-size:12.0pt;line-height:103%'>Split the
training set into a validation (30%) and a training (70%) subset,
respectively. </span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:0in;
margin-left:.25in;margin-bottom:.0001pt;background:#D9D9D9'><span
style='font-size:12.0pt;line-height:103%;font-family:"Arial",sans-serif'>inBuild
<- createDataPartition(input_train$classe, p=0.7, list=FALSE) </span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:0in;
margin-left:.25in;margin-bottom:.0001pt;background:#D9D9D9'><span
style='font-size:12.0pt;line-height:103%;font-family:"Arial",sans-serif'>validation_data
<- input_train[-inBuild,] # Create the validation set</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:6.0pt;
margin-left:.25in;text-align:justify;background:#D9D9D9'><span
style='font-size:12.0pt;line-height:103%;font-family:"Arial",sans-serif'>train_data<-input_train[inBuild,]
# Create the train set</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:12.0pt;
margin-left:.1in;text-align:justify'><span style='font-size:12.0pt;line-height:
103%'>The following set of commands trains a boosting model and checks its
accuracy on the </span><span style='font-size:12.0pt;line-height:103%'>validation
subset. It turns out that the accuracy is 0.967</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:0in;
margin-left:.25in;margin-bottom:.0001pt;background:#D9D9D9'><span
style='font-size:12.0pt;line-height:103%;font-family:"Arial",sans-serif'>modFit_gb
<- train(classe ~ ., method="gbm", data=train_data, verbose=FALSE)</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:6.0pt;
margin-left:.25in;line-height:104%;background:#D9D9D9'><span style='font-size:
12.0pt;line-height:104%;font-family:"Arial",sans-serif'>confusionMatrix(validation_data$classe,
predict(modFit_gb, validation_data)) </span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:12.0pt;
margin-left:.1in;text-align:justify'><span style='font-size:12.0pt;line-height:
103%'>The following set of commands trains a random forest model and checks its
accuracy on the validation subset. It turns out that the accuracy is 0.992</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:0in;
margin-left:.25in;margin-bottom:.0001pt;background:#D9D9D9'><span
style='font-size:12.0pt;line-height:103%;font-family:"Arial",sans-serif'>modFit_rf
<- train(classe ~ ., method="rf", data=train_data)</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:6.0pt;
margin-left:.25in;background:#D9D9D9'><span style='font-size:12.0pt;line-height:
103%;font-family:"Arial",sans-serif'>confusionMatrix(validation_data$classe,
predict(modFit_rf, validation_data))</span></p>
<p class=MsoNormal style='margin-bottom:6.0pt;text-align:justify'> </p>
<p class=MsoListParagraph style='margin-bottom:6.0pt;text-align:justify;
text-indent:-.25in'><span style='font-size:18.0pt;line-height:103%'>4.<span
style='font:7.0pt "Times New Roman"'> </span></span><span
style='font-size:18.0pt;line-height:103%'>Prediction Stage</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:12.0pt;
margin-left:.1in;text-align:justify'><span style='font-size:12.0pt;line-height:
103%'>In the final stage, we make predictions using the testing data. To
implement a majority classifier, one needs an odd number of models. However, it
turns out that the predictions provided by the two models are identical. This
suggests that the result are correct, which is </span><span style='font-size:
12.0pt;line-height:103%'>indeed the case.</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:0in;
margin-left:.25in;margin-bottom:.0001pt;background:#D9D9D9'><span
style='font-size:12.0pt;line-height:103%;font-family:"Arial",sans-serif'>pred_gb
<- predict(modFit_gb, input_test)</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:0in;
margin-left:.25in;margin-bottom:.0001pt;background:#D9D9D9'><span
style='font-size:12.0pt;line-height:103%;font-family:"Arial",sans-serif'>pred_rf
<- predict(modFit_rf, input_test)</span></p>
<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:0in;
margin-left:.25in;margin-bottom:.0001pt;background:#D9D9D9'><span
style='font-size:12.0pt;line-height:103%;font-family:"Arial",sans-serif'>pred_frame
<- data.frame(pred_gb, pred_rf)</span></p>
<p class=MsoNormal> </p>
</div>
</body>
</html>