This project analyzes data obtained from accelerometers on the belt, forearm, dumbell and arm of participants, each of whom was asked to perform barbell exercises both correctly, and in 5 different incorrect ways. We will analyze the data, select and implement a model to predict the manner in which the test subjects did the exercise, based on a classification system A-E. We will use the prediciton model to predict 20 different test cases. Data was obtained from http://groupware.les.inf.puc-rio.br/har.
After reading in the training and testing data, I remove from both sets the variables that appear logically inapplicable, such as subject name, various time stamp references, and all summary statistics (e.g., totals, standard deviations, and variances calculated on the raw data), in order to analyze the raw collected data only.
library(caret)
library(ggplot2)
library(lattice)
library(randomForest)
train <- read.csv("pml-training.csv")
test <- read.csv("pml-testing.csv")
train1<-train[,-c(1:7, 12:36, 49:59, 69:83, 87:101, 103:112, 125:139, 141:150)]
test1<-test[,-c(1:7, 12:36, 49:59, 69:83, 87:101, 103:112, 125:139, 141:150, 160)]
In order to validate my eventual model selection, I split the training set into a 70:20 training/validation split.
tsplit <- createDataPartition(y=train1$classe, p=0.7, list=FALSE)
training <- train1[tsplit, ]
validating <- train1[-tsplit, ]
I selected a random forest model approach, due to its high accuracy rate and the large number of variables. This method performed 5-fold cross validation, as shown below.
controlRf <- trainControl(method="cv", 5)
fit <- train(classe ~ ., data=training, method="rf", trControl=controlRf)
fit
## Random Forest
##
## 13737 samples
## 51 predictor
## 5 classes: 'A', 'B', 'C', 'D', 'E'
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 10990, 10989, 10989, 10989, 10991
## Resampling results across tuning parameters:
##
## mtry Accuracy Kappa
## 2 0.9909735 0.9885807
## 26 0.9914830 0.9892259
## 51 0.9855137 0.9816738
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 26.
I then apply the model to the validation set to gauge accuracy and out of sample error.
pred<-predict(fit, validating)
cf<-confusionMatrix(table(pred, validating$classe))
cf
## Confusion Matrix and Statistics
##
##
## pred A B C D E
## A 1673 12 0 0 0
## B 0 1123 9 0 0
## C 0 4 1013 9 0
## D 0 0 4 955 3
## E 1 0 0 0 1079
##
## Overall Statistics
##
## Accuracy : 0.9929
## 95% CI : (0.9904, 0.9949)
## No Information Rate : 0.2845
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.991
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.9994 0.9860 0.9873 0.9907 0.9972
## Specificity 0.9972 0.9981 0.9973 0.9986 0.9998
## Pos Pred Value 0.9929 0.9920 0.9873 0.9927 0.9991
## Neg Pred Value 0.9998 0.9966 0.9973 0.9982 0.9994
## Prevalence 0.2845 0.1935 0.1743 0.1638 0.1839
## Detection Rate 0.2843 0.1908 0.1721 0.1623 0.1833
## Detection Prevalence 0.2863 0.1924 0.1743 0.1635 0.1835
## Balanced Accuracy 0.9983 0.9920 0.9923 0.9946 0.9985
We see that our model produced accuracy of 0.9928632 and an out-of-sample error of 0.0071368. Based on this data, I will consider the model successful and apply it to our test data.
Application of the model to the test data yields the prediction results below.
testpred<-predict(fit, test1)
testpred
## [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