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
The data for this project come from this source: http://groupware.les.inf.puc-rio.br/har. If you use the document you create for this class for any purpose please cite them as they have been very generous in allowing their data to be used for this kind of assignment.
library(caret)
## Warning: package 'caret' was built under R version 3.4.1
## Loading required package: lattice
## Loading required package: ggplot2
library(randomForest)
## Warning: package 'randomForest' was built under R version 3.4.1
## 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
Load the training dataset and the testing dataset
dt_training <- read.csv("pml-training.csv")
dt_testing <- read.csv("pml-testing.csv")
Remove the first 7 features (because they are related to the time-series or are not numeric). Also, remove all columns containing NA and remove features that are not in the testing dataset.
features <- names(dt_testing[,colSums(is.na(dt_testing)) == 0])[8:59]
dt_training <- dt_training[,c(features,"classe")]
dt_testing <- dt_testing[,c(features,"problem_id")]
print(dim(dt_training))
## [1] 19622 53
print(dim(dt_testing))
## [1] 20 53
Split our data into a training data set (60% total cases) and a testing data set (40% total cases)
set.seed(1219)
inTrain <- createDataPartition(dt_training$classe, p=0.6, list=FALSE)
training <- dt_training[inTrain,]
testing <- dt_training[-inTrain,]
print(dim(training))
## [1] 11776 53
print(dim(testing))
## [1] 7846 53
Build random forest classifier and use 10-folds cross-validation
set.seed(1219)
control <- trainControl(method = "cv", number = 10)
model <- train(classe ~ ., data = training, method = "rf", trControl = control)
print(model)
## Random Forest
##
## 11776 samples
## 52 predictor
## 5 classes: 'A', 'B', 'C', 'D', 'E'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 10598, 10598, 10599, 10598, 10599, 10599, ...
## Resampling results across tuning parameters:
##
## mtry Accuracy Kappa
## 2 0.9905742 0.9880761
## 27 0.9902341 0.9876455
## 52 0.9830161 0.9785112
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
prediction=predict(model, newdata=testing)
confusionMatrix(prediction, testing$classe)
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 2232 9 0 0 0
## B 0 1504 7 0 0
## C 0 5 1360 20 3
## D 0 0 1 1264 3
## E 0 0 0 2 1436
##
## Overall Statistics
##
## Accuracy : 0.9936
## 95% CI : (0.9916, 0.9953)
## No Information Rate : 0.2845
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.9919
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 1.0000 0.9908 0.9942 0.9829 0.9958
## Specificity 0.9984 0.9989 0.9957 0.9994 0.9997
## Pos Pred Value 0.9960 0.9954 0.9798 0.9968 0.9986
## Neg Pred Value 1.0000 0.9978 0.9988 0.9967 0.9991
## Prevalence 0.2845 0.1935 0.1744 0.1639 0.1838
## Detection Rate 0.2845 0.1917 0.1733 0.1611 0.1830
## Detection Prevalence 0.2856 0.1926 0.1769 0.1616 0.1833
## Balanced Accuracy 0.9992 0.9948 0.9949 0.9911 0.9978
predictionRF <- predict(model, dt_testing)
predictionRF;
## [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