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.
The goal of your project is to predict the manner in which they did the exercise. This is the “classe” variable in the training set. You may use any of the other variables to predict with. You should create a report describing how you built your model, how you used cross validation, what you think the expected out of sample error is, and why you made the choices you did. You will also use your prediction model to predict 20 different test cases.
The following summarizes the steps used in the analysis of the data, from reading in the data from the supplied files to the final predication based on the test file.
if(!require(ggplot2))
{install.packages("ggplot2");
library(ggplot2);
}
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.3.3
if(!require(caret))
{install.packages("caret");
library(caret);
}
## Loading required package: caret
## Warning: package 'caret' was built under R version 3.3.3
## Loading required package: lattice
## Warning: package 'lattice' was built under R version 3.3.2
if(!require(randomForest))
{install.packages("randomForest");
library(randomForest);
}
## Loading required package: randomForest
## Warning: package 'randomForest' was built under R version 3.3.3
## 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
if(!require(rpart))
{install.packages("rpart");
library(rpart);
};
## Loading required package: rpart
if(!require(rpart.plot))
{install.packages("rpart.plot");
library(rpart.plot);
}
## Loading required package: rpart.plot
## Warning: package 'rpart.plot' was built under R version 3.3.3
if(!require(rattle))
{install.packages("rattle");
library(rattle);
}
## Loading required package: rattle
## Warning: package 'rattle' was built under R version 3.3.3
## 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.
Train<-read.csv("pml-training.csv", na.strings=c("NA", "#DIV/0!", ""))
Test<-read.csv("pml-testing.csv", na.strings=c("NA", "#DIV/0!", ""))
dim(Train)
## [1] 19622 160
dim(Test)
## [1] 20 160
StartVar <- grep("name|timestamp|window|X", colnames(Train), value=F)
Train<-Train[,-StartVar]
Train<-Train[, colSums(is.na(Train))==0]
We will want the Test file to match the Training file so we remove the “problem_id”" column and replace it with a “classe”" column with a 5 level factor. We also clean this data frame in the same manner as the training data frame.
Test<-Test[,-160]
Test["classe"]<-c("A", "B", "C","D","E")
Test$classe<-factor(Test$classe)
Test<-Test[,-StartVar]
Test<-Test[, colSums(is.na(Test))==0]
dim(Test)
## [1] 20 53
We split the Train data frame into a training set and a test (validation) set
inTrain<-createDataPartition(y=Train$classe, p=0.75, list=FALSE)
TrainTrain<-Train[inTrain,]
TrainTest<- Train[-inTrain, ]
dim(TrainTrain)
## [1] 14718 53
dim(TrainTest)
## [1] 4904 53
plot(TrainTrain$classe, col="yellow", main="Plot of levels of classe variable", xlab="classe", ylab="Frequency")
## Machine learning using decision trees ##
model_dt<-rpart(classe~., data=TrainTrain, method="class")
predict_dt<-predict(model_dt, TrainTest, type="class")
confusionMatrix(predict_dt, TrainTest$classe)
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1264 205 27 94 64
## B 34 567 83 30 63
## C 33 77 664 123 88
## D 46 57 53 493 53
## E 18 43 28 64 633
##
## Overall Statistics
##
## Accuracy : 0.7384
## 95% CI : (0.7258, 0.7506)
## No Information Rate : 0.2845
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.6669
## Mcnemar's Test P-Value : < 2.2e-16
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.9061 0.5975 0.7766 0.6132 0.7026
## Specificity 0.8889 0.9469 0.9207 0.9490 0.9618
## Pos Pred Value 0.7642 0.7297 0.6741 0.7023 0.8053
## Neg Pred Value 0.9597 0.9074 0.9513 0.9260 0.9349
## Prevalence 0.2845 0.1935 0.1743 0.1639 0.1837
## Detection Rate 0.2577 0.1156 0.1354 0.1005 0.1291
## Detection Prevalence 0.3373 0.1584 0.2009 0.1431 0.1603
## Balanced Accuracy 0.8975 0.7722 0.8487 0.7811 0.8322
#fancyRpartPlot(model_dt)
rpart.plot(model_dt)
As we can see, we achieve an accuracy of 76% with this method
model_rf<-randomForest(classe~., data=TrainTrain, method="class")
predict_rf<-predict(model_rf, TrainTest, type="class")
confusionMatrix(predict_rf, TrainTest$classe)
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1392 0 0 0 0
## B 1 948 8 0 0
## C 2 1 846 8 0
## D 0 0 1 796 0
## E 0 0 0 0 901
##
## Overall Statistics
##
## Accuracy : 0.9957
## 95% CI : (0.9935, 0.9973)
## No Information Rate : 0.2845
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.9946
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.9978 0.9989 0.9895 0.9900 1.0000
## Specificity 1.0000 0.9977 0.9973 0.9998 1.0000
## Pos Pred Value 1.0000 0.9906 0.9872 0.9987 1.0000
## Neg Pred Value 0.9991 0.9997 0.9978 0.9981 1.0000
## Prevalence 0.2845 0.1935 0.1743 0.1639 0.1837
## Detection Rate 0.2838 0.1933 0.1725 0.1623 0.1837
## Detection Prevalence 0.2838 0.1951 0.1748 0.1625 0.1837
## Balanced Accuracy 0.9989 0.9983 0.9934 0.9949 1.0000
We achieve an accuracy of over 99% using random forests. We therefore choose this model for the final prediction.
model_final<-randomForest(classe~., data=TrainTrain, method="class")
predict_final<-predict(model_final, Test, type="class")
predict_final
## 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
In this analysis of data relating to weight lifting exercises, the training data set of 19622 observations was divided into a set of 14718 observations used for training, and a set of 4904 observations that were used for internal validation of the training process. The training and prediction was based on a factor variable “classe” which classified performance on barbell lifts. It was required to predict on the basis of the machine learning algorithm chosen to predict the classifications based on 20 obervations in a “test” file.
Two machine learnning algorithms were tested, chosen to predict the required factor variable. These were decision trees and random forest. The decision tree method (rpart) was found to have an accuracy (assessed using the “confusionMatrix”) of just 76%, while the random forest model had an accuracy of over 99%. Thus the random forest method was used to find the predictions based on the test data.
This reported the following.
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