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. The goal of this coursework 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.
setwd("D:\\Coursera\\Data_Science_Specialization\\08 - Practical Machine Learing\\Course Project")
trainfn = "training.csv"
testfn = "testing.csv"
if(!file.exists(trainfn))
{
download.file("https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv", destfile = trainfn)
}
if(!file.exists(testfn))
{
download.file("https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv", destfile = testfn)
}
#Read the training data and replace empty values by NA
trainDf <- read.csv(trainfn, sep=",", header=TRUE, na.strings = c("NA","",'#DIV/0!'))
testDf <- read.csv(testfn, sep=",", header=TRUE, na.strings = c("NA","",'#DIV/0!'))
dim(trainDf)
## [1] 19622 160
The size of training dataset id 19622 rows and 160 columns
There is a lot of columns filled with N/A only. Remove these.
trainDf <- trainDf[, colSums(is.na(trainDf)) == 0]
testDf <- testDf[, colSums(is.na(testDf)) == 0]
dim(trainDf)
## [1] 19622 60
Now the train dataframe have 60 columns.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
numer <- which(lapply(trainDf, class) == "numeric")
names(numer) <- NULL
class = trainDf$classe
trainDf <- trainDf[, numer]
trainDf$classe <- class
mean_s <- trainDf
std_s <- trainDf
meanToSd <- trainDf
names(trainDf)
## [1] "roll_belt" "pitch_belt" "yaw_belt"
## [4] "gyros_belt_x" "gyros_belt_y" "gyros_belt_z"
## [7] "roll_arm" "pitch_arm" "yaw_arm"
## [10] "gyros_arm_x" "gyros_arm_y" "gyros_arm_z"
## [13] "roll_dumbbell" "pitch_dumbbell" "yaw_dumbbell"
## [16] "gyros_dumbbell_x" "gyros_dumbbell_y" "gyros_dumbbell_z"
## [19] "magnet_dumbbell_z" "roll_forearm" "pitch_forearm"
## [22] "yaw_forearm" "gyros_forearm_x" "gyros_forearm_y"
## [25] "gyros_forearm_z" "magnet_forearm_y" "magnet_forearm_z"
## [28] "classe"
# https://stackoverflow.com/questions/21644848/summarizing-multiple-columns-with-dplyr
mean_s <- mean_s %>% group_by(classe) %>% summarise_all(funs(mean))
std_s <- std_s %>% group_by(classe) %>% summarise_all(funs(sd))
mean_to_sd <- function(x){mean(x)/sd(x)}
meanToSd <- meanToSd %>% group_by(classe) %>% summarise_all(funs(mean_to_sd))
meanToSd
## # A tibble: 5 x 28
## classe roll_belt pitch_belt yaw_belt gyros_belt_x gyros_belt_y
## <fct> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 A 0.986 0.0151 -0.120 -0.0284 0.668
## 2 B 1.09 -0.000811 -0.138 -0.0246 0.701
## 3 C 1.06 -0.0461 -0.0753 -0.0794 0.665
## 4 D 0.991 0.0862 -0.200 -0.0763 0.361
## 5 E 1.05 0.0258 -0.0615 0.0338 0.357
## # ... with 22 more variables: gyros_belt_z <dbl>, roll_arm <dbl>,
## # pitch_arm <dbl>, yaw_arm <dbl>, gyros_arm_x <dbl>, gyros_arm_y <dbl>,
## # gyros_arm_z <dbl>, roll_dumbbell <dbl>, pitch_dumbbell <dbl>,
## # yaw_dumbbell <dbl>, gyros_dumbbell_x <dbl>, gyros_dumbbell_y <dbl>,
## # gyros_dumbbell_z <dbl>, magnet_dumbbell_z <dbl>, roll_forearm <dbl>,
## # pitch_forearm <dbl>, yaw_forearm <dbl>, gyros_forearm_x <dbl>,
## # gyros_forearm_y <dbl>, gyros_forearm_z <dbl>, magnet_forearm_y <dbl>,
## # magnet_forearm_z <dbl>
testDf <- testDf[, numer]
Now the train dataframe have 28 columns.
library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
split<- createDataPartition(trainDf$classe, p=0.7, list=FALSE)
training <- trainDf[split, ]
validation <- trainDf[- split, ]
dim(training)
## [1] 13737 28
dim(validation)
## [1] 5885 28
Random forests is the algorithm with the good performance with the default settings. Train the model with random forests, use cross-validation for tree fitting. Predict the validation set labels and show its confusion matrix.
# https://machinelearningmastery.com/feature-selection-with-the-caret-r-package/
fit1 <- train(classe ~., method="rf", data=training, trControl=trainControl(method='cv'), number=5, allowParallel=TRUE)
pred1 <- predict(fit1, validation)
#confusionMatrix(pred1, validation$classe)
print(paste0("Accuracy = ", confusionMatrix(pred1, validation$classe)$overall['Accuracy']))
## [1] "Accuracy = 0.994052676295667"
pred2 <- predict(fit1, testDf)
pred2
## [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
So, prediction is
B, A, B, A, A, E, D, B, A, A, B, C, B, A, E, E, A, B, B, B