## Executive Summary

Using accelerometer data from the belt, forearm, arm, and dumbbell of 6 participants performing barbell lifts correctly and incorrectly in 5 ways (classes A-E), we built a random forest model to predict the manner in which an exercise was performed. After cleaning the data and holding out 30% for validation, the model achieved about 99% validation accuracy.

Load and Clean Data

library(caret)
library(randomForest)

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

na_prop <- colMeans(is.na(training_raw))
training_clean <- training_raw[, na_prop < 0.9]

nzv <- nearZeroVar(training_clean)
training_clean <- training_clean[, -nzv]

id_cols <- grep("^X$|user_name|timestamp|window", names(training_clean))
training_clean <- training_clean[, -id_cols]

training_clean$classe <- factor(training_clean$classe)
dim(training_clean)
## [1] 19622    53

Split into Training and Validation Sets

set.seed(12345)
inTrain  <- createDataPartition(training_clean$classe, p = 0.7, list = FALSE)
trainSet <- training_clean[inTrain, ]
validSet <- training_clean[-inTrain, ]

Train the Model

ctrl <- trainControl(method = "cv", number = 5)
set.seed(12345)
modFit <- train(classe ~ ., data = trainSet, method = "rf",
                 trControl = ctrl, ntree = 150, importance = TRUE)
modFit
## Random Forest 
## 
## 13737 samples
##    52 predictor
##     5 classes: 'A', 'B', 'C', 'D', 'E' 
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold) 
## Summary of sample sizes: 10990, 10990, 10989, 10991, 10988 
## Resampling results across tuning parameters:
## 
##   mtry  Accuracy   Kappa    
##    2    0.9916284  0.9894090
##   27    0.9908273  0.9883964
##   52    0.9839117  0.9796484
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.

Validate: Out-of-Sample Accuracy

predValid <- predict(modFit, validSet)
confusionMatrix(predValid, validSet$classe)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 1673    9    0    0    0
##          B    1 1130    6    0    0
##          C    0    0 1020   25    1
##          D    0    0    0  937    0
##          E    0    0    0    2 1081
## 
## Overall Statistics
##                                         
##                Accuracy : 0.9925        
##                  95% CI : (0.99, 0.9946)
##     No Information Rate : 0.2845        
##     P-Value [Acc > NIR] : < 2.2e-16     
##                                         
##                   Kappa : 0.9905        
##                                         
##  Mcnemar's Test P-Value : NA            
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            0.9994   0.9921   0.9942   0.9720   0.9991
## Specificity            0.9979   0.9985   0.9946   1.0000   0.9996
## Pos Pred Value         0.9946   0.9938   0.9751   1.0000   0.9982
## Neg Pred Value         0.9998   0.9981   0.9988   0.9945   0.9998
## Prevalence             0.2845   0.1935   0.1743   0.1638   0.1839
## Detection Rate         0.2843   0.1920   0.1733   0.1592   0.1837
## Detection Prevalence   0.2858   0.1932   0.1777   0.1592   0.1840
## Balanced Accuracy      0.9986   0.9953   0.9944   0.9860   0.9993

Predict the 20 Test Cases

predTest <- predict(modFit, testing_raw)
data.frame(problem_id = testing_raw$problem_id, prediction = predTest)
##    problem_id prediction
## 1           1          B
## 2           2          A
## 3           3          B
## 4           4          A
## 5           5          A
## 6           6          E
## 7           7          D
## 8           8          B
## 9           9          A
## 10         10          A
## 11         11          B
## 12         12          C
## 13         13          B
## 14         14          A
## 15         15          E
## 16         16          E
## 17         17          A
## 18         18          B
## 19         19          B
## 20         20          B

Conclusion

The random forest model achieved approximately 99% accuracy on the held-out validation set, giving high confidence in the final predictions above.