Content
The dataset contains about 160 predictors (most of which are not required) and classifiers column is ‘classe’ and the exercise pattern is classified into 5 types- A, B, C, D, E.
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. Our goal here 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:
Link:- http://groupware.les.inf.puc-rio.br/har (see the section on the Weight Lifting Exercise Dataset).
The dataset contains about 160 predictors (most of which are not required) and classifiers column is ‘classe’ and the exercise pattern is classified into 5 types- A, B, C, D, E.
Velloso, E.; Bulling, A.; Gellersen, H.; Ugulino, W.; Fuks, H. Qualitative Activity Recognition of Weight Lifting Exercises. Proceedings of 4th International Conference in Cooperation with SIGCHI (Augmented Human ’13) . Stuttgart, Germany: ACM SIGCHI, 2013.
Read more: http://groupware.les.inf.puc-rio.br/har#wle_paper_section#ixzz4dPxKFugX
training <- read.csv("pml-training.csv")
na_info <- colSums(is.na(training))
na_var <- names((na_info[na_info > 0]))
trainingData <- training[, setdiff(x = names(training), y = na_var)]
dim(trainingData)
## [1] 19622 93
validation <- read.csv("pml-testing.csv")
na_info <- colSums(is.na(validation))
na_var <- names((na_info[na_info > 0]))
validationData <- validation[, setdiff(x = names(validation), y = na_var)]
dim(validationData)
## [1] 20 60
#For training set
classe <- trainingData$classe
trainRemove <- grepl("^X|timestamp|window", names(trainingData))
trainingData <- trainingData[, !trainRemove]
trainCleaned <- trainingData[, sapply(trainingData, is.numeric)]
trainCleaned$classe <- classe
#For validation set
validationRemove <- grepl("^X|timestamp|window", names(validationData))
validationData <- validationData[, !validationRemove]
validationCleaned <- validationData[, sapply(validationData, is.numeric)]
Now we are left with 53 features of training and validation set on which we can train our Predictive Algorithms.
We split the dataset into 70% training and 30% test set so that we can train our model on the 70% dataset and cross-validate it with the rest 30% of remaining dataset.
For reproducibality I have set the generation of random numbers to be fixed every time the code is executed.
smp_size <- floor(0.70 * nrow(trainCleaned))
inTrain <- sample(seq_len(nrow(trainCleaned)), size = smp_size)
trainData <- trainCleaned[inTrain, ]
testData <- trainCleaned[-inTrain, ]
library(randomForest)
## randomForest 4.6-14
## Type rfNews() to see new features/changes/bug fixes.
model <- randomForest(trainData$classe ~ ., data = trainData, ntree = 500, mtry = 6, importance = TRUE)
print(model)
##
## Call:
## randomForest(formula = trainData$classe ~ ., data = trainData, ntree = 500, mtry = 6, importance = TRUE)
## Type of random forest: classification
## Number of trees: 500
## No. of variables tried at each split: 6
##
## OOB estimate of error rate: 0.63%
## Confusion matrix:
## A B C D E class.error
## A 3862 4 0 0 1 0.001292992
## B 14 2666 5 0 0 0.007076350
## C 0 21 2408 1 0 0.009053498
## D 0 0 30 2224 0 0.013309672
## E 0 0 4 6 2489 0.004001601
predValid <- predict(model, testData, type = "class")
ValAcc <- mean(predValid == testData$classe)*100
sprintf("Validation Accuracy of the model is:- %f", ValAcc)
## [1] "Validation Accuracy of the model is:- 99.541362"
table(predValid,testData$classe)
##
## predValid A B C D E
## A 1713 0 0 0 0
## B 0 1112 6 0 0
## C 0 0 986 11 2
## D 0 0 0 951 8
## E 0 0 0 0 1098
Now, we apply the model to the original testing data set downloaded from the data source.
result <- predict(model, validationCleaned[, -length(names(validationCleaned))])
print(result)
## 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