Exercise Analysis

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. More information is available from the website here: http://groupware.les.inf.puc-rio.br/har (see the section on the Weight Lifting Exercise Dataset).

The 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. 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.

What you should submit

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.

  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.

Summary of the analysis carried out

The following summarizes the steps used in the analysis of the data, from reading in the data from the supplied files to the final predication based on the test file.

  1. Load the traing data set and clean this data by removing columns with too many NAs and by removing the start columns which do not contain explanatory variables
  2. Load and clean the testing data as in the previous step. Also exchange the final column for a column that replicates the 5 level factor variable in the training data. This is needed for the predication step
  3. Partition the training data: use cross-validation based on a 70:30 split of the training data into a training set and a testing (validation) set
  4. Apply a decision tree method to build a model and assess its accuracy
  5. Apply random forest method to build a model and assess its accuracy
  6. Check the most accurate model with the testing data set
  7. Apply the model to estimate classes of 20 observations

Load required packages

if(!require(ggplot2))
{install.packages("ggplot2");
  library(ggplot2);
  }
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.3.3
if(!require(caret))
{install.packages("caret");
  library(caret);
  }
## Loading required package: caret
## Warning: package 'caret' was built under R version 3.3.3
## Loading required package: lattice
## Warning: package 'lattice' was built under R version 3.3.2
if(!require(randomForest))
{install.packages("randomForest");
  library(randomForest);
  }
## Loading required package: randomForest
## Warning: package 'randomForest' was built under R version 3.3.3
## randomForest 4.6-12
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:ggplot2':
## 
##     margin
if(!require(rpart))
{install.packages("rpart");
  library(rpart);
  };
## Loading required package: rpart
if(!require(rpart.plot))
{install.packages("rpart.plot");
  library(rpart.plot);
  }
## Loading required package: rpart.plot
## Warning: package 'rpart.plot' was built under R version 3.3.3
if(!require(rattle))
{install.packages("rattle");
  library(rattle);
  }
## Loading required package: rattle
## Warning: package 'rattle' was built under R version 3.3.3
## Rattle: A free graphical interface for data mining with R.
## Version 4.1.0 Copyright (c) 2006-2015 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.

Load the data files and clean the data

Train<-read.csv("pml-training.csv", na.strings=c("NA", "#DIV/0!", ""))
Test<-read.csv("pml-testing.csv", na.strings=c("NA", "#DIV/0!", ""))
dim(Train)
## [1] 19622   160
dim(Test)
## [1]  20 160
StartVar <- grep("name|timestamp|window|X", colnames(Train), value=F)
Train<-Train[,-StartVar]
Train<-Train[, colSums(is.na(Train))==0]

Prepare the test file for prediction

We will want the Test file to match the Training file so we remove the “problem_id”" column and replace it with a “classe”" column with a 5 level factor. We also clean this data frame in the same manner as the training data frame.

Test<-Test[,-160]
Test["classe"]<-c("A", "B", "C","D","E")
Test$classe<-factor(Test$classe)
Test<-Test[,-StartVar]
Test<-Test[, colSums(is.na(Test))==0]
dim(Test)
## [1] 20 53

Cross-validation

We split the Train data frame into a training set and a test (validation) set

inTrain<-createDataPartition(y=Train$classe, p=0.75, list=FALSE)
TrainTrain<-Train[inTrain,]
TrainTest<- Train[-inTrain, ]
dim(TrainTrain)
## [1] 14718    53
dim(TrainTest)
## [1] 4904   53

Exploratory analysis of the “classe” variable

plot(TrainTrain$classe, col="yellow", main="Plot of levels of classe variable", xlab="classe", ylab="Frequency")

## Machine learning using decision trees ##

model_dt<-rpart(classe~., data=TrainTrain, method="class")
predict_dt<-predict(model_dt, TrainTest, type="class")
confusionMatrix(predict_dt, TrainTest$classe)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 1264  205   27   94   64
##          B   34  567   83   30   63
##          C   33   77  664  123   88
##          D   46   57   53  493   53
##          E   18   43   28   64  633
## 
## Overall Statistics
##                                           
##                Accuracy : 0.7384          
##                  95% CI : (0.7258, 0.7506)
##     No Information Rate : 0.2845          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.6669          
##  Mcnemar's Test P-Value : < 2.2e-16       
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            0.9061   0.5975   0.7766   0.6132   0.7026
## Specificity            0.8889   0.9469   0.9207   0.9490   0.9618
## Pos Pred Value         0.7642   0.7297   0.6741   0.7023   0.8053
## Neg Pred Value         0.9597   0.9074   0.9513   0.9260   0.9349
## Prevalence             0.2845   0.1935   0.1743   0.1639   0.1837
## Detection Rate         0.2577   0.1156   0.1354   0.1005   0.1291
## Detection Prevalence   0.3373   0.1584   0.2009   0.1431   0.1603
## Balanced Accuracy      0.8975   0.7722   0.8487   0.7811   0.8322
#fancyRpartPlot(model_dt)
rpart.plot(model_dt)

As we can see, we achieve an accuracy of 76% with this method

Machine learning using random forest

model_rf<-randomForest(classe~., data=TrainTrain, method="class")
predict_rf<-predict(model_rf, TrainTest, type="class")
confusionMatrix(predict_rf, TrainTest$classe)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 1392    0    0    0    0
##          B    1  948    8    0    0
##          C    2    1  846    8    0
##          D    0    0    1  796    0
##          E    0    0    0    0  901
## 
## Overall Statistics
##                                           
##                Accuracy : 0.9957          
##                  95% CI : (0.9935, 0.9973)
##     No Information Rate : 0.2845          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.9946          
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            0.9978   0.9989   0.9895   0.9900   1.0000
## Specificity            1.0000   0.9977   0.9973   0.9998   1.0000
## Pos Pred Value         1.0000   0.9906   0.9872   0.9987   1.0000
## Neg Pred Value         0.9991   0.9997   0.9978   0.9981   1.0000
## Prevalence             0.2845   0.1935   0.1743   0.1639   0.1837
## Detection Rate         0.2838   0.1933   0.1725   0.1623   0.1837
## Detection Prevalence   0.2838   0.1951   0.1748   0.1625   0.1837
## Balanced Accuracy      0.9989   0.9983   0.9934   0.9949   1.0000

We achieve an accuracy of over 99% using random forests. We therefore choose this model for the final prediction.

Using the chosen model for prediction

model_final<-randomForest(classe~., data=TrainTrain, method="class")
predict_final<-predict(model_final, Test, type="class")
predict_final
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 
##  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

Summary

In this analysis of data relating to weight lifting exercises, the training data set of 19622 observations was divided into a set of 14718 observations used for training, and a set of 4904 observations that were used for internal validation of the training process. The training and prediction was based on a factor variable “classe” which classified performance on barbell lifts. It was required to predict on the basis of the machine learning algorithm chosen to predict the classifications based on 20 obervations in a “test” file.

Two machine learnning algorithms were tested, chosen to predict the required factor variable. These were decision trees and random forest. The decision tree method (rpart) was found to have an accuracy (assessed using the “confusionMatrix”) of just 76%, while the random forest model had an accuracy of over 99%. Thus the random forest method was used to find the predictions based on the test data.

This reported the following.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

B A B A A E D B A A B C B A E E A B B B