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


Method

Loading and cleaning data

The following Libraries were used for this project. These should be first installed and loaded in the working environment.

library(caret)
## Warning: package 'caret' was built under R version 3.1.2
## Loading required package: lattice
## Loading required package: ggplot2
library(randomForest)
## Warning: package 'randomForest' was built under R version 3.1.2
## randomForest 4.6-10
## Type rfNews() to see new features/changes/bug fixes.

A pseudo random seed has been set to ensure reproducibility of results.

set.seed(5627)

The data were loaded in the working environment.

In order to provide consistent data to construct features, the Excel division error strings #DIV/0! were removed and replaced with NA values and empty strings were replaced by NA values.

trainUrl <- "http://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv"
testUrl <- "http://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv"
training <- read.csv(url(trainUrl), na.strings=c("NA","#DIV/0!",""))
testing <- read.csv(url(testUrl), na.strings=c("NA","#DIV/0!",""))

Cross Validation

Cross validation was achieved by partitioning the data by the class variable into a training (60%) and test(40%) set.

inTrain <- createDataPartition(y=training$classe, p=0.60, list=FALSE)
train <- training[inTrain, ] 
test<- training[-inTrain, ]

Next a function to remove entire NA columns was applied to both data frames. Then a function that removes any variables with missing NAs was applied to both data frames.

removeNAcols   <- function(x) { x[ , colSums( is.na(x) ) < nrow(x) ] }
train <- removeNAcols(train)
test  <- removeNAcols(test)
complete       <- function(x) {x[,sapply(x, function(y) !any(is.na(y)))] }
incompl        <- function(x) {names( x[,sapply(x, function(y) any(is.na(y)))] ) }
trtr.na.var    <- incompl(train)
trts.na.var    <- incompl(test)
train <- complete(train)
test  <- complete(test)

Prediction model

The random forest model was used for prediction.

random.forest <- train(train[,-57],
                       train$classe,
                       tuneGrid=data.frame(mtry=3),
                       trControl=trainControl(method="none")
                       )

Results

Below is a summary of the results from the prediction model

##                 Length Class      Mode     
## call                4  -none-     call     
## type                1  -none-     character
## predicted       11776  factor     numeric  
## err.rate         3000  -none-     numeric  
## confusion          30  -none-     numeric  
## votes           58880  matrix     numeric  
## oob.times       11776  -none-     numeric  
## classes             5  -none-     character
## importance         59  -none-     numeric  
## importanceSD        0  -none-     NULL     
## localImportance     0  -none-     NULL     
## proximity           0  -none-     NULL     
## ntree               1  -none-     numeric  
## mtry                1  -none-     numeric  
## forest             14  -none-     list     
## y               11776  factor     numeric  
## test                0  -none-     NULL     
## inbag               0  -none-     NULL     
## xNames             59  -none-     character
## problemType         1  -none-     character
## tuneValue           1  data.frame list     
## obsLevels           5  -none-     character

The results from the prediction model are then compared with the actual data:

confusionMatrix(predict(random.forest,
                        newdata=test[,-57]),
                test$classe
                )
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 2232    0    0    0    0
##          B    0 1518    0    0    0
##          C    0    0 1368    0    0
##          D    0    0    0 1286    0
##          E    0    0    0    0 1442
## 
## Overall Statistics
##                                      
##                Accuracy : 1          
##                  95% CI : (0.9995, 1)
##     No Information Rate : 0.2845     
##     P-Value [Acc > NIR] : < 2.2e-16  
##                                      
##                   Kappa : 1          
##  Mcnemar's Test P-Value : NA         
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            1.0000   1.0000   1.0000   1.0000   1.0000
## Specificity            1.0000   1.0000   1.0000   1.0000   1.0000
## Pos Pred Value         1.0000   1.0000   1.0000   1.0000   1.0000
## Neg Pred Value         1.0000   1.0000   1.0000   1.0000   1.0000
## Prevalence             0.2845   0.1935   0.1744   0.1639   0.1838
## Detection Rate         0.2845   0.1935   0.1744   0.1639   0.1838
## Detection Prevalence   0.2845   0.1935   0.1744   0.1639   0.1838
## Balanced Accuracy      1.0000   1.0000   1.0000   1.0000   1.0000

The Kappa statistic of 1 indicates perfect agreement.

In conclusion, the random forest algorithm appears to perform very well for predicting activities from accelerometers measurements.