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.

Analysis

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://web.archive.org/web/20161224072740/http:/groupware.les.inf.puc-rio.br/har (see the section on the Weight Lifting Exercise Dataset). This project uses the h2o library for machine learning modeling.

Code & Published Location

https://github.com/luisrh01/pml-project

Load data and remove unnecessary feilds

training = read.csv("data/pml-training.csv")

# remove columns that will not provide value from training and testing data sets
nearZeroVariance <- nearZeroVar(training)
training_clean <- training[, -nearZeroVariance]

# check for columns that are 95% or more NA
nearAllNA <- sapply(training_clean, function(x) mean(is.na(x))) > 0.95
training <- training_clean[, nearAllNA==FALSE]
# remove columns without predictive value
train2 <- training[-c(1:7)]

Create data set splits for machine learning modeling

# Split H2o data into Train/Validation/Test Sets
data_frame_h2o <- as.h2o(train2)
split_h2o <- h2o.splitFrame(data_frame_h2o, c(0.7, 0.15), seed = 1234)
train_h2o <- h2o.assign(split_h2o[[1]], "train") # 70%
valid_h2o <- h2o.assign(split_h2o[[2]], "valid") # 15%
test_h2o  <- h2o.assign(split_h2o[[3]], "test")  # 15%

Build models: Gradient Boost Machine, RandomForest, Naive Bayes, and XGBoost

y <- "classe" # Feature to Predict
x <- setdiff(names(data_frame_h2o), y) # All other data columns

h2o_gbm <- h2o.gbm(
  model_id = 'h2o_gbm',
  x = x,
  y = y,
  training_frame = train_h2o,
  validation_frame = valid_h2o,
  ntrees = 500,
  max_depth = 6,
  learn_rate = 0.1
)
h2o_rfe <- h2o.randomForest(
  model_id = 'h2o_rfe',
  x = x,
  y = y,
  training_frame = train_h2o,
  validation_frame = valid_h2o,
  ntrees = 500,
  max_depth = 6
)
h2o_nb <- h2o.naiveBayes(
  model_id = 'h2o_nb',
  x = x,
  y = y,
  training_frame = train_h2o,
  validation_frame = valid_h2o,
  nfolds = 10,
  laplace = 0
)
h2o_xgb <- h2o.xgboost(
  x = x,
  y = y,
  training_frame    = train_h2o,
  validation_frame = valid_h2o
)

Evaluate models based on R^2 to decide on model, plot R^2 results.

rsquare <-
  data.frame(
    Model = c(
      "GBM",
      "RFE",
      "NB",
      "XGBoost"
    ),
    Accuracy = c(
      h2o.r2(h2o_gbm),
      h2o.r2(h2o_rfe),
      h2o.r2(h2o_nb),
      h2o.r2(h2o_xgb)
    )
  )
ggplot(rsquare, aes(x = reorder(Model, -Accuracy), y = Accuracy)) + geom_bar(stat = 'identity') + 
  theme_economist() + ggtitle('Comparison of Model R^2') + labs (x="Model sorted by R^2", y="Accuracy")

Select GBM Model based on R^2 value.

Print confusionmatrix

print(h2o.confusionMatrix(h2o_gbm, valid = TRUE))
## Confusion Matrix: Row labels: Actual class; Column labels: Predicted class
##          A   B   C   D   E  Error         Rate
## A      816   1   0   0   0 0.0012 =    1 / 817
## B        1 574   0   0   0 0.0017 =    1 / 575
## C        0   2 527   1   0 0.0057 =    3 / 530
## D        0   0   6 469   0 0.0126 =    6 / 475
## E        0   0   0   2 541 0.0037 =    2 / 543
## Totals 817 577 533 472 541 0.0044 = 13 / 2,940

Plot variable importance of GBM model

Load new data to predict

testing = read.csv("data/pml-testing.csv")
predictiondata <- as.h2o(testing)

Predict new data and Print predicted results

predicted <- h2o.predict(object = h2o_gbm, newdata = predictiondata)
print(as.data.frame(predicted$predict))
##    predict
## 1        B
## 2        A
## 3        B
## 4        A
## 5        A
## 6        E
## 7        D
## 8        B
## 9        A
## 10       A
## 11       B
## 12       C
## 13       B
## 14       A
## 15       E
## 16       E
## 17       A
## 18       B
## 19       B
## 20       B