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. The aim of this project is to predict the manner in which participants perform a barbell lift. The data comes from http://groupware.les.inf.puc-rio.br/har wherein 6 participants were asked to perform the same set of exercises correctly and incorrectly with accelerometers placed on the belt, forearm, arm, and dumbell.
For the purpose of this project, the following steps would be followed:
First, we load the training and testing set from the online sources and then split the training set further into training and test sets.
library(caret)
setwd("C:/Users/nchimbwero/Documents/New folder (2)/Practice")
trainURL <- "http://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv"
testURL <- "http://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv"
training <- read.csv(url(trainURL))
testing <- read.csv(url(testURL))
label <- createDataPartition(training$classe, p = 0.7, list = FALSE)
train <- training[label, ]
test <- training[-label, ]
From among 160 variables present in the dataset, some variables have nearly zero variance whereas some contain a lot of NA terms which need to be excluded from the dataset. Moreover, other 5 variables used for identification can also be removed.
NZV <- nearZeroVar(train)
train <- train[ ,-NZV]
test <- test[ ,-NZV]
label <- apply(train, 2, function(x) mean(is.na(x))) > 0.95
train <- train[, -which(label, label == FALSE)]
test <- test[, -which(label, label == FALSE)]
train <- train[ , -(1:5)]
test <- test[ , -(1:5)]
As a result of the preprocessing steps, we were able to reduce 160 variables to 54.
Now that we have cleaned the dataset off absolutely useless varibles, we shall look at the dependence of these variables on each other through a correlation plot.
library(corrplot)
## corrplot 0.92 loaded
corrMat <- cor(train[,-54])
corrplot(corrMat, method = "color", type = "lower", tl.cex = 0.8, tl.col = rgb(0,0,0))
In the plot above, darker gradient correspond to having high correlation. A Principal Component Analysis can be run to further reduce the correlated variables but we aren’t doing that due to the number of correlations being quite few.
We will use 3 methods to model the training set and thereby choose the one having the best accuracy to predict the outcome variable in the testing set. The methods are Decision Tree, Random Forest and Generalized Boosted Model.
A confusion matrix plotted at the end of each model will help visualize the analysis better.
library(rpart)
library(rpart.plot)
library(rattle)
set.seed(13908)
modelDT <- rpart(classe ~ ., data = train, method = "class")
fancyRpartPlot(modelDT)
predictDT <- predict(modelDT, test, type = "class")
confMatDT <- confusionMatrix(predictDT,as.factor(test$classe))
confMatDT
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1492 209 19 72 38
## B 122 726 51 83 47
## C 9 64 816 133 73
## D 29 83 83 565 130
## E 22 57 57 111 794
##
## Overall Statistics
##
## Accuracy : 0.7465
## 95% CI : (0.7352, 0.7575)
## No Information Rate : 0.2845
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.6783
##
## Mcnemar's Test P-Value : 2.106e-10
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.8913 0.6374 0.7953 0.58610 0.7338
## Specificity 0.9197 0.9362 0.9426 0.93396 0.9486
## Pos Pred Value 0.8153 0.7055 0.7452 0.63483 0.7627
## Neg Pred Value 0.9551 0.9150 0.9562 0.92012 0.9405
## Prevalence 0.2845 0.1935 0.1743 0.16381 0.1839
## Detection Rate 0.2535 0.1234 0.1387 0.09601 0.1349
## Detection Prevalence 0.3110 0.1749 0.1861 0.15123 0.1769
## Balanced Accuracy 0.9055 0.7868 0.8690 0.76003 0.8412
library(caret)
set.seed(13908)
control <- trainControl(method = "cv", number = 3, verboseIter=FALSE)
modelRF <- train(classe ~ ., data = train, method = "rf", trControl = control)
modelRF$finalModel
##
## 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: 27
##
## OOB estimate of error rate: 0.24%
## Confusion matrix:
## A B C D E class.error
## A 3905 0 0 0 1 0.0002560164
## B 5 2649 4 0 0 0.0033860045
## C 0 5 2391 0 0 0.0020868114
## D 0 0 10 2241 1 0.0048845471
## E 0 1 0 6 2518 0.0027722772
predictRF <- predict(modelRF, test)
confMatRF <- confusionMatrix(predictRF,as.factor(test$classe))
confMatRF
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1673 3 0 0 0
## B 1 1136 2 0 2
## C 0 0 1024 5 0
## D 0 0 0 959 4
## E 0 0 0 0 1076
##
## Overall Statistics
##
## Accuracy : 0.9971
## 95% CI : (0.9954, 0.9983)
## No Information Rate : 0.2845
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.9963
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.9994 0.9974 0.9981 0.9948 0.9945
## Specificity 0.9993 0.9989 0.9990 0.9992 1.0000
## Pos Pred Value 0.9982 0.9956 0.9951 0.9958 1.0000
## Neg Pred Value 0.9998 0.9994 0.9996 0.9990 0.9988
## Prevalence 0.2845 0.1935 0.1743 0.1638 0.1839
## Detection Rate 0.2843 0.1930 0.1740 0.1630 0.1828
## Detection Prevalence 0.2848 0.1939 0.1749 0.1636 0.1828
## Balanced Accuracy 0.9993 0.9982 0.9985 0.9970 0.9972
library(caret)
set.seed(13908)
control <- trainControl(method = "repeatedcv", number = 5, repeats = 1, verboseIter = FALSE)
modelGBM <- train(classe ~ ., data = train, trControl = control, method = "gbm", verbose = FALSE)
modelGBM$finalModel
## A gradient boosted model with multinomial loss function.
## 150 iterations were performed.
## There were 53 predictors of which 53 had non-zero influence.
predictGBM <- predict(modelGBM, test)
confMatGBM <- confusionMatrix(predictGBM,as.factor(test$classe))
confMatGBM
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1667 15 0 0 0
## B 7 1110 7 3 3
## C 0 14 1015 11 3
## D 0 0 4 950 11
## E 0 0 0 0 1065
##
## Overall Statistics
##
## Accuracy : 0.9867
## 95% CI : (0.9835, 0.9895)
## No Information Rate : 0.2845
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.9832
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.9958 0.9745 0.9893 0.9855 0.9843
## Specificity 0.9964 0.9958 0.9942 0.9970 1.0000
## Pos Pred Value 0.9911 0.9823 0.9732 0.9845 1.0000
## Neg Pred Value 0.9983 0.9939 0.9977 0.9972 0.9965
## Prevalence 0.2845 0.1935 0.1743 0.1638 0.1839
## Detection Rate 0.2833 0.1886 0.1725 0.1614 0.1810
## Detection Prevalence 0.2858 0.1920 0.1772 0.1640 0.1810
## Balanced Accuracy 0.9961 0.9852 0.9918 0.9912 0.9921
As Random Forest offers the maximum accuracy of 99.75%, we will go with Random Forest Model to predict our test data class variable.
predictRF <- predict(modelRF, testing)
predictRF
## [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