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

Load Packages

library(caret)
## Warning: package 'caret' was built under R version 3.4.4
## Loading required package: lattice
## Loading required package: ggplot2
library(rpart)
## Warning: package 'rpart' was built under R version 3.4.4
library(rpart.plot)
## Warning: package 'rpart.plot' was built under R version 3.4.4
library(rattle)
## Warning: package 'rattle' was built under R version 3.4.4
## Rattle: A free graphical interface for data science with R.
## Version 5.1.0 Copyright (c) 2006-2017 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
library(RColorBrewer)
library(randomForest)
## Warning: package 'randomForest' was built under R version 3.4.4
## randomForest 4.6-14
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:rattle':
## 
##     importance
## The following object is masked from 'package:ggplot2':
## 
##     margin

Load Data

url_train <- "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv"
url_test <- "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv"

train <- read.csv(url(url_train))
test  <- read.csv(url(url_test))

Clean Data

Remove variables that are NA and are not a part of the testing dataset, and exlude the variables that aren’t useful at all for this exercise.

feat <- names(test[,colSums(is.na(test))==0])[8:59]

train <- train[,c(feat,"classe")]
test <- test[,c(feat, "problem_id")]

Partition training data

In this course we learned to break the training data up into a training set(60%) and a testing set(40%) in order to give us the ability to find out of sample error of our predictor.

in_train <- createDataPartition(train$classe, p=0.6, list=FALSE)
to_train <- train[in_train, ]; to_test <- train[-in_train, ]

dim(to_train)
## [1] 11776    53
dim(to_test)
## [1] 7846   53

Build A Decision Tree Model

DTM <- rpart(classe ~ ., data = to_train, method="class")
fancyRpartPlot(DTM, "Classe Decision Tree Model")

Decision Tree Model Prediction

The decision tree model here has an accuracy of 0.7366. We will try another model in an effort to get something more accurate.

set.seed(8765)

prediction <- predict(DTM, to_test, type = "class")
confusionMatrix(prediction, to_test$classe)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 2028  274   78  155   46
##          B   64  895  110  128  114
##          C   50  134 1043  189  174
##          D   51  138   99  699   73
##          E   39   77   38  115 1035
## 
## Overall Statistics
##                                           
##                Accuracy : 0.7265          
##                  95% CI : (0.7165, 0.7363)
##     No Information Rate : 0.2845          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.652           
##  Mcnemar's Test P-Value : < 2.2e-16       
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            0.9086   0.5896   0.7624  0.54355   0.7178
## Specificity            0.9015   0.9343   0.9156  0.94497   0.9580
## Pos Pred Value         0.7857   0.6827   0.6560  0.65943   0.7937
## Neg Pred Value         0.9613   0.9047   0.9480  0.91350   0.9378
## Prevalence             0.2845   0.1935   0.1744  0.16391   0.1838
## Detection Rate         0.2585   0.1141   0.1329  0.08909   0.1319
## Detection Prevalence   0.3290   0.1671   0.2027  0.13510   0.1662
## Balanced Accuracy      0.9050   0.7619   0.8390  0.74426   0.8379

Random Forests

The random forest model here has an accuracy of 0.9954! This is much improved from the decision tree model.

set.seed(8765)
RFM <- randomForest(classe ~ ., data = to_train, ntree = 1000)

prediction <- predict(RFM, to_test, type = "class")
confusionMatrix(prediction, to_test$classe)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 2229    9    0    0    0
##          B    3 1502    6    0    0
##          C    0    7 1361   17    0
##          D    0    0    1 1268    2
##          E    0    0    0    1 1440
## 
## Overall Statistics
##                                           
##                Accuracy : 0.9941          
##                  95% CI : (0.9922, 0.9957)
##     No Information Rate : 0.2845          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.9926          
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            0.9987   0.9895   0.9949   0.9860   0.9986
## Specificity            0.9984   0.9986   0.9963   0.9995   0.9998
## Pos Pred Value         0.9960   0.9940   0.9827   0.9976   0.9993
## Neg Pred Value         0.9995   0.9975   0.9989   0.9973   0.9997
## Prevalence             0.2845   0.1935   0.1744   0.1639   0.1838
## Detection Rate         0.2841   0.1914   0.1735   0.1616   0.1835
## Detection Prevalence   0.2852   0.1926   0.1765   0.1620   0.1837
## Balanced Accuracy      0.9985   0.9940   0.9956   0.9928   0.9992

Out of Sample Order

We will use the Random Forest model to predict the 20 cases from the testing data set.

predictions <- predict(RFM, test, type = "class")
predictions
##  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