-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectMachineLearning.Rmd
More file actions
307 lines (164 loc) · 11.3 KB
/
Copy pathProjectMachineLearning.Rmd
File metadata and controls
307 lines (164 loc) · 11.3 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
---
title: "MachLearning"
author: "Prosper KOUASSI"
date: "Saturday, July 25, 2015"
output: html_document
---
All Scripts have been produced and tested using RSudio Version 0.98.1103 and Windows 7.
This document is the Final Project for the MOOC "Practical Machine Learning" from Johns Hopkins University.
## Assignement Instructions ##
Background, Data and What you should submit sections are directly copied from course's assignement page.
### Background ###
Using devices such as Jawbone Up, Nike FuelBand, and Fitbit 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 how well they do it. 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: http://groupware.les.inf.puc-rio.br/har (see the section on the Weight Lifting Exercise Dataset).
### Data ###
The training data for this project are available here: [https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv]
The test data are available here: [https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv]
The data for this project come from this source: [http://groupware.les.inf.puc-rio.br/har]. If you use the document you create for this class for any purpose please cite them as they have been very generous in allowing their data to be used for this kind of assignment.
### What you should submit ###
The goal of your project is to predict the manner in which they did the exercise. This is the "classe" variable in the training set. You may use any of the other variables to predict with. You should create a report describing how you built your model, how you used cross validation, what you think the expected out of sample error is, and why you made the choices you did. You will also use your prediction model to predict 20 different test cases.
Your submission should consist of a link to a Github repo with your R markdown and compiled HTML file describing your analysis. Please constrain the text of the writeup to < 2000 words and the number of figures to be less than 5. It will make it easier for the graders if you submit a repo with a gh-pages branch so the HTML page can be viewed online (and you always want to make it easy on graders :-).
You should also apply your machine learning algorithm to the 20 test cases available in the test data above. Please submit your predictions in appropriate format to the programming assignment for automated grading. See the programming assignment for additional details.
## Preliminary Work ##
In order to reproduce the same results below, we set the seed at 4567. The packages "caret"" and "randomForest"are installed and loaded. These should also be load in order the retrieve the same results.
### How the model was built ###
The outcome variable is classe, a 5-level factor variable. For this data set, "participants were asked to perform one set of 10 repetitions of the Unilateral Dumbbell Biceps Curl in 5 different fashions:
* Exactly according to the specification (Class A)
* Throwing the elbows to the front (Class B)
* Lifting the dumbbell only halfway (Class C)
* Lowering the dumbbell only halfway (Class D)
* Throwing the hips to the front (Class E)
Class A corresponds to the specified execution of the exercise, while the other 4 classes correspond to common mistakes [1].
Prediction evaluations will be based on maximizing the accuracy and minimizing the out-of-sample error. All other available variables after cleaning will be used for prediction.
Two models will be tested using decision tree and random forest algorithms. The model with the highest accuracy will be chosen as our final model.
### Cross-validation ###
Cross-validation will be performed by subsampling the training data set randomly without replacement into 2 subsamples: subTraining data (75% of the original Training data set) and subTesting data (25%). Our models will be fitted on the subTraining data set, and tested on the subTesting data. Once the most accurate model is choosen, it will be tested on the original Testing data set.
### Expected out-of-sample error ###
The expected out-of-sample error will correspond to the quantity: 1-accuracy in the cross-validation data. Accuracy is the proportion of correct classified observation over the total sample in the subTesting data set. Expected accuracy is the expected accuracy in the out-of-sample data set (i.e. original testing data set). Thus, the expected value of the out-of-sample error will correspond to the expected number of missclassified observations/total observations in the Test data set, which is the quantity: 1-accuracy found from the cross-validation data set.
### Reasons for my choices ###
The outcome variable "classe" is an unordered factor variable. Thus, we can choose our error type as 1-accuracy. We have a large sample size with N= 19622 in the Training data set. This allow us to divide our Training sample into subTraining and subTesting to allow cross-validation. Features with all missing values will be discarded as well as features that are irrelevant. All other features will be kept as relevant variables.
Decision tree and random forest algorithms are known for their ability of detecting the features that are important for classification [2]. Feature selection is inherent, so it is not so necessary at the data preparation phase. Thus, there won't be any feature selection section in this report.
## Code and Results ##
```{r}
#install.packages("caret")
#install.packages("randomForest")
#install.packages("rpart")
```
```{r}
library(caret)
#library(ggplot2)
#library(lattice)
library(kernlab)
library(randomForest) #Random forest for classification and regression
library(rpart) # Regressive Partitioning and Regression trees
```
```{r}
#install.packages("rpart.plot")
library(rpart.plot) # Decision Tree plot
```
### Setting the overall seed for reproduceability ###
```{r}
set.seed(4567)
```
### Loading data sets and preliminary cleaning ###
```{r}
setInternet2(TRUE)
download.file(url = "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv",
destfile = "pml-training.csv")
download.file(url = "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv",
destfile = "pml-testing.csv")
```
We first load the data and make sure that missing values are correctly coded. Irrelevant variables are deleted. Results will be hidden from the report for clarity and space considerations.
```{r}
# Loading the training data set into my R session replacing all missing with "NA"
trainingset <- read.csv('pml-training.csv', na.strings=c("NA","#DIV/0!", ""))
# Loading the testing data set
testingset <- read.csv('pml-testing.csv', na.strings=c("NA","#DIV/0!", ""))
# Check dimensions for number of variables and number of observations
dim(trainingset)
dim(testingset)
```
```{r}
# Delete columns with all missing values
trainingset <- trainingset[,colSums(is.na(trainingset)) == 0]
testingset <- testingset[,colSums(is.na(testingset)) == 0]
# Some variables are irrelevant to our current project: user_name, raw_timestamp_part_1, raw_timestamp_part_,2 cvtd_timestamp, new_window, and num_window (columns 1 to 7). We can delete these variables.
trainingset <-trainingset[,-c(1:7)]
testingset <-testingset[,-c(1:7)]
# A look at our new datasets:
dim(trainingset)
dim(testingset)
```
```{r}
#head(trainingset)
#head(testingset)
```
### Partitioning the training data set to allow cross-validation ###
The training data set contains 53 variables and 19622 obs. The testing data set contains 53 variables and 20 obs. In order to perform cross-validation, the training data set is partionned into 2 sets: subTraining (75%) and subTest (25%). This will be performed using random subsampling without replacement.
```{r}
inTrain <- createDataPartition(y=trainingset$classe, p=0.75, list=FALSE)
subTraining <- trainingset[inTrain, ]
subTesting <- trainingset[-inTrain, ]
dim(subTraining)
dim(subTesting)
dim(subTesting)
```
```{r}
#head(subTraining)
#head(subTesting)
```
## Let's have a look at the Data ##
The variable "classe" contains 5 levels: A, B, C, D and E. A plot of the outcome variable will allow us to see the frequency of each levels in the subTraining data set and compare one another.
```{r}
plot(subTraining$classe, col="red", main="Bar Plot of levels of the variable classe within the subTraining data set", xlab="classe levels", ylab="Frequency")
```
From the previous graph, we can see that each level frequency is within the same order of magnitude of each other. Level A is the most frequent with more than 4000 occurrences while level D is the least frequent with about 2500 occurrences.
### First prediction model: Using Decision Tree ###
```{r}
modFit1 <- rpart(classe ~ ., data=subTraining, method="class")
```
```{r}
#Predicting
prediction1 <- predict(modFit1, subTesting, type = "class")
```
```{r}
#Plot of the Decision Tree
rpart.plot(modFit1, main ="Classification Tree", extra=102, under=TRUE, faclen=0)
```
## Testing results on our subTesting data set ##
```{r}
confusionMatrix(prediction1, subTesting$classe)
```
### Second prediction model: Using Random Forest###
```{r}
modFit2 <- randomForest(classe ~. , data=subTraining, method="class")
```
## Predicting ##
```{r}
prediction2 <- predict(modFit2, subTesting, type = "class")
```
## Testint results on subTesting data set ##
```{r}
confusionMatrix(prediction2, subTesting$classe)
```
### Decision ###
As expected, Random Forest algorithm performed better than Decision Trees. Accuracy for Random Forest model was 0.997 (95% CI: (0.995, 0.998)) compared to 0.758 (95% CI: (0.746, 0.770)) for Decision Tree model. The random Forest model is choosen. The accuracy of the model is 0.997. The expected out-of-sample error is estimated at 0.003, or 0.3%. The expected out-of-sample error is calculated as 1 - accuracy for predictions made against the cross-validation set. Our Test data set comprises 20 cases. With an accuracy above 99% on our cross-validation data, we can expect that very few, or none, of the test samples will be missclassified.
## Submission ##
### Predict outcome levels on the original Testing data set using Random Forest algorithm ###
```{r}
predAnswer <- predict(modFit2, testingset, type="class")
predAnswer
```
```{r}
pml_write_files = function(x){
n = length(x)
for(i in 1:n){
filename = paste0("problem_id_",i,".txt")
write.table(x[i],file=filename,quote=FALSE,row.names=FALSE,col.names=FALSE)
}
}
pml_write_files(as.character(predAnswer))
pml_write_files(predAnswer)
```
## References ##
[1] Velloso, E.; Bulling, A.; Gellersen, H.; Ugulino, W.; Fuks, H. Qualitative Activity Recognition of Weight Lifting Exercises. Proceedings of 4th International Conference in Cooperation with SIGCHI (Augmented Human '13) . Stuttgart, Germany: ACM SIGCHI, 2013.
[2] Krzysztof Gra??bczewski and Norbert Jankowski. Feature Selection with Decision Tree Criterion.