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 data for this project come from this source: http://groupware.les.inf.puc-rio.br/har. Their data to be used for this kind of assignment.
The training & test data sets are downloaded directly to avoide any dependentcies on file paths. All miscellaneous NA, #DIV/0! and empty fields are read in as NA.
pmlTrainOriginal <- read.csv("https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv", na.strings=c("NA", "#DIV/0!", "") )
pmlTestOriginal <- read.csv("https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv", na.strings=c("NA", "#DIV/0!", "") )
Load requried libraries. Also set the seed so that we will consitant results.
library(Hmisc); library(caret); library(randomForest); library(foreach); library(doParallel)
set.seed(1234)
Undestand the data we are dealing with.
str(pmlTrainOriginal); summary(pmlTrainOriginal)
Clean up Data. First, remove unnessary coloumns (First 6 columns). Then, use ‘apply(
pmlTrain <-pmlTrainOriginal[, 7:160]
pmlTest <- pmlTestOriginal[, 7:160]
keepCol<- apply(!is.na(pmlTrain), 2, sum) > 19621 #Sum of not NAs need to be 19622. Added the > sign incase new records are added in the original dataset.
pmlTrain <- pmlTrain[, keepCol]
pmlTest <- pmlTest[, keepCol]
dim(pmlTrain)
## [1] 19622 54
Now we need to create Data Partition to evaluate our models. Lets take 70% for training.
inTrain <- createDataPartition(y=pmlTrain$classe, p=0.7, list=FALSE )
training <- pmlTrain[inTrain,]
testing <- pmlTrain[-inTrain,]
correlation <- caret::findCorrelation(cor(training[, 1:52]), cutoff=0.80);
# Variables which are correlated
names(training)[correlation]
## [1] "accel_belt_z" "roll_belt" "accel_belt_y"
## [4] "accel_dumbbell_z" "accel_belt_x" "pitch_belt"
## [7] "accel_arm_x" "accel_dumbbell_x" "magnet_arm_y"
## [10] "gyros_forearm_y" "gyros_dumbbell_x" "gyros_dumbbell_z"
## [13] "gyros_arm_x"
# Sample plot
qplot(roll_belt, pitch_belt, colour=classe, data=training)
library(doParallel)
x <- training[-ncol(training)]
y <- training$classe
library(foreach); library(randomForest);
rf <- foreach(ntree=rep(150, 6), .combine=randomForest::combine, .packages='randomForest') %dopar% {
randomForest(x, y, ntree=ntree)
}
## Warning: executing %dopar% sequentially: no parallel backend registered
#install.packages("e1071")
predPMLtrain <- predict(rf, newdata=training)
confusionMatrix(predPMLtrain,training$classe)
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 3906 0 0 0 0
## B 0 2658 0 0 0
## C 0 0 2396 0 0
## D 0 0 0 2252 0
## E 0 0 0 0 2525
##
## Overall Statistics
##
## Accuracy : 1
## 95% CI : (0.9997, 1)
## No Information Rate : 0.2843
## 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.0000 1.0000
## Specificity 1.0000 1.0000 1.0000 1.0000 1.0000
## Pos Pred Value 1.0000 1.0000 1.0000 1.0000 1.0000
## Neg Pred Value 1.0000 1.0000 1.0000 1.0000 1.0000
## Prevalence 0.2843 0.1935 0.1744 0.1639 0.1838
## Detection Rate 0.2843 0.1935 0.1744 0.1639 0.1838
## Detection Prevalence 0.2843 0.1935 0.1744 0.1639 0.1838
## Balanced Accuracy 1.0000 1.0000 1.0000 1.0000 1.0000
predPMLtest <- predict(rf, newdata=testing)
confusionMatrix(predPMLtest,testing$classe)
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1674 3 0 0 0
## B 0 1135 4 0 0
## C 0 1 1022 4 0
## D 0 0 0 960 0
## E 0 0 0 0 1082
##
## Overall Statistics
##
## Accuracy : 0.998
## 95% CI : (0.9964, 0.9989)
## No Information Rate : 0.2845
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.9974
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 1.0000 0.9965 0.9961 0.9959 1.0000
## Specificity 0.9993 0.9992 0.9990 1.0000 1.0000
## Pos Pred Value 0.9982 0.9965 0.9951 1.0000 1.0000
## Neg Pred Value 1.0000 0.9992 0.9992 0.9992 1.0000
## Prevalence 0.2845 0.1935 0.1743 0.1638 0.1839
## Detection Rate 0.2845 0.1929 0.1737 0.1631 0.1839
## Detection Prevalence 0.2850 0.1935 0.1745 0.1631 0.1839
## Balanced Accuracy 0.9996 0.9978 0.9975 0.9979 1.0000
I have developed PCA ande few other models such as svmRadial (Suppoart Vector Machine - Radial) and LogitBoost (Logit Boosted Model) using the caTools package. However, the Accuracy of Random Forest was lot higher in trainning & testing sets compared to all other models.
pml_write_files = function(x){
n = length(x)
for(i in 1:n){
filename = paste0("problem_id_",i,".txt")
write.table(x[i],file=filename,quote=FALSE,row.names=FALSE,col.names=FALSE)
}
}
rfValid <- predict(rf, pmlTest); # Random Forest
pml_write_files(rfValid)
Confusion matrix clearly shows that our model is damn good :). It feels as too good to be true. Possibly the random model is overfitting. Following screenshot shows that ou model worked perfecto. 20/20!
library(png)
img <- readPNG("C:/Users/San/Dropbox/Coursera/PracticalMachineLearning/Project/SubmissionResults.png")
plot(0, type='n', xlim=0:1, ylim=0:1, main="Submission Results (Not the best use, but this gives the idea)")
rasterImage(img, 0, 0, 1, 1)