Background

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 (see the section on the Weight Lifting Exercise Dataset).

Data

The training data for this project are available here:

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

The test data are available here:

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

Purpose of the analysis

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

  • Description of the model
  • Use of cross validation
  • Expected out of sample error
  • Use of the prediction model to predict 20 different test cases

Exploratory data analysis and data preparation

archivo = "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv"
file <- download.file(archivo, destfile = "pml.csv")
data <- read.csv("pml.csv", stringsAsFactors = F)

Factor the variable classe

str(data$classe)
##  chr [1:19622] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" ...
unique(data$classe)
## [1] "A" "B" "C" "D" "E"
data$classe <- factor(data$classe)

Check the proportion of values of the variable class

round(prop.table(table(data$classe)), 2)
## 
##    A    B    C    D    E 
## 0.28 0.19 0.17 0.16 0.18

At this moment we have 160 variables. Next, we will eliminate variables that are not significant for the analysis.

data_clean <- data
dim(data_clean)
## [1] 19622   160
  • We remove the first seven columns, since they do not give information about the movement
data_clean <- data_clean[,-c(1:7)]
dim(data_clean)
## [1] 19622   153
  • We will also remove columns with a variance close to zero, since they do not give information
nzv <- nearZeroVar(data_clean, saveMetrics = T)
data_clean <- data_clean[,nzv$nzv==FALSE]
dim(data_clean)
## [1] 19622    94
  • We will remove those variables that contain na values in a proportion greater than 95%
AllNA    <- sapply(data_clean, function(x) mean(is.na(x))) > 0.95
data_clean <- data_clean[, AllNA==FALSE]
dim(data_clean)
## [1] 19622    53

We have left 19,000 observations of 53 variables. We divide the dataset into training and validation

set.seed(3455)
inTrain <- createDataPartition(y = data_clean$classe, p = 0.75, list = F)
training <- data_clean[inTrain,]
validation <- data_clean[-inTrain,]
# Comprobamos que se mantienen las proporciones
round(prop.table(table(training$classe)), 2)
## 
##    A    B    C    D    E 
## 0.28 0.19 0.17 0.16 0.18
round(prop.table(table(validation$classe)), 2)
## 
##    A    B    C    D    E 
## 0.28 0.19 0.17 0.16 0.18

Predictive model

We will use Random Forest beccause it is an algorithm that performs well on most problems, can handle categorical or continuous features, and can be used on very large datasets.

rf <- randomForest::randomForest(classe~., data = training)
prediction <- predict(rf, validation)
confusionMatrix(prediction, validation$classe)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 1394    6    0    0    0
##          B    1  943    3    0    0
##          C    0    0  852    7    0
##          D    0    0    0  797    0
##          E    0    0    0    0  901
## 
## Overall Statistics
##                                          
##                Accuracy : 0.9965         
##                  95% CI : (0.9945, 0.998)
##     No Information Rate : 0.2845         
##     P-Value [Acc > NIR] : < 2.2e-16      
##                                          
##                   Kappa : 0.9956         
##                                          
##  Mcnemar's Test P-Value : NA             
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            0.9993   0.9937   0.9965   0.9913   1.0000
## Specificity            0.9983   0.9990   0.9983   1.0000   1.0000
## Pos Pred Value         0.9957   0.9958   0.9919   1.0000   1.0000
## Neg Pred Value         0.9997   0.9985   0.9993   0.9983   1.0000
## Prevalence             0.2845   0.1935   0.1743   0.1639   0.1837
## Detection Rate         0.2843   0.1923   0.1737   0.1625   0.1837
## Detection Prevalence   0.2855   0.1931   0.1752   0.1625   0.1837
## Balanced Accuracy      0.9988   0.9963   0.9974   0.9956   1.0000

Model application to test data

archivo = "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv"
file <- download.file(archivo, destfile = "pml_testing.csv")
data_testing <- read.csv("pml_testing.csv", stringsAsFactors = F)
prediction <- predict(rf, data_testing)
prediction
##  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