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).
This Machine Learning Write UP predicts the achievement of participants exercise, which are represented by categorical variables,classe A, B, C, D, E. Two model choices for training the predictor are applied. The Random Forest confirms high Accuracy which is than used for answering 20 test cases.
install.packages(‘rattle’); install.packages(‘rpart’); install.packages(‘rpart.plot’); install.packages(‘RColorBrewer’); library(rattle); library(rpart.plot); library(RColorBrewer); library(caret) ; library(repmis); library(randomForest); ###Process the Data
testing <- read.csv("pml-testing.csv", na.strings = c("NA", ""))
training <- read.csv("pml-training.csv", na.strings = c("NA", ""))
Summary of the dataset: Dataset training has 1’9622 observations and 160 variables, where the testing dataset has 20 observations with same number of variables as training-dataset.The prediction of outcome will be applied on the training-dataset. ###CLEANING Columns to be deleted within the training set which contain missing values.
testing <- testing[, colSums(is.na(testing)) == 0]
training <- training[, colSums(is.na(training)) == 0]
trainData1 <- training[, -c(1:7)]
testData1 <- testing[, -c(1:7)]
Split the cleaned training set trainData (70%/20%) into training set ( computation of out-of-sample errors)
library(caret)
## Warning: package 'caret' was built under R version 3.3.1
## Loading required package: lattice
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.3.1
set.seed(8000)
inTrain <- createDataPartition(trainData1$classe, p = 0.7, list = FALSE)
train <- trainData1[inTrain, ]
valid <- trainData1[-inTrain, ]
control <- trainControl(method = "cv", number = 5)
fit_rpart <- train(classe ~ ., data = train, method = "rpart", trControl = control)
## Loading required package: rpart
print(fit_rpart, digits = 4)
## CART
##
## 13737 samples
## 52 predictor
## 5 classes: 'A', 'B', 'C', 'D', 'E'
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 10989, 10989, 10991, 10989, 10990
## Resampling results across tuning parameters:
##
## cp Accuracy Kappa
## 0.03428 0.5097 0.3601
## 0.06076 0.4155 0.2077
## 0.11647 0.3328 0.0738
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was cp = 0.03428.
## Loading required package: rpart.plot
## Warning: package 'rpart.plot' was built under R version 3.3.1
## Loading required package: rattle
## Warning: package 'rattle' was built under R version 3.3.1
## Rattle: Ein kostenloses grafisches Interface für Data Mining mit R.
## Version 4.1.0 Copyright (c) 2006-2015 Togaware Pty Ltd.
## Geben Sie 'rattle()' ein, um Ihre Daten mischen.
## Loading required package: RColorBrewer
fancyRpartPlot(fit_rpart$finalModel)
###Outcome prediction applied on validation set
prediction_rpart <- predict(fit_rpart, valid)
#Prediction Results
(conf_rpart <- confusionMatrix(valid$classe, prediction_rpart))
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1509 41 118 0 6
## B 454 383 302 0 0
## C 460 32 534 0 0
## D 448 163 353 0 0
## E 155 147 302 0 478
##
## Overall Statistics
##
## Accuracy : 0.4935
## 95% CI : (0.4806, 0.5063)
## No Information Rate : 0.5142
## P-Value [Acc > NIR] : 0.9993
##
## Kappa : 0.3385
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.4987 0.50000 0.33188 NA 0.98760
## Specificity 0.9423 0.85231 0.88494 0.8362 0.88817
## Pos Pred Value 0.9014 0.33626 0.52047 NA 0.44177
## Neg Pred Value 0.6398 0.91930 0.77876 NA 0.99875
## Prevalence 0.5142 0.13016 0.27341 0.0000 0.08224
## Detection Rate 0.2564 0.06508 0.09074 0.0000 0.08122
## Detection Prevalence 0.2845 0.19354 0.17434 0.1638 0.18386
## Balanced Accuracy 0.7205 0.67616 0.60841 NA 0.93789
(accuracy_rpart <- conf_rpart$overall[1])
## Accuracy
## 0.4934579
The accuracy rate is 0.49. Use of classification tree is suboptimal
#library(randomForest)
fit_rf <- train(classe ~ ., data = train, method = "rf", trControl = control)
## Loading required package: randomForest
## Warning: package 'randomForest' was built under R version 3.3.1
## 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
print(fit_rf, digits = 4)
## Random Forest
##
## 13737 samples
## 52 predictor
## 5 classes: 'A', 'B', 'C', 'D', 'E'
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 10990, 10988, 10990, 10990, 10990
## Resampling results across tuning parameters:
##
## mtry Accuracy Kappa
## 2 0.9901 0.9875
## 27 0.9906 0.9881
## 52 0.9809 0.9758
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 27.
predict_rf <- predict(fit_rf, valid)
# prediction result
(conf_rf <- confusionMatrix(valid$classe, predict_rf))
## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1672 2 0 0 0
## B 1 1135 3 0 0
## C 0 3 1019 4 0
## D 0 0 13 950 1
## E 0 0 1 6 1075
##
## Overall Statistics
##
## Accuracy : 0.9942
## 95% CI : (0.9919, 0.996)
## No Information Rate : 0.2843
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.9927
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.9994 0.9956 0.9836 0.9896 0.9991
## Specificity 0.9995 0.9992 0.9986 0.9972 0.9985
## Pos Pred Value 0.9988 0.9965 0.9932 0.9855 0.9935
## Neg Pred Value 0.9998 0.9989 0.9965 0.9980 0.9998
## Prevalence 0.2843 0.1937 0.1760 0.1631 0.1828
## Detection Rate 0.2841 0.1929 0.1732 0.1614 0.1827
## Detection Prevalence 0.2845 0.1935 0.1743 0.1638 0.1839
## Balanced Accuracy 0.9995 0.9974 0.9911 0.9934 0.9988
(accuracy_rf <- conf_rf$overall[1])
## Accuracy
## 0.9942226
Very high Accuracy achieved with Random Forest, 99.42%
(predict(fit_rf, testData1))
## [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