-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathpredictionAssignment.Rmd
More file actions
250 lines (183 loc) · 8 KB
/
predictionAssignment.Rmd
File metadata and controls
250 lines (183 loc) · 8 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
---
title: 'Coursera: Practical Machine Learning Prediction Assignment'
author: "Benjamin Chan [GitHub](https://github.com/benjamin-chan)"
output:
html_document:
keep_md: yes
toc: yes
---
```{r, echo=FALSE}
message(sprintf("Run time: %s\nR version: %s", Sys.time(), R.Version()$version.string))
```
> **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.
> 1. Your submission should consist of a link to a Github repo with your R markdown and compiled HTML file describing your analysis. Please constrain the text of the writeup to < 2000 words and the number of figures to be less than 5. It will make it easier for the graders if you submit a repo with a gh-pages branch so the HTML page can be viewed online (and you always want to make it easy on graders :-).
> 2. You should also apply your machine learning algorithm to the 20 test cases available in the test data above. Please submit your predictions in appropriate format to the programming assignment for automated grading. See the programming assignment for additional details.
> **Reproducibility **
> Due to security concerns with the exchange of R code, your code will not be run during the evaluation by your classmates. Please be sure that if they download the repo, they will be able to view the compiled HTML version of your analysis.
# Prepare the datasets
Read the training data into a data table.
```{r}
require(data.table)
setInternet2(TRUE)
url <- "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv"
D <- fread(url)
```
Read the testing data into a data table.
```{r}
url <- "https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv"
DTest <- fread(url)
```
Which variables in the test dataset have zero `NA`s?
Use this tip: [finding columns with all missing values in r](http://stackoverflow.com/a/11330265).
Belt, arm, dumbbell, and forearm variables that do not have any missing values in the test dataset will be **predictor candidates**.
```{r}
isAnyMissing <- sapply(DTest, function (x) any(is.na(x) | x == ""))
isPredictor <- !isAnyMissing & grepl("belt|[^(fore)]arm|dumbbell|forearm", names(isAnyMissing))
predCandidates <- names(isAnyMissing)[isPredictor]
predCandidates
```
Subset the primary dataset to include only the **predictor candidates** and the outcome variable, `classe`.
```{r}
varToInclude <- c("classe", predCandidates)
D <- D[, varToInclude, with=FALSE]
dim(D)
names(D)
```
Make `classe` into a factor.
```{r}
D <- D[, classe := factor(D[, classe])]
D[, .N, classe]
```
Split the dataset into a 60% training and 40% probing dataset.
```{r}
require(caret)
seed <- as.numeric(as.Date("2014-10-26"))
set.seed(seed)
inTrain <- createDataPartition(D$classe, p=0.6)
DTrain <- D[inTrain[[1]]]
DProbe <- D[-inTrain[[1]]]
```
Preprocess the prediction variables by centering and scaling.
```{r}
X <- DTrain[, predCandidates, with=FALSE]
preProc <- preProcess(X)
preProc
XCS <- predict(preProc, X)
DTrainCS <- data.table(data.frame(classe = DTrain[, classe], XCS))
```
Apply the centering and scaling to the probing dataset.
```{r}
X <- DProbe[, predCandidates, with=FALSE]
XCS <- predict(preProc, X)
DProbeCS <- data.table(data.frame(classe = DProbe[, classe], XCS))
```
Check for near zero variance.
```{r}
nzv <- nearZeroVar(DTrainCS, saveMetrics=TRUE)
if (any(nzv$nzv)) nzv else message("No variables with near zero variance")
```
Examine groups of prediction variables.
```{r histGroup}
histGroup <- function (data, regex) {
col <- grep(regex, names(data))
col <- c(col, which(names(data) == "classe"))
require(reshape2)
n <- nrow(data)
DMelted <- melt(data[, col, with=FALSE][, rownum := seq(1, n)], id.vars=c("rownum", "classe"))
require(ggplot2)
ggplot(DMelted, aes(x=classe, y=value)) +
geom_violin(aes(color=classe, fill=classe), alpha=1/2) +
# geom_jitter(aes(color=classe, fill=classe), alpha=1/10) +
# geom_smooth(aes(group=1), method="gam", color="black", alpha=1/2, size=2) +
facet_wrap(~ variable, scale="free_y") +
scale_color_brewer(palette="Spectral") +
scale_fill_brewer(palette="Spectral") +
labs(x="", y="") +
theme(legend.position="none")
}
histGroup(DTrainCS, "belt")
histGroup(DTrainCS, "[^(fore)]arm")
histGroup(DTrainCS, "dumbbell")
histGroup(DTrainCS, "forearm")
```
# Train a prediction model
Using random forest, the out of sample error should be small.
The error will be estimated using the 40% probing sample.
I would be quite happy with an error estimate of 3% or less.
Set up the parallel clusters.
```{r}
require(parallel)
require(doParallel)
cl <- makeCluster(detectCores() - 1)
registerDoParallel(cl)
```
Set the control parameters.
```{r}
ctrl <- trainControl(classProbs=TRUE,
savePredictions=TRUE,
allowParallel=TRUE)
```
Fit model over the tuning parameters.
```{r}
method <- "rf"
system.time(trainingModel <- train(classe ~ ., data=DTrainCS, method=method))
```
Stop the clusters.
```{r}
stopCluster(cl)
```
## Evaluate the model on the training dataset
```{r}
trainingModel
hat <- predict(trainingModel, DTrainCS)
confusionMatrix(hat, DTrain[, classe])
```
## Evaluate the model on the probing dataset
```{r}
hat <- predict(trainingModel, DProbeCS)
confusionMatrix(hat, DProbeCS[, classe])
```
## Display the final model
```{r finalModel}
varImp(trainingModel)
trainingModel$finalModel
```
**The estimated error rate is less than 1%.**
Save training model object for later.
```{r}
save(trainingModel, file="trainingModel.RData")
```
# Predict on the test data
Load the training model.
```{r}
load(file="trainingModel.RData", verbose=TRUE)
```
Get predictions and evaluate.
```{r}
DTestCS <- predict(preProc, DTest[, predCandidates, with=FALSE])
hat <- predict(trainingModel, DTestCS)
DTest <- cbind(hat , DTest)
subset(DTest, select=names(DTest)[grep("belt|[^(fore)]arm|dumbbell|forearm", names(DTest), invert=TRUE)])
```
## Submission to Coursera
Write submission files to `predictionAssignment_files/answers`.
```{r}
pml_write_files = function(x){
n = length(x)
path <- "predictionAssignment_files/answers"
for(i in 1:n){
filename = paste0("problem_id_",i,".txt")
write.table(x[i],file=file.path(path, filename),quote=FALSE,row.names=FALSE,col.names=FALSE)
}
}
pml_write_files(hat)
```