-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
450 lines (450 loc) · 14.7 KB
/
.Rhistory
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
library(manipulate)
myPlot <- function(s) {
plot(cars$dist - mean(cars$dist), cars$speed - mean(cars$speed))
abline(0, s)
}
manipulate(myPlot(s), slider = x(0, 2, step = 0.1))
library(manipulate)
myPlot <- function(s) {
plot(cars$dist - mean(cars$dist), cars$speed - mean(cars$speed))
abline(0, s)
}
manipulate(myPlot, s = slider(0, 2, step = 0.1))
library(manipulate)
myPlot <- function(s) {
plot(cars$dist - mean(cars$dist), cars$speed - mean(cars$speed))
abline(0, s)
}
manipulate(myPlot, s = slider(0, 2, step = 0.1))
manipulate(myPlot, s = slider(0, 2, step = 0.1))
manipulate(myPlot(s), s = slider(0, 2, step = 0.1))
install.packages("rCharts")
install_github('rCharts', 'ramnathv')
library(devtools)
install.packages("devtools")
install_github('rCharts', 'ramnathv')
library(devtools)
install_github('rCharts', 'ramnathv')
airquality
dTable(airquality, sPaginationType = "full_numbers")
d
?dTable
??dTable
library(rCharts)
dTable(airquality, sPaginationType = "full_numbers")
fruit
fruits
Fruits
data(Fruits)
install.packages("devtools")
install.packages("rtools")
install.packages("caret")
z = 10*(12-10)/4
z
10*2/12
pnorm(5)
1-pnorm(5)
b0 = c(140,138,150,148,135)
b1 = c(132,135,151,146,130)
b = b1-b0
mean(b)
sd(b)
z = sqrt(5)*mean(b)/sd(b)
z
qnorm(0.975)
pnorm(z)
pt(z,4)
sqrt(9)*1100/30
1100 + c(-1,1)*(30/sqrt(9))*pt(0.975)
1100 + c(-1,1)*(30/sqrt(9))*pt(0.975,8)
1100 + c(-1,1)*(30/sqrt(9))*qt(0.975,8)
4 + c(-1,1)*(0.5/sqrt(100))*qt(0.975,99)
6 + c(-1,1)*(2/sqrt(100))*qt(0.975,99)
4 + c(-1,1)*(0.5/sqrt(100))*qnorm(0.975,99)
4 + c(-1,1)*(0.5/sqrt(100))*qnorm(0.975)
6 + c(-1,1)*(2/sqrt(100))*qnorm(0.975)
-3 + c(-1,1)*(1.5/sqrt(9))*qt(0.975,8)
1 + c(-1,1)*(1.8/sqrt(9))*qt(0.975,8)
cit <- -3 + c(-1,1)*(1.5/sqrt(9))*qt(0.975,8)
cip <- 1 + c(-1,1)*(1.8/sqrt(9))*qt(0.975,8)
cit-cip
?createDataPartition
install.packages("AppliedPredictiveModeling")
?createDataPartition
library(AppliedPredictiveModeling)
library(caret)
data(AlzheimerDisease)
?createDataPartition
library(AppliedPredictiveModeling)
library(caret)
data(AlzheimerDisease)
library(AppliedPredictiveModeling)
library(caret)
data(AlzheimerDisease)
adData = data.frame(diagnosis,predictors)
trainIndex = createDataPartition(diagnosis, p = 0.50)
training = adData[trainIndex,]
testing = adData[-trainIndex,]
adData = data.frame(diagnosis,predictors)
testIndex = createDataPartition(diagnosis, p = 0.50,list=FALSE)
training = adData[-testIndex,]
testing = adData[testIndex,]
library(AppliedPredictiveModeling)
data(concrete)
library(caret)
set.seed(975)
inTrain = createDataPartition(mixtures$CompressiveStrength, p = 3/4)[[1]]
training = mixtures[ inTrain,]
testing = mixtures[-inTrain,]
library(ggplot2)
plot(concrete$CompressiveStrength,pch=19)
?plot
head(concrete)
plot(concrete$CompressiveStrength,pch=19,col=concrete$cement)
?plot
qplot(concrete$CompressiveStrength,pch=19)
qplot(concrete$CompressiveStrength)
plot(concrete$CompressiveStrength,pch=19,col=concrete$cement)
plot(concrete$CompressiveStrength,pch=19,col=3)
summary(concrete)
plot(concrete$CompressiveStrength,pch=19,col=cement/100)
cc <- concrete
plot(cc$CompressiveStrength,pch=19,col=cc$cement/100)
library(data.table)
cc <- data.table(cc)
cc[,CementCol := trunc(Cement/100)]
plot(cc$CompressiveStrength,pch=19,col=cc$CementCol)
plot(cc$CompressiveStrength,pch=19,col=1)
plot(cc$CompressiveStrength,pch=19,col=2)
plot(cc$CompressiveStrength,pch=19,col=3)
plot(cc$CompressiveStrength,pch=19,col=4)
plot(cc$CompressiveStrength,pch=19,col=5)
plot(cc$CompressiveStrength,pch=19,col=6)
plot(cc$CompressiveStrength,pch=19,col=7)
plot(cc$CompressiveStrength,pch=19,col=cc$CementCol)
cc[,FlyAshCol := trunc(FlyAsh/100)]
plot(cc$FlyAsh,pch=19,col=cc$CementCol)
plot(cc$FlyAsh,pch=19)
featurePlot(x=cc[,-c(CompressiveStrenth)],y=cc$CompressiveStrength,plot="pairs")
featurePlot(x=cc[,-c(CompressiveStrength)],y=cc$CompressiveStrength,plot="pairs")
featurePlot(x=cc,y=cc$CompressiveStrength,plot="pairs")
ccc <- cc[,-c("CementCol","FlyAshCol")]
ccc <- cc[-c("CementCol","FlyAshCol")]
ccdf <- data.fram(cc)
ccdf <- data.frame(cc)
ccc <- ccdf[-c("CementCol","FlyAshCol")]
ccc <- ccdf[1:9]
featurePlot(x=ccc,y=ccc$CompressiveStrength,plot="pairs")
plot(concrete$Age,pch=19,col=3)
plot(concrete$CompressiveStrength,pch=19,col=1)
plot(concrete$CompressiveStrength,concrete$Aage,pch=19,col=1)
plot(concrete$CompressiveStrength,concrete$Age,pch=19,col=1)
plot(concrete$CompressiveStrength,pch=19,col=1)
plot(concrete$CompressiveStrength,concrete$Age,pch=19,col=1)
plot(concrete$Age,pch=19,col=3)
plot(concrete$CompressiveStrength,pch=19,col=1)
cor(cc)
cor(ccc)
cor(ccc)
cor(ccc)
fit <- lm( CompressiveStrength ~ Cement + CoarseAggregate + Age)
fit <- lm( CompressiveStrength ~ Cement + CoarseAggregate + Age, data=ccc)
summary(fit)
summary(ccc)
histo(ccc$SuperPlasticizer)
hist(ccc$SuperPlasticizer)
head(ccc)
hist(ccc$Superplasticizer)
cccc <- ccc[,sp1 := ln(Superplasticizer+1)]
cccc <- ccc[,sp1 := log(Superplasticizer+1)]
class(ccc)
ccc <- data.table(ccc)
cccc <- ccc[,sp1 := log(Superplasticizer+1)]
hist[cccc$sp1]
cccc
hist[cccc$sp1]
sp1 <- cccc$sp1
hist(sp1)
hist(sp1,50)
hist(cccc$sp1)
hist(cccc$sp1,50)
hist(cccc$Superplasticizer,50)
hist(cccc$sp1,50)
library(caret)
library(AppliedPredictiveModeling)
set.seed(3433)
data(AlzheimerDisease)
adData = data.frame(diagnosis,predictors)
inTrain = createDataPartition(adData$diagnosis, p = 3/4)[[1]]
training = adData[ inTrain,]
testing = adData[-inTrain,]
head(adData)
s1 <- 1.5
s2 <- 1.8
n1 <- 9
n2 <- 9
num <- ((s1*s2/n1) + (s2*s2)/n2)^2
fk1 <- s1*s1/n1
fk2 <- s2*s2/n2
num <- (f1 + f2)^2
num <- (fk1 + fk2)^2
denom <- (fk1)^2*(n1-1) + (fk2)^2*(n2-1)
num/denom
num
denom
t <- (-3-1)/sqrt(fk1 + fk2)
t
df <- num/denom
pt(t,df)
pt(t,8)
fk1
fk2
2.25/9
denom <- (fk1)^2/(n1-1) + (fk2)^2/(n2-1)
denom
num/denom
df <- nu/denom
df <- num/denom
pt(t,df)
-4 + c(1,-1)*sqrt(9)*qt(0.975,df)/sqrt(sqrt(num))
-4 + c(1,-1)*sqrt(9)*qt(0.975,df)/sqrt(num)
-4 + c(1,-1)*sqrt(9)*qt(0.975,df)/num
-4 + c(1,-1)*sqrt(9)*qt(0.975,df)/sqrt(denom)
-4 + c(1,-1)*sqrt(9)*qt(0.975,df)/denom
sqrt(num)
sqrt(sqrt(num))
-4 + c(1,-1)*qt(0.975,df)/sqrt(sqrt(num))
-4 + c(1,-1)*qt(0.975,df)/sqrt(num)
-4 + c(1,-1)*qt(0.975,df)/denom
-4 + c(1,-1)*sqrt(9)*qt(0.95,df)/sqrt(sqrt(num))
-4 + c(1,-1)*qt(0.975,df)/denom
-4 + c(1,-1)*qt(0.975,df)/sqrt(sqrt(num))
-4 + c(1,-1)*qt(0.95,df)/sqrt(sqrt(num))
-4 + c(-1,1)*qt(0.95,df)/sqrt(sqrt(num))
(c[1]+c[2])/2
ci <- -4 + c(-1,1)*qt(0.95,df)/sqrt(sqrt(num))
(ci[1]+ci[2])/2
ai[1] <- -5.044
a1 <- -5.044
a2 <- -2.956
(a1+a2)/2
ci <- -4 + c(-1,1)*qt(0.95,16)/sqrt(sqrt(num))
ci
df
sqrt(sqrt(num))
ss <- sqrt(sqrt(num))
0 / 22
9 / ss
2/1.5
3/1.5
3/1.8
ci <- -4 + c(-1,1)*qt(0.95,16)/1.8
ci <- -4 + c(-1,1)*qt(0.95,16)/1.9
ci <- -4 + c(-1,1)*qt(0.95,16)/1.7
3/(sqrt(1.5^2 + 1.8^2))
3/(sqrt(1.5^2/2 + 1.8^2/2))
bl <- c(140, 138, 150, 148, 135)
fu <- c(132, 135, 151, 146, 130)
t.test(fu, bl, alternative = "two.sided", paired = TRUE)
b <- fu-bl
t <- sqrt(5)*mean(b)/sd(b)
t
2*pt(t,4)
s <- sqrt(mean(bl)/5 + mean(fu)/5)
s
s <- sqrt(16/5 + 16/5)
s
t <- -3.4/s
t
2*pt(t,4)
4/sqrt(5)
s <- 4/sqrt(5)
s
2*pt(t,8)
s <- sqrt(16/5 + 16/5)
bl <- c(140, 138, 150, 148, 135)
fu <- c(132, 135, 151, 146, 130)
m <- mean(fu-bl)
s <- sd(fu-bl)
t <- sqrt(2)*m/s
t
s
m
pt(t,4)
t <- sqrt(4)*m/s
t
t <- sqrt(5)*m/s
t
pt(t,4)
2*pt(t,4)
m + s*qt(t,4)
qt(t,4)
m + s*qt(0.95,4)
m + s*qt(0.975,4)
m + c(-1,1)* s*qt(0.975,4)
m + c(-1,1)* s*qt(0.975,4)/sqrt(5)
sp <- sd(fu-bl)
s
sp
su <- sqrt(sd(bl)^2/sqrt(5) + sd(fu)^2/sqrt(5))
su
tu <- m/su
tu
2*pt(t,8)
su <- sqrt(sd(bl)^2/5 + sd(fu)^2/5
)
su <- sqrt(sd(bl)^2/5 + sd(fu)^2/5)
su
tu <- m/su
tu
2*pt(t,8)
2*pt(tu,8)
2*pt(tu,4)
2*pt(tu,8)
m + c(-1,1)*su*qt(0.975,4)
t.test(bl-fu)
t.test(fu-bl)
t.test(fu,bl)
340 / 28
m + c(-1,1)*su*qt(0.975,8)
library(knitr)
setwd("C:/DataCert/ML/PmlProject")
knit2html('PmlProject.Rmd')
browseURL("PmlProject.html")
knit2html('PmlProject.Rmd')
browseURL("PmlProject.html")
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
browseURL("PmlProject.html")
library(reshape)
library(reshape,quietly=T
)
library(reshape)
melt
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
dfva <- read.csv("dftestAv-3it.csv")
dfva$varnum <- "54 vars"
dfv7 <- read.csv("dftest7v-3it.csv")
dfv7$varnum <- "7 vars"
dfv3 <- read.csv("dftest3v-3it.csv")
dfv3$varnum <- "3 vars"
dfall <- merge(dfv3,dfv7,all=T)
dfall <- merge(dfall,dfva,all=T)
dfall <- transform(dfall, varnum=factor(varnum,levels=c("3 vars","7 vars","54 vars") ))
qplot(prob,acc,data=dfall) + geom_smooth(method=loess) + facet_grid( . ~ varnum ) +
ggtitle("Accuracy vs. Training Percentage") + labs(x="Training Percentage",y="Accuracy")
View(dfall)
dfva <- read.csv("dftest54v-3it.csv")
dfva$varnum <- "54 vars"
dfv7 <- read.csv("dftest7v-3it.csv")
dfv7$varnum <- "7 vars"
dfv3 <- read.csv("dftest3v-3it.csv")
dfv3$varnum <- "3 vars"
dfall <- merge(dfv3,dfv7,all=T)
dfall <- merge(dfall,dfva,all=T)
dfall <- transform(dfall, varnum=factor(varnum,levels=c("3 vars","7 vars","54 vars") ))
qplot(prob,acc,data=dfall) + geom_smooth(method=loess) + facet_grid( . ~ varnum ) +
ggtitle("Accuracy vs. Training Percentage") + labs(x="Training Percentage",y="Accuracy")
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
print(sprintf("Validation set has %d and training set has %d rows",nrow(nvld),nrow(ntrn)))
7+15+7+2+10+3+4+3+2+1+5
(7+15+7+2+10+3+4+3+2+1+5)/4905
wp <-(7+15+7+2+10+3+4+3+2+1+5)/4905
1-2p
1-wp
1388+922+845+798+892
1388+922+845+798+892+59
knit2html('PmlProject.Rmd')
?sample
ntrn1 <- sample(ntrn,100)
ntrn1 <- sample(ntrn,prob=0.1)
ntrn1 <- sample(ntrn,100)
idx <- sample(1:length(ntrn),2000)
idx <- sample(1:nrow(ntrn),2000)
ntr1 <- ntrn[ idx]
ntr1 <- ntrn[ idx,]
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')
# Judging Exercise Correctness from Accelerator Data
Practical Machine Learning - Course Project Write-up
Mike Wise - 25 Aug 2014 - predmachlearn-004
# Background
Using devices such as Jawbone Up, Nike Fuel Band, 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 the quality - i.e.how well they do it. The goal here is to use data from accelerometers on the belt, forearm, arm, and dumbbell to assess the quality.
In this exercise 6 young health participants were asked to perform one set of 10 repetitions of the Unilateral Dumbbell Biceps Curl. The quality of the execution was judged by an expert and fell into 5 different classes or grades:
- Class A - correct - exactly according to the specification
- Class B - mistake - throwing the elbows to the front
- Class C - mistake - lifting the dumbbell only halfway
- Class D - mistake - lowering the dumbbell only halfway
- Class E - mistake - throwing the hips to the front
At the same time these exercises were being performed data was being recorded from a large number of sensors mounted on the body of the exercise participant. There are 19622 observations in our data training set, the goal here is to see if this judging can be can automate.
More information is available from the website here (see the section on the Weight Lifting Exercise Dataset):
http://groupware.les.inf.puc-rio.br/har
# Overall Approach
The overall approach was as follow:
- 1. **Data Preperation** - here the training and test data was read and new datasets were created containing only numeric columns that were approprate for modeling.
- 2. **Data Exploration and Model Selection** - here the data was investigated, and an investigation of the tradeoff between accuracy and training/validation split size was performed for various combinations of modeling techniques and variable subsets.
- 3. **Modeling and Analysis** - In this section the split was performed and selected model was run and an analysis of the results was perfomed. The Out of sample error is also calculated.
- 4. **Prediction** - Here the results for the Prediction Assignment Submission are calculated.
Some additional code is also presented in the Appendix.
# Data Preperation
Our data came in two spreadsheet files:
- pml-training.csv - a csv file containing 19622 observations with 160 columns with labeled training data. This data is to be used to train and validate our model.
- pml-training.csv - a csv file containing 20 observations with the same columns (but minus the label). This data is t be predicted with our final model and submitted. it is only used in the last step of this project.
## Loading and preprocessing the data
First of course we load the data.
```{r}
library(data.table,quietly=T)
otrn <- data.table(read.csv("pml-training.csv"))
otst <- data.table(read.csv("pml-testing.csv"))
```
## Data Cleaning
There are 160 columns in the original data, many of them blank and filled with NA values. We reduce the dataset, throwing away all columns that are not numeric or integer. We also exclude the columns "num_window" and "user_name" out of the training set as these are not sensor data columns.
```{r}
## Create new tables without of all non-numeric columns not present in both datasets
cnstst <- colnames(otrn)[7:160]
ntrn <- data.table(user_name=otrn[["user_name"]],classe=otrn[["classe"]])
ntst <- data.table(user_name=otst[["user_name"]])
for (i in 1:length(cnstst))
{
cn <- cnstst[[i]]
if (cn=="num_window") next
if (cn=="user_window") next
clstst <- class(otst[[cn]])
clstrn <- class(otrn[[cn]])
if (clstst!="numeric" && clstst !="integer") next
if (clstrn!="numeric" && clstrn !="integer") next
ntrn[[cn]] <- otrn[[cn]]
ntst[[cn]] <- otst[[cn]]
}
```
Here the quality of the data is checked, showing what columns we have retained (all relevant sensor data columns) and showing the reduction in the number of "NA" values from 1.2 million to zero, mostly by eliminating columns that consisted mostly of "NA" values.
```{r}
ona <- sum(is.na(otrn))
nna <- sum(is.na(ntrn))
print(sprintf("Original training NA count:%d - After processing:%d",ona,nna))
```
## Data Exploration
Now that we have a more acceptable number of data columns (54 vs. 160) we look at the overall data.
```{r message=FALSE,fig.width=11, fig.height=10}
library(reshape)
library(ggplot2,quietly=T)
hdat <- melt(ntrn)
ggplot(hdat, aes(x=value)) + facet_wrap(~variable, scales="free",ncol=6) + geom_histogram()
knit2html('PmlProject.Rmd')
knit2html('PmlProject.Rmd')