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, the 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 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 this document for any purpose please cite them as they have been very generous in allowing their data to be used for this project.
The goal of this project is to predict the manner in which people exercise (given 20 different test cases), using the data from the aforementioned participant, namely using the “classe” variable in the training set.
# install.packages("caret")
# install.packages("(randomForest")
library(caret)
## Warning: package 'caret' was built under R version 3.1.3
## Loading required package: lattice
## Loading required package: ggplot2
library(randomForest)
## Warning: package 'randomForest' was built under R version 3.1.3
## randomForest 4.6-12
## Type rfNews() to see new features/changes/bug fixes.
set.seed(1)
urlTraining <- "http://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv"
urlTesting <- "http://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv"
training <- read.csv(url(urlTraining), na.strings=c("NA","#DIV/0!",""))
testing <- read.csv(url(urlTesting), na.strings=c("NA","#DIV/0!",""))
Clean the Data: remove near zero value, NA entries and any other unused data
nzv <- nearZeroVar(training)
training <- training[, -nzv]
testing <- testing[, -nzv]
training <- training[, colSums(is.na(testing)) == 0]
testing <- testing[, colSums(is.na(testing)) == 0]
raw_classe <- training$classe
unnecessary <- grepl("^X|timestamp|window", names(training))
training <- training[, !unnecessary]
training <- training[, sapply(training, is.numeric)]
unnecessary <- grepl("^X|timestamp|window", names(testing))
testing <- testing[, !unnecessary]
testing <- testing[, sapply(testing, is.numeric)]
training$classe <- raw_classe
Partition the Data: get validation data
partition <- createDataPartition(training$classe, p=0.8, list=FALSE)
trainingData <- training[partition,]
testData <- training[-partition,]
Fit the data (using random forest, 3-fold cross validation)
fit <- train(classe ~ ., data=trainingData, method="rf",
trControl=trainControl(method="cv", number=3, verboseIter=F))
Get the final model
model <- fit$finalModel
Show model details
model
##
## 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: 2
##
## OOB estimate of error rate: 0.54%
## Confusion matrix:
## A B C D E class.error
## A 4462 1 0 0 1 0.0004480287
## B 10 3024 4 0 0 0.0046082949
## C 0 19 2716 3 0 0.0080350621
## D 0 0 41 2531 1 0.0163233579
## E 0 0 0 4 2882 0.0013860014
Per the above model details, the out of sample error estimate is 0.54%
Validate using the partitioned test data
confusionMatrix(testData$classe, predict(fit, testData))
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1115 1 0 0 0
## B 4 754 1 0 0
## C 0 4 680 0 0
## D 0 0 10 631 2
## E 0 0 0 0 721
##
## Overall Statistics
##
## Accuracy : 0.9944
## 95% CI : (0.9915, 0.9965)
## No Information Rate : 0.2852
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.9929
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.9964 0.9934 0.9841 1.0000 0.9972
## Specificity 0.9996 0.9984 0.9988 0.9964 1.0000
## Pos Pred Value 0.9991 0.9934 0.9942 0.9813 1.0000
## Neg Pred Value 0.9986 0.9984 0.9966 1.0000 0.9994
## Prevalence 0.2852 0.1935 0.1761 0.1608 0.1843
## Detection Rate 0.2842 0.1922 0.1733 0.1608 0.1838
## Detection Prevalence 0.2845 0.1935 0.1744 0.1639 0.1838
## Balanced Accuracy 0.9980 0.9959 0.9914 0.9982 0.9986
Per the above matrix, the accuracy estimate is 99.4%
Since the estimated error and accuracy are respectively low and high, let’s go ahead and use the above model to predict the exercise “classe”
prediction <- predict(fit, newdata=testing)
Predicted Values: for 20 different cases
results = "Results: "
for(i in 1:20){
results <- paste(results, prediction[[i]], sep=" ")
}
results
## [1] "Results: B A B A A E D B A A B C B A E E A B B B"