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 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
## Loading required package: lattice
## Loading required package: ggplot2
## dummies-1.5.6 provided by Decision Patterns
## Rattle: A free graphical interface for data mining with R.
## Version 4.1.0 Copyright (c) 2006-2015 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
if(!file.exists("training.csv")) {
download.file('https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv', destfile = "training.csv")
}
if(!file.exists("testing.csv")) {
download.file('https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv', destfile = "testing.csv")
}
We here use the training data set to create an additional validaiton data set. We want to predict the “classe” variable.
raw_training <- read.csv("training.csv", na.strings=c("NA","#DIV/0!",""))
raw_testing <- read.csv("testing.csv", na.strings=c("NA","#DIV/0!",""))
inValidation <- createDataPartition(raw_training$classe, p = 0.2)[[1]]
validation <- raw_training[inValidation,]
training <- raw_training[-inValidation,]
testing <- raw_testing
Removing all the variables with NANs and other irrelevant variables:
# in this simple approach we remove all columns with at least one NAN
idcNotNACol <- apply(training, 2, function(x) sum(is.na(x)) == 0 )
validation <- validation[,idcNotNACol]
training <- training[,idcNotNACol]
testing <- testing[,idcNotNACol]
idcNotNeededCol <- c(1:5)
validation <- validation[,-idcNotNeededCol]
training <- training[,-idcNotNeededCol]
testing <- testing[,-idcNotNeededCol]
set.seed(111222)
mod.dt <- rpart(classe ~ ., data = training, method = "class")
pred.mod.dt <- predict(mod.dt, validation, type = "class")
cm.mod.dt <- confusionMatrix(pred.mod.dt, validation$classe)
cm.mod.dt
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 979 71 1 11 3
## B 71 598 38 34 48
## C 0 34 572 21 2
## D 58 38 65 545 82
## E 8 19 9 33 587
##
## Overall Statistics
##
## Accuracy : 0.8355
## 95% CI : (0.8235, 0.847)
## No Information Rate : 0.2842
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.7924
## Mcnemar's Test P-Value : 3.236e-16
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.8772 0.7868 0.8350 0.8463 0.8130
## Specificity 0.9694 0.9397 0.9824 0.9260 0.9785
## Pos Pred Value 0.9192 0.7579 0.9094 0.6916 0.8948
## Neg Pred Value 0.9521 0.9484 0.9657 0.9685 0.9587
## Prevalence 0.2842 0.1935 0.1744 0.1640 0.1839
## Detection Rate 0.2493 0.1523 0.1457 0.1388 0.1495
## Detection Prevalence 0.2712 0.2009 0.1602 0.2007 0.1670
## Balanced Accuracy 0.9233 0.8633 0.9087 0.8861 0.8957
plot(cm.mod.dt$table, main = paste("Decision Tree - Accuracy: ", round(cm.mod.dt$overall[[1]], 3)))
set.seed(111222)
if(!file.exists("mod.rf.RData")) {
mod.rf <- train(classe ~ ., data = training, method = "rf")
save(mod.rf, file = "mod.rf.RData")
} else {
load(file = "mod.rf.RData")
}
pred.mod.rf <- predict(mod.rf, validation)
## Loading required package: randomForest
## randomForest 4.6-12
## Type rfNews() to see new features/changes/bug fixes.
##
## Attaching package: 'randomForest'
## The following object is masked from 'package:ggplot2':
##
## margin
cm.mod.rf <- confusionMatrix(pred.mod.rf, validation$classe)
cm.mod.rf
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1116 0 0 0 0
## B 0 760 0 0 0
## C 0 0 685 0 0
## D 0 0 0 644 0
## E 0 0 0 0 722
##
## Overall Statistics
##
## Accuracy : 1
## 95% CI : (0.9991, 1)
## No Information Rate : 0.2842
## 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.000 1.0000
## Specificity 1.0000 1.0000 1.0000 1.000 1.0000
## Pos Pred Value 1.0000 1.0000 1.0000 1.000 1.0000
## Neg Pred Value 1.0000 1.0000 1.0000 1.000 1.0000
## Prevalence 0.2842 0.1935 0.1744 0.164 0.1839
## Detection Rate 0.2842 0.1935 0.1744 0.164 0.1839
## Detection Prevalence 0.2842 0.1935 0.1744 0.164 0.1839
## Balanced Accuracy 1.0000 1.0000 1.0000 1.000 1.0000
plot(cm.mod.rf$table, main = paste("Random Forest - Accuracy: ", round(cm.mod.rf$overall[[1]], 3)))
For the final prediction we utilize the random forest model due to its superior performance
pred.mod.rf.testing <- predict(mod.rf, testing)
pred.mod.rf.testing
## [1] B A B A A E D B A A B C B A E E A B B B
## Levels: A B C D E