Synopsis

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).

Process Data

Download Files

setwd("C:/Users/Apple/Desktop/RStudio Tour/assignment/project8.1")
library(caret)
## Warning: package 'caret' was built under R version 4.0.3
## Loading required package: lattice
## Loading required package: ggplot2
library(ggplot2)
library(data.table)
## Warning: package 'data.table' was built under R version 4.0.2
if(!file.exists("train.csv")){
    download.file("https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv",destfile = "train.csv")
}

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

Read File

trainData <- fread("train.csv")
testData <- fread("test.csv")
trainData$classe<-as.factor(trainData$classe)

Tidy Data

remove the columns contain NA

tidy.trainDT  <- trainData[,which(colSums(is.na(trainData))==0),with=FALSE]

tidy.testDT <- testData[,which(colSums(is.na(testData))==0),with=FALSE]

remove zero covariable

nzv<-nearZeroVar(tidy.trainDT,saveMetrics = TRUE)
tidy.trainDT <- tidy.trainDT[,nzv$nzv==FALSE,with=FALSE]

nzv<-nearZeroVar(tidy.testDT,saveMetrics = TRUE)
tidy.testDT <- tidy.testDT[,nzv$nzv==FALSE,with=FALSE]

Remove the column1 since it is the serial number and could distort the prediction.

tidy.trainDT<-tidy.trainDT[,-1,with=FALSE]
tidy.testDT<- tidy.testDT[,-1,with=FALSE]

Create cross-validation data

split train-data into training and validation

set.seed(2020)
inTrain<-createDataPartition(tidy.trainDT$classe,p=0.7,list=FALSE)
training<-tidy.trainDT[inTrain,]
testing<-tidy.trainDT[-inTrain,]

Model

Apply random forests as the model

set.seed(1229)
training<-as.data.frame(training)
mod <- train(classe~.,data=training,method="rf",
             trControl=trainControl(method="cv",number=5),ntree=200)

test the model

pred<-predict(mod,testing)
confusionMatrix(pred,testing$classe)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 1674    1    0    0    0
##          B    0 1137    1    0    0
##          C    0    1 1025    0    0
##          D    0    0    0  964    1
##          E    0    0    0    0 1081
## 
## Overall Statistics
##                                           
##                Accuracy : 0.9993          
##                  95% CI : (0.9983, 0.9998)
##     No Information Rate : 0.2845          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.9991          
##                                           
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            1.0000   0.9982   0.9990   1.0000   0.9991
## Specificity            0.9998   0.9998   0.9998   0.9998   1.0000
## Pos Pred Value         0.9994   0.9991   0.9990   0.9990   1.0000
## Neg Pred Value         1.0000   0.9996   0.9998   1.0000   0.9998
## Prevalence             0.2845   0.1935   0.1743   0.1638   0.1839
## Detection Rate         0.2845   0.1932   0.1742   0.1638   0.1837
## Detection Prevalence   0.2846   0.1934   0.1743   0.1640   0.1837
## Balanced Accuracy      0.9999   0.9990   0.9994   0.9999   0.9995

Result

predict(mod,newdata=tidy.testDT)
##  [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