-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.Rmd
188 lines (164 loc) · 9.42 KB
/
project.Rmd
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
---
title: Practical Machine Learning Project - Quantified Self Movement Data Analysis Report
author: "Bhautik A. Mangukiya"
output:
html_document:
toc: true
theme: united
md_document:
variant: markdown_github
---
Practical Machine Learning Course Project Report
================================================
These is a file produced during a homework assignment of Coursera's MOOC <b>Practical Machine Learning</b> from <b>Johns Hopkins Bloomberg School of Public Health</b>.
For more information about the several MOOCs comprised in this Specialization, please visit: [https://www.coursera.org/specialization/jhudatascience/](https://www.coursera.org/specialization/jhudatascience/)
## 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, our 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](http://groupware.les.inf.puc-rio.br/har) (see the section on the Weight Lifting Exercise Dataset).
## Data Sources
The training data for this project is available here:
[https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv](https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv)
The test data is available here:
[https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv](https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv)
The data for this project comes from this original source: [http://groupware.les.inf.puc-rio.br/har](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.
## Intended Results
The goal of this 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.
1. 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 :-).
2. 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.
## Reproducibility
In order to reproduce the same results, you need a certain set of packages as well as setting a pseudo random seed equal to the one I have used.
`Note`: To install, for instance, the `rattle` package in R, run this command: `install.packages("rattle")`.
The following Libraries were used for this project, which you should install and load them in your working environment.
```{r warning=FALSE, echo=FALSE}
library(rattle)
library(caret)
library(rpart)
library(rpart.plot)
library(corrplot)
library(randomForest)
library(RColorBrewer)
```
Finally, load the same seed with the following line of code:
```{r warning=FALSE, error=FALSE}
set.seed(123)
```
## Getting Data
The following code fragment downloads the dataset to the `data` folder in the current working directory.
```{r warning=FALSE, error=FALSE}
trainUrl <-"https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv"
testUrl <- "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv"
trainFile <- "./data/pml-training.csv"
testFile <- "./data/pml-testing.csv"
if (!file.exists("./data")) {
dir.create("./data")
}
if (!file.exists(trainFile)) {
download.file(trainUrl, destfile = trainFile, method = "curl")
}
if (!file.exists(testFile)) {
download.file(testUrl, destfile = testFile, method = "curl")
}
rm(trainUrl)
rm(testUrl)
```
## Reading Data
After downloading the data from the data source, we can read the two csv files into two data frames.
```{r warning=FALSE, error=FALSE}
trainRaw <- read.csv(trainFile)
testRaw <- read.csv(testFile)
dim(trainRaw)
dim(testRaw)
rm(trainFile)
rm(testFile)
```
The training data set contains `r dim(trainRaw)[1]` observations and `r dim(trainRaw)[2]` variables, while the testing data set contains `r dim(testRaw)[1]` observations and `r dim(testRaw)[2]` variables. The `classe` variable in the training set is the outcome to predict.
## Cleaning Data
In this step, we will clean the dataset and get rid of observations with missing values as well as some meaningless variables.
1. We clean the <b>Near Zero Variance</b> Variables.
```{r warning=FALSE, error=FALSE}
NZV <- nearZeroVar(trainRaw, saveMetrics = TRUE)
head(NZV, 20)
training01 <- trainRaw[, !NZV$nzv]
testing01 <- testRaw[, !NZV$nzv]
dim(training01)
dim(testing01)
rm(trainRaw)
rm(testRaw)
rm(NZV)
```
2. Removing some columns of the dataset that do not contribute much to the accelerometer measurements.
```{r warning=FALSE, error=FALSE}
regex <- grepl("^X|timestamp|user_name", names(training01))
training <- training01[, !regex]
testing <- testing01[, !regex]
rm(regex)
rm(training01)
rm(testing01)
dim(training)
dim(testing)
```
3. Removing columns that contain `NA's`.
```{r warning=FALSE, error=FALSE}
cond <- (colSums(is.na(training)) == 0)
training <- training[, cond]
testing <- testing[, cond]
rm(cond)
```
Now, the cleaned training data set contains `r dim(training)[1]` observations and `r dim(training)[2]` variables, while the testing data set contains `r dim(testing)[1]` observations and `r dim(testing)[2]` variables.
Correlation Matrix of Columns in the Training Data set.
```{r warning=FALSE, error=FALSE}
corrplot(cor(training[, -length(names(training))]), method = "color", tl.cex = 0.5)
```
## Partitioning Training Set
we split the cleaned training set into a pure training data set (70%) and a validation data set (30%). We will use the validation data set to conduct cross validation in future steps.
```{r warning=FALSE, error=FALSE}
set.seed(56789) # For reproducibile purpose
inTrain <- createDataPartition(training$classe, p = 0.70, list = FALSE)
validation <- training[-inTrain, ]
training <- training[inTrain, ]
rm(inTrain)
```
The Dataset now consists of `r dim(training)[2]` variables with the observations divided as following:
1. Training Data: `r dim(training)[1]` observations.
2. Validation Data: `r dim(validation)[1]` observations.
3. Testing Data: `r dim(testing)[1]` observations.
## Data Modelling
### Decision Tree
We fit a predictive model for activity recognition using <b>Decision Tree</b> algorithm.
```{r warning=FALSE, error=FALSE}
modelTree <- rpart(classe ~ ., data = training, method = "class")
prp(modelTree)
```
Now, we estimate the performance of the model on the <b>validation</b> data set.
```{r warning=FALSE, error=FALSE}
predictTree <- predict(modelTree, validation, type = "class")
confusionMatrix(as.factor(validation$classe), as.factor(predictTree))
accuracy <- confusionMatrix(as.factor(validation$classe), as.factor(predictTree))$overall[1]
ose <- 1 - accuracy
rm(predictTree)
rm(modelTree)
```
The Estimated Accuracy of the Random Forest Model is `r accuracy[1]*100`% and the Estimated Out-of-Sample Error is `r ose*100`%.
### Random Forest
We fit a predictive model for activity recognition using <b>Random Forest</b> algorithm because it automatically selects important variables and is robust to correlated covariates & outliers in general.
We will use <b>5-fold cross validation</b> when applying the algorithm.
```{r warning=FALSE, error=FALSE}
modelRF <- randomForest(as.factor(classe) ~ ., data = training, ntree=500,mtry=5)
modelRF
```
Now, we estimate the performance of the model on the <b>validation</b> data set.
```{r warning=FALSE, error=FALSE}
predictRF <- predict(modelRF, validation)
confusionMatrix(as.factor(validation$classe), as.factor(predictRF))
accuracy <- confusionMatrix(as.factor(validation$classe), as.factor(predictRF))$overall[1]
ose <- 1 - accuracy
rm(predictRF)
```
The Estimated Accuracy of the Random Forest Model is `r accuracy[1]*100`% and the Estimated Out-of-Sample Error is `r ose*100`%.
Random Forests yielded better Results, as expected!
## Predicting The Manner of Exercise for Test Data Set
Now, we apply the <b>Random Forest</b> model to the original testing data set downloaded from the data source. We remove the problem_id column first.
```{r warning=FALSE, error=FALSE}
rm(accuracy)
rm(ose)
predict(modelRF, testing[, -length(names(testing))])
```