Synopsis (from PML course page)

Introduction: 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). The goal of this project was to predict the manner in which they did the exercise.

Results: I built models based on random forests, boosted trees and linear discriminant analysis, as well as a combined model. The random forest model was most accurate (99%). Predictions for the test data set are shown below.

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.

library(caret)
library(e1071)
library(randomForest)
library(gbm)

# load training and testing datasets
training = read.csv("pml-training.csv")
testing = read.csv("pml-testing.csv")

The testing data does not have values for many of the variables. Therefore, these variables will be useless for predicting, so I will remove them from both the training and the testing datasets:

# find cols with data
allmisscols <- apply(testing, 2, function(x)all(is.na(x)));  
colsWithData <-names(allmisscols[allmisscols==0]);
# of these, the first few columns don't contain pertinent information
selectedCols <- colsWithData[9:length(colsWithData)-1]
training1 <- training[,selectedCols]
training1$classe <- training$classe
testing1 <- testing[,selectedCols]
testing1$problem_id <- testing$problem_id
# create training and validation set
inTrain <- createDataPartition(y=training1$classe, p=0.7, list=FALSE)
training1train <- training1[inTrain,]
training1test <- training1[-inTrain,]

I will use all remaining variables in the model as predictors. (I’m in a hurry, so I don’t have time to do principal component analysis first or something like that.) I also split up the training set into training1train and training1test sets. I will fit the models to the training1train set and estimate the errors on the training1test set.

Model

The goal of this assignment is to predict the manner in which they did the exercise. This is stored in the variable classe. I predict classe using three different models with cross validation: a random forest, boosted trees and linear discriminant analysis model. Then I combine the three models by blending. I test the accuracy of each model and then choose the model that works best for predicting the test set classe values.

# models
set.seed(3333)
tr <- trainControl(method="cv", number=3)
model1 <- train(classe ~., method="rf", data=training1train, trControl=tr)
model2 <- train(classe ~., method="gbm", data=training1train, verbose=FALSE)
model3 <- train(classe ~., method="lda", data=training1train)
c1 <- confusionMatrix(predict(model1, training1test), training1test$classe)
c2 <- confusionMatrix(predict(model2, training1test), training1test$classe)
c3 <- confusionMatrix(predict(model3, training1test), training1test$classe)
# get accuracy
c1.accuracy <- c1$overall['Accuracy']
c2.accuracy <- c2$overall['Accuracy']
c3.accuracy <- c3$overall['Accuracy']

# the combined model 
predDF <- data.frame(pred1 = predict(model1, training1train), pred2 = predict(model2, training1train), pred3 = predict(model3, training1train), classe = training1train$classe)
combinedModel <- train(classe ~ ., data = predDF, method = "gam")
predDFtest <- data.frame(pred1 = predict(model1, training1test), pred2 = predict(model2, training1test), pred3 = predict(model3, training1test), classe = training1test$classe)
c <- confusionMatrix(predict(combinedModel, predDFtest), predDFtest$classe)
c.accuracy <- c$overall['Accuracy']

The accuracy of the predictions for the different models are: 99.3882753% for Model 1, 96.4655905% for Model 2, and 71.4868309% for Model 3. For the combined model, the accuracy is 47.6975361%. Therefore, I select Model 1 for the predictions on the test data set.

Model Predictions

The model predictions for the testing set are:

predictions <- data.frame(problem_id = testing1$problem_id, prediction = predict(model1, testing1))
predictions
##    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

Thanks.