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://web.archive.org/web/20161224072740/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.
library(knitr)
## Warning: package 'knitr' was built under R version 4.1.2
library(caret)
## Warning: package 'caret' was built under R version 4.1.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.1.2
## Loading required package: lattice
## Warning: package 'lattice' was built under R version 4.1.2
library(rpart)
library(rpart.plot)
## Warning: package 'rpart.plot' was built under R version 4.1.2
library(rattle)
## Warning: package 'rattle' was built under R version 4.1.2
## Loading required package: tibble
## Warning: package 'tibble' was built under R version 4.1.2
## Loading required package: bitops
## Rattle: A free graphical interface for data science with R.
## Version 5.4.0 Copyright (c) 2006-2020 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
library(randomForest)
## Warning: package 'randomForest' was built under R version 4.1.2
## randomForest 4.6-14
## Type rfNews() to see new features/changes/bug fixes.
##
## Attaching package: 'randomForest'
## The following object is masked from 'package:rattle':
##
## importance
## The following object is masked from 'package:ggplot2':
##
## margin
library(corrplot)
## Warning: package 'corrplot' was built under R version 4.1.2
## corrplot 0.92 loaded
set.seed(1234)
traincsv <- read.csv("C:/Users/ef_al/OneDrive/Desktop/Coursera/Data Science Course/Practical Machine Learning/pml-training.csv")
testcsv <- read.csv("C:/Users/ef_al/OneDrive/Desktop/Coursera/Data Science Course/Practical Machine Learning/pml-testing.csv")
dim(traincsv)
## [1] 19622 160
dim(testcsv)
## [1] 20 160
traincsv <- traincsv[,colMeans(is.na(traincsv)) < .9]
traincsv <- traincsv[,-c(1:7)]
nvz <- nearZeroVar(traincsv)
traincsv <- traincsv[,-nvz]
dim(traincsv)
## [1] 19622 53
inTrain <- createDataPartition(y=traincsv$classe, p=0.7, list=F)
train <- traincsv[inTrain,]
valid <- traincsv[-inTrain,]
control <- trainControl(method="cv", number=3, verboseIter=F)
For this Project, three prediction methods are utilized namely:
mod_rf <- train(classe~., data=train, method="rf", trControl = control, tuneLength = 5)
pred_rf <- predict(mod_rf, valid)
cmrf <- confusionMatrix(pred_rf, factor(valid$classe))
cmrf
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1673 3 0 0 0
## B 1 1132 11 0 0
## C 0 4 1014 6 0
## D 0 0 1 957 0
## E 0 0 0 1 1082
##
## Overall Statistics
##
## Accuracy : 0.9954
## 95% CI : (0.9933, 0.997)
## No Information Rate : 0.2845
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.9942
##
## 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.9927 1.0000
## Specificity 0.9993 0.9975 0.9979 0.9998 0.9998
## Pos Pred Value 0.9982 0.9895 0.9902 0.9990 0.9991
## Neg Pred Value 0.9998 0.9985 0.9975 0.9986 1.0000
## Prevalence 0.2845 0.1935 0.1743 0.1638 0.1839
## Detection Rate 0.2843 0.1924 0.1723 0.1626 0.1839
## Detection Prevalence 0.2848 0.1944 0.1740 0.1628 0.1840
## Balanced Accuracy 0.9993 0.9957 0.9931 0.9963 0.9999
plot(mod_rf)
mod_trees <- train(classe~., data=train, method="rpart", trControl = control, tuneLength = 5)
fancyRpartPlot(mod_trees$finalModel)
pred_trees <- predict(mod_trees, valid)
cmtrees <- confusionMatrix(pred_trees, factor(valid$classe))
cmtrees
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1519 473 484 451 156
## B 28 355 45 10 130
## C 83 117 423 131 131
## D 40 194 74 372 176
## E 4 0 0 0 489
##
## Overall Statistics
##
## Accuracy : 0.5366
## 95% CI : (0.5238, 0.5494)
## No Information Rate : 0.2845
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.3957
##
## Mcnemar's Test P-Value : < 2.2e-16
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.9074 0.31168 0.41228 0.38589 0.45194
## Specificity 0.6286 0.95512 0.90492 0.90165 0.99917
## Pos Pred Value 0.4927 0.62500 0.47797 0.43458 0.99189
## Neg Pred Value 0.9447 0.85255 0.87940 0.88228 0.89002
## Prevalence 0.2845 0.19354 0.17434 0.16381 0.18386
## Detection Rate 0.2581 0.06032 0.07188 0.06321 0.08309
## Detection Prevalence 0.5239 0.09652 0.15038 0.14545 0.08377
## Balanced Accuracy 0.7680 0.63340 0.65860 0.64377 0.72555
mod_gbm <- train(classe~., data=train, method="gbm", trControl = control, tuneLength = 5, verbose = F)
pred_gbm <- predict(mod_gbm, valid)
cmgbm <- confusionMatrix(pred_gbm, factor(valid$classe))
cmgbm
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1670 5 0 0 0
## B 2 1128 13 0 0
## C 2 6 1011 9 3
## D 0 0 2 953 1
## E 0 0 0 2 1078
##
## Overall Statistics
##
## Accuracy : 0.9924
## 95% CI : (0.9898, 0.9944)
## No Information Rate : 0.2845
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.9903
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.9976 0.9903 0.9854 0.9886 0.9963
## Specificity 0.9988 0.9968 0.9959 0.9994 0.9996
## Pos Pred Value 0.9970 0.9869 0.9806 0.9969 0.9981
## Neg Pred Value 0.9990 0.9977 0.9969 0.9978 0.9992
## Prevalence 0.2845 0.1935 0.1743 0.1638 0.1839
## Detection Rate 0.2838 0.1917 0.1718 0.1619 0.1832
## Detection Prevalence 0.2846 0.1942 0.1752 0.1624 0.1835
## Balanced Accuracy 0.9982 0.9936 0.9906 0.9940 0.9979
plot(mod_gbm)
pred <- predict(mod_rf, testcsv)
print(pred)
## [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
corrPlot <- cor(train[, -length(names(train))])
corrplot(corrPlot, method="color")
plot(mod_trees)