Introduction

The goal of the project is to predict the manner in which someone does exercise. This is the “classe” variable in the training set.

Loading the data

The data was downloaded from:

Training data:

https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv

Test data:

https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv

the data is loaded in R as follows:

training_set <- read.csv("pml-training.csv", header=TRUE, sep=",")

test_set <- read.csv("pml-testing.csv", header=TRUE, sep=",")

Exploring the data

dim(training_set)
## [1] 19622   160
dim(test_set)
## [1]  20 160
summarise(group_by(training_set,user_name), n = n())
## # A tibble: 6 x 2
##   user_name     n
##   <fct>     <int>
## 1 adelmo     3892
## 2 carlitos   3112
## 3 charles    3536
## 4 eurico     3070
## 5 jeremy     3402
## 6 pedro      2610
summarise(group_by(training_set,classe), n = n())
## # A tibble: 5 x 2
##   classe     n
##   <fct>  <int>
## 1 A       5580
## 2 B       3797
## 3 C       3422
## 4 D       3216
## 5 E       3607

The dataframes training_set and test_set both have 160 variables. The training set has 19622 observations and the test set has 20 observations. The classe variable has five different classes in the training set and are more or less equally distributed.

Selecting predictors

Of all 160 variables in the training set, the 160th is the outcome class and the rest are either raw or processed data from the four sensors together with user and timestamps tags. Most of the processed data columns are incomplete therefore I will only use the raw data as predictors:

# indices vector to select raw data only
subset_ind <- c(
  #euler angles belt
  8,9,10,
  #gyros belt
  37,38,39,
  #accel belt
  40,41,42,
  #magnet belt
  43,44,45,
  
  #euler angles arm
  46,47,48,
  #gyros arm
  60,61,62,
  #accel arm
  63,64,65,
  #magnet arm
  66,67,68,
  
  #euler angles dumbbell
  84,85,86,
  #gyros dumbbell
  113,114,115,
  #accel dumbbell
  116,117,118,
  #magnet dumbbell
  119,120,121,
  
  #euler angles forearm
  122,123,124,
  #gyros forearm
  151,152,153,
  #accel forearm
  154,155,156,
  #magnet forearm
  157,158,159,
  
  # outcome classe
  160
)

# select only the 48 columns for raw data from the 4 sensors
training_set_sub <- select(training_set,subset_ind)
test_set_sub <- select(test_set,subset_ind, -(160))

Training

Based on what is reported in:

W. Ugulino, D. Cardador, K. Vega, E. Velloso, R. Milidiú, and H. Fuks, “Wearable Computing: Accelerometers’ Data Classification of Body Postures and Movements,” in Proceedings of 21st Brazilian Symposium on Artificial Intelligence, 2012, vol. 7589, pp. 52–61.

I will use Random Forest with 10-fold cross validadtion to estimate the mtry parameter and the out of sample error.

set.seed(1234)

# 10 k-fold cross validadtion
customizedControl <- trainControl(method = "cv", number = 10, allowParallel = TRUE)

# train with random forest, this takes a while
model_rf <- train(classe ~ ., data = training_set_sub, method = "rf", trControl = customizedControl)

model_rf
## Random Forest 
## 
## 19622 samples
##    48 predictor
##     5 classes: 'A', 'B', 'C', 'D', 'E' 
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 17659, 17660, 17661, 17662, 17659, 17659, ... 
## Resampling results across tuning parameters:
## 
##   mtry  Accuracy   Kappa    
##    2    0.9951588  0.9938761
##   25    0.9945473  0.9931029
##   48    0.9890437  0.9861392
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.

With mtry=2, the accuracy, and therefore the estimated out of sample error is 99.5%.

Prediction

Now we can predict the 20 samples in the test set:

# Obtained after doing the project Quiz
ground_truth_test_set <- as.factor(c('B', 'A', 'B', 'A', 'A', 'E', 'D', 'B', 'A', 'A', 'B', 'C', 'B', 'A', 'E', 'E', 'A', 'B', 'B', 'B'))

# Apply the model
predictions <- predict(model_rf, newdata = test_set_sub)

# Check confusion matrix
confusionMatrix(data = predictions, reference = ground_truth_test_set)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction A B C D E
##          A 7 0 0 0 0
##          B 0 8 0 0 0
##          C 0 0 1 0 0
##          D 0 0 0 1 0
##          E 0 0 0 0 3
## 
## Overall Statistics
##                                      
##                Accuracy : 1          
##                  95% CI : (0.8316, 1)
##     No Information Rate : 0.4        
##     P-Value [Acc > NIR] : 1.1e-08    
##                                      
##                   Kappa : 1          
##                                      
##  Mcnemar's Test P-Value : NA         
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity              1.00      1.0     1.00     1.00     1.00
## Specificity              1.00      1.0     1.00     1.00     1.00
## Pos Pred Value           1.00      1.0     1.00     1.00     1.00
## Neg Pred Value           1.00      1.0     1.00     1.00     1.00
## Prevalence               0.35      0.4     0.05     0.05     0.15
## Detection Rate           0.35      0.4     0.05     0.05     0.15
## Detection Prevalence     0.35      0.4     0.05     0.05     0.15
## Balanced Accuracy        1.00      1.0     1.00     1.00     1.00

All 20 samples were predicted correctly.