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://groupware.les.inf.puc-rio.br/har (see the section on the Weight Lifting Exercise Dataset).

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.

Project Implementation Details

The goal of the project is to predict the manner in which the group of enthusiasts did exercise. R has been used to implement the project. Since the dataset has a large number of variables, appropriate feature selection needs to be done. Before that, parallel processing in R needs to be set up. It is done as follows.

library(doParallel)    
cl <- makeCluster(detectCores())
registerDoParallel(cl)

The input file is now loaded and unnecessary features are filtered out. These include columns with over 80% NA values, #DIV/0! values, timestamp, user name, window and summary statistics specific values.

trainfile <- read.csv("pml-training.csv")
trainSub <- trainfile[ , (colSums(is.na(trainfile)) <= 0.8*nrow(trainfile))]
trainSub <- trainSub[,(colSums(trainSub[,] == "#DIV/0!")) == 0]

features <- names(trainSub)
toMatch <- c("timestamp","name","window","kurtosis","skewness")
trainSub <- trainSub[,-grep(paste(toMatch,collapse="|"),features)]
trainSub <- trainSub[,-1]
trainSub <- trainSub[,-1]

A 3 fold cross validation was used on a 50% training set sample to train the Random Forests model.

modControl <- trainControl(method = "cv", number = 3)
inTrain <- createDataPartition(y= trainSub$classe, p=0.5, list=FALSE)
training <- trainSub[inTrain, ]
testing <- trainSub[-inTrain, ]
modFit <- train(classe ~ ., data = training, method = "rf", trControl = modControl, prox = TRUE)

The output of the model is as shown below:

Random Forest

9812 samples 52 predictor 5 classes: ‘A’, ‘B’, ‘C’, ‘D’, ‘E’

No pre-processing Resampling: Cross-Validated (3 fold)

Summary of sample sizes: 6541, 6541, 6542

Resampling results across tuning parameters:

mtry Accuracy Kappa Accuracy SD Kappa SD
2 0.9821650 0.9774326 0.002468435 0.003123457 27 0.9841012 0.9798868 0.001616820 0.002044145 52 0.9796169 0.9742139 0.001269553 0.001603578

Accuracy was used to select the optimal model using the largest value. The final value used for the model was mtry = 27.

The insample accuracy of the final model was 0.984 and the corresponding Kappa value is 0.979 which indicates a very good model. The real test is the out of sample error rate though.

red <- predict(modFit, testing)
table(pred, testing$classe)

The output in this case is:

pred A B C D E A 2786 7 0 0 0 B 1 1881 7 1 1 C 0 10 1693 24 3 D 2 0 11 1582 13 E 1 0 0 1 1786

The out of sample error rate in this case is 0.835%. On the final test sample the predictions were also made as follows.

testfile <- read.csv("pml-testing.csv")
predOut <- predict(modFit, testfile)

The obtained output in this case is as follows: “B”,“A”,“B”,“A”,“A”,“E”,“D”,“B”,“A”,“A”,“B”,“C”,“B”,“A”,“E”,“E”,“A”,“B”,“B”,“B”

This output exactly matches the test set output.

Author

The above project was done as part of the 'Practical Machine Learning' course of the Coursera Data Science Specialization by Roshan Shetty. Please contact me on rosshanabshetty@gmail.com.