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 here: http://groupware.les.inf.puc-rio.br/har (see the section on the Weight Lifting Exercise Dataset)

  1. Preparation of directory and library
## Loading required package: lattice
## Loading required package: ggplot2
## randomForest 4.6-10
## Type rfNews() to see new features/changes/bug fixes.
## Loading required package: bitops
## 
## Attaching package: 'dplyr'
## 
## The following object is masked from 'package:randomForest':
## 
##     combine
## 
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
  1. Downloading Data
training<-read.csv("pml-training.csv", header=TRUE, sep=",", dec=".")
testing<-read.csv("pml-testing.csv", header=TRUE, sep=",", dec=".")

training has 160 variables of 19622 rows. Data partition is created.

training<-subset(training,select=-c(cvtd_timestamp))

inTrain<- createDataPartition(y= training$classe, p=0.6, list=FALSE)

training_sample<-training[inTrain,]
testing_sample<-training[-inTrain,]

The training data frame is divided in training_sample and testing_sample. training_sample has 11776 rows while testing_sample has 7846 rows.

  1. Cleaning and Filtering of Data

The purpose is discarding of irrelevant, NA´s or with NULL variance variables. First we clean and preProcess training_sample data.

## Training Data


training2<-subset(training_sample,select=-c(classe))


## nonzeroVar is the problematic vector with unique value 

nonzeroVar<- nearZeroVar(training2, saveMetrics = FALSE)
colnameswithNA<-colnames(training2[ ,colSums(is.na(training2)) > 0])
columnwithNA<-which(names(training2) %in% colnameswithNA)

##  it is choosen only variables without NA and with Variance
columnstodelete<-unique(c(columnwithNA,nonzeroVar))
training3 <- training2[,-c(columnstodelete)]

## numeric_vector is the logical vector, which determines if a predictor is numerical or is not
numeric_vector <- as.vector(sapply(training3, is.numeric))
only_numeric<-names(training3[,numeric_vector==TRUE])
training_numeric<- training3[,c(only_numeric)]
columnsnumeric<-which(names(training3) %in% only_numeric)

training3 has 56 numeric variables. PCA analysis is executed and Variables correlated are reemplaced by PCA variables. PCA is applied only to numeric variables This is done for training and testing data

preproc <- preProcess(training_numeric, method="pca")
train.pca<-predict(preproc, training_numeric) ## PCA variables are created

##correlated variables is deleted because they aren´t needed anymore. Instead of them, PCA variables will be used
classe<-training_sample$classe
df_training<-cbind(user_name=training3[,-c(columnsnumeric)],train.pca) ##variables correlated are deleted

PCA analysis is executed and 27 PCA variables reemplaces
56 variables correlated of the training data. PCA is applied only to numeric variables.The same proccess is done for testing data

  1. Cross Validation and Analysis of training data on Random Forest Model

Because of the characteristics of data, random forest could be a good model. However, cross validation is incorporated in the analysis and if error.cv is low and stable with k=10, random forest will be the chosen model.

library(randomForest)


rf.cv <- rfcv(df_training, classe, cv.fold=10)
with(rf.cv, plot(n.var, error.cv, type="b", col="red"))

The Cross validation is plotted and found that the mean of error.cv trend to 0 with more than 12 variables. This is an excelent orientation to choose Random Forest as chosen model (with the all PCA variables).

## the plot suggests to use Random Forest

set.seed(12345)
modfit<-randomForest(classe ~., data=df_training)


rf.pred.training=predict(modfit,df_training,type="class")
confussion.training<-confusionMatrix(rf.pred.training, classe)
confussion.training
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 3348    0    0    0    0
##          B    0 2279    0    0    0
##          C    0    0 2054    0    0
##          D    0    0    0 1930    0
##          E    0    0    0    0 2165
## 
## Overall Statistics
##                                      
##                Accuracy : 1          
##                  95% CI : (0.9997, 1)
##     No Information Rate : 0.2843     
##     P-Value [Acc > NIR] : < 2.2e-16  
##                                      
##                   Kappa : 1          
##  Mcnemar's Test P-Value : NA         
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            1.0000   1.0000   1.0000   1.0000   1.0000
## Specificity            1.0000   1.0000   1.0000   1.0000   1.0000
## Pos Pred Value         1.0000   1.0000   1.0000   1.0000   1.0000
## Neg Pred Value         1.0000   1.0000   1.0000   1.0000   1.0000
## Prevalence             0.2843   0.1935   0.1744   0.1639   0.1838
## Detection Rate         0.2843   0.1935   0.1744   0.1639   0.1838
## Detection Prevalence   0.2843   0.1935   0.1744   0.1639   0.1838
## Balanced Accuracy      1.0000   1.0000   1.0000   1.0000   1.0000

The accuracy is of 100% with training data. Now the confusion matrix is evaluated with testing_sample data

  1. Confusion Matrix and accuracy

I use testing_sample to predict and check performance of accuracy. First at all, testing data has the same cleaning preProccess and getting of PCA variables

testing_sample2<-subset(testing_sample,select=-c(classe))
testing_sample2 <- testing_sample2[,-c(columnstodelete)]
testing_numeric<- testing_sample2[,c(only_numeric)]
testing.pca<-predict(preproc, testing_numeric) ## PCA variables are created
classe<-testing_sample$classe
df_testing<-cbind(user_name=testing_sample2[,-c(columnsnumeric)],testing.pca)
## df_testing<-cbind(testing_sample2[,-c(columnsnumeric)],testing.pca)



set.seed(12345)
rf.pred=predict(modfit,df_testing,type="class")
predMatrix = with(df_testing,table(rf.pred,classe))
confussion.testing<-confusionMatrix(rf.pred, classe)
confussion.testing
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    A    B    C    D    E
##          A 2229   12    0    0    0
##          B    3 1494   14    0    0
##          C    0   12 1350   27    0
##          D    0    0    4 1252   11
##          E    0    0    0    7 1431
## 
## Overall Statistics
##                                           
##                Accuracy : 0.9885          
##                  95% CI : (0.9859, 0.9908)
##     No Information Rate : 0.2845          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.9855          
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            0.9987   0.9842   0.9868   0.9736   0.9924
## Specificity            0.9979   0.9973   0.9940   0.9977   0.9989
## Pos Pred Value         0.9946   0.9887   0.9719   0.9882   0.9951
## Neg Pred Value         0.9995   0.9962   0.9972   0.9948   0.9983
## Prevalence             0.2845   0.1935   0.1744   0.1639   0.1838
## Detection Rate         0.2841   0.1904   0.1721   0.1596   0.1824
## Detection Prevalence   0.2856   0.1926   0.1770   0.1615   0.1833
## Balanced Accuracy      0.9983   0.9908   0.9904   0.9856   0.9956

The accuracy is of 99 % with testing data.