-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmlwk4q1.rmd
103 lines (76 loc) · 4.12 KB
/
pmlwk4q1.rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
---
title: "PMLQ5"
output: html_document
---
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).
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
The data for this project come from this source: http://groupware.les.inf.puc-rio.br/har. If you use the document you create for this class for any purpose please cite them as they have been very generous in allowing their data to be used for this kind of assignment.
What you should submit
The goal of your project is to predict the manner in which they did the exercise. This is the "classe" variable in the training set. You may use any of the other variables to predict with. You should create a report describing how you built your model, how you used cross validation, what you think the expected out of sample error is, and why you made the choices you did. You will also use your prediction model to predict 20 different test cases.
```{r}
library(caret)
library(rpart)
library(rattle)
rm(list = ls())
if (!file.exists("pmlwk4qz-train.csv")) {
download.file("http://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv", destfile = "pmlwk4qz-train.csv")
}
if (!file.exists("pmlwk4qz-test.csv")) {download.file("http://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv", destfile = "pmlwk4qz-test.csv")
}
train <- read.csv("pmlwk4qz-train.csv", na.strings = c("NA", "#DIV/0!", ""))
test <- read.csv("pmlwk4qz-test.csv", na.strings = c("NA", "#DIV/0!", ""))
```
Data Partitioning
We'll split our training set into two subsets: 70% to train the model, 30% to evaluate it.
```{r}
partition <- createDataPartition(train$classe, p=0.7, list=FALSE)
train1 <- train[partition, ]
test1 <- train[-partition, ]
```
Data Cleaning
Take out the X variable, which just seems to be a row (observation) number. We're in trouble if it has predictive value.
```{r}
train1 <- train1[c(-1)]
test1 <- test1[c(-1)]
test <- test[c(-1)]
```
R has a function nearZeroVar to identify which features have low variance. These are worth dropping
```{r}
zeroVarianceFeatures<- nearZeroVar(train, saveMetrics=TRUE)
train1 <- train1[,zeroVarianceFeatures$nzv == FALSE]
test1 <- train1[,zeroVarianceFeatures$nzv == FALSE]
test <- test[,zeroVarianceFeatures$nzv == FALSE]
```
Feature Selection
Learned this from an R blog: how to drop variables that are missing a lot of data (ie. have a lot of NAs)
```{r}
na_threshold = 0.7 # set this to the threshold of NAs you want to filter
tmp <- train1 # create a temporary dataframe
for(i in 1:length(train1)) {
if( sum( is.na(train1[,i]))/ nrow(train1) >= na_threshold) {
for(j in 1:length(tmp)) {
if (length ( grep(names(train1[i]), names(tmp)[j])) == 1) {
tmp <- tmp[, -j]
}
}
}
}
train1 <- tmp
test1 <- test1[colnames(train1)]
test <- test[colnames(train1)]
```
Prediction
1. Start with a bayes
2. Fit to the full data set
3.
```{r}
set.seed(20161125)
mod1 <- train(classe ~ ., data=train1, method="Rborist")
predictions_mod1 <- predict(mod1, data=test1)
confusionMatrix(predictions_mod1, myTesting$classe)
```