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).

Purpose of this assignment

The purpose of this assignment is to do the following:

The assignment

First we load the data, eliminate variables that are all NAs and variables that are not measurements (or outcome):

library(dplyr)
setwd("/Users/nielsoledam/Desktop/Coursera/pracmachlearn_assignment")
# download.file("https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv", "pml-training.csv", method="curl")
dataset <- read.csv("pml-training.csv", header=TRUE, na.strings = c("", " ","NA","#DIV/0!"), stringsAsFactors=FALSE)
dataset$classe <- as.factor(dataset$classe)
dataset <- dataset[,colSums(is.na(dataset)) != nrow(dataset)]
dataset <- select(dataset, roll_belt:classe)

Then we remove all variables with near zero variance using nearZeroVar() and make a training and a testing set - the latter to be used for cross validation. We choose to split the data 70/30 because it is only a medium sized data set. After that we get at look at the data:

library(caret)
nz <- nearZeroVar(dataset)
dataset <- dataset[, -nz] 
inTrain <- createDataPartition(y=dataset$classe, p=0.7, list=FALSE)
training <- dataset[inTrain, ]
testing <- dataset[-inTrain, ]
View(training)

As we can see that the data set contains a lot of missing values, we now preprocess the training data set imputing the missing values using the k nearest neighbour method. We also use the same preprocessing on the testing data (and remember to set the seed in order to achieve the same randomization for both data partitions):

set.seed(12345)
library(RANN)
preObj <- preProcess(training[, -ncol(training)], method="knnImpute")
training2 <- predict(preObj, training[, -ncol(training)])
training2$classe <- training$classe
testing2 <- predict(preObj, testing[, -ncol(training)])
testing2$classe <- testing$classe

We now use train() with Random Forest for making a model using all the measurement variables:

modFit <- train(classe~., method="rf", data=training2)
save(modFit, file = "model.rda") # Save for possible reuse in a later RStudio session
modFit$finalModel

Call:
 randomForest(x = x, y = y, mtry = param$mtry) 
               Type of random forest: classification
                     Number of trees: 500
No. of variables tried at each split: 59

        OOB estimate of  error rate: 0.77%
Confusion matrix:
     A    B    C    D    E class.error
A 3901    3    0    0    2 0.001280082
B   25 2624    9    0    0 0.012791573
C    0   20 2367    9    0 0.012103506
D    1    1   22 2226    2 0.011545293
E    0    1    4    7 2513 0.004752475

As we can see the model seem fit the out of sample data very well, with a Out of Bag estimate of only 0.77%. So we would expect the model to predict new data very well. In order to test this assumption we now cross-validate the model with the testing data set and look at the resulting confusion matrix:

predictions <- predict(modFit, newdata=testing2)
cm <- confusionMatrix(predictions, testing2$classe)
cm$table; cm$overall[1]

          Reference
Prediction    A    B    C    D    E
         A 1671    4    0    0    0
         B    1 1126   19    0    0
         C    2    9 1006   11    1
         D    0    0    1  952    3
         E    0    0    0    1 1078
Accuracy 
0.991164 

As it can be seen, the accuracy of the cross-validation is 99.1% which is very good and confirm that our model is not overfitting the training dataset. So it is with confidence, we now use the model to predict the final 20 data samples.

Prediction and submission of the 20 data samples

Now we load the 20 data samples to predict:

# download.file("https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv", "pml-testing.csv", method="curl")
toPredict <- read.csv("pml-testing.csv", na.strings = c("", " ","NA","#DIV/0!"), stringsAsFactors=FALSE)

# Keep only the variables that are also present in training dataset and add a classe variable
toPredict <- toPredict[ , which(names(toPredict) %in% names(training))]
toPredict$classe <- as.factor(NA)

And preprocess the data the same way as with the training set and then use our model to make the prediction and save the result as 20 seperate files:

toPredict2 <- predict(preObj, toPredict[, -ncol(toPredict)])
answers <- as.character(predict(modFit, newdata=toPredict2))

pml_write_files = function(x){
  n = length(x)
  for(i in 1:n){
    filename = paste0("problem_id_",i,".txt")
    write.table(x[i],file=filename,quote=FALSE,row.names=FALSE,col.names=FALSE)
  }
} 
pml_write_files(answers)

The 20 files are then uploaded to Coursera for evaluation.

(And got all 20 predictions right! :-D )