Introduction

The instructions for this project have been the following:

“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."

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://web.archive.org/web/20161224072740/http:/groupware.les.inf.puc-rio.br/har (see the section on the Weight Lifting Exercise Dataset).

The data have been the following:

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://web.archive.org/web/20161224072740/http:/groupware.les.inf.puc-rio.br/har.

Step 1: Loading libraries

library(knitr)
library(caret)
library(rpart)
library(rpart.plot)
library(randomForest)
library(corrplot)
library(rattle)

Step 2: Loading and cleaning Data

Set the URL for the download

UrlTrain <- "http://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv"
UrlTest  <- "http://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv"

Download the datasets

training <- read.csv(url(UrlTrain))
testing  <- read.csv(url(UrlTest))
dim(training)
## [1] 19622   160
dim(testing)
## [1]  20 160

Removing Variables which are having Nearly Zero Variance.

nzv <- nearZeroVar(training)
training_data <- training[,-nzv]
testing_data <- testing[,-nzv]
dim(training_data)
## [1] 19622   100
dim(testing_data)
## [1]  20 100

Removing NA Values of Variables.

allNA <- sapply(training_data, function(x) mean(is.na(x))) > 0.95
training_data <- training_data[,allNA == FALSE]
testing_data <- testing_data[,allNA == FALSE]
dim(training_data)
## [1] 19622    59
dim(testing_data)
## [1] 20 59

Remove identification only variables (columns 1 to 6)

training_data<- training_data[, 7:59]
testing_data<- testing_data[, 7:59]
dim(training_data)
## [1] 19622    53
dim(testing_data)
## [1] 20 53

Step 3: Prediction Model Building

I will use three model:

  • Random Forest Model
  • Decision trees with CART (rpart)

Data Partioning:

inTrain<- createDataPartition(training_data$classe, p=0.7, list=FALSE)
training<- training_data[inTrain,]
testing<- training_data[-inTrain,]
dim(training)
## [1] 13737    53
dim(testing)
## [1] 5885   53

Random Forest Model and Prediction:

set.seed(11111)
   
RF_model<- train(classe ~. , data=training, method= "rf", ntree=100)
 
RF_prediction<- predict(RF_model, testing)
RF_cm<-confusionMatrix(RF_prediction, testing$classe)
RF_cm
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 1674    8    0    0    0
##          B    0 1131    6    1    1
##          C    0    0 1014   10    5
##          D    0    0    6  953    1
##          E    0    0    0    0 1075
## 
## Overall Statistics
##                                           
##                Accuracy : 0.9935          
##                  95% CI : (0.9911, 0.9954)
##     No Information Rate : 0.2845          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.9918          
##                                           
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            1.0000   0.9930   0.9883   0.9886   0.9935
## Specificity            0.9981   0.9983   0.9969   0.9986   1.0000
## Pos Pred Value         0.9952   0.9930   0.9854   0.9927   1.0000
## Neg Pred Value         1.0000   0.9983   0.9975   0.9978   0.9985
## Prevalence             0.2845   0.1935   0.1743   0.1638   0.1839
## Detection Rate         0.2845   0.1922   0.1723   0.1619   0.1827
## Detection Prevalence   0.2858   0.1935   0.1749   0.1631   0.1827
## Balanced Accuracy      0.9991   0.9956   0.9926   0.9936   0.9968

From the Random Forest Model we see the prediction accuracy is 99%.

Decision Tree Model and Prediction

#Fit the model and plot   
set.seed(3333)
DT_model<- train(classe ~. , data=training, method= "rpart")

#Prediction   
DT_prediction<- predict(DT_model, testing)
confusionMatrix(DT_prediction, testing$classe)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 1519  457  474  450  150
##          B   34  404   31  163  153
##          C  119  278  521  351  292
##          D    0    0    0    0    0
##          E    2    0    0    0  487
## 
## Overall Statistics
##                                           
##                Accuracy : 0.498           
##                  95% CI : (0.4852, 0.5109)
##     No Information Rate : 0.2845          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.3441          
##                                           
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            0.9074  0.35470  0.50780   0.0000  0.45009
## Specificity            0.6364  0.91972  0.78596   1.0000  0.99958
## Pos Pred Value         0.4980  0.51465  0.33376      NaN  0.99591
## Neg Pred Value         0.9453  0.85588  0.88321   0.8362  0.88973
## Prevalence             0.2845  0.19354  0.17434   0.1638  0.18386
## Detection Rate         0.2581  0.06865  0.08853   0.0000  0.08275
## Detection Prevalence   0.5183  0.13339  0.26525   0.0000  0.08309
## Balanced Accuracy      0.7719  0.63721  0.64688   0.5000  0.72484

From the Decision Tree Model we see the prediction accuracy is 50%

Step 4: Conclusion

We conclude that, Random Forest is more accurate than Decision Tree Model.

Step 5: Applying the Selected Model to the Test Data

prediction_test<- predict(RF_model, testing_data)
prediction_test
##  [1] B A B A A E D B A A B C B A E E A B B B
## Levels: A B C D E