Roddy Mendoza Marriott 2026-02-09
Background
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
-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.
Some insights about the project
The goal of your project is to predict the manner in which they did
the exercise. This is the classe variable in the training
set. You may use any of the other variables to predict with. You should
create a report describing how you built your model, how you used cross
validation, what you think the expected out of sample error is, and why
you made the choices you did. You will also use your prediction model to
predict 20 different test cases.
Data processing
First, loading the packages and libraries.
library(plyr)
library(dplyr)
library(lattice)
library(ggplot2)
library(caret)
library(rpart)
library(rpart.plot)
library(RColorBrewer)
library(kernlab)
library(randomForest)
library(knitr)
library(e1071)Getting and cleaning data
## [1] 19622 160
## [1] 20 160
Preprocessing and cleaning data
we should Exclude the obvious columns i.e X,
user_name,raw_timestamp_part_1,raw_timestamp_part_2,
cvtd_timestamp,roll_belt which are the first 7
columns. We should also delete missing values and variables with near
zero variance.
#Deleting missing values
trainingst <- read.csv("pml-training.csv", na.strings=c("NA","#DIV/0!",""))
testingst <- read.csv("pml-testing.csv", na.strings=c("NA","#DIV/0!",""))#Deleting missing values
trainingst<-trainingst[,colSums(is.na(trainingst)) == 0]
testingst <-testingst[,colSums(is.na(testingst)) == 0]#Removing columns that are not predictors, which are the the seven first columns
trainingst <-trainingst[,-c(1:7)]
testingst <-testingst[,-c(1:7)]## [1] 19622 53
## [1] 20 53
From the above code block sum(completeCase) == nrows confirm that the
number of complete case is equal to number of rows in
trainingdf same for testingdf
Now we have only 53 columns(features) are left. we can preproccess
the training and testing i.e converting into scales of 0 to
1 and replacing any NA values to average of
that columns.
Partition the data set into training and
testing data from trainingst
inTrain <- createDataPartition(y = trainingst$classe, p=0.75, list = FALSE)
training <- trainingst[inTrain, ]
testing <- trainingst[-inTrain, ]Training the model
Two methods will be applied to model, and the best one will be used for the(testingst) predictions.
The methods are: Decision Tree and
Random Forests.
Model 1: Training the model with Decision Trees
#Use model to predict classe in validation testing set
predictionDT <- predict(fitDT, testing, type = "class")#Estimate the errors of the prediction algorithm in the Decision Tree model
cmdt <-confusionMatrix(as.factor(testing$classe), predictionDT)
cmdt## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1278 23 36 29 29
## B 212 514 102 70 51
## C 54 80 635 62 24
## D 137 28 126 414 99
## E 50 64 97 46 644
##
## Overall Statistics
##
## Accuracy : 0.7106
## 95% CI : (0.6977, 0.7233)
## No Information Rate : 0.353
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.6308
##
## Mcnemar's Test P-Value : < 2.2e-16
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.7383 0.7250 0.6376 0.66667 0.7603
## Specificity 0.9631 0.8963 0.9437 0.90894 0.9367
## Pos Pred Value 0.9161 0.5416 0.7427 0.51493 0.7148
## Neg Pred Value 0.8709 0.9507 0.9108 0.94951 0.9493
## Prevalence 0.3530 0.1446 0.2031 0.12663 0.1727
## Detection Rate 0.2606 0.1048 0.1295 0.08442 0.1313
## Detection Prevalence 0.2845 0.1935 0.1743 0.16395 0.1837
## Balanced Accuracy 0.8507 0.8106 0.7906 0.78780 0.8485
# Accuracy plot
plot(cmdt$table, col = cmdt$byClass,
main = paste("Decision Tree Confusion Matrix: Accuracy =", round(cmdt$overall['Accuracy'], 4)))Model 2: Training the model using Random Forest
##
## Call:
## randomForest(formula = as.factor(classe) ~ ., data = training)
## Type of random forest: classification
## Number of trees: 500
## No. of variables tried at each split: 7
##
## OOB estimate of error rate: 0.52%
## Confusion matrix:
## A B C D E class.error
## A 4183 2 0 0 0 0.0004778973
## B 17 2826 5 0 0 0.0077247191
## C 0 17 2549 1 0 0.0070120764
## D 0 0 24 2387 1 0.0103648425
## E 0 0 3 6 2697 0.0033259424
# Confusion matrix with testing
predTesting <- predict(rfModel, testing)
rfcfm <- confusionMatrix(as.factor(testing$classe), predTesting)
rfcfm## Confusion Matrix and Statistics
##
## Reference
## Prediction A B C D E
## A 1391 2 0 0 2
## B 1 946 2 0 0
## C 0 6 849 0 0
## D 0 0 10 794 0
## E 0 0 0 2 899
##
## Overall Statistics
##
## Accuracy : 0.9949
## 95% CI : (0.9925, 0.9967)
## No Information Rate : 0.2838
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.9936
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: A Class: B Class: C Class: D Class: E
## Sensitivity 0.9993 0.9916 0.9861 0.9975 0.9978
## Specificity 0.9989 0.9992 0.9985 0.9976 0.9995
## Pos Pred Value 0.9971 0.9968 0.9930 0.9876 0.9978
## Neg Pred Value 0.9997 0.9980 0.9970 0.9995 0.9995
## Prevalence 0.2838 0.1945 0.1756 0.1623 0.1837
## Detection Rate 0.2836 0.1929 0.1731 0.1619 0.1833
## Detection Prevalence 0.2845 0.1935 0.1743 0.1639 0.1837
## Balanced Accuracy 0.9991 0.9954 0.9923 0.9975 0.9986
plot(rfcfm$table, col = rfcfm$byClass, main = paste("Random Forest Confusion Matrix: Accuracy =", round(rfcfm$overall['Accuracy'], 4)))Remarks
-Decision Tree Model is the worst model running, it has
the low mean and the highest standard deviation.
-Random Forest Model it has the highest mean accuracy
and lowest standard deviation.
Depending on how your model is to be used, the interpretation of the kappa statistic might vary One common interpretation is shown as follows:
• Poor agreement = Less than 0.20
• Fair agreement = 0.20 to 0.40
• Moderate agreement = 0.40 to 0.60
• Good agreement = 0.60 to 0.80
• Very good agreement = 0.80 to 1.00
This two models preforms as expected, the deviation from the cross validation accuracy is low.
The predictive accuracy of the Random Forest Model is
excellent at 99.8 %. Accuracy has plateaued, and further tuning would
only yield decimal gain.
Making prediction on the 20 data pointsusing random forest
Decision Tree Model: 73.43%, Random Forest Model: 99.53%
The Random Forest model is selected and applied to make predictions on
the 20 data points from the original testing dataset (testingst)
## 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