R Markdown

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://web.archive.org/web/20161224072740/http:/groupware.les.inf.puc-rio.br/har

Add the Librarys and download the testing and training data


``` r
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
library(rpart)
library(rpart.plot)
library(RColorBrewer)
library(rattle)
## Loading required package: tibble
## Loading required package: bitops
## Rattle: A free graphical interface for data science with R.
## Version 5.5.1 Copyright (c) 2006-2021 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
library(ggplot2)
library(randomForest)
## randomForest 4.7-1.1
## 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(knitr)

rm(list = ls())

if (!file.exists("pml-training.csv")) {
  download.file("http://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv", destfile = "pml-training.csv")
}

if (!file.exists("pml-testing.csv")) {
  download.file("http://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv", destfile = "pml-testing.csv")
}

training <- read.csv("pml-training.csv", na.strings=c("NA","#DIV/0!",""))
testing <- read.csv("pml-testing.csv", na.strings=c("NA","#DIV/0!",""))

Clean the data into training and testing data

training <-training[,colSums(is.na(training)) == 0]
testing <-testing[,colSums(is.na(testing)) == 0]

training   <-training[,-c(1:7)]
testing <-testing[,-c(1:7)]

dim(training)
## [1] 19622    53
dim(testing)
## [1] 20 53

Split data into training and testing set. set the seed.

inTrain <- createDataPartition(y = training$classe, p=0.75, list = FALSE)
training <- training[inTrain, ]
testing <- training[-inTrain, ]

set.seed(311411)

The rpart.plot function is used to create a visual representation of the decision tree

fitData <- rpart(classe ~ .,training, method="class")
rpart.plot(fitData)

code for making predictions with the decision tree model and evaluating its performance using a confusion matrix

predData <- predict(fitData, testing, type = "class")

#Estimate the errors of the prediction algorithm in the Decision Tree model
confusionMatrixData <-confusionMatrix(as.factor(testing$classe), predData)
confusionMatrixData
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   A   B   C   D   E
##          A 867  30  21 103  12
##          B 126 397  51 132  32
##          C  10  32 473  77  38
##          D  46  27  88 415  29
##          E  22  50  79  91 427
## 
## Overall Statistics
##                                           
##                Accuracy : 0.7018          
##                  95% CI : (0.6867, 0.7165)
##     No Information Rate : 0.2914          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.6236          
##                                           
##  Mcnemar's Test P-Value : < 2.2e-16       
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            0.8095   0.7407   0.6643   0.5073   0.7937
## Specificity            0.9363   0.8914   0.9470   0.9335   0.9229
## Pos Pred Value         0.8393   0.5379   0.7508   0.6860   0.6383
## Neg Pred Value         0.9228   0.9527   0.9215   0.8687   0.9631
## Prevalence             0.2914   0.1459   0.1937   0.2226   0.1464
## Detection Rate         0.2359   0.1080   0.1287   0.1129   0.1162
## Detection Prevalence   0.2811   0.2008   0.1714   0.1646   0.1820
## Balanced Accuracy      0.8729   0.8160   0.8057   0.7204   0.8583

code below for fitting a Random Forest model and evaluating its performance with a confusion matrix

randomForestModel <- randomForest(as.factor(classe)~., data=training)
# Summary of the model
randomForestModel
## 
## 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.45%
## Confusion matrix:
##      A    B    C    D    E  class.error
## A 4183    2    0    0    0 0.0004778973
## B   14 2827    7    0    0 0.0073735955
## C    0   13 2553    1    0 0.0054538372
## D    0    0   20 2392    0 0.0082918740
## E    0    0    1    8 2697 0.0033259424
predTesting <- predict(randomForestModel, testing)
rfcfm  <- confusionMatrix(as.factor(testing$classe), predTesting)
rfcfm
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 1033    0    0    0    0
##          B    0  738    0    0    0
##          C    0    0  630    0    0
##          D    0    0    0  605    0
##          E    0    0    0    0  669
## 
## Overall Statistics
##                                     
##                Accuracy : 1         
##                  95% CI : (0.999, 1)
##     No Information Rate : 0.2811    
##     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.000
## Specificity            1.0000   1.0000   1.0000   1.0000    1.000
## Pos Pred Value         1.0000   1.0000   1.0000   1.0000    1.000
## Neg Pred Value         1.0000   1.0000   1.0000   1.0000    1.000
## Prevalence             0.2811   0.2008   0.1714   0.1646    0.182
## Detection Rate         0.2811   0.2008   0.1714   0.1646    0.182
## Detection Prevalence   0.2811   0.2008   0.1714   0.1646    0.182
## Balanced Accuracy      1.0000   1.0000   1.0000   1.0000    1.000

Convert the Confusion Matrix table to dataframe and Plot the graph ggplot

cm_df <- as.data.frame(rfcfm$table)

g <- ggplot(data = cm_df, aes(x = Prediction, y = Reference, fill = Freq)) +
  geom_tile() +
  scale_fill_gradient(low = "white", high = "blue") +
  labs(title = paste("Random Forest Confusion Matrix: Accuracy =", 
                     round(rfcfm$overall['Accuracy'], 4)), fill = "Frequency") +
  theme_minimal()

print(g)

Using the varImpPlot function with your Random Forest model will provide a visual representation of the importance of each variable used in the model.

varImpPlot(randomForestModel)

The plot function for a Random Forest model typically generates a visual representation of the model’s error rate as the number of trees increases, along with the importance of individual trees

plot(randomForestModel)