Executive Summary

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, we 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:

Our goal is to predict whether the lifts were performed correctly.

Data Loading and Processing

We load the necessary libraries and download the data sets.

library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
library(randomForest)
## 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

The data for this project come from this source: http://groupware.les.inf.puc-rio.br/har

training <- read.csv('pml-training.csv', na.strings = c("NA", "#DIV/0!", ""), header=TRUE)
testing <- read.csv('pml-testing.csv', na.strings = c("NA", "#DIV/0!", ""), header=TRUE)

# str(training)

The variable we want to predict is the last one classe.

Data Cleaning

We remove any columns that have missing values.

training <- training[, colSums(is.na(training)) == 0]
testing <- testing[, colSums(is.na(testing)) == 0]

# str(training)

Next, we remove irrelevant data.

classe <- training$classe
train_clean <- training[, sapply(training, is.numeric)]
train_clean <- training[, -c(1:7)]
train_clean$classe <- classe

test_clean <- testing[, sapply(testing, is.numeric)]
test_clean <- testing[, -c(1:7)]

# str(test_clean)

This gives us clean training data with 19622 observations in 53 variables and clean test data with 20 observations in 53 variables.

Split the data

We partition off part of the training data (30%) for cross-validation to assess the model’s performance.

set.seed(666) 
index_train <- createDataPartition(train_clean$classe, p = 0.7, list = FALSE)
train <- train_clean[index_train, ]
valid <- train_clean[-index_train, ]

# str(valid)

Training the Model

We will use the Random Forrest model since this will automatically select the important variables. We do a 5-fold cross validation (default setting in trainControl function is 10) when implementing the Random Forrest algorithm to save some computing time.

control <- trainControl(method = "cv", number = 5)
rf_model <- randomForest(classe ~ ., data = train)
print(rf_model)
## 
## Call:
##  randomForest(formula = classe ~ ., data = train) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 7
## 
##         OOB estimate of  error rate: 0.49%
## Confusion matrix:
##      A    B    C    D    E  class.error
## A 3903    3    0    0    0 0.0007680492
## B   12 2641    5    0    0 0.0063957863
## C    0   12 2380    4    0 0.0066777963
## D    0    0   22 2229    1 0.0102131439
## E    0    0    2    6 2517 0.0031683168

Cross Validation

We apply cross validation on the data split above to determine the out-of-sample error of the model that we fit.

cv_predict <- predict(rf_model, valid)
cv_summary <- confusionMatrix(valid$classe, cv_predict)
cv_summary
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 1673    1    0    0    0
##          B    8 1129    2    0    0
##          C    0    2 1024    0    0
##          D    0    0   12  951    1
##          E    0    0    0    7 1075
## 
## Overall Statistics
##                                           
##                Accuracy : 0.9944          
##                  95% CI : (0.9921, 0.9961)
##     No Information Rate : 0.2856          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.9929          
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            0.9952   0.9973   0.9865   0.9927   0.9991
## Specificity            0.9998   0.9979   0.9996   0.9974   0.9985
## Pos Pred Value         0.9994   0.9912   0.9981   0.9865   0.9935
## Neg Pred Value         0.9981   0.9994   0.9971   0.9986   0.9998
## Prevalence             0.2856   0.1924   0.1764   0.1628   0.1828
## Detection Rate         0.2843   0.1918   0.1740   0.1616   0.1827
## Detection Prevalence   0.2845   0.1935   0.1743   0.1638   0.1839
## Balanced Accuracy      0.9975   0.9976   0.9930   0.9950   0.9988

Let’s look at the accuracy and out-of-sample-error.

accuracy <- postResample(cv_predict, valid$classe)
accuracy
##  Accuracy     Kappa 
## 0.9943925 0.9929063
oose <- 1 - as.numeric(confusionMatrix(valid$classe, cv_predict)$overall[1])
oose
## [1] 0.005607477

For this data set, the accuracy is roughly 99.4% with out-of-sample error is roughly 0.6%. This could be a consequence of high correlation between predictors.

I attempted to use the Classification Tree model, but the the run time was too long (after 45 minutes, I stopped the program).

Predicition on the Test Set

Finally, we test the Random Forrest model on the test data.

results <- predict(rf_model, test_clean)
results
##  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

References

[1] Velloso, E.; Bulling, A.; Gellersen, H.; Ugulino, W.; Fuks, H. Qualitative Activity Recognition of Weight Lifting Exercises. Proceedings of 4th International Conference in Cooperation with SIGCHI (Augmented Human ’13) . Stuttgart, Germany: ACM SIGCHI, 2013.