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.
#Loading libraries
library(caret)
library(rpart)
library(rpart.plot)
library(randomForest)
#Loading the training data set
TRAINING <- read.csv("pml-training.csv", na.strings=c("NA","#DIV/0!", ""))
#Loading the testing data set
TESTING <- read.csv("pml-testing.csv", na.strings=c("NA","#DIV/0!", ""))
#Deleting missing values in training and testing data set
TRAINING <- TRAINING[,colSums(is.na(TRAINING)) == 0]
TESTING <- TESTING[,colSums(is.na(TESTING)) == 0]
#Count per class
table(TRAINING$classe)
##
## A B C D E
## 5580 3797 3422 3216 3607
#Partitioning the training set
TR <- createDataPartition(TRAINING$classe,p=.9,list=FALSE)
TRset <- TRAINING [TR,]
TRVal <- TRAINING [-TR,]
#Removing of non-numeric variables and near-zero values
TRset <- TRset [,-(1:7)]
zero <- ncol (TRset)
TRset [,-zero] <- data.frame (sapply(TRset[,-zero],as.numeric))
zd <- nearZeroVar(TRset[,-zero],saveMetrics=TRUE)
TRset <- TRset [,!as.logical(zd$nzv)]
#Count per class with non-numeric variables and near-zero values
table(TRset$classe)
##
## A B C D E
## 5022 3418 3080 2895 3247
#Prediction with Regression model
#First prediction model based on RPART method
mdOne <- rpart (classe ~., data=TRset, method="class")
prdOne <- predict (mdOne,TRVal,type="class")
#Plot to present the classication tree
rpart.plot(mdOne, main="Classification", extra=102, under=TRUE, faclen=0)
#Confusion Matrix
print(confusionMatrix(prdOne,TRVal$classe))
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 510 57 7 20 15
## B 19 246 28 26 44
## C 13 35 275 57 33
## D 8 25 19 201 11
## E 8 16 13 17 257
##
## Overall Statistics
##
## Accuracy : 0.7597
## 95% CI : (0.7401, 0.7785)
## No Information Rate : 0.2847
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.6949
## Mcnemar's Test P-Value : 2.946e-11
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.9140 0.6491 0.8041 0.6262 0.7139
## Specificity 0.9294 0.9260 0.9147 0.9616 0.9663
## Pos Pred Value 0.8374 0.6777 0.6659 0.7614 0.8264
## Neg Pred Value 0.9645 0.9167 0.9567 0.9292 0.9375
## Prevalence 0.2847 0.1934 0.1745 0.1638 0.1837
## Detection Rate 0.2602 0.1255 0.1403 0.1026 0.1311
## Detection Prevalence 0.3107 0.1852 0.2107 0.1347 0.1587
## Balanced Accuracy 0.9217 0.7875 0.8594 0.7939 0.8401
#Second prediction model based on Random Forest method
mdTwo <- randomForest(classe ~. , data=TRset, method="class")
#Plot 1 for Random Forest
varImpPlot(mdTwo)
#Plot 2 for Random Forest
plot(mdTwo,log="y")
## Warning in xy.coords(x, y, xlabel, ylabel, log = log): 1 y value <= 0
## omitted from logarithmic plot
#Confusion Matrix
print(confusionMatrix(predict(mdTwo,TRset),TRset$classe))
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 5022 0 0 0 0
## B 0 3418 0 0 0
## C 0 0 3080 0 0
## D 0 0 0 2895 0
## E 0 0 0 0 3247
##
## Overall Statistics
##
## Accuracy : 1
## 95% CI : (0.9998, 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
#As we can see the accuracy with Random Forest is very higher and significative.
#Let's use the Random Forest algorithm for the final classification
#Final Test Set Classification
lastPred <- predict(mdTwo,TESTING,type="class")
print(lastPred)
## 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
#Submission WriteUp Project
# Write files for submission
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)
}
}
pml_write_files(lastPred)
#End