-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathcluster-example.Rmd
181 lines (94 loc) · 4.12 KB
/
cluster-example.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
# EDA Case Study - Understanding Human Activity with Smart Phones
## Samsung Galaxy S3
<img class=center src=images/samsung.png height=450>
[http://www.samsung.com/global/galaxys3/](http://www.samsung.com/global/galaxys3/)
## Samsung Data
<img class=center src=images/ucisamsung.png height=450>
[http://archive.ics.uci.edu/ml/datasets/Human+Activity+Recognition+Using+Smartphones](http://archive.ics.uci.edu/ml/datasets/Human+Activity+Recognition+Using+Smartphones)
## Slightly processed data
[Samsung data file]("https://dl.dropboxusercontent.com/u/7710864/courseraPublic/samsungData.rda")
```{r loaddata,tidy=TRUE}
load("data/samsungData.rda")
names(samsungData)[1:12]
table(samsungData$activity)
```
## Plotting average acceleration for first subject
```{r processData,fig.height=4,fig.width=8,tidy=TRUE}
par(mfrow=c(1, 2), mar = c(5, 4, 1, 1))
samsungData <- transform(samsungData, activity = factor(activity))
sub1 <- subset(samsungData, subject == 1)
plot(sub1[, 1], col = sub1$activity, ylab = names(sub1)[1])
plot(sub1[, 2], col = sub1$activity, ylab = names(sub1)[2])
legend("bottomright",legend=unique(sub1$activity),col=unique(sub1$activity), pch = 1)
```
## Clustering based just on average acceleration
<!-- ## source("http://dl.dropbox.com/u/7710864/courseraPublic/myplclust.R") -->
```{r dependson="processData",fig.height=5,fig.width=8}
source("myplclust.R")
distanceMatrix <- dist(sub1[,1:3])
hclustering <- hclust(distanceMatrix)
myplclust(hclustering, lab.col = unclass(sub1$activity))
```
## Plotting max acceleration for the first subject
```{r ,dependson="processData",fig.height=5,fig.width=10}
par(mfrow=c(1,2))
plot(sub1[,10],pch=19,col=sub1$activity,ylab=names(sub1)[10])
plot(sub1[,11],pch=19,col = sub1$activity,ylab=names(sub1)[11])
```
## Clustering based on maximum acceleration
```{r dependson="processData",fig.height=5,fig.width=10}
source("myplclust.R")
distanceMatrix <- dist(sub1[,10:12])
hclustering <- hclust(distanceMatrix)
myplclust(hclustering,lab.col=unclass(sub1$activity))
```
## Singular Value Decomposition
```{r svdChunk,dependson="processData",fig.height=5,fig.width=10,cache=TRUE,tidy=TRUE}
svd1 = svd(scale(sub1[,-c(562,563)]))
par(mfrow=c(1,2))
plot(svd1$u[,1],col=sub1$activity,pch=19)
plot(svd1$u[,2],col=sub1$activity,pch=19)
```
## Find maximum contributor
```{r dependson="svdChunk",fig.height=5,fig.width=6,cache=TRUE,tidy=TRUE}
plot(svd1$v[,2],pch=19)
```
## New clustering with maximum contributer
```{r dependson="svdChunk",fig.height=5,fig.width=8,cache=TRUE,tidy=TRUE}
maxContrib <- which.max(svd1$v[,2])
distanceMatrix <- dist(sub1[, c(10:12,maxContrib)])
hclustering <- hclust(distanceMatrix)
myplclust(hclustering,lab.col=unclass(sub1$activity))
```
## New clustering with maximum contributer
```{r dependson="svdChunk",fig.height=4.5,fig.width=4.5,cache=TRUE}
names(samsungData)[maxContrib]
```
## K-means clustering (nstart=1, first try)
```{r kmeans1,dependson="processData",fig.height=4,fig.width=4}
kClust <- kmeans(sub1[,-c(562,563)],centers=6)
table(kClust$cluster,sub1$activity)
```
## K-means clustering (nstart=1, second try)
```{r dependson="kmeans1",fig.height=4,fig.width=4,cache=TRUE,tidy=TRUE}
kClust <- kmeans(sub1[,-c(562,563)],centers=6,nstart=1)
table(kClust$cluster,sub1$activity)
```
## K-means clustering (nstart=100, first try)
```{r dependson="kmeans1",fig.height=4,fig.width=4,cache=TRUE}
kClust <- kmeans(sub1[,-c(562,563)],centers=6,nstart=100)
table(kClust$cluster,sub1$activity)
```
## K-means clustering (nstart=100, second try)
```{r kmeans100,dependson="kmeans1",fig.height=4,fig.width=4,cache=TRUE,tidy=TRUE}
kClust <- kmeans(sub1[,-c(562,563)],centers=6,nstart=100)
table(kClust$cluster,sub1$activity)
```
## Cluster 1 Variable Centers (Laying)
```{r dependson="kmeans100",fig.height=4,fig.width=8,cache=FALSE,tidy=TRUE}
plot(kClust$center[1,1:10],pch=19,ylab="Cluster Center",xlab="")
```
## Cluster 2 Variable Centers (Walking)
```{r dependson="kmeans100",fig.height=4,fig.width=8,cache=FALSE}
plot(kClust$center[4,1:10],pch=19,ylab="Cluster Center",xlab="")
```