forked from mGalarnyk/datasciencecoursera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourseProjectMachineLearning.Rmd
211 lines (152 loc) · 4.86 KB
/
courseProjectMachineLearning.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
---
title: "Project Practical Machine Learning"
author: "Jose Nicolas Molina"
date: "2022-09-11"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Introduction
The goal of this project is to predict how six participants performed barbell lifts correctly and incorrectly in five different ways. Data from accelerometers attached to the belt, forearm, upper arm, and dumbbell are used to determine how well each participant was performing the barbell lifts. The machine learning algorithm that is developed in this report is applied to 20 test cases available in the test data. The predictions are used for the project prediction evaluation questionnaire for qualification.
## Data Loading, Packages and Library
Download data from url:
```{r}
dataTrain <- "http://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv"
dataTest <- "http://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv"
```
Load the datasets
```{r}
training <- read.csv(url(dataTrain))
testing <- read.csv(url(dataTest))
```
Load packages and library
```{r}
library(knitr)
library(caret)
library(rpart)
library(rpart.plot)
library(rattle)
library(randomForest)
library(corrplot)
```
Data spliting
A training data set (70%) will be created for the modeling process and a test data set (30%) for validations. The testing data set is used in the choice of results for the prediction quiz.
```{r}
inTrain <- createDataPartition(training$classe, p=0.7, list=FALSE)
trainSet <- training[inTrain, ]
testSet <- training[-inTrain, ]
```
Exploratory Analysis and Cleaning
```{r}
dim(trainSet)
dim(testSet)
```
Note: The data created from trainSet and testSet contains 160 variables. In the variables there are NA, which will be applied cleaning. Likewise, unnecessary variables such as Nearly Zero Variance and id are eliminated.
Remove NAs in variables
```{r}
nonNA <- sapply(trainSet, function(x) mean(is.na(x))) > 0.95
trainSet <- trainSet[, nonNA==FALSE]
testSet <- testSet[, nonNA==FALSE]
```
```{r}
dim(trainSet)
dim(testSet)
```
Remove variables with almost zero variance
```{r}
neZeVar <- nearZeroVar(trainSet)
trainSet <- trainSet[, -neZeVar]
testSet <- testSet[, -neZeVar]
```
```{r}
dim(trainSet)
dim(testSet)
```
Remove id variables
```{r}
trainSet <- trainSet[, -(1:5)]
testSet <- testSet[, -(1:5)]
```
```{r}
dim(trainSet)
dim(testSet)
```
Note: 54 will be the variables that will be analyzed after the cleaning process.
Correlation Analysis
```{r}
cMatrix <- cor(trainSet[, -54])
corrplot(cMatrix, order = "FPC", method = "color", type = "lower",
tl.cex = 0.8, tl.col = rgb(0, 0, 0))
```
Note: Variables with high correlations are presented in dark colors.
## Prediction Model Building
Random Forest, Decision Trees and Generalized Boosted Model methods are applied to model the regressions. The one with the highest precision, when applied to the test data set, is used in the predictions of the questionnaire.
### Random Forest
Model fit
```{r}
fitRfc <- trainControl(method="cv", number=3, verboseIter=FALSE)
modFitRf <- train(classe ~ ., data=trainSet, method="rf",
trControl=fitRfc)
modFitRf$finalModel
```
Prediction on test dataset
```{r}
predictRf <- predict(modFitRf, newdata=testSet)
confMatRf <- confusionMatrix(predictRf, as.factor(testSet$classe))
confMatRf
```
Plot matrix results
```{r}
plot(confMatRf$table, col = confMatRf$byClass,
main = paste("Random Forest - Accuracy =",
round(confMatRf$overall['Accuracy'], 4)))
```
### Decision Trees
Model fit
```{r}
set.seed(12345)
modFitDTree <- rpart(classe ~ ., data=trainSet, method="class")
fancyRpartPlot(modFitDTree)
```
Prediction on test dataset
```{r}
predictDTree <- predict(modFitDTree, newdata=testSet, type="class")
confMatDTree <- confusionMatrix(predictDTree, as.factor(testSet$classe))
confMatDTree
```
Plot matrix results
```{r}
plot(confMatDTree$table, col = confMatDTree$byClass,
main = paste("Decision Tree - Accuracy =",
round(confMatDTree$overall['Accuracy'], 4)))
```
### Generalized Boosted Model
Model fit
```{r}
set.seed(12345)
fitGBM <- trainControl(method = "repeatedcv", number = 5, repeats = 1)
modFitGBM <- train(classe ~ ., data=trainSet, method = "gbm",
trControl =fitGBM, verbose = FALSE)
modFitGBM$finalModel
```
Prediction on test dataset
```{r}
predictGBM <- predict(modFitGBM, newdata=testSet)
cfMatGBM <- confusionMatrix(predictGBM, as.factor(testSet$classe))
cfMatGBM
```
Plot matrix results
```{r}
plot(cfMatGBM$table, col = cfMatGBM$byClass,
main = paste("GBM - Accuracy =", round(cfMatGBM$overall['Accuracy'], 4)))
```
## Prediction
The Random Forest model is used to predict the 20 results of the questionnaire for qualification
Random Forest : 0.999
Decision Tree : 0.7342
GBM : 0.9871
```{r}
predictTest<- predict(modFitRf, newdata=testing)
predictTest
```