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://web.archive.org/web/20161224072740/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 dataset is available at http://web.archive.org/web/20161224072740/http:/groupware.les.inf.puc-rio.br/har and it was published with the title “Qualitative activity recognition of weight lifting exercises” (E. Velloso, A. Bulling, H. Gellersen, W. Ugulino, and H. Fuks - Proceedings of the 4th Augmented Human International Conference, 2013, 116–123).
Loading of the useful libraries
library(caret)
library(lattice)
library(ggplot2)
library(randomForest)
library(rpart)
library(rpart.plot)
set.seed(123)
Downloading the dataset (if not already locally present).
trainURL = "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv"
testURL = "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv"
# download the datasets (results are cached)
train <- read.csv(url(trainURL), na.strings=c("NA","#DIV/0!", ""))
test <- read.csv(url(testURL), na.strings=c("NA","#DIV/0!", ""))
Check the dimensions of the dataset
dim(train)
## [1] 19622 160
dim(test)
## [1] 20 160
First we remove the first 7 columns, as not subject of this study. Then we remove the columns with NAs (i.e. missing, null or not present values).
#Keep only columns in which the sum of NAs is null
train <- train[,colSums(is.na(train)) == 0]
test <- test[,colSums(is.na(test)) == 0]
#Remove the first 7 columns
train <- train[,-c(1:7)]
test <- test[,-c(1:7)]
Check that the new dimensions are compatible with the performed operations
dim(train)
## [1] 19622 53
dim(test)
## [1] 20 53
We first plot the variable “classe” in order to have an idea of how the data is partitioned in the training set.
plot(train$classe)
First we partition the training dataset into 70% training and 30% testing (used to validate the model and assess its performances).
subset <- createDataPartition(y = train$classe, p=0.70, list=FALSE)
train_train_subset <- train[subset, ]
train_test_subset <- train[-subset, ]
We select a random forest model for the classification as it combines the advantages of decision trees in a more robust algorithm, almost neglecting the overfitting aptitude of decision trees. This allows us to let the algorithm select the relevant parameters on which performing the classification. The performance is tested by classifying the rest of the training set and then the algorithm is applied to the test dataset.
rf_model <- randomForest(classe ~. , data=train_train_subset, method="class")
The so-created rf_model is used to classify the test subset of the training set.
validation <- predict(rf_model, train_test_subset, type = "class")
And, for validation purposes, we plot the confusion matrix and verify that the accuracy (99.4%) and confidence intervals are compatible with our needs. To further improve the model, one can decide to increase the training set to 0.8 when calling createDataPartition, leading to an accuracy of 99.6 %.
confusionMatrix(validation, train_test_subset$classe)
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1673 6 0 0 0
## B 1 1132 12 0 0
## C 0 1 1014 13 0
## D 0 0 0 950 0
## E 0 0 0 1 1082
##
## Overall Statistics
##
## Accuracy : 0.9942
## 95% CI : (0.9919, 0.996)
## No Information Rate : 0.2845
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.9927
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.9994 0.9939 0.9883 0.9855 1.0000
## Specificity 0.9986 0.9973 0.9971 1.0000 0.9998
## Pos Pred Value 0.9964 0.9886 0.9864 1.0000 0.9991
## Neg Pred Value 0.9998 0.9985 0.9975 0.9972 1.0000
## Prevalence 0.2845 0.1935 0.1743 0.1638 0.1839
## Detection Rate 0.2843 0.1924 0.1723 0.1614 0.1839
## Detection Prevalence 0.2853 0.1946 0.1747 0.1614 0.1840
## Balanced Accuracy 0.9990 0.9956 0.9927 0.9927 0.9999
The trained model is then applied to the classification of the real test subset.
prediction <- predict(rf_model, test, type="class")
# Plot the results
prediction
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
## 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