-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysoln.spin.Rmd
264 lines (154 loc) · 4.99 KB
/
mysoln.spin.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# Project for Practical Machine learning course
****
The solution follows the below steps
1. Read in the input train and test data and understand it
2. Clean up the data
3. Do Principal component analysis and find the most important variables.
4. Create cross validation datasets
5. Fit two models, LDA and random forests
We select the model with the greater accuracy
******
```{r }
library(caret)
library(randomForest)
mainDir="/home/sdhandap/WeightLiftPredict"
setwd(mainDir)
source("helperfuncs.R")
```
##STEP1: Read in the input train and test data and understand it
Read in the training and test data
```{r }
pmltraindata <- read.table("pml-training.csv",sep=",",header=TRUE)
pmltestdata <- read.table("pml-testing.csv",sep=",",header=TRUE)
```
Explore the structure of the data
```{r }
preclean_explore(pmltraindata, pmltestdata)
```
##STEP2: Clean up the data
start with eliminating the NA variables
```{r }
cleantrain <- eliminate_NAs(pmltraindata)
cleantest <- eliminate_NAs(pmltestdata)
```
next eliminating the NULLs
```{r }
cleantrain <- eliminate_Nulls(cleantrain)
cleantest <- eliminate_Nulls(cleantest)
```
next eliminating the near zero variance variables
```{r }
cleantrain <- eliminate_zeroVarFactors(cleantrain)
cleantest <- eliminate_zeroVarFactors(cleantest)
```
Drop unnecessary varibles for prediction
```{r }
answer <- cleantest["problem_id"]
drops <- c("problem_id","X")
cleantest <- cleantest[ , !(names(cleantest) %in% drops)]
cleantrain <- cleantrain[ , !(names(cleantrain) %in% drops)]
#do_some_visualisation(cleantrain)
```
Just explore the data again after exploration
```{r }
postclean_explore(cleantrain, cleantest)
```
now combine the training and test data so that when we do prediction we dont get the errors like mismatch of type of predictors
("Type of predictors in new data do not match that of the training data.")
```{r }
testnumrows <- nrow(cleantest)
cleantest[,"classe"] <- NA
combinedData <- rbind(cleantrain,cleantest)
allrows <- nrow(combinedData)
finaltest <- combinedData[(allrows-testnumrows+1):allrows, ]
trainingset <- combinedData[1:(allrows-testnumrows), ]
```
We are done with the data clean up stage
##STEP3: Do Principal component analysis
```{r }
nonNumericVars <- c("user_name","classe","cvtd_timestamp")
pcadata <- combinedData[ , !(names(combinedData) %in% nonNumericVars)]
pca <- prcomp(pcadata, scale = TRUE)
biplot(pca, scale = 0)
std_dev <- pca$sdev
pr_var <- std_dev^2
prop_varex <- pr_var/sum(pr_var)
plot(prop_varex, xlab = "Principal Component",
ylab = "Proportion of Variance Explained",
type = "b")
plot(cumsum(prop_varex), xlab = "Principal Component",
ylab = "Cumulative Proportion of Variance Explained",
type = "b")
selectcols = which(prop_varex >= 0.002)
dataAfterPCA <- pcadata[,selectcols]
pr_cols <- names(dataAfterPCA)
pr_cols <- c(pr_cols, "classe")
train.pca.data <- trainingset[,(names(trainingset) %in% pr_cols)]
test.pca.data <- finaltest[,(names(finaltest) %in% pr_cols)]
```
Now we are done with the principal component analysis.
Next step is to split the data for cross validatiopn
do the simple train test and validation split
##STEP4: Create cross validation datasets
```{r }
training.rows <- createDataPartition(train.pca.data$classe, p = 0.8, list = FALSE)
train.batch <- train.pca.data[training.rows, ]
test.batch <- train.pca.data[-training.rows, ]
```
Now we have the data split into three: the train.batch, test.batch and the finaltest dataset
##STEP5: Model creation and Validation
```{r }
dependentvarname <- "classe"
AllVariables <- names(train.pca.data)
PredictorVariables <- setdiff(AllVariables, dependentvarname)
Formula <- formula(paste( paste(dependentvarname, " ~ ", sep =""),
paste(PredictorVariables, collapse=" + ")))
print(Formula)
```
Fit the random forest model
```{r }
rf_fit <- randomForest(Formula,
data=train.batch,
importance=TRUE,
ntree=2000)
pred.rf <- predict(rf_fit, test.batch)
confusionMatrix(pred.rf, test.batch$classe)
accuracy.rf <- (round(mean(pred.rf == test.batch$classe),3))
```
The accuracy in the random forest model
```{r }
print(accuracy.rf)
```
Fit the Linear discriminant analysis model
```{r }
model_lda <- train(Formula, method = "lda", data = train.batch)
pred.lda <- predict(model_lda, test.batch)
confusionMatrix(pred.lda, test.batch$classe)
accuracy.lda <-(round(mean(pred.lda == test.batch$classe),3))
```
The accuracy in the LDA model is
```{r }
print(accuracy.lda)
if(accuracy.rf >= accuracy.lda) {
#'
#' Select Random Forest model for the final prediction
#'
#'
pred.final <- predict(rf_fit, finaltest)
}else {
#'
#' Select LDA model for the final prediction
#'
#'
pred.final <- predict(model_lda, finaltest)
}
```
This is the final prediction output.
```{r }
print(pred.final)
```
---
title: "mysoln.R"
author: "sdhandap"
date: "Sun Aug 21 00:44:54 2016"
---