-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.Rmd
180 lines (126 loc) · 6.53 KB
/
index.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
---
title: "Health Data Analysis"
author: "Saif Ul Mehdi"
date: "08/07/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Overview
This document is the final report of the Peer Assignment from the course Practical Machine Learning offered by JHU. It was built in RStudio using knitr function and published as html document.
## 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
## 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.
The following statements were made by the original authors of the dataset.
Six young health participants were asked to perform one set of 10 repetitions of the Unilateral Dumbbell Biceps Curl in five 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) and 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.
Participants were supervised by an experienced weight lifter to make sure the execution complied to the manner they were supposed to simulate.
The exercises were performed by six male participants aged between 20-28 years, with little weight lifting experience.
We made sure that all participants could easily simulate the mistakes in a safe and controlled manner by using a relatively light dumbbell (1.25kg).
Source : Velloso, E.; Bulling, A.; Gellersen, H.; Ugulino, W.; Fuks, H. Qualitative Activity Recognition of Weight Lifting Exercises. Proceedings of 4th Augmented Human (AH) International Conference in cooperation with ACM SIGCHI (Augmented Human'13) . Stuttgart, Germany: ACM SIGCHI, 2013.
## Loading Required Libraries
```{r}
suppressPackageStartupMessages(library(caret))
suppressPackageStartupMessages(library(rpart))
suppressPackageStartupMessages(library(rpart.plot))
suppressPackageStartupMessages(library(rattle))
suppressPackageStartupMessages(library(randomForest))
suppressPackageStartupMessages(library(corrplot))
```
## Loading Dataset and cleaning
We will be using the training data for analysis and testing data for answering quiz questions.
Training data will be split into training and testing set in the ratio 70:30. Many columns have NAs which would be removed and the identity columns along with ID columns will be removed.
```{r}
download.file('https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv',destfile='./training_set.csv',method='curl')
download.file('https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv',destfile='./testing_set.csv',method='curl')
train_dat<-read.csv('./training_set.csv')
test_dat<-read.csv('./testing_set.csv')
inTrain<-createDataPartition(y=train_dat$classe,p=0.7,list=FALSE)
train_set<-train_dat[inTrain,]
test_set<-train_dat[-inTrain,]
NZV<-nearZeroVar(train_set)
train_set<-train_set[,-NZV]
test_set<-test_set[,-NZV]
All_NA<-sapply(train_set,function(x) mean(is.na(x)))
train_set<-train_set[,All_NA==FALSE]
test_set<-test_set[,All_NA==FALSE]
train_set<-train_set[,-(1:5)]
test_set<-test_set[,-(1:5)]
```
After cleaning cleaning process, both training and testing sets have 54 columns
## Corelation
Before creating prediction models, correlation is found among the variables
```{r echo=FALSE}
corMatrix<-cor(train_set[,-54])
corrplot(corMatrix,order='FPC',method='color',type='lower',tl.cex=0.8,tl.col=rgb(0,0,0))
```
The highly correlated variables are depicted in dark colors in the above plot.
Since, there are very few variables which are correlated strongly, PCA is not necessary to be performed as pre-processing step.
## Prediction Model Building
Three methods are used for model building and the one with highest accuracy score will be applied to the testing data.
### Random Forest
```{r comment=''}
set.seed(12345)
controlRF<-trainControl(method='cv',number=3,verboseIter=FALSE)
mdlRF<-train(classe~.,data=train_set,method='rf',trControl=controlRF)
mdlRF$finalModel
```
```{r comment=''}
predRF<-predict(mdlRF,newdata=test_set)
conmatRF<-confusionMatrix(predRF,as.factor(test_set$classe))
conmatRF
```
```{r echo=FALSE}
plot(conmatRF$table,col=conmatRF$byClass,main=paste('Random forest - accuracy =',round(conmatRF$overall['Accuracy'],4)))
```
### Decision Trees
```{r}
set.seed(12345)
mdldtree<-rpart(classe~.,data=train_set,method='class')
fancyRpartPlot(mdldtree)
```
```{r comment=''}
preddtree<-predict(mdldtree,newdata=test_set,type='class')
conmatdtree<-confusionMatrix(preddtree,as.factor(test_set$classe))
conmatdtree
```
```{r echo=FALSE}
plot(conmatdtree$table,col=conmatdtree$byClass,main=paste('Decision trees - accuracy =',round(conmatdtree$overall['Accuracy'],4)))
```
### Generalized Boosting Method
```{r comment=''}
set.seed(12345)
controlGBM<-trainControl(method='repeatedcv',number=5,repeats=1)
mdlGBM<-train(classe~.,data=train_set,method='gbm',trControl=controlGBM,verbose=FALSE)
mdlGBM$finalModel
```
```{r comment=''}
predGBM<-predict(mdlGBM,newdata=test_set)
conmatGBM<-confusionMatrix(predGBM,as.factor(test_set$classe))
conmatGBM
```
```{r echo=FALSE}
plot(conmatGBM$table,col=conmatGBM$byClass,main=paste('Random forest - accuracy =',round(conmatGBM$overall['Accuracy'],4)))
```
## Applying Best model to Test Data
The accuracy of the 3 regression models are -
a) Random Forest : 0.9976
b) Decision Trees : 0.7443
c) Generalized Boosting Method : 0.9891
So, the best model is random forest.
```{r comment=''}
predictTest<-predict(mdlRF,newdata=test_dat)
predictTest
```