-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproject_writeup.Rhtml
170 lines (129 loc) · 7.95 KB
/
project_writeup.Rhtml
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
<html>
<head>
<title>Prediction Assignment Writeup</title>
</head>
<body>
<h1> Prediction Assignment Writeup </h1>
<h2> <i> Alaettin Serhan Mete </i> </h2>
<h3> <i> Apr. 2017 </i> </h3>
<section>
<h1> Overview </h1>
<p>This document summarizes the work done for the <i>Prediction Assignment Writeup</i> project for the <i>Coursera</i> <i>Practical Machine Learning</i> course. It's created using the functionalities of the <i>knitr</i> package in <i>RStudio</i> using the actual analysis code. The repository for this work can be found at <a href="https://github.com/amete/PracticalMachineLearningAssignment">https://github.com/amete/PracticalMachineLearningAssignment</a>. </p>
</section>
<section>
<h1> Background </h1>
<p>Using devices such as <i>Jawbone Up</i>, <i>Nike FuelBand</i>, and <i>Fitbit</i> it is now possible to collect a large amount of data about personal activity relatively inexpensively. These type of devices are part of the quantified self movement – a group of enthusiasts who take measurements about themselves regularly to improve their health, to find patterns in their behavior, or because they are tech geeks. One thing that people regularly do is quantify how much of a particular activity they do, but they rarely quantify <i>how well they do it</i>. In this project, your goal will be to use data from accelerometers on the belt, forearm, arm, and dumbell of 6 participants. They were asked to perform barbell lifts correctly and incorrectly in 5 different ways. More information is available from the website here: <a href="http://groupware.les.inf.puc-rio.br/har">http://groupware.les.inf.puc-rio.br/har</a> (see the section on the Weight Lifting Exercise Dataset).</p>
</section>
<section>
<h1> Data </h1>
<p>The training data for this project are available here:</p>
<p><a href="https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv">https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv</a></p>
<p>The test data are available here:</p>
<p><a href="https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv">https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv</a></p>
</section>
<section>
<h1> Analysis </h1>
<p>First, we begin by exporting the data. One can simply download the training and testing datasets using:</p>
<!--begin.rcode eval=TRUE
# Download the training and testing datasets
download.file("https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv","training.csv",method = "curl")
download.file("https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv","testing.csv",method = "curl")
end.rcode-->
<p>Then, we load some useful packages using:</p>
<!--begin.rcode eval=TRUE
# Require the necessary packages
require(data.table)
require(dplyr)
require(caret)
end.rcode-->
<p>Now let's load the data into memory:</p>
<!--begin.rcode eval=TRUE
# Load the training and testing datasets
training <- tbl_df(fread("training.csv",na.strings=c('#DIV/0!', '', 'NA')))
testing <- tbl_df(fread("testing.csv",na.strings=c('#DIV/0!', '', 'NA')))
end.rcode-->
<p>Now that we have the data in the memory, let's get to the fun part. First thing we better do is to split the training data into two parts. We'll use 70% of this data to actually train our model and the remaining 30% to validate it:</p>
<!--begin.rcode eval=TRUE
# Now split the training into to as actual testing and validation
set.seed(1234) # Don't forget the reproducibility!
trainingDS <- createDataPartition( y = training$classe,
p = 0.7,
list = FALSE)
actual.training <- training[trainingDS,]
actual.validation <- training[-trainingDS,]
end.rcode-->
<p>Next, we need to prepare the data for modeling. If you look at the training data you'll see that there are a number of variables that have either no variance or a large fraction of missing values. These will not really help us in any meaningful way. Therefore, let's clean them up for a healthy modeling:</p>
<!--begin.rcode eval=TRUE
# Now clean-up the variables w/ zero variance
# Be careful, kick out the same variables in both cases
nzv <- nearZeroVar(actual.training)
actual.training <- actual.training[,-nzv]
actual.validation <- actual.validation[,-nzv]
# Remove variables that are mostly NA
mostlyNA <- sapply(actual.training,function(x) mean(is.na(x))) > 0.95
actual.training <- actual.training[,mostlyNA==FALSE]
actual.validation <- actual.validation[,mostlyNA==FALSE]
# At this point we're already down to 59 variables from 160
# See that the first 5 variables are identifiers that are
# not probably useful for prediction so get rid of those
# Dropping the total number of variables to 54 (53 for prediction)
actual.training <- actual.training[,-(1:5)]
actual.validation <- actual.validation[,-(1:5)]
end.rcode-->
<p>At this point, we have healthy clean data that we can use for building models. We'll build two models: a <i>random forest</i> and a <i>generalized boosted model</i>. We'll train these in the training portion of the original training dataset and then test them in the validation portion of the original training dataset: </p>
<!--begin.rcode eval=TRUE
# Now let's build a random forest model
set.seed(1234)
modelRF <- train( classe ~.,
data = actual.training,
method = "rf",
trControl = trainControl(method="cv",number=3) )
end.rcode-->
<!--begin.rcode eval=TRUE
# One can also build a generalized boosted model and compare its accuracy
# to random forest model
set.seed(1234)
modelBM <- train( classe ~.,
data = actual.training,
method = "gbm",
trControl = trainControl(method="repeatedcv",number = 5,repeats = 1),
verbose = FALSE)
end.rcode-->
<p>Then let's see how well these two models perform predicting the values in the validation dataset. This can be easily accomplished by predicting the values in the validation set, and then comparing the predictions with the actual values.</p>
<!--begin.rcode eval=TRUE
# Now get the prediction in the validation portion and see how well we do
prediction.validation.rf <- predict(modelRF,actual.validation)
conf.matrix.rf <- confusionMatrix(prediction.validation.rf,actual.validation$classe)
print(conf.matrix.rf)
end.rcode-->
<!--begin.rcode eval=TRUE
# Now get the prediction in the validation portion and see how well we do
prediction.validation.bm <- predict(modelBM,actual.validation)
conf.matrix.bm <- confusionMatrix(prediction.validation.bm,actual.validation$classe)
print(conf.matrix.bm)
end.rcode-->
<p>We can investigate our <i>generalized boosted model</i> a bit further to see which variables have the highest relative influence:</p>
<!--begin.rcode eval=TRUE
# Print the summary of our GBM
print(summary(modelBM))
end.rcode-->
<p>The above list shows the ranking of variables in our GBM. We see that <i>num_window</i>, <i>roll_belt</i>, and <i>pitch_forearm</i> are the most performant ones. We can checkout a few plots demonstrating their power:</p>
<!--begin.rcode fig.width=7, fig.height=6
qplot(num_window, roll_belt , data = actual.training, col = classe)
end.rcode-->
<!--begin.rcode fig.width=7, fig.height=6
qplot(num_window, pitch_forearm, data = actual.training, col = classe)
end.rcode-->
<!--begin.rcode fig.width=7, fig.height=6
qplot(roll_belt , pitch_forearm, data = actual.training, col = classe)
end.rcode-->
<p>At this point we see the <i>random forest</i> has marginally better performance (Accuracy : 0.998) than the <i>generalized boosted model</i> (Accuracy : 0.9876). Actually we can go w/ either or ensemble them but that might be an overkill at this point. In any case they yield the same result. Let's test our model in the actual testing dataset:</p>
<!--begin.rcode eval=TRUE
# Now get the prediction in the testing portion and see what we get
prediction.testing.rf <- predict(modelRF,testing)
print(prediction.testing.rf)
end.rcode-->
</section>
<b>Copyright Ⓒ Alaettin Serhan Mete, 2017. All rights reserved.</b>
</body>
</html>