Loading Libraries and dataset

library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
library(randomForest)
## randomForest 4.7-1.2
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:ggplot2':
## 
##     margin
training.url <- 'http://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv'
test.cases.url <- 'http://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv'
downloadcsv <- function(url, nastrings) {
  temp <- tempfile()
  download.file(url, temp)
  data <- read.csv(temp, na.strings = nastrings)
  unlink(temp)
  return(data)
}

remove missing value from the dataset

train <- downloadcsv(training.url, c("", "NA", "#DIV/0!"))
test <- downloadcsv(test.cases.url, c("", "NA", "#DIV/0!"))

The training data has 19622 observations and 160 features, and the distribution of the five measured stances A,B,C,D,E is:

dim(train)
## [1] 19622   160
table(train$classe)
## 
##    A    B    C    D    E 
## 5580 3797 3422 3216 3607
set.seed(1234)
trainset <- createDataPartition(train$classe, p = 0.8, list = FALSE)
Training <- train[trainset, ]
Validation <- train[-trainset, ]

Check for near zero variance predictors and drop them if necessary

# exclude columns with 40%  more missing values exclude descriptive columns

nonzerocol <- nearZeroVar(Training)
Training <- Training[, -nonzerocol]
countlength <- sapply(Training, function(x) {
  sum(!(is.na(x) | x == ""))
})
nullCol <- names(countlength[countlength < 0.6 * length(Training$classe)])
descriptcol <- c("X", "user_name", "raw_timestamp_part_1", "raw_timestamp_part_2", 
                 "cvtd_timestamp", "new_window", "num_window")
excludecolumns <- c(descriptcol, nullCol)
Training <- Training[, !names(Training) %in% excludecolumns]

Model Validation

# Using 'union' to ensure same level
rfModel <- randomForest(as.factor(classe)~ ., data = Training, importance = TRUE, ntrees = 10)
ptraining <- predict(rfModel, Training)
u1 <- union(ptraining,Training$classe)
t1 <- table(factor(ptraining, u1), factor(Training$classe, u1))
print(confusionMatrix(t1))
## Confusion Matrix and Statistics
## 
##    
##        A    B    C    D    E
##   A 4464    0    0    0    0
##   B    0 3038    0    0    0
##   C    0    0 2738    0    0
##   D    0    0    0 2573    0
##   E    0    0    0    0 2886
## 
## Overall Statistics
##                                      
##                Accuracy : 1          
##                  95% CI : (0.9998, 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

confusion Matrix and pre-prediction

pvalidation <- predict(rfModel, Validation)
u2 <- union(pvalidation,Validation$classe)
t2 <- table(factor(pvalidation, u2), factor(Validation$classe, u2))
print(confusionMatrix(t2))
## Confusion Matrix and Statistics
## 
##    
##        A    B    C    D    E
##   A 1116    4    0    0    0
##   B    0  754    2    0    0
##   C    0    1  681   11    2
##   D    0    0    1  632    3
##   E    0    0    0    0  716
## 
## Overall Statistics
##                                           
##                Accuracy : 0.9939          
##                  95% CI : (0.9909, 0.9961)
##     No Information Rate : 0.2845          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.9923          
##                                           
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: A Class: B Class: C Class: D Class: E
## Sensitivity            1.0000   0.9934   0.9956   0.9829   0.9931
## Specificity            0.9986   0.9994   0.9957   0.9988   1.0000
## Pos Pred Value         0.9964   0.9974   0.9799   0.9937   1.0000
## Neg Pred Value         1.0000   0.9984   0.9991   0.9967   0.9984
## Prevalence             0.2845   0.1935   0.1744   0.1639   0.1838
## Detection Rate         0.2845   0.1922   0.1736   0.1611   0.1825
## Detection Prevalence   0.2855   0.1927   0.1772   0.1621   0.1825
## Balanced Accuracy      0.9993   0.9964   0.9956   0.9908   0.9965

Predictive Outcomes

ptest <- predict(rfModel, test)
ptest
##  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