Introduction

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.

Loading Packages

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

Data Preparation

# set the url for the download
urlTrain <- "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv"
urlValid <- "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv"

# download the datasets
training <- read.csv(url(urlTrain))
validation <- read.csv(url(urlValid))
training$classe <- as.factor(training$classe)

# create a partition with the training dataset
inTrain <- createDataPartition(y=training$classe,p=0.7,list=FALSE)
trainSet <- training[inTrain,]
testSet <- training[-inTrain,]
dim(trainSet)
## [1] 13737   160
dim(testSet)
## [1] 5885  160
# remove variables with near zero variance
NZV <- nearZeroVar(trainSet)
trainSet <- trainSet[,-NZV]
testSet <- testSet[,-NZV]

# remove variables that are mostly NA
allNA <- sapply(trainSet,function(x)mean(is.na(x)))>0.95
trainSet <- trainSet[,allNA==FALSE]
testSet <- testSet[,allNA==FALSE]

# remove identification only variables
trainSet <- trainSet[,-(1:5)]
testSet <- testSet[,-(1:5)]
dim(trainSet)
## [1] 13737    54
dim(testSet)
## [1] 5885   54

Correlation Analysis

#library(corrplot)
corMatrix <- cor(trainSet[,-54])
corrplot(corMatrix,order="FPC",method="color",type="lower",tl.cex=0.8,tl.col=rgb(0,0,0))

Prediction Model building

Method - Random Forest

# model fit
set.seed(123)
controlRF <- trainControl(method="cv",number=3,verboseIter=FALSE)
modFitRandForest <- train(classe~.,data=trainSet,method="rf",trControl=controlRF)
modFitRandForest$finalModel
## 
## Call:
##  randomForest(x = x, y = y, mtry = param$mtry) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 27
## 
##         OOB estimate of  error rate: 0.25%
## Confusion matrix:
##      A    B    C    D    E  class.error
## A 3905    1    0    0    0 0.0002560164
## B    5 2650    3    0    0 0.0030097818
## C    0    6 2390    0    0 0.0025041736
## D    0    0   11 2240    1 0.0053285968
## E    0    0    0    7 2518 0.0027722772
# prediction on test set
predRandForest <- predict(modFitRandForest,testSet)
confMatRandForest <- confusionMatrix(predRandForest,testSet$classe)

# plot matrix results
plot(confMatRandForest$table,col=confMatRandForest$byClass,
     main=paste("Random Forest - Accuracy =",round(confMatRandForest$overall['Accuracy'],4)))

Method - Decision Trees

# model fit
set.seed(1234)
modFitDecTree <- rpart(classe~.,data=trainSet,method="class")
fancyRpartPlot(modFitDecTree)

# prediction on test data set
predDecTree <- predict(modFitDecTree,newdata=testSet,type="class")
confMatDecTree <- confusionMatrix(predDecTree,testSet$classe)
confMatDecTree
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 1519  276   43  108   99
##          B   44  600   44   23   97
##          C    8   64  812  140   63
##          D   85  134   57  641  129
##          E   18   65   70   52  694
## 
## Overall Statistics
##                                           
##                Accuracy : 0.7249          
##                  95% CI : (0.7133, 0.7363)
##     No Information Rate : 0.2845          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.6496          
##                                           
##  Mcnemar's Test P-Value : < 2.2e-16       
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            0.9074   0.5268   0.7914   0.6649   0.6414
## Specificity            0.8751   0.9562   0.9434   0.9177   0.9573
## Pos Pred Value         0.7428   0.7426   0.7470   0.6128   0.7720
## Neg Pred Value         0.9596   0.8938   0.9554   0.9333   0.9222
## Prevalence             0.2845   0.1935   0.1743   0.1638   0.1839
## Detection Rate         0.2581   0.1020   0.1380   0.1089   0.1179
## Detection Prevalence   0.3475   0.1373   0.1847   0.1777   0.1528
## Balanced Accuracy      0.8912   0.7415   0.8674   0.7913   0.7994
# plot matrix results
plot(confMatDecTree$table,col=confMatDecTree$byClass,
     main = paste("Decision Tree - Accuracy =",round(confMatDecTree$overall['Accuracy'], 4)))

Method - Generalized Boosted Model

# model fit
set.seed(12345)
controlGBM <- trainControl(method="repeatedcv",number=5,repeats=1)
modFitGBM <- train(classe~.,data=trainSet,method="gbm",trControl=controlGBM,verbose=F)
modFitGBM$finalModel
## A gradient boosted model with multinomial loss function.
## 150 iterations were performed.
## There were 53 predictors of which 53 had non-zero influence.
# prediction on test data set
predGBM <- predict(modFitGBM,newdata=testSet)
confMatGBM <- confusionMatrix(predGBM,testSet$classe)
confMatGBM
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 1663    7    0    3    0
##          B   10 1115    9    2    4
##          C    0   16 1013   11    5
##          D    1    0    3  947   16
##          E    0    1    1    1 1057
## 
## Overall Statistics
##                                           
##                Accuracy : 0.9847          
##                  95% CI : (0.9812, 0.9877)
##     No Information Rate : 0.2845          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.9807          
##                                           
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            0.9934   0.9789   0.9873   0.9824   0.9769
## Specificity            0.9976   0.9947   0.9934   0.9959   0.9994
## Pos Pred Value         0.9940   0.9781   0.9694   0.9793   0.9972
## Neg Pred Value         0.9974   0.9949   0.9973   0.9965   0.9948
## Prevalence             0.2845   0.1935   0.1743   0.1638   0.1839
## Detection Rate         0.2826   0.1895   0.1721   0.1609   0.1796
## Detection Prevalence   0.2843   0.1937   0.1776   0.1643   0.1801
## Balanced Accuracy      0.9955   0.9868   0.9904   0.9892   0.9881
# plot matrix results
plot(confMatGBM$table,col=confMatGBM$byClass,
     main=paste("GBM - Accuracy =",round(confMatGBM$overall['Accuracy'],4)))

Applying best fit model

# Applying the Selected Model to the Validation Data
predValidation <- predict(modFitRandForest,validation)
predValidation
##  [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